main.js 4.2 KB

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