main.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import Vue from 'vue'
  2. import App from './App'
  3. import store from './store'
  4. import * as config from './config'
  5. const baseUrl = config.def().baseUrl
  6. Vue.config.productionTip = false
  7. Vue.prototype.$api = {
  8. uploadImgByPath,
  9. };
  10. Vue.prototype.request = function(method, endpoint, token, data = {}) {
  11. let netWork = ''
  12. uni.getNetworkType({
  13. success: function(res) {
  14. netWork = res.networkType
  15. }
  16. })
  17. if (netWork === 'none') {
  18. uni.showToast({
  19. title: '当前无网络',
  20. icon: 'none',
  21. duration: 1500
  22. })
  23. return;
  24. }
  25. return new Promise((resolve, reject) => {
  26. let content = 'application/x-www-form-urlencoded; charset=UTF-8';
  27. if (method === 'post') {
  28. content = 'application/json; charset=UTF-8';
  29. }
  30. const header = {
  31. 'Content-Type': content
  32. }
  33. if (token) {
  34. header['Authorization'] = 'Bearer ' + token
  35. }
  36. uni.request({
  37. url: baseUrl + "/" + endpoint,
  38. method: method,
  39. data: data,
  40. header,
  41. success: function(res) {
  42. if (res.data.code === 200) {
  43. resolve(res.data);
  44. } else if (res.data.code === 401) {
  45. let pages = getCurrentPages();
  46. let curPath = `/${pages[pages.length - 1].route}`; // 当前页
  47. let loginPath = '/pages/login/index'; // 登录页
  48. store.commit('logout')
  49. if (curPath === loginPath) {
  50. resolve(res.data)
  51. } else {
  52. uni.redirectTo({
  53. url: loginPath
  54. })
  55. }
  56. } else if (res.data.code === 500 && res.data.msg === '用户类型不正确') {
  57. // 特殊处理用户类型不正确的情况,不弹窗,直接返回错误
  58. reject(res.data)
  59. } else {
  60. // 其他错误情况仍然弹窗提示
  61. uni.showToast({
  62. title: res.data.msg,
  63. icon: 'none',
  64. duration: 1500
  65. })
  66. reject(res.data)
  67. }
  68. },
  69. fail: function(err) {
  70. uni.showToast({
  71. title: '请稍后再试',
  72. icon: 'none'
  73. })
  74. reject(err)
  75. }
  76. })
  77. })
  78. }
  79. Vue.prototype.dateFtt = function(fmt, date) {
  80. var o = {
  81. "M+": date.getMonth() + 1, //月份
  82. "d+": date.getDate(), //日
  83. "h+": date.getHours(), //小时
  84. "m+": date.getMinutes(), //分
  85. "s+": date.getSeconds(), //秒
  86. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  87. "S": date.getMilliseconds() //毫秒
  88. };
  89. if (/(y+)/.test(fmt))
  90. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  91. for (var k in o)
  92. if (new RegExp("(" + k + ")").test(fmt))
  93. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  94. return fmt;
  95. }
  96. const uploadImgByPath = (filePath, successCallback, failCallback = undefined) => {
  97. userInfo = uni.getStorageSync('userInfo');
  98. var accessToken = userInfo ? userInfo.accessToken : '';
  99. let baseUrl = config.def().baseUrl
  100. uni.getImageInfo({
  101. src: filePath,
  102. success: image => {
  103. console.log(image)
  104. uni.showLoading({
  105. title: '图片上传中',
  106. mask: true
  107. })
  108. uni.uploadFile({
  109. url: baseUrl + `/oss/app/upload`,
  110. file: image,
  111. filePath: image.path,
  112. header: {
  113. Authorization: 'Bearer ' + accessToken,
  114. },
  115. name: 'file',
  116. success: res => {
  117. if (successCallback) {
  118. successCallback(JSON.parse(res.data).data.url)
  119. }
  120. },
  121. fail: err => {
  122. if (failCallback) {
  123. failCallback(err)
  124. } else {
  125. uni.showToast({
  126. title: '上传图片失败',
  127. icon: 'none',
  128. duration: 2000,
  129. })
  130. }
  131. },
  132. complete: res => {
  133. uni.hideLoading()
  134. },
  135. })
  136. },
  137. fail: err => {
  138. uni.showToast({
  139. title: '获取图片信息失败',
  140. icon: 'none',
  141. duration: 2000,
  142. })
  143. },
  144. })
  145. }
  146. App.mpType = 'app'
  147. const app = new Vue({
  148. store,
  149. ...App
  150. })
  151. app.$mount()