zhangzengfei
2022-09-05 fa6247894dd69aa63daa26f12b1a6f99cacbdabb
static/api/api.js
@@ -1,64 +1,63 @@
export default class Api {
   syncRequest = function(option){
      //正式
      const BASEURL = 'http://111.205.161.131:5116';
      //测试
      //const BASEURL = 'http://192.168.20.10:7006';
      //处理请求url
      if(option.url.indexOf('weather')<0 && option.url.indexOf('smartai')<0){
         option.url = BASEURL+option.url;
      }
      option.method = option.method || "GET"
      option.header = option.header || {}
      option.data = option.data || {}
      option.loading = option.loading ? true : false
      option.loadtext = option.loadtext || '加载中...'
      if(option.method == 'GET'){
         option.header['Content-Type'] = 'application/json;charset=UTF-8'
      }else{
         option.header['Content-Type'] = 'application/x-www-form-urlencoded'
         option.header['Content-Type'] = 'application/json'
      }
      if(option.loading){
         uni.showLoading({
            mask: true,
            title: option.loadtext
         });
      }
      return new Promise((resolve,reject)=>{
         uni.request({
            url: option.url,
            method: option.method,
            header: option.header,
            data: option.data,
            success: (res)=>{
               if(option.loading){
                  uni.hideLoading()
               }
               console.log(res)
               if(res.statusCode==200){
                  resolve(res)
               }else{
                  uni.showToast({
                     icon: "none",
                     title: "数据获取失败,请稍后重试"
                  });
               }
            },
            fail: (err)=>{
               console.log(err)
               if (option.loading) {
                  uni.hideLoading()
               }
               uni.showToast({
                  icon: "none",
                  title: "数据获取失败,请稍后重试"
               })
               reject(err)
            }
         })
      })
   }
export default class Api {
   syncRequest = function(option){
      //正式
      const BASEURL = 'http://yyparking.smartai.com:5116';
      //测试
      // const BASEURL = 'http://58.118.176.201:5116';
      if(option.url.indexOf('weather')<0 && option.url.indexOf('smartai')<0){
         option.url = BASEURL+option.url;
      }
      option.method = option.method || "GET"
      option.header = option.header || {}
      option.data = option.data || {}
      option.loading = option.loading ? true : false
      option.loadtext = option.loadtext || '加载中...'
      if(option.method == 'GET'){
         option.header['Content-Type'] = 'application/json;charset=UTF-8'
      }else{
         option.header['Content-Type'] = 'application/x-www-form-urlencoded'
         option.header['Content-Type'] = 'application/json'
      }
      if(option.loading){
         uni.showLoading({
            mask: true,
            title: option.loadtext
         });
      }
      return new Promise((resolve,reject)=>{
         uni.request({
            url: option.url,
            method: option.method,
            header: option.header,
            data: option.data,
            success: (res)=>{
               if(option.loading){
                  uni.hideLoading()
               }
               console.log(res)
               if(res.statusCode==200){
                  resolve(res)
               }else{
                  uni.showToast({
                     icon: "none",
                     title: "数据获取失败,请稍后重试"
                  });
               }
            },
            fail: (err)=>{
               console.log(err)
               if (option.loading) {
                  uni.hideLoading()
               }
               uni.showToast({
                  icon: "none",
                  title: "数据获取失败,请稍后重试"
               })
               reject(err)
            }
         })
      })
   }
}