main.js 4.6 KB

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