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