request.js 3.3 KB

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