You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

237 lines
5.2 KiB

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