api.service.ts 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
import Vue from "vue";
import axios, { AxiosRequestConfig } from "axios";
import VueAxios from "vue-axios";
import { MethodType } from "@/constants/enum/general/method-type.enum";
import { HeaderType } from "@/constants/enum/general/header-type.enum";
import { statusCode } from "@/constants/enum/general/status-code.enum";
import { tokenStore } from "@/stores/index";
import { NativeUI } from '@/public/ts/NativeUI';
import SMUtil from '@/public/ts/SMEncryt';
import { Code } from "@/constants/enum/general/code.enum";
import router from "@/router";
// import emUtil from '../js/em.util';

interface Query {
  [key: string]: any;
}

/**
 * @description: REST请求服务
 * @author ChenRui
 * @date 2020/7/29 18:45
 */
class ApiService {
  /**
   * @description: 初始化
   * @author ChenRui
   * @date 2020/8/29 23:36
   */
  init() {
    Vue.use(VueAxios, axios);
//http request 拦截器
    Vue.axios.interceptors.request.use(
      config => {
        config.headers['Access-Control-Allow-Origin'] = '*';

        if(!config.headers['Content-Type']){
          config.headers['Content-Type'] = 'application/json; charset=utf-8';
        }
        const token = sessionStorage.getItem('accessToken');
        if (token && token != '') {
          config.headers.Authorization = 'bearer ' + token;
        }
        return Promise.resolve(config);
      },
      error => {
        console.error(error);
        return Promise.reject(error);
      }
    );

    const requestConfig: any = this.createBasicHeaders();
    Object.keys(requestConfig.headers).forEach(function (key) {
      Vue.axios.defaults.headers.common[key] = requestConfig.headers[key];
    });
    Vue.axios.interceptors.request.use(
      (config) => {
        // Do something before request is sent
        // if (this.verificationToken(config)) {
        //   return Promise.resolve(config);
        // } else {
        //   return oauthService.refreshToken().then((response: RestfulResponse) => {
        //     if (response) {
        //       config.headers.Authorization = "bearer " + tokenStore.token?.access_token;
        //       config.headers["user-identity"] = "bearer " + tokenStore.token?.jti;
        //       return config;
        //     } else {
        //       return oauthService.logOut().then();
        //     }
        //   });
        // }
        return Promise.resolve(config);
      },
      (error) => {
        // Do something with request error
        console.log("Do something with request error");
        return Promise.reject(error);
      }
    );
  }

