main.js 4.5 KB

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