特易招,招聘小程序
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.

144 lines
2.4 KiB

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