  /**
   * @description:  创建基础消息头
   * @author ChenRui
   * @date 2020/8/28 15:18
   */
  createBasicHeaders(): any {
    return {
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
      },
    };
  }

  /**
   * @description: 创建认证消息头
   * @author ChenRui
   * @date 2020/8/28 15:18
   */
  createAuthHeaders(): any {
    return {
      headers: {
        Authorization: "bearer " + tokenStore.token?.access_token,
        "user-identity": tokenStore.token?.jti,
        "Content-Type": "application/json",
        Accept: "application/json",
      },
    };
  }

  /**
   * @description: 创建上传认证消息头
   * @author ChenRui
   * @date 2021/4/6 11:17
   */
  createFileUploadAuthorizationHeader() {
    return {
      headers: {
        Accept: "application/json",
        Authorization: "bearer " + tokenStore.token?.access_token,
        "user-identity": tokenStore.token?.jti,
        "Content-type": "application/x-www-form-urlencoded"
      },
    };
  }

  /**
   * @description: 创建认证下载消息头
   * @author ChenRui
   * @date 2020/8/28 15:18
   */
  createFileDownloadAuthorizationHeader() {
    return {
      headers: {
        "Content-Type": "application/json",
        Authorization: "bearer " + tokenStore.token?.access_token,
        "user-identity": tokenStore.token?.jti,
      },
      responseType: "blob",
    };
  }
  /**
   * @param headerType
   * @param headerConfig
   */
  getRequestConfig(headerType: any, requestConfig: any) {
    requestConfig = requestConfig || {};
    let requestConfigBase = {};
    switch (headerType) {
      case HeaderType.BASE.code:
        requestConfigBase = this.createBasicHeaders();
        break;
      case HeaderType.AUTH.code:
        requestConfigBase = this.createAuthHeaders();
        break;
      case HeaderType.UPLOAD_AUTH.code:
        requestConfigBase = this.createFileUploadAuthorizationHeader();
        break;
      case HeaderType.DOWNLOAD_AUTH.code:
        requestConfigBase = this.createFileDownloadAuthorizationHeader();
        break;
    }
    Object.keys(requestConfig).forEach((value: string, index: number, array: string[]) => {
      requestConfigBase[value] = requestConfig[value];
    });
    return requestConfigBase;
  }
  get(path: string, query: Query | undefined, requestConfig: AxiosRequestConfig): any {
     
     path = query != null ? this.urlQueryConvert(path, query) : path;
    return Vue.axios.get(`${path}`, requestConfig).then(this.createBusCodeHandler(query)).catch(this.createErrorHandler());
  }

  post(path: string, params: any, query: Query | undefined, requestConfig: AxiosRequestConfig): any {

    path = query != null ? this.urlQueryConvert(path, query) : path;
    console.log("服务调用--" + path);

    // return Vue.axios.post(`${path}`, params, requestConfig).then(this.createBusCodeHandler(query)).catch(this.createErrorHandler());

     return Vue.axios.post(`${path}`, params, requestConfig).then(res=>{
      //TODO
      return res.data
    });
  }
  /**
   * POST请求 文件上传
   * @param path
   * @param params
   * @param requestConfig
   * @returns
   */
  uploadFile(path: string, params: any, requestConfig: any) {
    return Vue.axios.post(`${path}`, params, requestConfig).then(this.createBusCodeHandler(params)).catch(this.createErrorHandler());
  }
  put(path: string, params: any, query: Query | undefined, requestConfig: AxiosRequestConfig): any {
    path = query != null ? this.urlQueryConvert(path, query) : path;
    return Vue.axios.put(`${path}`, params, requestConfig).then(this.createBusCodeHandler()).catch(this.createErrorHandler());
  }

  delete(path: string, query: Query | undefined, requestConfig: AxiosRequestConfig): any {
    path = query != null ? this.urlQueryConvert(path, query) : path;
    return Vue.axios.delete(path, requestConfig).then(this.createBusCodeHandler()).catch(this.createErrorHandler());
  }

  /**
   * @description: 通用请求函数
   * @author ChenRui
   * @date 2020/8/28 15:19
   * @param api               调用接口的对象
   * @param query             问号?后参数
   * @param params            post、put、delete参数 非必须
   * @param requestConfig     请求配置,如果上传文件、form提交等配置 非必须
   */
  general(api: any, query?: Query | undefined, params?: any | undefined, requestConfig?: AxiosRequestConfig | any): any {
    if (!!api.url && !!api.method) {
      if (requestConfig == null) {

        switch (api.header) {
          case HeaderType.BASE.code:
            requestConfig = this.createBasicHeaders();
            break;
          case HeaderType.AUTH.code:
            requestConfig = this.createAuthHeaders();
            break;
          case HeaderType.UPLOAD_AUTH.code:
            requestConfig = this.createFileUploadAuthorizationHeader();
            break;
          case HeaderType.DOWNLOAD_AUTH.code:
            requestConfig = this.createFileDownloadAuthorizationHeader();
            break;
        }
      } else {
        let requestConfigBase: any = {};
        switch (api.header) {
          case HeaderType.BASE.code:
            requestConfigBase = this.createBasicHeaders();
            break;
          case HeaderType.AUTH.code:
            requestConfigBase = this.createAuthHeaders();
            break;
          case HeaderType.UPLOAD_AUTH.code:
            requestConfigBase = this.createFileUploadAuthorizationHeader();
            break;
          case HeaderType.DOWNLOAD_AUTH.code:
            requestConfigBase = this.createFileDownloadAuthorizationHeader();
            break;
        }
        Object.keys(requestConfig).forEach((value, index, array) => {
          requestConfigBase[value] = requestConfig[value];
        });
        requestConfig = requestConfigBase;
      }
      switch (api.method) {
        case MethodType.GET.code:
          return this.get(api.url, query, requestConfig);
          break;
        case MethodType.PUT.code:
          return this.put(api.url, params, query, requestConfig);
          break;
        case MethodType.POST.code:
          return this.post(api.url, params, query, requestConfig);
          break;
        case MethodType.DELETE.code:
          return this.delete(api.url, query, requestConfig);
          break;
      }
    }
  }

  /**
   * 服务返回统一解密处理
   */
  createBusCodeHandler(params?: any) {
    return function(response: any) {
      
      if (response.status === 200) {
        let data = response.data;
        
        console.log("返回统一解密处理",params)
        if (params.tc === 'MCEP') {
          let encryData = data.data || {};
          console.log('返回原数据:', data);
          //移动后台
          if (typeof encryData != 'object') {
            encryData = JSON.parse(encryData);
          }
          
          // //三方数据 
          // encryData.sm4key ? (data.data = SMUtil.decodeData(encryData)) : data;
          // if (data.data && typeof data.data != 'object') {
          //   if (data.data.indexOf('{') > -1) {
          //     data.data = JSON.parse(data.data);
          //   }
          // }
          if (['local', 'production'].indexOf(process.env.NODE_ENV) == -1) {
             }
          console.log('返回数据:', data);
         
          if (data.code == '40902') {
            //token 失效,退出登录
            NativeUI.confirm('', "登录超时,请退出重新登录").then(() => {
              // emUtil.closeWindow();
              // 清空数据并回到登录页面
              sessionStorage.setItem("branchNo", "");
              sessionStorage.setItem("branchNo", "");
              router.push({path: "/login"}).then();
            });
          } else {
            return data;
          }
        } else {
          //三方数据
          if (data.code == '40902') {
            //token 失效,退出登录
            NativeUI.confirm("", "登录超时,请退出重新登录").then(() => {
              // emUtil.closeWindow();
              // 清空数据并回到登录页面
              sessionStorage.setItem("branchNo", "");
              sessionStorage.setItem("branchNo", "");
              router.push({path: "/login"}).then();
            });
          } else if (data.code == 1) {
            console.log(data);
            //成功
           // data = data.data;
          //  debugger
            if (typeof data != 'object') data = JSON.parse(data);
            data = data.sm4key ? SMUtil.decodeData(data) : data;
            if (typeof data != 'object') {
              if (data.indexOf('{') > -1) {
                data = JSON.parse(data);
              }
            }
            // if (['local', 'production'].indexOf(process.env.NODE_ENV) == -1) {
            console.log('**返回数据**', params.tc, data);
            // }
            return data;
          } else if (data.code == -1) {
            NativeUI.toast(data.msg);
            return data;
          } else {
            NativeUI.toast(data.msg);
            return data;
          }
        }
      }
      return response;
    };
  }

  /**
   * @description: 异常处理器
   * @author ChenRui
   * @date 2020/8/28 15:19
   */
  createErrorHandler() {
    return function (error: any) {
      if (error.response) {
        // The request was made and the server responded with a status code
        // that falls out of the range of 2xx
        console.log(error.response);
        return {
          code: Code.FAIL.code,
          msg: error.response?.data?.msg || "网络连接异常,稍后请重试或联系管理员",
        };
      } else if (error.request) {
        console.log(error.request);
        return {
          code: Code.FAIL.code,
          msg: error.request?.data?.msg || "网络连接异常,稍后请重试或联系管理员",
        };
      } else {
        // Something happened in setting up the request that triggered an Error
        console.log("Error", error.message);
        return {
          code: Code.FAIL.code,
          msg: "未知异常,稍后请重试或联系管理员",
        };
      }
    };
  }

  /**
   * @description: 校验令牌
   * @author ChenRui
   * @date 2021/8/11 15:02
   */
  verificationToken(config: AxiosRequestConfig) {
    if (config.headers != null && config.headers.Authorization != null && tokenStore.token != null) {
      // return oauthService.isTokenExpiration;
    }
    return true;
  }

  /**
   * @description: 日期格式化
   * @author ChenRui
   * @date 2020/8/28 15:20
   */
  formatDate(date: any) {
    date = new Date(date);
    const YY = date.getFullYear() + "-";
    const MM = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    const DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    const hh = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
    const mm = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
    const ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    return YY + MM + DD + " " + hh + mm + ss;
  }

  /**
   * @description: json格式转表单格式

   * @date 2020/8/28 15:20
   */
  jsonToFormData(params: any) {
    if (params != null) {
      const formData = new FormData();
      Object.keys(params).forEach((key) => {
        formData.append(key, params[key]);
      });
      return formData;
    }
  }

  /**
   * @description: url请求参数组装
   * @author ChenRui
   * @date 2020/8/28 15:21
   */
  urlQueryConvert(url: string, query: Query) {
    let connectiveSymbol = "";
    if (url.indexOf("?") !== -1) {
      connectiveSymbol = "&";
    } else {
      connectiveSymbol = "?";
    }
    if (query) {
      Object.keys(query).forEach((key, idx) => {
        const val = query[key];
        if (idx === 0) {
          if (val != null && val !== "null" && val !== "undefined") {
            url += connectiveSymbol + key + "=" + val;
          } else {
            url += connectiveSymbol + key + "=";
          }
        } else {
          if (val != null && val !== "null" && val !== "undefined") {
            url += "&" + key + "=" + val;
          } else {
            url += "&" + key + "=";
          }
        }
      });
    }
    return url;
  }
}
const apiService = new ApiService();
export default apiService;