混凝土运输管理微信小程序、替班
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.

95 lines
1.5 KiB

1 week ago
  1. function http(uri, data, callback, method = 'GET'){
  2. uni.request({
  3. url: import.meta.env.VITE_GLOB_API + uri,
  4. data: enhanceData(data),
  5. method: method,
  6. header: { 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ' },
  7. success: (res) => {
  8. callback(res.data)
  9. },
  10. fail: () => {
  11. uni.showLoading({})
  12. setTimeout(()=>{
  13. uni.hideLoading()
  14. uni.showToast({icon:"none", title:"网络异常"})
  15. }, 3000)
  16. }
  17. });
  18. }
  19. function deleted(uri, data, callback){
  20. http(uri, data, callback, 'DELETE')
  21. }
  22. function post(uri, data, callback){
  23. http(uri, data, callback, 'POST')
  24. }
  25. function get(uri, data, callback){
  26. http(uri, data, callback, 'GET')
  27. }
  28. function enhanceData(data){
  29. const userid = uni.getStorageSync("userid")
  30. const enter_id = uni.getStorageSync("enter_id")
  31. if (!data){
  32. data = {}
  33. }
  34. if (userid){
  35. data.userid = userid
  36. }
  37. if (enter_id){
  38. data.enter_id = enter_id
  39. }
  40. return data
  41. }
  42. function sync(method, uri, data){
  43. return new Promise((resolve, reject) => {
  44. uni.request({
  45. url: uri,
  46. data: data,
  47. method: method,
  48. header: { 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ' },
  49. success: (res) => {
  50. resolve(res.data)
  51. },
  52. fail: (err) => {
  53. reject(err);
  54. }
  55. })
  56. })
  57. }
  58. let cache = null
  59. function async(method, uri, data){
  60. const promise = sync(method, uri, data).then(res => {
  61. cache = res
  62. }).catch(err => {
  63. })
  64. }
  65. function syncHttp(uri, data, method = 'GET'){
  66. async(method, uri, data)
  67. }
  68. export default {
  69. http: http,
  70. delete: deleted,
  71. post: post,
  72. get: get,
  73. syncHttp: syncHttp
  74. }