http.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { noEmpty } from '@utils'
  2. const Http = (options = { data: {} }) => {
  3. return new Promise((resolve, reject) => {
  4. if (options.loadingStr === 'loading') {
  5. uni.showLoading({
  6. mask: true,
  7. title: '加载中..'
  8. })
  9. }
  10. let token = uni.getStorageSync('MD_token') || ''
  11. let header = {
  12. 'Content-Type': 'application/json',
  13. 'token': token,
  14. // 'Content-Type': 'application/x-www-form-urlencoded'
  15. }
  16. let params = {...options.data}
  17. if (uni.getStorageSync('APP_token')) params.token = uni.getStorageSync('APP_token')
  18. for (let key in options.data) {
  19. if (options.data[key] === undefined || options.data[key] === 'undefined') {
  20. params[key] = ''
  21. }
  22. }
  23. let url = uni.baseUrl + options.url
  24. // let url = options.url
  25. if (options.queryData) {
  26. let qStr = ''
  27. for(let k in options.queryData) {
  28. qStr += `${k}=${options.queryData[k]}&`
  29. }
  30. qStr = qStr.substr(0, qStr.length - 1)
  31. url += `?${qStr}`
  32. }
  33. uni.request({
  34. url,
  35. data: {
  36. ...params
  37. },
  38. header,
  39. method: options.method || 'POST',
  40. complete: res => {
  41. if (options.loadingStr === 'loading') {
  42. uni.hideLoading()
  43. }
  44. },
  45. success: res => {
  46. const { data: cData } = res
  47. const code = cData.errno
  48. switch (code) {
  49. case 0:
  50. return resolve(noEmpty(cData.data) ? cData.data : cData)
  51. break
  52. case 401:
  53. case 404:
  54. case 405:
  55. uni.removeStorageSync('MD_userInfo')
  56. const u2 = uni.getStorageSync('MD_userInfo2')
  57. if (u2.bind_wechat === '1' && uni.getStorageSync('MD_token')) {
  58. uni.login({
  59. success: function (res) {
  60. if (res.code) {
  61. uni.api.base.apiwxautologin({code: res.code}).then(cData => {
  62. if (cData.token === 'error') {
  63. uni.removeStorageSync('MD_token')
  64. } else {
  65. uni.setStorageSync('MD_userInfo', cData)
  66. uni.setStorageSync('MD_token', cData.token)
  67. uni.api.base.apiuserinfo().then(res2 => {
  68. uni.setStorageSync('MD_userInfo2', res2)
  69. uni.reLaunch({
  70. url: '/pages/index/index'
  71. })
  72. })
  73. }
  74. })
  75. }
  76. }
  77. })
  78. } else {
  79. uni.showToast({
  80. title: `请先登录~`,
  81. icon: 'none',
  82. })
  83. uni.navigateTo({
  84. url: '/pages/user/login/login'
  85. })
  86. }
  87. break
  88. default:
  89. uni.showModal({
  90. title: '提示',
  91. content: `${cData.errmsg}(${code})`,
  92. })
  93. return reject(cData)
  94. }
  95. // resolve()
  96. },
  97. fail: err => {
  98. console.log(err)
  99. reject(err)
  100. }
  101. })
  102. })
  103. }
  104. // export default Http
  105. function request (url, data, loadingStr, queryData) {
  106. return Http({
  107. url,
  108. data,
  109. loadingStr,
  110. queryData,
  111. })
  112. }
  113. export {
  114. request,
  115. }