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.
 
 
 
 
 
 

228 rivejä
4.8 KiB

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