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.

main.js 4.7 KiB

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