铝交易,微信公众号
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.

136 lines
2.3 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 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' : 'application/json'
  15. // 'Content-Type' : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json'
  16. },
  17. success: (res) => {
  18. if(showLoading){
  19. uni.hideLoading();
  20. }
  21. if(res.statusCode == 401 ||
  22. res.data.message == '操作失败,token非法无效!' ||
  23. res.data.message == '操作失败,用户不存在!'){
  24. uni.removeStorageSync('token')
  25. console.error('登录过期');
  26. uni.navigateTo({
  27. url: '/pages_order/auth/loginAndRegisterAndForgetPassword'
  28. })
  29. }
  30. if(res.statusCode == 200 && res.data.code != 200){
  31. uni.showToast({
  32. mask: true,
  33. duration: 1000,
  34. title: res.data.message,
  35. icon:'none'
  36. });
  37. }
  38. callback(res.data)
  39. },
  40. fail: () => {
  41. uni.showLoading({})
  42. setTimeout(()=>{
  43. uni.hideLoading()
  44. uni.showToast({icon:"none", title:"网络异常"})
  45. }, 3000)
  46. if(showLoading){
  47. uni.hideLoading();
  48. }
  49. }
  50. });
  51. }
  52. function deleted(uri, data, callback) {
  53. http(uri, data, callback, 'DELETE')
  54. }
  55. function post(uri, data, callback) {
  56. http(uri, data, callback, 'POST')
  57. }
  58. function get(uri, data, callback) {
  59. http(uri, data, callback, 'GET')
  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. 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: {
  78. 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ'
  79. },
  80. success: (res) => {
  81. resolve(res.data)
  82. },
  83. fail: (err) => {
  84. reject(err);
  85. }
  86. })
  87. })
  88. }
  89. let cache = null
  90. function async (method, uri, data) {
  91. const promise = sync(method, uri, data).then(res => {
  92. cache = res
  93. }).catch(err => {
  94. })
  95. }
  96. function syncHttp(uri, data, method = 'GET') {
  97. async (method, uri, data)
  98. }
  99. export default {
  100. http: http,
  101. delete: deleted,
  102. post: post,
  103. get: get,
  104. syncHttp: syncHttp
  105. }