api.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. let isToLoginSend = false;
  2. var requestTask = null
  3. var elabprojectname = ''
  4. var elabsystem = ''
  5. var elabEnvironment = '';
  6. import MinCache from '../static/cache.js'
  7. export const myRequest = (options) => {
  8. if (!options.hasOwnProperty("showLoading")) {
  9. var localDate = uni.getStorageSync('loadingStamp') || 0
  10. var date = new Date().getTime()
  11. if (date - localDate > 2000) {
  12. uni.setStorageSync('loadingStamp', date)
  13. uni.showLoading({
  14. mask: true,
  15. title: "正在加载中"
  16. })
  17. }
  18. }
  19. let token = (new MinCache()).get('_token_owner_union')
  20. if (token) {
  21. options.token = token
  22. } else {
  23. options.token = null
  24. }
  25. let BASE_URL = getApp().globalData.BASE_URL;
  26. return new Promise((resolve, reject) => {
  27. requestTask = uni.request({
  28. url: BASE_URL + options.url,
  29. method: options.method || "POST",
  30. data: options.data || {},
  31. header: {
  32. 'token': options.token || null,
  33. },
  34. success: res => {
  35. uni.hideLoading()
  36. uni.hideNavigationBarLoading();
  37. uni.stopPullDownRefresh();
  38. if(res.data.errorCode=='401'){
  39. uni.$emit('unLogin')
  40. }
  41. resolve(res)
  42. },
  43. fail: (err) => {
  44. uni.hideLoading()
  45. uni.hideNavigationBarLoading();
  46. uni.stopPullDownRefresh();
  47. reject(err)
  48. }
  49. })
  50. })
  51. }