main.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import Vue from 'vue'
  2. import store from './store'
  3. import App from './App'
  4. import uView from '@/uni_modules/uview-ui';
  5. Vue.use(uView);
  6. // 下拉
  7. import drawer from "@/components/drawer/drawer.vue"
  8. import md5 from "./common/md5.js"
  9. import mixin from "common/share.js"
  10. import filter from 'common/filter.js'
  11. import config from './config/config'
  12. // 引入全局uView
  13. // import uView from 'uview-ui'
  14. // Vue.use(uView);
  15. Vue.mixin(mixin)
  16. /**
  17. * 因工具函数属于公司资产, 所以直接在Vue实例挂载几个常用的函数
  18. * 所有测试用数据均存放于根目录json.js
  19. *
  20. * css部分使用了App.vue下的全局样式和iconfont图标,有需要图标库的可以留言。
  21. * 示例使用了uni.scss下的变量, 除变量外已尽量移除特有语法,可直接替换为其他预处理器使用
  22. */
  23. const msg = (title, duration=1500, mask=false, icon='none',)=>{
  24. //统一提示方便全局修改
  25. if(Boolean(title) === false){
  26. return;
  27. }
  28. uni.showToast({
  29. title,
  30. duration,
  31. mask,
  32. icon,
  33. });
  34. }
  35. const json = type=>{
  36. //模拟异步请求数据
  37. return new Promise(resolve=>{
  38. resolve(Json[type]);
  39. })
  40. }
  41. const prePage = ()=>{
  42. let pages = getCurrentPages();
  43. let prePage = pages[pages.length - 2];
  44. // #ifdef H5
  45. return prePage;
  46. // #endif
  47. return prePage.$vm;
  48. }
  49. const getSign = (str)=>{
  50. return md5(str + "A1B2C3");
  51. }
  52. const requst = (opt) => {
  53. opt = opt || {};
  54. opt.data = opt.data || null;
  55. opt.method = opt.method || 'POST';
  56. opt.header = opt.header || {
  57. "Content-Type": "application/x-www-form-urlencoded"
  58. };
  59. opt.success = opt.success || function () {};
  60. let url = opt.url
  61. let json = getApp().globalData.storeInfo;
  62. let baseUrl = getApp().globalData.apiUrl;
  63. if (url.indexOf("/api/") == -1) {
  64. // 生产环境通过nginx根据校区code路由,本地开发统一用a1(context-path)
  65. if (baseUrl.indexOf('127.0.0.1') != -1 || baseUrl.indexOf('localhost') != -1) {
  66. url = 'a1' + url
  67. } else if (json) {
  68. url = json.code + url
  69. }
  70. }
  71. if (url.indexOf('http') != 0) {
  72. // 去掉前导斜杠,避免 baseUrl + '/' + url 产生双斜杠 //
  73. if (url.charAt(0) == '/') {
  74. url = url.substring(1);
  75. }
  76. // /api/ 前缀的请求走管理平台服务(client-plat:8080),其他走移动端服务(client-app:8081)
  77. if (url.indexOf('api/') == 0 && getApp().globalData.platApiUrl) {
  78. url = getApp().globalData.platApiUrl + '/' + url;
  79. } else {
  80. url = baseUrl + '/' + url;
  81. }
  82. }
  83. uni.request({
  84. url: url,
  85. data: opt.data,
  86. method: opt.method,
  87. header: opt.header,
  88. dataType: 'json',
  89. success: function (res) {
  90. if (res && res.data && res.data.rtnBean) {
  91. opt.success(res);
  92. } else {
  93. console.error('响应数据格式异常:', res);
  94. uni.showToast({
  95. title: '服务响应异常,请稍后重试',
  96. icon: 'none'
  97. });
  98. }
  99. },
  100. fail: function (err) {
  101. console.error('请求失败:', err);
  102. uni.showToast({
  103. title: '网络请求失败,请稍后重试',
  104. icon: 'none'
  105. });
  106. }
  107. })
  108. }
  109. const moneyConverter = (money)=> {
  110. let m = Number(money) / Number(100);
  111. return Math.round(m * 100) / 100;
  112. }
  113. const loginx = (code,storeUserId)=> {
  114. uni.get
  115. uni.login({
  116. provider: 'weixin',
  117. success: (loginRes) => {
  118. uni.request({
  119. url: getApp().globalData.apiUrl + '/' + code + '/storeUserLoginApi/login',
  120. data: {
  121. code: loginRes.code,
  122. storeUserId: storeUserId
  123. },
  124. method: 'POST',
  125. header: {
  126. 'content-type': 'application/x-www-form-urlencoded',
  127. },
  128. dataType: 'json',
  129. success: function (res) {
  130. // store.state.hasLogin = true;
  131. // store.state.userInfo = res.data.obj;
  132. // //缓存用户登陆状态
  133. // uni.setStorage({
  134. // key: 'userInfo',
  135. // data: res.data.obj
  136. // })
  137. },
  138. fail: function (err) {
  139. console.log(err);
  140. uni.showLoading({
  141. title: '请稍后重试'
  142. });
  143. }
  144. })
  145. },
  146. });
  147. }
  148. Vue.config.productionTip = false
  149. Vue.prototype.$fire = new Vue();
  150. Vue.prototype.requst = requst;
  151. Vue.prototype.moneyConverter = moneyConverter;
  152. Vue.prototype.loginx = loginx;
  153. Vue.prototype.$store = store;
  154. Vue.prototype.getSign = getSign;
  155. Vue.prototype.$api = {msg, prePage};
  156. Object.keys(filter).forEach(key=>{
  157. Vue.filter(key, filter[key])
  158. })
  159. // 设置全局色号 config/config文件 配置色号
  160. Object.keys(config).forEach(i => {
  161. if (i.includes('Color') || i.includes('color')) {
  162. Vue.prototype['$' + i] = config[i]
  163. }
  164. })
  165. Vue.component('drawer', drawer)
  166. App.mpType = 'app'
  167. const app = new Vue({
  168. ...App
  169. })
  170. app.$mount()