景徳镇旅游微信小程序
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.2 KiB

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