爱简收旧衣按件回收前端代码仓库
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.

67 lines
1.6 KiB

3 weeks ago
1 week ago
3 weeks ago
1 week ago
3 weeks ago
1 week ago
3 weeks ago
1 week 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: 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. uni.showLoading({})
  48. setTimeout(() => {
  49. uni.hideLoading()
  50. uni.showToast({ icon: 'none', title: '网络异常' })
  51. }, 3000)
  52. if (showLoading) {
  53. uni.hideLoading()
  54. }
  55. }
  56. })
  57. })
  58. }
  59. export default {
  60. http
  61. }