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.
 
 
 
 
 
 

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