推广小程序前端代码
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.

86 lines
1.6 KiB

2 months ago
2 months ago
2 months ago
3 weeks ago
2 months ago
1 month ago
2 months ago
2 months ago
2 months ago
1 month ago
2 months ago
3 weeks ago
2 months ago
3 weeks ago
2 months ago
3 weeks ago
2 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: enhanceData(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. store.commit('logout')
  32. console.error('登录过期');
  33. utils.toLogin()
  34. }
  35. if(res.statusCode == 200 && res.data.code != 200
  36. && res.data.code != 902){
  37. uni.showToast({
  38. mask: true,
  39. duration: 1000,
  40. title: res.data.message,
  41. icon:'none'
  42. });
  43. }
  44. callback && callback(res.data)
  45. resolve(res.data)
  46. },
  47. fail: () => {
  48. reject('api fail')
  49. uni.showLoading({})
  50. setTimeout(()=>{
  51. uni.hideLoading()
  52. uni.showToast({icon:"none", title:"网络异常"})
  53. }, 3000)
  54. if(showLoading){
  55. uni.hideLoading();
  56. }
  57. }
  58. });
  59. return promise
  60. }
  61. function enhanceData(data) {
  62. const userid = uni.getStorageSync("userid")
  63. if (!data) {
  64. data = {}
  65. }
  66. if (userid) {
  67. data.userid = userid
  68. }
  69. return data
  70. }
  71. export default {
  72. http: http,
  73. }