http.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.apidicttree().then(res => {
  68. const cObj = res || {}
  69. let newDict = {}
  70. for (let k in cObj) {
  71. const cArr = cObj[k].map(item => {
  72. return {
  73. ...item,
  74. key: item.dict_label,
  75. val: item.dict_value
  76. }
  77. })
  78. newDict[k] = cArr
  79. }
  80. uni.setStorageSync('MD_dict', newDict)
  81. })
  82. uni.api.base.apiuserinfo().then(res2 => {
  83. uni.setStorageSync('MD_userInfo2', res2)
  84. uni.reLaunch({
  85. url: '/pages/index/index'
  86. })
  87. })
  88. }
  89. })
  90. }
  91. }
  92. })
  93. } else {
  94. uni.showToast({
  95. title: `请先登录~`,
  96. icon: 'none',
  97. })
  98. uni.navigateTo({
  99. url: '/pages/user/login/login'
  100. })
  101. }
  102. break
  103. default:
  104. uni.showModal({
  105. title: '提示',
  106. content: `${cData.errmsg}(${code})`,
  107. })
  108. return reject(cData)
  109. }
  110. // resolve()
  111. },
  112. fail: err => {
  113. console.log(err)
  114. reject(err)
  115. }
  116. })
  117. })
  118. }
  119. // export default Http
  120. function request (url, data, loadingStr, queryData) {
  121. return Http({
  122. url,
  123. data,
  124. loadingStr,
  125. queryData,
  126. })
  127. }
  128. export {
  129. request,
  130. }