main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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': '8871d05eacc4406083d3bb0a085b6999',
  61. // 'tenantId': Vue.prototype.tenantId 或者 'tenantId': `${tenantId}`
  62. // application/x-www-form-urlencoded application/json
  63. },
  64. success: (res) => {
  65. console.log('resres', res)
  66. uni.hideLoading();
  67. if (res.statusCode === 200) {
  68. resolve(res.data);
  69. } else {
  70. if (showErrMsg) {
  71. resolve({
  72. result: false,
  73. msg: res.data.msg
  74. })
  75. } else {
  76. resolve(false);
  77. if (this.$refs && this.$refs.uNotify) {
  78. this.$refs.uNotify.show({
  79. type: 'error',
  80. top: 0,
  81. message: res.data.msg,
  82. icon: 'error-circle'
  83. })
  84. }
  85. }
  86. }
  87. },
  88. fail: (err) => {
  89. uni.hideLoading()
  90. resolve(false)
  91. }
  92. })
  93. })
  94. }
  95. Vue.prototype.autoLogin = async () => { // 小程序自动登录
  96. return new Promise(resolve => {
  97. uni.login({
  98. success: async (res) => {
  99. const result = await Vue.prototype.$request('post', '/miniLogin', {
  100. code: res.code,
  101. appid: '000000'
  102. //appid: Vue.prototype.appId
  103. })
  104. if (result) {
  105. uni.setStorageSync("token", result.data.access_token);
  106. //await Vue.prototype.getUser()
  107. resolve(true)
  108. } else {
  109. uni.showModal({
  110. title: '温馨提示',
  111. content: '您尚未注册,是否前往注册?',
  112. showCancel: true,
  113. confirmText: '去注册',
  114. success: (response) => {
  115. if (response.confirm) {
  116. // 用户点击确定
  117. uni.navigateTo({
  118. url: '/pages/bindPhone/bindPhone'
  119. })
  120. resolve(true)
  121. } else if (response.cancel) {
  122. // 用户点击取消
  123. resolve(false)
  124. }
  125. },
  126. fail: () => {
  127. resolve(false)
  128. }
  129. })
  130. }
  131. },
  132. fail: (e) => {
  133. resolve(false)
  134. }
  135. })
  136. })
  137. }
  138. // 拼接文件完整地址
  139. Vue.prototype.getFilePath = (path) => {
  140. return `${Vue.prototype.serverFilePath}${path}`
  141. }
  142. App.mpType = 'app'
  143. const app = new Vue({
  144. ...App
  145. })
  146. app.$mount()
  147. // #ifdef VUE3
  148. import {
  149. createSSRApp
  150. } from 'vue'
  151. export function createApp() {
  152. const app = createSSRApp(App)
  153. return {
  154. app
  155. }
  156. }
  157. // #endif