Skip to main content

7.网络编码规范

myddd-electron抽像了网络相关类,以便于使用

网络类接口#

export interface IRequest {    /**     * GET请求     * @param url      */    requestForGet<T>(url:string):Promise<IResponse<T>>;
    /**     * POST 请求     * @param url POST URL     * @param data      */    requestForPost<T>(url:string,data:any):Promise<IResponse<T>>;
    /**    * PUT 请求    * @param url     * @param data     */    requestForPut<T>(url:string,data:any):Promise<IResponse<T>>;
    /**    * DELETE 请求    * @param url     */    requestForDelete<T>(url:string):Promise<IResponse<T>>;
}

使用网络类#

import {BaseRequest} from 'components/http/impl/BaseRequest'import Config from 'components/Config';import { BaseResponse, IRequest } from 'components/http/IRequest';import { ILoginNet } from '../ILoginNet';import { AuthStore } from 'stores/AuthStore';
export class LoginNet extends ILoginNet{
    config:Config = Config.getInstance();
    request:IRequest = BaseRequest.getInstance();
    public constructor(){        super()    }
    /**     * 请求endpoints     * @param accessToken      */    public async endpoint():Promise<BaseResponse> {        const accessToken = AuthStore.getInstance().token;        const url:string = this.config.api + "/endpoints?access_token="+accessToken;
        const params = {            "locale":"zh_CN",            "encrypt_type":0,            "system_name":'PC'        }        return await this.request.requestForPost<BaseResponse>(url,params);    }

    /**     * 登录     */    public async login(username:string,password:string):Promise<BaseResponse> {
        const url:string = this.config.api + "/token";        const config:Config = this.config;
        const params = {            "grant_type":"password",            "scope":"user",            "domain_id":config.domainId,            "client_id":username,            "client_secret":password,            "client_secret_encrypt":false,            "device_id":config.deviceId(),            "device_platform":"PC"        }        return await this.request.requestForPost<BaseResponse>(url,params);    }}