耀实惠小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

  1. let config = require('./config.js');
  2. import functions from './functions.js';
  3. function request(url, param = {}, way = 'POST', contentType = true) {
  4. // 获取保存的token
  5. let token = uni.getStorageSync("token");
  6. let data = param || {} ;
  7. let header = {};
  8. if (contentType == true) {
  9. header = {
  10. "content-type": "application/x-www-form-urlencoded",
  11. "X-Access-Token": token
  12. }
  13. } else {
  14. header = {
  15. "content-type": "application/json;charset=UTF-8",
  16. "X-Access-Token": token
  17. }
  18. }
  19. console.log(token)
  20. // console.info("config",config)
  21. return new Promise((resolve, reject) => {
  22. uni.request({
  23. url: config.API_URL + url,
  24. data: data,
  25. header: header,
  26. method: way,
  27. success: (response) => {
  28. let result = response.data;
  29. if (result.code == 200) {
  30. resolve(result);
  31. }else if (result.code == 401) {
  32. if(url == 'info/info' && data.pages == 'home'){
  33. // resolve(result);
  34. }else{
  35. uni.showToast({
  36. icon:'none',
  37. title:'登录过期,请重新登陆'
  38. })
  39. setTimeout(()=>{
  40. uni.clearStorageSync()
  41. uni.navigateTo({
  42. url: "/pages/login/login"
  43. })
  44. },1000)
  45. }
  46. }else if (result.code == 500) {
  47. functions.showToast(result.message)
  48. if(result.message.indexOf("token") > -1){
  49. setTimeout(()=>{
  50. uni.clearStorageSync()
  51. uni.navigateTo({
  52. url: "/pages/login/login"
  53. })
  54. },1000)
  55. }
  56. resolve(result);
  57. } else {
  58. reject(result);
  59. }
  60. },
  61. fail: (error) => {
  62. functions.showToast('数据加载异常!');
  63. }
  64. });
  65. })
  66. }
  67. export default request