main.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.request = function(method, endpoint, token, data = {}, failCallback = undefined) {
  8. let netWork = ''
  9. uni.getNetworkType({
  10. success: function(res) {
  11. netWork = res.networkType
  12. }
  13. })
  14. if (netWork === 'none') {
  15. uni.showToast({
  16. title: '当前无网络',
  17. icon: 'none',
  18. duration: 1500
  19. })
  20. return;
  21. }
  22. return new Promise((resolve, reject) => {
  23. let content = 'application/x-www-form-urlencoded; charset=UTF-8';
  24. if (method === 'post') {
  25. content = 'application/json; charset=UTF-8';
  26. }
  27. const header = {
  28. 'Content-Type': content
  29. }
  30. if (token) {
  31. header['Authorization'] = 'Bearer ' + token
  32. }
  33. if (!failCallback) {
  34. failCallback = function(err) {
  35. if (showToast) {
  36. uni.showToast({
  37. title: '请稍后再试',
  38. icon: 'none'
  39. })
  40. }
  41. reject(err)
  42. }
  43. }
  44. uni.request({
  45. url: baseUrl + "/" + endpoint,
  46. method: method,
  47. data: data,
  48. header,
  49. success: function(res) {
  50. if (res.data.code === 200) {
  51. resolve(res.data);
  52. } else if (res.data.code === 401) {
  53. let pages = getCurrentPages();
  54. let curPath = `/${pages[pages.length - 1].route}`; // 当前页
  55. let loginPath = '/pages/login/index'; // 登录页
  56. store.commit('logout')
  57. if (curPath === loginPath) {
  58. resolve(res.data)
  59. } else {
  60. uni.redirectTo({
  61. url: loginPath
  62. })
  63. }
  64. } else if (res.data.code === 500 && res.data.msg === '用户类型不正确') {
  65. // 特殊处理用户类型不正确的情况,不弹窗,直接返回错误
  66. reject(res.data)
  67. } else {
  68. // 其他错误情况仍然弹窗提示
  69. uni.showToast({
  70. title: res.data.msg,
  71. icon: 'none',
  72. duration: 1500
  73. })
  74. reject(res.data)
  75. }
  76. },
  77. fail: failCallback
  78. })
  79. })
  80. }
  81. Vue.prototype.dateFtt = function(fmt, date) {
  82. var o = {
  83. "M+": date.getMonth() + 1, //月份
  84. "d+": date.getDate(), //日
  85. "h+": date.getHours(), //小时
  86. "m+": date.getMinutes(), //分
  87. "s+": date.getSeconds(), //秒
  88. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  89. "S": date.getMilliseconds() //毫秒
  90. };
  91. if (/(y+)/.test(fmt))
  92. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  93. for (var k in o)
  94. if (new RegExp("(" + k + ")").test(fmt))
  95. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  96. return fmt;
  97. }
  98. const uploadImgByPath = (filePath, token, successCallback, failCallback = undefined) => {
  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 ' + token,
  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. Vue.prototype.$api = {
  147. uploadImgByPath
  148. };
  149. App.mpType = 'app'
  150. const app = new Vue({
  151. store,
  152. ...App
  153. })
  154. app.$mount()