request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.navigateTo({
  36. url: "/pages/login/index"
  37. })
  38. } else if (res.data && res.data.code !== 0 && res.data.code !== 1016 && res.data.code !== 1018 && res.data.code !== 1023 && res.data.code !== 1022) {
  39. uni.showToast({
  40. title: res.data.msg || '请求错误,请重试。',
  41. icon: 'none',
  42. duration: 2000
  43. })
  44. }
  45. reslove(res.data)
  46. },
  47. fail: () => {
  48. uni.showToast({
  49. title: '网络错误,请重试。',
  50. icon: 'none',
  51. duration: 2000
  52. })
  53. reject()
  54. }
  55. })
  56. })
  57. }
  58. const $upload = (fileUrl, type) => {
  59. return new Promise((reslove, reject) => {
  60. let timestamp = parseInt(new Date().getTime())
  61. uni.uploadFile({
  62. url: getSignUrl(`/api/v1/mp/image/remote/upload/post/${type || 0}`, {}, timestamp),
  63. header: {
  64. 'Content-Type': 'multipart/form-data',
  65. 'Authorization': uni.getStorageSync('token') ?
  66. `Bearer ${uni.getStorageSync('token')}` : '',
  67. 'x-zz-timestamp': timestamp
  68. },
  69. filePath: fileUrl,
  70. name: 'file',
  71. success: res => {
  72. reslove(JSON.parse(res.data))
  73. },
  74. fail: reject
  75. })
  76. })
  77. }
  78. const $put = (url, data) => {
  79. var formData = new FormData()
  80. Object.keys(data).forEach(key => {
  81. formData.append(key, data[key])
  82. })
  83. return new Promise((reslove, reject) => {
  84. let timestamp = parseInt(new Date().getTime())
  85. uni.uploadFile({
  86. url: getSignUrl(url, formData, timestamp),
  87. headers: {
  88. 'Content-Type': 'multipart/form-data',
  89. 'Authorization': uni.getStorageSync('token') ?
  90. `Bearer ${uni.getStorageSync('token')}` : '',
  91. 'x-zz-timestamp': timestamp
  92. },
  93. fileType: formData || {},
  94. name: 'file',
  95. success: res => {
  96. reslove(res.data)
  97. },
  98. fail: () => {
  99. reject()
  100. }
  101. })
  102. })
  103. }
  104. module.exports = {
  105. get: (url, data) => {
  106. return $http(url, data, 'GET')
  107. },
  108. post: (url, data) => {
  109. return $http(url, data, 'POST')
  110. },
  111. put: (url, data) => {
  112. return $put(url, data)
  113. },
  114. upload: (file, type) => {
  115. return $upload(file, type)
  116. },
  117. }