app-auth-service.ts 4.5 KB
Newer Older
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
import { AppServiceBase, Http, IParams, LogUtil, Util, ViewTool } from "ibiz-core";
import { clearCookie, getCookie, setCookie } from "qx-util";

/**
 * 应用权限类
 *
 * @export
 * @class AppAuthService
 */
export class AppAuthService {

    /**
     * 唯一实例
     * 
     * @private
     * @static
     * @memberof AppAuthService
     */
    private static readonly instance = new AppAuthService();

    /**
     * token 刷新结果(防止单一时间重复刷新token)
     * 
     * @private
     * @memberof AppAuthService
     */
    private tokenResult: any;

    /**
     * 获取唯一实例
     *
     * @static
     * @return {*}  {AppAuthService}
     * @memberof AppAuthService
     */
    public static getInstance(): AppAuthService {
        return AppAuthService.instance;
    }

    /**
     * 用户登录
     *
     * @memberof AppAuthService
     */
    public async login(loginForm: IParams) {
        //  请求头
        const headers = {};
        const tempViewParam = ViewTool.getDcSystemIdViewParam();
        if (tempViewParam && tempViewParam.srfdcsystem) {
            Object.assign(headers, { srfdcsystem: tempViewParam.srfdcsystem });
        }
        const post: any = await Http.getInstance().post('/v7/login', loginForm, true, headers);
        const { status, data } = post;
        if (status == 200) {
            this.setExpiredDate(data.expirein);
        }
        return post;
    }

    /**
     * 刷新token
     *
     * @public
     * @param {*} [data={}]
     * @memberof AppAuthService
     */
    public async refreshToken(data: any = {}) {
        if (this.tokenResult) {
            return this.tokenResult;
        }
        this.tokenResult = new Promise((resolve: any, reject: any) => {
            if (data && (data.url.startsWith("/v7") || data.url.startsWith('./assets') || data.url == "/uaa/refreshtoken2")) {
                this.tokenResult = null;
                return resolve(true);
            } else {
                Http.getInstance().get('/uaa/refreshtoken2').then((response: any) => {
                    this.tokenResult = null;
                    if (response && response.status === 200) {
                        const data = response.data;
                        this.setExpiredDate(data.expirein);
                        const expirein = Util.formatExpirein(data.expirein);
                        if (data) {
                            setCookie('ibzuaa-token', data.token, expirein, true);
                        }
                        resolve(true);
                    } else {
                        LogUtil.warn(`[刷新token出错]:${JSON.stringify(response)}`);
                        resolve(false);
                    }
                }).catch((error: any) => {
                    this.tokenResult = null;
                    LogUtil.warn(`[刷新token出错]:${JSON.stringify(error)}`);
                    resolve(false);
                })
            }
        })
        return this.tokenResult;
    }

    /**
     * 是否过期
     *
     * @public
     * @param {Date} [date]
     * @memberof AppAuthService
     */
    public isExpired(date: Date, args: IParams): boolean {
        if (args && (args.url.startsWith("/v7") || args.url.startsWith('./assets') || args.url == "/uaa/refreshtoken2")) {
            return false;
        }
        const environment = AppServiceBase.getInstance().getAppEnvironment();
        if (this.getExpiredDate()) {
            if (environment) {
                return date.getTime() > ((this.getExpiredDate() as Date).getTime() - (environment.refreshTokenTime / 1000));
            } else {
                return date.getTime() > (this.getExpiredDate() as Date).getTime();
            }
        } else {
            return false;
        }
    }

    /**
     * 获取过期时间
     *
     * @memberof AppAuthService
     */
    public getExpiredDate() {
        const expiredTime = getCookie('ibzuaa-expired');
        if (expiredTime) {
            return new Date(Number(expiredTime));
        } else {
            return null;
        }
    }

    /**
     * 设置过期时间
     *
     * @param expirein 过期秒数
     * @memberof AppAuthService
     */
    public setExpiredDate(expirein: number) {
        if (expirein) {
            const curTime = new Date();
            const expireinTime = new Date(curTime.setSeconds(curTime.getSeconds() + expirein));
            setCookie('ibzuaa-expired', expireinTime.getTime().toString(), 7, true);
        } else {
            clearCookie('ibzuaa-expired', true);
        }
    }

}