request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import MD5 from './md5.js'
  2. import env from '../config/env.js'
  3. const md5key = "3Jr8S1K18rcC1wAfv8"
  4. function getSignUrl(url, data, timestamp) {
  5. function randomStr20() {
  6. for (var e = "0123456789abcdef", t = "", n = 0; n < 20; n++) t += e[Math.floor(16 * Math.random())]
  7. return t
  8. }
  9. let nonce = randomStr20()
  10. let signStr = JSON.stringify(data) + 'nonce' + timestamp + '' + md5key
  11. const fulUrl = `${ env.apiBase }${ url }`
  12. return `${ fulUrl }${ fulUrl.indexOf('?') > -1 ? '&' : '?' }sign=${ MD5(signStr) }&nonce=${ nonce }`
  13. }
  14. const $http = (url, data, methods) => {
  15. return new Promise((reslove, reject) => {
  16. let timestamp = parseInt(new Date().getTime())
  17. uni.request({
  18. url: getSignUrl(url, data, timestamp),
  19. header: {
  20. 'Content-Type': 'application/json;charset=utf-8',
  21. 'Authorization': uni.getStorageSync('token') && !data.noToken ?
  22. `Bearer ${uni.getStorageSync('token')}` : '',
  23. 'x-zz-timestamp': timestamp
  24. },
  25. data: data || {},
  26. method: methods,
  27. success: res => {
  28. if (res.data && res.data.code === 401) {
  29. // uni.showToast({
  30. // title: res.data.msg || '授权过期,请重新登录。',
  31. // icon: 'none',
  32. // duration: 2000
  33. // })
  34. uni.removeStorageSync('token')
  35. uni.showModal({
  36. title: '提示',
  37. content: '登录身份已过期,请重新登录!',
  38. success(res) {
  39. if (res.confirm) {
  40. uni.navigateTo({
  41. url: "/pages/login/index"
  42. })
  43. }
  44. }
  45. })
  46. } else if (res.data && res.data.code !== 0 && res.data.code !== 1016) {
  47. uni.showToast({
  48. title: res.data.msg || '请求错误,请重试。',
  49. icon: 'none',
  50. duration: 2000
  51. })
  52. }
  53. reslove(res.data)
  54. },
  55. fail: () => {
  56. uni.showToast({
  57. title: '网络错误,请重试。',
  58. icon: 'none',
  59. duration: 2000
  60. })
  61. reject()
  62. }
  63. })
  64. })
  65. }
  66. const $upload = (fileUrl, type) => {
  67. return new Promise((reslove, reject) => {
  68. let timestamp = parseInt(new Date().getTime())
  69. uni.uploadFile({
  70. url: getSignUrl(`/api/v1/mp/image/remote/upload/post/${type || 0}`, {}, timestamp),
  71. header: {
  72. 'Content-Type': 'multipart/form-data',
  73. 'Authorization': uni.getStorageSync('token') ?
  74. `Bearer ${uni.getStorageSync('token')}` : '',
  75. 'x-zz-timestamp': timestamp
  76. },
  77. filePath: fileUrl,
  78. name: 'file',
  79. success: res => {
  80. reslove(JSON.parse(res.data))
  81. },
  82. fail: reject
  83. })
  84. })
  85. }
  86. const $put = (url, data) => {
  87. var formData = new FormData()
  88. Object.keys(data).forEach(key => {
  89. formData.append(key, data[key])
  90. })
  91. return new Promise((reslove, reject) => {
  92. let timestamp = parseInt(new Date().getTime())
  93. uni.uploadFile({
  94. url: getSignUrl(url, formData, timestamp),
  95. headers: {
  96. 'Content-Type': 'multipart/form-data',
  97. 'Authorization': uni.getStorageSync('token') ?
  98. `Bearer ${uni.getStorageSync('token')}` : '',
  99. 'x-zz-timestamp': timestamp
  100. },
  101. fileType: formData || {},
  102. name: 'file',
  103. success: res => {
  104. reslove(res.data)
  105. },
  106. fail: () => {
  107. reject()
  108. }
  109. })
  110. })
  111. }
  112. module.exports = {
  113. get: (url, data) => {
  114. return $http(url, data, 'GET')
  115. },
  116. post: (url, data) => {
  117. return $http(url, data, 'POST')
  118. },
  119. put: (url, data) => {
  120. return $put(url, data)
  121. },
  122. upload: (file, type) => {
  123. return $upload(file, type)
  124. },
  125. }