import { AbstractLogin } from "../../../../abstract/login" ; import { Login } from "../../../../decorator/Login" ; import { LoginClassParams } from "../../../../abstract/login/Type" ; import { ax } from "../../../utils/Axios" ; import { WeChat } from "../../../../constants/Process" ; import { getConnection } from "typeorm" ; import { AxiosResponse } from "axios" ; import { ServiceUser } from "../../../service/user/User" ; import { ServiceUserWeChat } from "../../../service/user/UserWeChat" ; import { ServiceCloudStorageFiles } from "../../../service/cloudStorage/CloudStorageFiles" ; import { ServiceCloudStorageConfigs } from "../../../service/cloudStorage/CloudStorageConfigs" ; import { ServiceCloudStorageUserFiles } from "../../../service/cloudStorage/CloudStorageUserFiles" ; @ Login ( ) export class LoginWechat extends AbstractLogin { public readonly svc : RegisterService ; constructor ( params ) { super ( params ) ; this . svc = { user : new ServiceUser ( this . userUUID ) , userWeChat : new ServiceUserWeChat ( this . userUUID ) , cloudStorageFiles : new ServiceCloudStorageFiles ( ) , cloudStorageConfigs : new ServiceCloudStorageConfigs ( this . userUUID ) , cloudStorageUserFiles : new ServiceCloudStorageUserFiles ( this . userUUID ) , } ; } public async register ( info ) { await getConnection ( ) . transaction ( async ( t ) => { const createUser = this . svc . user . create ( info , t ) ; const createUserWeChat = this . svc . userWeChat . create ( info , t ) ; return await Promise . all ( [ createUser , createUserWeChat , this . setGuidePPTX ( this . svc , t ) , ] ) ; } ) ; } public static async getToken ( code , type ) { const result = await LoginWechat . wechatRequest < WeChatResponseToken > ( `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${ WeChat [ type ] . APP_ID }&secret=${ WeChat [ type ] . APP_SECRET }&code=${ code }&grant_type=authorization_code` ) ; return { accessToken : result . access_token , openUUID : result . openid , } ; } public static async getUserInfoByAPI ( params ) { const result = await LoginWechat . wechatRequest < WeChatResponseUserInfo > ( `https://api.weixin.qq.com/sns/userinfo?access_token=${ params . accessToken }&openid=${ params . openUUID }` ) ; return { unionUUID : result . unionid , avatarURL : result . headimgurl , userName : result . nickname , } ; } public static async getUserInfoAndToken ( code , type ) { const token = await LoginWechat . getToken ( code , type ) ; const userInfo = await LoginWechat . getUserInfoByAPI ( token ) ; return { ... userInfo , ... token , } ; } private static async wechatRequest < T > ( url ) { const response = await ax . get ( url ) ; if ( "errmsg" in response . data ) { throw new Error ( response . data . errmsg ) ; } return response . data ; } } interface RegisterService { user : ServiceUser ; userWeChat : ServiceUserWeChat ; cloudStorageFiles : ServiceCloudStorageFiles ; cloudStorageUserFiles : ServiceCloudStorageUserFiles ; cloudStorageConfigs : ServiceCloudStorageConfigs ; } interface RegisterInfo { userName : string ; avatarURL : string ; unionUUID : string ; openUUID : string ; } interface WeChatResponseToken { readonly access_token : string ; readonly expires_in : string ; readonly openid : string ; readonly scope : string ; } interface WeChatToken { accessToken : string ; openUUID : string ; } interface WeChatResponseUserInfo { readonly openid : string ; readonly nickname : string ; readonly sex : 1 | 2 ; readonly province : string ; readonly city : string ; readonly country : string ; readonly headimgurl : string ; readonly privilege : string [ ] ; readonly unionid : string ; } interface WeChatUserInfo { readonly unionUUID : string ; readonly userName : string ; readonly avatarURL : string ; } interface WeChatRequestFailed { readonly errcode : number ; readonly errmsg : string ; }