main.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import App from './App'
  2. import Vue from 'vue'
  3. import './uni.promisify.adaptor'
  4. import uView from "uview-ui";
  5. Vue.use(uView);
  6. Vue.config.productionTip = false
  7. import './css/style.css'
  8. import mySwiper from './components/my-swiper/my-swiper.vue';
  9. Vue.component('my-swiper', mySwiper);
  10. import useEmpty from './components/use-empty/use-empty.vue';
  11. Vue.component('use-empty', useEmpty);
  12. import useLoadmore from './components/use-loadmore/use-loadmore.vue';
  13. Vue.component('use-loadmore', useLoadmore);
  14. import topSearch from './components/top-search/top-search.vue';
  15. Vue.component('top-search', topSearch);
  16. import myGap from './components/my-gap/my-gap.vue';
  17. Vue.component('my-gap', myGap);
  18. import myGoods from './components/my-goods/my-goods.vue';
  19. Vue.component('my-goods', myGoods);
  20. import sendType from './components/send-type/send-type.vue';
  21. Vue.component('send-type', sendType);
  22. import useListTitle from './components/use-list-title/use-list-title.vue';
  23. Vue.component('use-list-title', useListTitle);
  24. // 配置项
  25. const config = process.env.ENV_PATH ? require(process.env.ENV_PATH) : require('./env/dev.js')
  26. Vue.prototype.serverPath = config.serverPath // 请求路径
  27. Vue.prototype.serverFilePath = config.serverFilePath // 文件路径
  28. Vue.prototype.appName = config.name // 应用名称
  29. Vue.prototype.appVersion = config.appVersion // 小程序版本
  30. Vue.prototype.webSocketURL = config.webSocketURL // socket 链接
  31. Vue.prototype.logo = config.logo //
  32. Vue.prototype.appId = config.appId // 微信小程序 appid
  33. Vue.prototype.$isIphoneX = false
  34. Vue.prototype.$request = function(method, url, data, isJSON, hideLoading, showErrMsg) {
  35. return new Promise((resolve) => {
  36. if (!hideLoading) {
  37. uni.showLoading({
  38. title: '请稍后...'
  39. })
  40. }
  41. // 过滤 null,undefind,'' 的参数
  42. for (let key in data) {
  43. if (data[key] === null || data[key] === undefined || data[key] === '') {
  44. delete data[key]
  45. }
  46. }
  47. const token = uni.getStorageSync('token');
  48. const tenantId = uni.getStorageSync('tenantId');
  49. // if (tenantId) {
  50. // data.tenantId = tenantId;
  51. // }
  52. url = url.indexOf('http') === 0 ? url : `${Vue.prototype.serverPath}${url}`;
  53. uni.request({
  54. url,
  55. method,
  56. data,
  57. header: {
  58. 'Content-Type': isJSON ? 'application/json' : 'application/x-www-form-urlencoded',
  59. 'Authorization': 'Bearer ' + token,
  60. 'clientId': 'e5cd7e4891bf95d1d19206ce24a7b32e',
  61. // 'tenantId': Vue.prototype.tenantId 或者 'tenantId': `${tenantId}`
  62. // application/x-www-form-urlencoded application/json
  63. },
  64. success: (res) => {
  65. uni.hideLoading();
  66. if (res.statusCode === 200) {
  67. resolve(res.data);
  68. } else {
  69. if (showErrMsg) {
  70. resolve({
  71. result: false,
  72. msg: res.data.data.msg
  73. })
  74. } else {
  75. resolve(false);
  76. uni.showToast({
  77. title: res.data.data.msg,
  78. icon: 'error',
  79. duration: 2000
  80. });
  81. // if (this.$refs && this.$refs.uNotify) {
  82. // this.$refs.uNotify.show({
  83. // type: 'error',
  84. // top: 0,
  85. // message: res.data.msg,
  86. // icon: 'error-circle'
  87. // })
  88. // }
  89. }
  90. }
  91. },
  92. fail: (err) => {
  93. uni.hideLoading()
  94. resolve(false)
  95. }
  96. })
  97. })
  98. }
  99. Vue.prototype.autoLogin = async () => { // 小程序自动登录
  100. return new Promise(resolve => {
  101. uni.login({
  102. success: async (res) => {
  103. const result = await Vue.prototype.$request('post', '/miniLogin', {
  104. code: res.code,
  105. appid: '000000'
  106. //appid: Vue.prototype.appId
  107. })
  108. if (result) {
  109. uni.setStorageSync("token", result.data.access_token);
  110. //await Vue.prototype.getUser()
  111. resolve(true)
  112. } else {
  113. uni.showModal({
  114. title: '温馨提示',
  115. content: '您尚未注册,是否前往注册?',
  116. showCancel: true,
  117. confirmText: '去注册',
  118. success: (response) => {
  119. if (response.confirm) {
  120. // 用户点击确定
  121. uni.navigateTo({
  122. url: '/pages/bindPhone/bindPhone'
  123. })
  124. resolve(true)
  125. } else if (response.cancel) {
  126. // 用户点击取消
  127. resolve(false)
  128. }
  129. },
  130. fail: () => {
  131. resolve(false)
  132. }
  133. })
  134. }
  135. },
  136. fail: (e) => {
  137. resolve(false)
  138. }
  139. })
  140. })
  141. }
  142. // 拼接文件完整地址
  143. Vue.prototype.getFilePath = (path) => {
  144. return `${Vue.prototype.serverFilePath}${path}`
  145. }
  146. App.mpType = 'app'
  147. const app = new Vue({
  148. ...App
  149. })
  150. app.$mount()
  151. // #ifdef VUE3
  152. import {
  153. createSSRApp
  154. } from 'vue'
  155. export function createApp() {
  156. const app = createSSRApp(App)
  157. return {
  158. app
  159. }
  160. }
  161. // #endif