request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import ext from "./ext.js";
  2. // 请求域名
  3. var domain = 'https://openwork.dfwy.tech/'
  4. //var domain = 'http://saas.com/'
  5. // #ifdef MP-WEIXIN
  6. // 'http://127.0.0.1:8000/';
  7. // domain = uni.getAccountInfoSync().miniProgram.envVersion == 'release' ? 'https://openwork.dfwy.tech/' : 'https://openwork.dfwy.tech/';
  8. domain = uni.getAccountInfoSync().miniProgram.envVersion == 'release' ? ext.getExtStoreId('release_host_url') : ext.getExtStoreId('host_url');
  9. var app_id = ext.getExtStoreId('app_id') ? ext.getExtStoreId('app_id') : '';
  10. // #endif
  11. // 发送网络请求的函数
  12. const request = (url, data = {}, method = 'GET') => {
  13. // 获取登录标识
  14. let userLogin = uni.getStorageSync('userLogin');
  15. let shopId = uni.getStorageSync("shopId");
  16. // 合并参数
  17. if( userLogin && userLogin.authcode ) data = Object.assign({authcode:userLogin.authcode},data);
  18. if (shopId) data = Object.assign({shop_id:shopId},data);
  19. if (app_id) data = Object.assign({app_id:app_id},data);
  20. // 封装
  21. return new Promise((resolve, reject) => {
  22. uni.request({
  23. url: domain+url,
  24. method: method,
  25. data: data,
  26. success: (res) => {
  27. // 登录提示
  28. if(res.data.code == 'no_login'){
  29. // 清空登录标识
  30. uni.setStorageSync('userLogin',null);
  31. // 清空用户信息
  32. uni.setStorageSync('userInfo',null);
  33. // 前去登录
  34. uni.showModal({
  35. title:"请登录",
  36. success(res){
  37. if (res.confirm) {
  38. // 用户点击确定按钮
  39. uni.navigateTo({
  40. url:"/pages/login/index"
  41. })
  42. }
  43. }
  44. })
  45. }
  46. // 返回结果
  47. resolve(res.data)
  48. },
  49. fail: (err) => {
  50. reject(err)
  51. }
  52. })
  53. })
  54. }
  55. const fileupload = (url, filePath,data = {}) =>{
  56. // 获取登录标识
  57. let userLogin = uni.getStorageSync('userLogin');
  58. // 合并参数
  59. if( userLogin && userLogin.authcode ) data = Object.assign({authcode:userLogin.authcode},data);
  60. if (app_id) data = Object.assign({app_id:app_id},data);
  61. // 封装
  62. return new Promise((resolve, reject) => {
  63. uni.uploadFile({
  64. url: domain+url, // 你的上传接口地址
  65. filePath: filePath,
  66. name: 'file', // 必须填写,为了后端接收文件流的参数名字
  67. formData: data,// 其他要上传的参数
  68. success: (res) => {
  69. // 登录提示
  70. if(res.data.code == 'no_login'){
  71. // 前去登录
  72. uni.showModal({
  73. title:"请登录",
  74. success(res){
  75. if (res.confirm) {
  76. // 用户点击确定按钮
  77. uni.navigateTo({
  78. url:"/pages/login/index"
  79. })
  80. }
  81. }
  82. })
  83. }
  84. // 转json,php返回可能会带bom头需要先替换
  85. let resdata = JSON.parse(res.data.replace('\uFEFF',''));
  86. resolve(resdata)
  87. },
  88. fail: (err) => {
  89. reject(err)
  90. }
  91. });
  92. })
  93. }
  94. // 字符串键值对转参数对象
  95. const strToParam = (str, separator = '&')=> {
  96. // 先转码
  97. str = decodeURIComponent(str);
  98. let pairs = str.split(separator);
  99. let result = {};
  100. pairs.forEach(pair => {
  101. let [key, value] = pair.split('=');
  102. result[key] = value;
  103. });
  104. // 返回结果
  105. return result;
  106. }
  107. // 模块导出,{name:object} 为对象式,导出多个使用
  108. // export default {request:request,request1:request1};
  109. // 单个对象,直接导出如下
  110. export default {request:request,fileupload:fileupload,strToParam:strToParam}