request.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. if (!options.msgFlag) {
  109. uni.showToast({
  110. title: data.msg || '返回失败',
  111. icon: 'none',
  112. duration: 3000
  113. })
  114. }
  115. return rej(data)
  116. }
  117. },
  118. fail: error => {
  119. uni.hideLoading()
  120. if (error.errMsg && error.errMsg.indexOf('fail timeout') != -1 || error
  121. .errMsg && error.errMsg.indexOf('请求超时') != -1) {
  122. uni.showToast({
  123. title: error.errMsg,
  124. icon: 'none',
  125. duration: 2500
  126. })
  127. } else {
  128. // uni.showToast({
  129. // title: '服务维护中,请稍后尝试',
  130. // icon: 'none',
  131. // duration: 2500
  132. // });
  133. }
  134. return rej(error)
  135. }
  136. })
  137. })
  138. },
  139. // get请求
  140. get(url, data = {}, options = {}, needtoken = true) {
  141. options.url = url
  142. // 参数是否去空
  143. if (data.notFliter) {
  144. delete data.notFliter
  145. options.data = data
  146. } else {
  147. options.data = jsonFilterEmpty(data)
  148. }
  149. options.method = 'GET'
  150. options.token = needtoken
  151. return this.request(options)
  152. },
  153. // post请求
  154. post(url, data = {}, options = {}, needtoken = true) {
  155. options.url = url
  156. // 参数是否去空
  157. if (data.notFliter) {
  158. delete data.notFliter
  159. options.data = data
  160. } else {
  161. options.data = jsonFilterEmpty(data)
  162. }
  163. options.method = 'POST'
  164. options.token = needtoken
  165. return this.request(options)
  166. },
  167. // put请求
  168. put(url, data = {}, options = {}, needtoken = true) {
  169. options.url = url
  170. options.data = data
  171. options.method = 'PUT'
  172. options.token = needtoken
  173. return this.request(options)
  174. },
  175. // delete请求
  176. del(url, data = {}, options = {}, needtoken = true) {
  177. options.url = url
  178. options.data = data
  179. options.method = 'DELETE'
  180. options.token = needtoken
  181. return this.request(options)
  182. },
  183. // 上传文件
  184. upload(url, data, onProgress = false) {
  185. return new Promise((result, reject) => {
  186. // 上传
  187. let token = uni.getStorageSync('token')
  188. if (!token) {
  189. uni.showToast({
  190. title: '请先登录',
  191. icon: 'none'
  192. })
  193. // token不存在时跳转
  194. return uni.reLaunch({ url: '/pages/home/dashboard/index', })
  195. }
  196. // uni.showLoading({
  197. // title: '上传中...',
  198. // mask: true
  199. // });
  200. const uploadTask = uni.uploadFile({
  201. url: $C.baseUrl + url,
  202. filePath: data.filePath,
  203. name: data.name || 'files',
  204. header: {
  205. 'api-token': token,
  206. 'api-name': 'wxapp',
  207. },
  208. success: res => {
  209. uni.hideLoading()
  210. let message = JSON.parse(res.data)
  211. if (res.statusCode === 200) {
  212. result(message.data)
  213. } else if (res.statusCode === 401) {
  214. uni.showToast({
  215. title: message.msg,
  216. icon: 'none'
  217. })
  218. uni.reLaunch({ url: '/pages/home/dashboard/index', })
  219. reject(message)
  220. } else {
  221. uni.showToast({
  222. title: message.msg,
  223. icon: 'none'
  224. })
  225. result(false)
  226. }
  227. },
  228. fail: err => {
  229. uni.hideLoading()
  230. reject(err)
  231. }
  232. })
  233. uploadTask.onProgressUpdate(res => {
  234. if (typeof onProgress === 'function') {
  235. onProgress(res.progress)
  236. }
  237. })
  238. })
  239. }
  240. }