瑶都万能墙
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.

139 lines
2.3 KiB

1 year ago
11 months ago
1 year ago
7 months ago
1 year ago
7 months ago
1 year ago
7 months ago
1 year ago
11 months ago
1 year ago
1 year ago
  1. import Vue from 'vue'
  2. import utils from '../utils/utils.js'
  3. function http(uri, data, callback, method = 'GET', showLoading, title, req) {
  4. if(showLoading){
  5. uni.showLoading({
  6. title: title || '加载中...'
  7. });
  8. }
  9. let header = {
  10. 'X-Access-Token': uni.getStorageSync('token'),
  11. 'Content-Type' : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json'
  12. }
  13. uni.request({
  14. url: Vue.prototype.$config.baseUrl + uri,
  15. data: enhanceData(data),
  16. method: method,
  17. header : {
  18. ...header,
  19. ...req.header,
  20. },
  21. success: (res) => {
  22. if(showLoading){
  23. uni.hideLoading();
  24. }
  25. if(res.statusCode == 401 ||
  26. res.data.message == '操作失败,token非法无效!' ||
  27. res.data.message == '操作失败,用户不存在!'){
  28. uni.removeStorageSync('token')
  29. console.error('登录过期');
  30. utils.toLogin()
  31. }
  32. if(res.statusCode == 200 && res.data.code != 200){
  33. uni.showToast({
  34. mask: true,
  35. duration: 1000,
  36. title: res.data.message,
  37. icon:'none'
  38. });
  39. }
  40. callback(res.data)
  41. },
  42. fail: (e) => {
  43. console.log(e);
  44. uni.showLoading({})
  45. setTimeout(()=>{
  46. uni.hideLoading()
  47. uni.showToast({icon:"none", title:"网络异常"})
  48. }, 3000)
  49. if(showLoading){
  50. uni.hideLoading();
  51. }
  52. }
  53. });
  54. }
  55. function deleted(uri, data, callback) {
  56. http(uri, data, callback, 'DELETE')
  57. }
  58. function post(uri, data, callback) {
  59. http(uri, data, callback, 'POST')
  60. }
  61. function get(uri, data, callback) {
  62. http(uri, data, callback, 'GET')
  63. }
  64. function enhanceData(data) {
  65. const userid = uni.getStorageSync("userid")
  66. if (!data) {
  67. data = {}
  68. }
  69. if (userid) {
  70. data.userid = userid
  71. }
  72. return data
  73. }
  74. function sync(method, uri, data) {
  75. return new Promise((resolve, reject) => {
  76. uni.request({
  77. url: uri,
  78. data: data,
  79. method: method,
  80. header: {
  81. 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ'
  82. },
  83. success: (res) => {
  84. resolve(res.data)
  85. },
  86. fail: (err) => {
  87. reject(err);
  88. }
  89. })
  90. })
  91. }
  92. let cache = null
  93. function async (method, uri, data) {
  94. const promise = sync(method, uri, data).then(res => {
  95. cache = res
  96. }).catch(err => {
  97. })
  98. }
  99. function syncHttp(uri, data, method = 'GET') {
  100. async (method, uri, data)
  101. }
  102. export default {
  103. http: http,
  104. delete: deleted,
  105. post: post,
  106. get: get,
  107. syncHttp: syncHttp
  108. }