request.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {signVal,md5} from "./MD5.js"
  2. import {
  3. getGuid,
  4. getTimeStamp
  5. } from './guid'
  6. const successCode = 200
  7. /**
  8. * 简易封装网络请求
  9. * @param {*} options
  10. */
  11. export default function fetch(options) {
  12. const {
  13. url,
  14. payload = {},
  15. method = 'GET',
  16. showToast = true
  17. } = options
  18. const client = '2'
  19. const guid = getGuid() + ''
  20. const timestamp = getTimeStamp() + ''
  21. const header = {
  22. // "x-token": uni.getStorageSync('token'),
  23. // "client": client,
  24. // "guid": guid,
  25. // "timestamp": timestamp,
  26. // "sig": md5(client + 'lz2019_random' + guid + timestamp),
  27. // "x-app-id": "lzyd",
  28. // "authorization": uni.getStorageSync('token')?uni.getStorageSync('token'):''
  29. }
  30. if (method === 'POST') {
  31. header['content-type'] = 'application/json'
  32. } else if (method === 'get') {
  33. header['content-type'] = 'application/x-www-form-urlencoded'
  34. }
  35. return new Promise((resolve, reject) => {
  36. return uni.request({
  37. url,
  38. method,
  39. data: signVal(payload),
  40. header: header,
  41. success:(res) => {
  42. resolve(res.data)
  43. },
  44. fail: (err) => {
  45. reject("")
  46. uni.showToast({
  47. title: '请求失败',
  48. icon: 'none'
  49. })
  50. }
  51. })
  52. }).catch((e) => {})
  53. }