艺易修小程序24.08.21
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.

143 lines
2.4 KiB

11 months ago
11 months ago
11 months ago
  1. import Vue from 'vue'
  2. function http(uri, data, callback, method = 'GET', showLoading, title) {
  3. if(showLoading){
  4. uni.showLoading({
  5. title: title || '正在提交...'
  6. });
  7. }
  8. uni.request({
  9. url: Vue.prototype.$config.baseUrl + uri,
  10. data: enhanceData(data),
  11. method: method,
  12. header: {
  13. 'X-Access-Token': uni.getStorageSync('token'),
  14. 'Content-Type' : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json'
  15. },
  16. success: (res) => {
  17. if(showLoading){
  18. uni.hideLoading();
  19. }
  20. if(res.statusCode == 401){
  21. localStorage.removeItem('token')
  22. localStorage.removeItem('userInfo')
  23. console.error('登录过期');
  24. uni.navigateTo({
  25. url: '/pages/login/login'
  26. })
  27. }
  28. //后端接口token出现问题
  29. if(res.data && res.data.code == 500 && res.data.message == '操作失败,token非法无效!'){
  30. localStorage.removeItem('token')
  31. localStorage.removeItem('userInfo')
  32. console.error('登录过期');
  33. uni.navigateTo({
  34. url: '/pages/login/login'
  35. })
  36. }
  37. if(res.statusCode == 200 && res.data.code != 200){
  38. uni.showToast({
  39. mask: true,
  40. duration: 1000,
  41. title: res.data.message,
  42. });
  43. }
  44. callback(res.data)
  45. },
  46. 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. function deleted(uri, data, callback) {
  59. http(uri, data, callback, 'DELETE')
  60. }
  61. function post(uri, data, callback) {
  62. http(uri, data, callback, 'POST')
  63. }
  64. function get(uri, data, callback) {
  65. http(uri, data, callback, 'GET')
  66. }
  67. function enhanceData(data) {
  68. const userid = uni.getStorageSync("userid")
  69. if (!data) {
  70. data = {}
  71. }
  72. if (userid) {
  73. data.userid = userid
  74. }
  75. return data
  76. }
  77. function sync(method, uri, data) {
  78. return new Promise((resolve, reject) => {
  79. uni.request({
  80. url: uri,
  81. data: data,
  82. method: method,
  83. header: {
  84. 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ'
  85. },
  86. success: (res) => {
  87. resolve(res.data)
  88. },
  89. fail: (err) => {
  90. reject(err);
  91. }
  92. })
  93. })
  94. }
  95. let cache = null
  96. function async (method, uri, data) {
  97. const promise = sync(method, uri, data).then(res => {
  98. cache = res
  99. }).catch(err => {
  100. })
  101. }
  102. function syncHttp(uri, data, method = 'GET') {
  103. async (method, uri, data)
  104. }
  105. export default {
  106. http: http,
  107. delete: deleted,
  108. post: post,
  109. get: get,
  110. syncHttp: syncHttp
  111. }