request.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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') ?
  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.redirectTo({
  36. url: '/pages/login/entry',
  37. })
  38. } else if (res.data && res.data.code !== 0 && res.data.code !== 1016) {
  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. console.log(formData);
  81. Object.keys(data).forEach(key => {
  82. formData.append(key, data[key])
  83. })
  84. return new Promise((reslove, reject) => {
  85. let timestamp = parseInt(new Date().getTime())
  86. uni.uploadFile({
  87. url: getSignUrl(url, formData, timestamp),
  88. headers: {
  89. 'Content-Type': 'multipart/form-data',
  90. 'Authorization': uni.getStorageSync('token') ?
  91. `Bearer ${uni.getStorageSync('token')}` : '',
  92. 'x-zz-timestamp': timestamp
  93. },
  94. fileType: formData || {},
  95. name: 'file',
  96. success: res => {
  97. reslove(res.data)
  98. },
  99. fail: () => {
  100. reject()
  101. }
  102. })
  103. })
  104. }
  105. module.exports = {
  106. get: (url, data) => {
  107. return $http(url, data, 'GET')
  108. },
  109. post: (url, data) => {
  110. return $http(url, data, 'POST')
  111. },
  112. put: (url, data) => {
  113. return $put(url, data)
  114. },
  115. upload: (file, type) => {
  116. return $upload(file, type)
  117. },
  118. }