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.
 
 
 
 
 
 

233 lines
5.1 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. uni.showModal({
  70. cancelText:'Cancel',
  71. confirmText:'OK',
  72. title: '温馨提示',
  73. content: '你还没登录,请先登录',
  74. success(res) {
  75. if (res.confirm) {
  76. // 账户秘密登录
  77. let url = '/pages/public/login';
  78. uni.navigateTo({
  79. url: url
  80. });
  81. }
  82. resolve(false);
  83. }
  84. })
  85. } else {
  86. resolve(true);
  87. }
  88. });
  89. }
  90. // 深拷贝
  91. const deepCopy = (p, c) => {
  92. var c = c || {};
  93. for (var i in p) {
  94. if (typeof p[i] === "object") {
  95. c[i] = (p[i].constructor === Array) ? [] : {};
  96. deepCopy(p[i], c[i])
  97. } else {
  98. c[i] = p[i]
  99. }
  100. }
  101. return c;
  102. }
  103. // 同步网络请求
  104. const request = async (url, method = 'GET', data = {}, showMsg = true) => {
  105. let header = {
  106. 'content-type': 'application/x-www-form-urlencoded',
  107. 'lang': Vue.prototype.$store.state.lang,
  108. 'platform': Vue.prototype.$platform
  109. };
  110. if (Vue.prototype.$store.state.userInfo.token) {
  111. header.token = Vue.prototype.$store.state.userInfo.token;
  112. }
  113. if (Vue.prototype.$store.state.cookie) {
  114. header.cookie = Vue.prototype.$store.state.cookie;
  115. }
  116. var [error, res] = await uni.request({
  117. url: Vue.prototype.$unishow + url,
  118. method: method,
  119. header: header,
  120. data: data,
  121. timeout: 5000
  122. });
  123. if (url == '/pay/submit') {
  124. console.log(res);
  125. }
  126. return new Promise(function(revolve) {
  127. if (error) {
  128. showMsg && msg(JSON.stringify(res));
  129. revolve(false);
  130. }
  131. if (res) {
  132. if (res.header.hasOwnProperty('Set-Cookie')) {
  133. let cookie = res.header['Set-Cookie'].replace("; path=/", "");
  134. Vue.prototype.$store.commit('setCookie', cookie);
  135. }
  136. if (res.hasOwnProperty('data')) {
  137. if (res.data.hasOwnProperty('code') && res.data.code == 401) {
  138. // 未登录 或 登录失效
  139. Vue.prototype.$store.commit('logout');
  140. }
  141. if (res.data.hasOwnProperty('code') && res.data.code == 1) {
  142. if (res.data.msg) {
  143. showMsg && msg(res.data.msg);
  144. } else {
  145. uni.hideToast();
  146. }
  147. revolve(res.data.data);
  148. } else {
  149. if (res.data.hasOwnProperty('msg')) {
  150. showMsg && msg(res.data.msg);
  151. } else {
  152. showMsg && msg('返回参数错误');
  153. }
  154. revolve(false);
  155. }
  156. } else {
  157. showMsg && msg('不能识别数据');
  158. revolve(false);
  159. }
  160. }
  161. });
  162. }
  163. // 跳转判断是否登录
  164. const navTo = (url, check = true) => {
  165. console.log(1111111111, url)
  166. if (check && !Vue.prototype.$store.state.hasLogin) {
  167. url = '/pages/public/login';
  168. }
  169. uni.navigateTo({
  170. url: url
  171. });
  172. }
  173. Vue.config.productionTip = false
  174. Vue.prototype.$fire = new Vue();
  175. Vue.prototype.$store = store;
  176. Vue.prototype.$api = {
  177. msg,
  178. prePage,
  179. checkLogin,
  180. request,
  181. deepCopy,
  182. navTo
  183. };
  184. // #ifdef MP-WEIXIN
  185. // 微信小程序
  186. const wechatMiniLogin = async () => {
  187. msg('登录中');
  188. let [error, loginRes] = await uni.login({
  189. provider: 'weixin'
  190. });
  191. if (loginRes.hasOwnProperty('code')) {
  192. let data = await request('/user/authSession', 'GET', {
  193. code: loginRes.code
  194. });
  195. if (data) {
  196. if (data.hasOwnProperty('userInfo') && data.userInfo.token && data.userInfo.token != '') {
  197. Vue.prototype.$store.commit('login', data.userInfo);
  198. //Vue.prototype.$store.mutations.login(data.userInfo)
  199. }
  200. }
  201. return true;
  202. } else {
  203. msg('登录失败');
  204. return false;
  205. }
  206. };
  207. Vue.prototype.$wechatMiniLogin = wechatMiniLogin;
  208. // #endif
  209. App.mpType = 'app'
  210. const app = new Vue({
  211. ...App
  212. })
  213. app.$mount()