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.

131 lines
2.2 KiB

8 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.VITE_GLOB_API + uri,
  10. data: enhanceData(data),
  11. method: method,
  12. header: {
  13. 'X-Access-Token': localStorage.getItem('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. // uni.navigateTo({
  24. // url: '/pages/login/mobile'
  25. // })
  26. }
  27. if(res.statusCode == 200 && res.data.code != 200){
  28. // uni.showToast({
  29. // mask: true,
  30. // duration: 1000,
  31. // title: res.data.message,
  32. // });
  33. }
  34. callback(res.data)
  35. },
  36. fail: () => {
  37. uni.showLoading({})
  38. setTimeout(()=>{
  39. uni.hideLoading()
  40. uni.showToast({icon:"none", title:"网络异常"})
  41. }, 3000)
  42. if(showLoading){
  43. uni.hideLoading();
  44. }
  45. }
  46. });
  47. }
  48. function deleted(uri, data, callback){
  49. http(uri, data, callback, 'DELETE')
  50. }
  51. function post(uri, data, callback){
  52. http(uri, data, callback, 'POST')
  53. }
  54. function get(uri, data, callback){
  55. http(uri, data, callback, 'GET')
  56. }
  57. function enhanceData(data){
  58. const userid = uni.getStorageSync("userid")
  59. const enter_id = uni.getStorageSync("enter_id")
  60. if (!data){
  61. data = {}
  62. }
  63. if (userid){
  64. data.userid = userid
  65. }
  66. if (enter_id){
  67. data.enter_id = enter_id
  68. }
  69. return data
  70. }
  71. function sync(method, uri, data){
  72. return new Promise((resolve, reject) => {
  73. uni.request({
  74. url: uri,
  75. data: data,
  76. method: method,
  77. header: { 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ' },
  78. success: (res) => {
  79. resolve(res.data)
  80. },
  81. fail: (err) => {
  82. reject(err);
  83. }
  84. })
  85. })
  86. }
  87. let cache = null
  88. function async(method, uri, data){
  89. const promise = sync(method, uri, data).then(res => {
  90. cache = res
  91. }).catch(err => {
  92. })
  93. }
  94. function syncHttp(uri, data, method = 'GET'){
  95. async(method, uri, data)
  96. }
  97. export default {
  98. http: http,
  99. delete: deleted,
  100. post: post,
  101. get: get,
  102. syncHttp: syncHttp
  103. }