裂变星小程序-25.03.04
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.

75 lines
1.4 KiB

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