request.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // 引入配置文件
  2. import config from "./config.js";
  3. export default{
  4. config:{
  5. baseUrl:config.webUrl,
  6. header:{
  7. 'Content-Type':'application/json;charset=UTF-8',
  8. 'Content-Type':'application/x-www-form-urlencoded'
  9. },
  10. data: {},
  11. method: "GET",
  12. dataType: "json",
  13. },
  14. request(options = {}){
  15. options.header = options.header || this.config.header;
  16. options.method = options.method || this.config.method;
  17. options.dataType = options.dataType || this.config.dataType;
  18. options.url = this.config.baseUrl+options.url;
  19. // TODO:token增加等操作
  20. if (options.token) {
  21. options.data.token = uni.getStorageSync("userinfo").token;
  22. options.data.uid = uni.getStorageSync("userinfo").id;
  23. }
  24. var version='web';
  25. // #ifdef APP-PLUS
  26. var version=plus.runtime.version
  27. // #endif
  28. options.data.version=version
  29. return uni.request(options).then(res => {
  30. if(res[1].data.msg=='version10001'){
  31. uni.showModal({
  32. title: '更新提示',
  33. content:"当前版本需要更新,请及时更新",
  34. showCancel: false,
  35. confirmText: "确定",
  36. success: function (ress) {
  37. plus.runtime.openURL(res[1].data.data);
  38. uni.reLaunch({
  39. url:'/pages/public/login'
  40. })
  41. }
  42. });
  43. return false;
  44. }
  45. return res;
  46. });
  47. },
  48. get(url,data,options={}){
  49. options.url = url;
  50. options.data = data;
  51. options.method = 'GET';
  52. return this.request(options);
  53. },
  54. post(url,data,options={}){
  55. options.url = url;
  56. options.data = data;
  57. options.method = 'POST';
  58. return this.request(options);
  59. },
  60. // 上传图片
  61. upload(url,options = {}){
  62. options.url = this.config.baseUrl+url;
  63. options.header = options.header || this.config.header;
  64. options.fileType = options.fileType || "image";
  65. options.formData = options.formData || {};
  66. options.filePath = options.filePath;
  67. options.name = options.name;
  68. // TODO:token增加等操作
  69. if (options.token) {
  70. // 验证是否登录
  71. if (!this.checkToken(options.checkToken)) return;
  72. // 验证权限
  73. if (!this.checkAuth(options.checkAuth)) return;
  74. options.header.token = User.token;
  75. }
  76. return uni.uploadFile(options);
  77. },
  78. // 错误处理
  79. errorCheck(err,res,errfun = false,resfun = false){
  80. if (err) {
  81. typeof errfun === 'function' && errfun();
  82. uni.showToast({ title: '加载失败',icon:"none" });
  83. return false;
  84. }
  85. if (res.data.errorCode) {
  86. typeof errfun === 'function' && resfun();
  87. uni.showToast({ title: res.data.msg,icon:"none" });
  88. return false;
  89. }
  90. return true;
  91. },
  92. // 验证用户是否登录
  93. checkToken(checkToken){
  94. if (checkToken && !User.token) {
  95. uni.showToast({ title: '请先登录', icon:"none" })
  96. uni.navigateTo({
  97. url: '/pages/login/login'
  98. });
  99. return false;
  100. }
  101. return true;
  102. },
  103. // 验证用户权限
  104. checkAuth(checkAuth){
  105. if (checkAuth && !User.userinfo.phone) {
  106. uni.showToast({ title: '请先绑定手机号码', icon:"none" })
  107. uni.navigateTo({
  108. url: '/pages/user-bind-phone/user-bind-phone'
  109. });
  110. return false;
  111. }
  112. return true;
  113. }
  114. }