合伙人运营小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // api.jshttp://zlink.bjdexn.cn
  2. // const baseUrl = 'http://192.168.8.198:8888'; // 替换为您的接口基础 URL
  3. // const baseUrl = 'https://zlink.bjdexn.cn'; // 替换为您的接口基础 URL
  4. const baseUrl = 'https://esos-iot.bjdexn.cn'; // 替换为您的接口基础 URL
  5. // 封装网络请求方法
  6. function request(url, method, data) {
  7. return new Promise((resolve, reject) => {
  8. if (url!='/platform/login') {
  9. wx.getStorage({
  10. key: 'cookies',
  11. success: function(res) {
  12. wx.request({
  13. url: baseUrl + url,
  14. header: {
  15. 'Content-Type': 'application/x-www-form-urlencoded',
  16. Cookie:res.data},
  17. method: method,
  18. data:data,
  19. success:function(res) {
  20. // console.log(res);
  21. // 请求成功处理
  22. if (res.data.code === 0) {
  23. resolve(res.data);
  24. } else {
  25. reject(res.data.msg);
  26. if (res.data.status === 400) {
  27. wx.navigateTo({
  28. url: '/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
  29. });
  30. }else{
  31. wx.navigateTo({
  32. url: '/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
  33. });
  34. }
  35. }
  36. },
  37. fail:function(err) {
  38. console.log(err);
  39. wx.navigateTo({
  40. url: '/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
  41. });
  42. }
  43. });
  44. },
  45. fail: function(err) {
  46. console.log(err);
  47. wx.navigateTo({
  48. url: '/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
  49. });
  50. }
  51. })
  52. }else{
  53. wx.request({
  54. url: baseUrl + url,
  55. header: {
  56. 'Content-Type': 'application/x-www-form-urlencoded'},
  57. method: method,
  58. data:data,
  59. success:function(res) {
  60. // 请求成功处理
  61. if (res.statusCode === 200) {
  62. resolve(res.data);
  63. } else {
  64. reject(res.statusCode);
  65. }
  66. },
  67. fail:function(err) {
  68. wx.navigateTo({
  69. url: '/pages/login/index', // 登录页面的路径,注意路径前面加上 '/' 表示从根目录开始
  70. });
  71. }
  72. });
  73. }
  74. });
  75. }
  76. // 导出封装的请求方法
  77. module.exports = {
  78. request: request
  79. };
  80. // // 在需要使用网络请求的页面中引入封装的 api.js 文件
  81. // const api = require('api.js');
  82. // // 示例:发送一个 GET 请求
  83. // api.request('/users', 'GET')
  84. // .then((res) => {
  85. // console.log('请求成功:', res);
  86. // // 在这里处理返回的数据
  87. // })
  88. // .catch((err) => {
  89. // console.error('请求失败:', err);
  90. // // 在这里处理请求失败的情况
  91. // });
  92. // // 示例:发送一个 POST 请求
  93. // api.request('/users', 'POST', { name: 'John', age: 30 })
  94. // .then((res) => {
  95. // console.log('请求成功:', res);
  96. // // 在这里处理返回的数据
  97. // })
  98. // .catch((err) => {
  99. // console.error('请求失败:', err);
  100. // // 在这里处理请求失败的情况
  101. // });