main.js 3.9 KB

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