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.

91 lines
3.3 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import util from '../utils/util';
  2. // import { app as vm } from '../main';
  3. const islog = true;
  4. export class Server {
  5. request(url,data,method,header,isDefaultGet,failMsg){
  6. return new Promise(async (rs,rj)=>{
  7. // const _store = vm.$store;
  8. // let storeInfo = _store.state.storeInfo || null;
  9. // if(storeInfo)data['brand_id'] = storeInfo.brand_id;
  10. const _token = uni.getStorageSync('token') || '';
  11. if(_token)data['token'] = _token;
  12. if(method === 'POST'&&_token)url = url + `?token=${_token}`
  13. uni.request({
  14. url,
  15. data,
  16. method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  17. header: {...header, Pcassistant: 2}, // 设置请求的 header // {'custom-header': 'application/json'}
  18. success: res=>{
  19. if(islog)console.log('req success---->',{
  20. link: url,
  21. query: data,
  22. method: method,
  23. data: res,
  24. })
  25. if(isDefaultGet){
  26. if(failMsg == '')throw Error('默认回调,失败提示不能为空 key -> failMsg');
  27. defaultGet({
  28. url,
  29. data,
  30. res,
  31. failMsg,
  32. resolve: rs,
  33. reject: rj
  34. });
  35. return
  36. }
  37. rs(res);
  38. },
  39. fail: err=>{
  40. if(islog)console.log('req fail---->',{
  41. link: url,
  42. query: data,
  43. method: method,
  44. data: err,
  45. })
  46. util.hideLoad();
  47. if(typeof(err.errMsg) == 'string'&&(err.errMsg.indexOf('timeout')!=-1 || err.errMsg.indexOf('interrupted')!=-1 || err.errMsg.indexOf('请求超时')!=-1)){
  48. util.showNone('网络超时!');
  49. }else if(typeof(err.errMsg) == 'string'){
  50. util.showNone(err.errMsg);
  51. }else{
  52. util.showNone('请求数据失败!请检查当前网络状态。');
  53. }
  54. rj(err);
  55. },
  56. })
  57. })
  58. function defaultGet({res,failMsg,resolve,reject,url,data}){
  59. if(res.data.code == 0){
  60. resolve(res.data.data);
  61. }else{
  62. util.hideLoad();
  63. util.showNone(res.data.message || failMsg || '');
  64. reject({url,res,data});
  65. }
  66. }
  67. }
  68. get({url,data={},header={},isDefaultGet=true,failMsg=''}){
  69. return this.request(url,data,'GET',header,isDefaultGet,failMsg);
  70. }
  71. post({url,data={},header={'custom-header': 'application/json'},isDefaultGet=true,failMsg=''}){
  72. return this.request(url,data,'POST',header,isDefaultGet,failMsg);
  73. }
  74. uploadFile({url,filePath,onProgressCallBack,formData={}}){
  75. return new Promise((rs,rj)=>{
  76. let uploadTask = wx.uploadFile({
  77. url,filePath,formData,name:'file',success:rs,fail:rj
  78. })
  79. uploadTask.onProgressUpdate(res=>onProgressCallBack&&onProgressCallBack(res))
  80. })
  81. }
  82. }
  83. export const servers = new Server();
  84. export default { servers, Server };