request.js 993 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const request = (url, data, method = 'GET') => {
  2. return new Promise((resolve, reject) => {
  3. let header = {
  4. 'Content-Type': 'application/json',
  5. }
  6. const res = uni.getStorageSync('storage_key');
  7. // const token = res.token
  8. const token = 'eyJ0eXAiOiJKc29uV2ViVG9rZW4iLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiTEJZIiwidG9rZW5fdHlwZSI6InRva2VuIiwidXNlcmlkIjoiMTMxNDc2OTM4MjQ5NjI3MjM4NCIsImFjY291bnQiOiJvQm56ODRoR3g3QTh2VVBqakNLWEJnQUhLb29nIiwiZXhwIjoxNjAyMzM5NDc3LCJuYmYiOjE2MDIzMTA2Nzd9.TyrNEt6F_tY36xtiLULCiExV2LaS7n_TUO-fb578Ae4'
  9. if (token) {
  10. header['token'] = 'Bearer ' + token
  11. }
  12. uni.request({
  13. url: url,
  14. data: data,
  15. method: method,
  16. header: header,
  17. success: res => {
  18. if (res.statusCode == 200) {
  19. let data = res.data
  20. if (data.code == 0 || data.code == 200) {
  21. resolve(res.data)
  22. } else {
  23. reject(res)
  24. }
  25. } else {
  26. reject(res)
  27. }
  28. },
  29. fail: res => {
  30. reject(res)
  31. }
  32. })
  33. });
  34. }
  35. module.exports = {
  36. request: request
  37. }