request.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import $C from './config.js'
  2. import $U from './util.js'
  3. import { jsonFilterEmpty } from './tools.js'
  4. export default {
  5. // 全局配置 application/json;
  6. common: {
  7. baseUrl: $C.baseUrl,
  8. header: {
  9. // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  10. 'Content-Type': 'application/json;charset=utf-8'
  11. },
  12. data: {},
  13. method: 'GET',
  14. dataType: 'json',
  15. token: true
  16. },
  17. // 请求 返回promise
  18. request(options = {}) {
  19. let _that = this
  20. // 组织参数
  21. if (options.commonBaseUrl) {
  22. options.url = options.commonBaseUrl + options.url
  23. } else {
  24. options.url = this.common.baseUrl + options.url
  25. }
  26. options.header = options.header || this.common.header
  27. options.data = options.data || this.common.data
  28. options.method = options.method || this.common.method
  29. options.dataType = options.dataType || this.common.dataType
  30. options.header['api-type'] = 'wxapp'
  31. if (uni.getStorageSync('token')) {
  32. options.header.Authorization = uni.getStorageSync('token')
  33. options.header['api-token'] = uni.getStorageSync('token')
  34. }
  35. options.timeout = 10000
  36. uni.showLoading()
  37. // 请求
  38. return new Promise((res, rej) => {
  39. // 请求之前检测有没有网络
  40. uni.request({
  41. ...options,
  42. success: result => {
  43. uni.hideLoading()
  44. let {
  45. statusCode,
  46. data
  47. } = result
  48. // 返回原始数据
  49. if (options.native) {
  50. return res(result)
  51. }
  52. // 成功
  53. if (statusCode == 200) {
  54. if (data.code === 200) {
  55. res(data)
  56. } else if (data.code === 401) {
  57. uni.removeStorageSync('userInfo')
  58. uni.removeStorage({
  59. key: 'token',
  60. success: () => {
  61. setTimeout(function() {
  62. uni.reLaunch({ url: '/pages/home/dashboard/index', })
  63. }, 500)
  64. }
  65. })
  66. return rej(data)
  67. } else if (data.code === 403) {
  68. uni.removeStorageSync('userInfo')
  69. uni.removeStorage({
  70. key: 'token',
  71. success: () => {
  72. setTimeout(function() {
  73. uni.reLaunch({ url: '/pages/home/dashboard/index', })
  74. }, 500)
  75. }
  76. })
  77. return rej(data)
  78. } else if (data.code === 500) {
  79. uni.showToast({
  80. title: data.msg || '',
  81. icon: 'none',
  82. duration: 2000
  83. })
  84. return rej(data)
  85. } else {
  86. uni.hideLoading()
  87. uni.showToast({
  88. title: data.msg || '返回失败',
  89. icon: 'none',
  90. duration: 3000
  91. })
  92. return rej(data)
  93. }
  94. } else if (statusCode == 401 || statusCode == 403 || statusCode == 204) {
  95. // uni.removeStorageSync(`userInfo`)
  96. // uni.removeStorage({
  97. // key: `token`,
  98. // success: () => {
  99. // setTimeout(function() {
  100. // uni.reLaunch({
  101. // url: `/pages/home/dashboard/index`,
  102. // });
  103. // }, 500);
  104. // }
  105. // })
  106. return rej(data)
  107. } else {
  108. uni.showToast({
  109. title: data.msg || '返回失败',
  110. icon: 'none',
  111. duration: 3000
  112. })
  113. return rej(data)
  114. }
  115. },
  116. fail: error => {
  117. uni.hideLoading()
  118. if (error.errMsg && error.errMsg.indexOf('fail timeout') != -1 || error
  119. .errMsg && error.errMsg.indexOf('请求超时') != -1) {
  120. uni.showToast({
  121. title: error.errMsg,
  122. icon: 'none',
  123. duration: 2500
  124. })
  125. } else {
  126. // uni.showToast({
  127. // title: '服务维护中,请稍后尝试',
  128. // icon: 'none',
  129. // duration: 2500
  130. // });
  131. }
  132. return rej(error)
  133. }
  134. })
  135. })
  136. },
  137. // get请求
  138. get(url, data = {}, options = {}, needtoken = true) {
  139. options.url = url
  140. // 参数是否去空
  141. if (data.notFliter) {
  142. delete data.notFliter
  143. options.data = data
  144. } else {
  145. options.data = jsonFilterEmpty(data)
  146. }
  147. options.method = 'GET'
  148. options.token = needtoken
  149. return this.request(options)
  150. },
  151. // post请求
  152. post(url, data = {}, options = {}, needtoken = true) {
  153. options.url = url
  154. // 参数是否去空
  155. if (data.notFliter) {
  156. delete data.notFliter
  157. options.data = data
  158. } else {
  159. options.data = jsonFilterEmpty(data)
  160. }
  161. options.method = 'POST'
  162. options.token = needtoken
  163. return this.request(options)
  164. },
  165. // put请求
  166. put(url, data = {}, options = {}, needtoken = true) {
  167. options.url = url
  168. options.data = data
  169. options.method = 'PUT'
  170. options.token = needtoken
  171. return this.request(options)
  172. },
  173. // delete请求
  174. del(url, data = {}, options = {}, needtoken = true) {
  175. options.url = url
  176. options.data = data
  177. options.method = 'DELETE'
  178. options.token = needtoken
  179. return this.request(options)
  180. },
  181. // 上传文件
  182. upload(url, data, onProgress = false) {
  183. return new Promise((result, reject) => {
  184. // 上传
  185. let token = uni.getStorageSync('token')
  186. if (!token) {
  187. uni.showToast({
  188. title: '请先登录',
  189. icon: 'none'
  190. })
  191. // token不存在时跳转
  192. return uni.reLaunch({ url: '/pages/home/dashboard/index', })
  193. }
  194. // uni.showLoading({
  195. // title: '上传中...',
  196. // mask: true
  197. // });
  198. const uploadTask = uni.uploadFile({
  199. url: $C.baseUrl + url,
  200. filePath: data.filePath,
  201. name: data.name || 'files',
  202. header: {
  203. 'api-token': token,
  204. 'api-name': 'wxapp',
  205. },
  206. success: res => {
  207. uni.hideLoading()
  208. let message = JSON.parse(res.data)
  209. if (res.statusCode === 200) {
  210. result(message.data)
  211. } else if (res.statusCode === 401) {
  212. uni.showToast({
  213. title: message.msg,
  214. icon: 'none'
  215. })
  216. uni.reLaunch({ url: '/pages/home/dashboard/index', })
  217. reject(message)
  218. } else {
  219. uni.showToast({
  220. title: message.msg,
  221. icon: 'none'
  222. })
  223. result(false)
  224. }
  225. },
  226. fail: err => {
  227. uni.hideLoading()
  228. reject(err)
  229. }
  230. })
  231. uploadTask.onProgressUpdate(res => {
  232. if (typeof onProgress === 'function') {
  233. onProgress(res.progress)
  234. }
  235. })
  236. })
  237. }
  238. }