request.js 6.0 KB

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