爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. import utils from '../utils/utils.js'
  2. import config from '../config.js' // 新增,导入全局配置
  3. function http(uri, data, callback, method = 'GET', showLoading, title) {
  4. if (showLoading) {
  5. uni.showLoading({
  6. title: typeof title === 'string' ? title : '加载中...'
  7. })
  8. }
  9. return new Promise((resolve, reject) => {
  10. uni.request({
  11. url: config.baseUrl + uri, // 使用 config.baseUrl
  12. data,
  13. method,
  14. header: {
  15. 'X-Access-Token': uni.getStorageSync('token'),
  16. 'Content-Type': 'application/x-www-form-urlencoded'
  17. },
  18. success: (res) => {
  19. if (showLoading) {
  20. uni.hideLoading()
  21. }
  22. if (
  23. res.statusCode == 401 ||
  24. res.data.message == '操作失败,token非法无效!' ||
  25. res.data.message == '操作失败,用户不存在!'
  26. ) {
  27. console.error('登录过期')
  28. utils.toLogin()
  29. }
  30. if (
  31. res.statusCode == 200 &&
  32. res.data.code != 200 &&
  33. res.data.code != 902
  34. ) {
  35. uni.showToast({
  36. mask: true,
  37. duration: 1000,
  38. title: res.data.message,
  39. icon: 'none'
  40. })
  41. }
  42. callback && callback(res.data)
  43. resolve(res.data)
  44. },
  45. fail: () => {
  46. reject('api fail')
  47. setTimeout(() => {
  48. uni.hideLoading()
  49. uni.showToast({ icon: 'none', title: '网络异常' })
  50. }, 3000)
  51. if (showLoading) {
  52. uni.hideLoading()
  53. }
  54. }
  55. })
  56. })
  57. }
  58. export default {
  59. http
  60. }