main.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import Vue from 'vue'
  2. import store from './store'
  3. import App from './App'
  4. // 后端api地址
  5. //Vue.prototype.$unishow = "https://81f.dev.ytxxjs.cn/addons/unishop";
  6. // Vue.prototype.$unishow = "https://81fdev.dev.ytxxjs.cn/addons/unishop";
  7. Vue.prototype.$unishow = "https://81f.ytxxjs.cn/addons/unishop";
  8. // 为了方便每次上传的时候忘记修改上面的参数
  9. uni.getSystemInfo({
  10. success(res) {
  11. //console.log(res)
  12. if (res.platform != "devtools") {
  13. //Vue.prototype.$unishow = "https://shop.weivee.com/addons/unishop";
  14. }
  15. }
  16. });
  17. // 平台号
  18. // #ifdef APP-PLUS
  19. Vue.prototype.$platform = 'APP-PLUS';
  20. // #endif
  21. // #ifdef H5
  22. Vue.prototype.$platform = 'H5';
  23. // #endif
  24. // #ifdef MP-WEIXIN
  25. Vue.prototype.$platform = 'MP-WEIXIN';
  26. // #endif
  27. // #ifdef MP-ALIPAY
  28. Vue.prototype.$platform = 'MP-ALIPAY';
  29. // #endif
  30. // #ifdef MP-BAIDU
  31. Vue.prototype.$platform = 'MP-BAIDU';
  32. // #endif
  33. // #ifdef MP-TOUTIAO
  34. Vue.prototype.$platform = 'MP-TOUTIAO';
  35. // #endif
  36. // 提示
  37. const msg = (title, duration = 3000, mask = false, icon = 'none') => {
  38. //统一提示方便全局修改
  39. if (Boolean(title) === false) {
  40. return;
  41. }
  42. uni.showToast({
  43. title,
  44. duration,
  45. mask,
  46. icon
  47. });
  48. setTimeout(function() {
  49. uni.hideToast();
  50. }, duration)
  51. }
  52. // 返回上一页
  53. const prePage = () => {
  54. let pages = getCurrentPages();
  55. let prePage = pages[pages.length - 2];
  56. // #ifdef H5
  57. return prePage;
  58. // #endif
  59. return prePage.$vm;
  60. }
  61. // 检查有没有登录
  62. const checkLogin = async () => {
  63. let user = uni.getStorageSync('userInfo');
  64. if (user) {
  65. Vue.prototype.$store._mutations.login[0](user);
  66. } else {
  67. let result = await request('/user/status');
  68. if (!result) {
  69. // 若没有登录则清空个人信息
  70. Vue.prototype.$store._mutations.logout[0]();
  71. }
  72. }
  73. }
  74. // 深拷贝
  75. const deepCopy = (p, c) => {
  76. var c = c || {};
  77. for (var i in p) {
  78. if (typeof p[i] === "object") {
  79. c[i] = (p[i].constructor === Array) ? [] : {};
  80. deepCopy(p[i], c[i])
  81. } else {
  82. c[i] = p[i]
  83. }
  84. }
  85. return c;
  86. }
  87. // 同步网络请求
  88. const request = async (url, method = 'GET', data = {}, showMsg = true) => {
  89. let header = {
  90. 'content-type': 'application/x-www-form-urlencoded',
  91. 'lang': Vue.prototype.$store.state.lang,
  92. 'platform': Vue.prototype.$platform
  93. };
  94. if (Vue.prototype.$store.state.userInfo && Vue.prototype.$store.state.userInfo.token) {
  95. header.token = Vue.prototype.$store.state.userInfo.token;
  96. }
  97. if (Vue.prototype.$store.state.cookie) {
  98. header.cookie = Vue.prototype.$store.state.cookie;
  99. }
  100. var [error, res] = await uni.request({
  101. url: Vue.prototype.$unishow + url,
  102. method: method,
  103. header: header,
  104. data: data,
  105. timeout: 5000
  106. });
  107. if (url == '/pay/submit'){
  108. console.log(res);
  109. }
  110. return new Promise(function(revolve){
  111. if (error) {
  112. showMsg && msg(JSON.stringify(res));
  113. revolve(false);
  114. }
  115. if (res) {
  116. if (res.header.hasOwnProperty('Set-Cookie')) {
  117. let cookie = res.header['Set-Cookie'].replace("; path=/", "");
  118. Vue.prototype.$store.commit('setCookie', cookie);
  119. }
  120. if (res.hasOwnProperty('data')) {
  121. if (res.data.hasOwnProperty('code') && res.data.code == 401) {
  122. // 未登录 或 登录失效
  123. Vue.prototype.$store.commit('logout');
  124. }
  125. if (res.data.hasOwnProperty('code') && res.data.code == 1) {
  126. if (res.data.msg) {
  127. showMsg && msg(res.data.msg);
  128. } else {
  129. uni.hideToast();
  130. }
  131. revolve(res.data.data);
  132. } else {
  133. if (res.data.hasOwnProperty('msg')) {
  134. showMsg && msg(res.data.msg);
  135. } else {
  136. showMsg && msg('返回参数错误');
  137. }
  138. revolve(false);
  139. }
  140. } else {
  141. showMsg && msg('不能识别数据');
  142. revolve(false);
  143. }
  144. }
  145. });
  146. }
  147. // 跳转判断是否登录
  148. const navTo = (url, check = true) => {
  149. if (check && !Vue.prototype.$store.state.hasLogin) {
  150. url = '/pages/public/login';
  151. }
  152. uni.navigateTo({
  153. url: url
  154. });
  155. }
  156. Vue.config.productionTip = false
  157. Vue.prototype.$fire = new Vue();
  158. Vue.prototype.$store = store;
  159. Vue.prototype.$api = {
  160. msg,
  161. prePage,
  162. checkLogin,
  163. request,
  164. deepCopy,
  165. navTo
  166. };
  167. // #ifdef MP-WEIXIN
  168. // 微信小程序
  169. const wechatMiniLogin = async (noMsg = false) => {
  170. if (!noMsg) msg('登录中');
  171. if (Vue.wechatMiniLoginLock) return;
  172. Vue.wechatMiniLoginLock = true;
  173. Vue.wechatMiniLoginTag = setTimeout(() => {
  174. Vue.wechatMiniLoginLock = false;
  175. }, 10000);
  176. let [error, loginRes] = await uni.login({
  177. provider: 'weixin'
  178. });
  179. if (loginRes.hasOwnProperty('code')) {
  180. let data = await request('/user/authSession', 'GET', {
  181. code: loginRes.code
  182. });
  183. if (data) {
  184. if (data.hasOwnProperty('userInfo') && data.userInfo.token && data.userInfo.token != '') {
  185. Vue.prototype.$store.commit('login', data.userInfo);
  186. uni.setStorage({ //缓存用户信息
  187. key: 'access_token',
  188. data: data.access_token
  189. });
  190. }
  191. }
  192. return data.userInfo;
  193. } else {
  194. msg('登录失败');
  195. return false;
  196. }
  197. };
  198. Vue.wechatMiniLoginLock = false;
  199. Vue.wechatMiniLoginTag = 0;
  200. Vue.prototype.$wechatMiniLogin = wechatMiniLogin;
  201. // #endif
  202. App.mpType = 'app'
  203. const app = new Vue({
  204. ...App
  205. })
  206. app.$mount()