request.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //请求头
  2. import { baseUrl } from '@/https/url.js'
  3. export default(url, method, params) => { // 传参: 接口url, method类型, params参数
  4. const header = {
  5. 'accept':'application/json, text/plain, */*',
  6. 'Content-type': 'application/json',
  7. 'Token':uni.getStorageSync('token')?uni.getStorageSync('token'):"",
  8. }
  9. function getParamet(data){
  10. data = data || {};
  11. if(uni.getStorageSync('chanId')){
  12. if(uni.getStorageSync('chanId')!='lh'){
  13. data.chanId = uni.getStorageSync('chanId')
  14. }
  15. }
  16. return data
  17. }
  18. return new Promise((resolve, reject) => {
  19. uni.request({
  20. method: method,
  21. url: url.indexOf('http') !== -1 ? url : baseUrl + url,
  22. data: method=='GET'?getParamet(params):JSON.stringify(getParamet(params)),
  23. header: header
  24. })
  25. .then((response) => {
  26. // 请求成功后的处理
  27. if(response[1].data.code==401){
  28. uni.removeStorageSync('token');
  29. // uni.showToast({
  30. // title: '登录失效,请刷新界面.',
  31. // duration: 2000,
  32. // icon: 'none'
  33. // });
  34. }
  35. resolve(response[1])
  36. // if(response[1].)
  37. }).catch((reject) => {
  38. // 请求失败后的处理
  39. console.log('请求失败')
  40. reject(reject)
  41. })
  42. })
  43. }