UNPKG

@shencom/api

Version:
203 lines (171 loc) 5.45 kB
import type { Unfurl } from '@shencom/typing'; import { getInitializedApiConfig } from '../config'; interface ReqWechatBindPhoneByEncrypt { encryptedData: string; iv: string; appid: string; openid: string; code: string; } interface ReqWechatBindPhoneByRecord { code: string; appid: string; /** 使用历史绑定账号登录的一次性 key */ recordId?: string; } type ReqWechatBindPhone = ReqWechatBindPhoneByEncrypt | ReqWechatBindPhoneByRecord; type ResWechatBindPhone = Unfurl<SC.User.RootInfo>; /** 微信小程序手机号绑定并登录 */ export const ApiWechatMiniBindPhone = ( body: Unfurl<ReqWechatBindPhone>, headers?: Record<string, any>, ) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/auth/miniprogram/phone/login`; return http.post<ResWechatBindPhone>(api, body, { headers: { Authorization: null, ...headers }, }); }; interface ReqWechatMiniLogin { code: string; appid: string; } type ResWechatMiniLogin = Unfurl<SC.User.RootInfo>; /** 微信小程序登录 */ export const ApiWechatMiniLogin = ( body: Unfurl<ReqWechatMiniLogin>, headers?: Record<string, any>, ) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/auth/miniprogram/login`; return http.post<ResWechatMiniLogin>(api, body, { headers: { Authorization: null, ...headers }, }); }; type ResWechatCodeLogin = | { openid: string; oAuth2AccessToken: SC.User.RootInfo; } | string; /** 微信二维码登录 */ export const ApiWechatCodeLogin = (code: string) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/sys/user/scan/login`; return http.get<ResWechatCodeLogin>(api, { code }); }; /** 更新微信信息 */ export const ApiWechatUpdateInfo = ( body: Unfurl<SC.User.UpdateWxInfo>, headers?: Record<string, any>, ) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/wechat/wxuserinfo/openid/update`; return http.post<ResWechatMiniLogin>(api, body, { headers, }); }; /** 退出微信登录状态 */ export const ApiWechatLogout = (headers?: Record<string, any>) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/wx/userinfo/logout`; return http.post(api, {}, { headers }); }; interface ReqCodeBindPhone { /** 手机号 */ phone: string; /** 验证码 */ code: string; /** 微信 token */ wxToken?: string; } /** 绑定微信用户登录 */ export const ApiWechatCodeBindPhone = ( body: Unfurl<ReqCodeBindPhone>, headers?: Record<string, any>, ) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/wechat/fast/login`; const { wxToken, code, phone } = body; return http.post<SC.User.RootInfo>( api, { phone, code }, { headers: { Authorization: wxToken, ...headers } }, ); }; interface ReqWechatCodeInfo { /** 静默授权 code */ code: string; /** 当前应用 scid */ scid: string; } export interface ResWechatCodeInfo { user: SC.User.Info; sctoken: string; expiration: number; refreshToken: SC.User.RefreshToken; } /** 通过 code 获取用户信息 */ export const ApiWechatCodeAuth = (body: ReqWechatCodeInfo, headers?: Record<string, any>) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/csm/wechat/auth`; return http.get<ResWechatCodeInfo>(api, body, { headers: { Authorization: null, ...headers } }); }; interface ReqWechatAuthUrl { scid: string; } export interface ResWechatAuthUrl { redirect_url: string; } /** 获取授权跳转链接 */ export const ApiWechatAuthUrl = (body: ReqWechatAuthUrl, headers?: Record<string, any>) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/sc/wechat/auth`; return http.get<ResWechatAuthUrl>(api, body, { headers: { Authorization: null, ...headers } }); }; interface ReqWechatUserRecords { /** 静默授权 code */ code: string; /** 当前小程序 appid */ appid: string; } export interface ResWechatUserRecords { /** 用于账号登录的一次性key */ id: string; phone: string; openid: string; appid: string; } /** 获取微信用户绑定过的账号列表 */ export const ApiWechatUserRecords = (body: ReqWechatUserRecords, headers?: Record<string, any>) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/wx/user/phone/record/by-code`; return http.post<ResWechatUserRecords[]>(api, body, { headers }); }; interface ReqWechatBindPhoneUpdate { code: string; phone: string; openid: string; } /** 平台端-通过手机号绑定微信扫码登录 */ export const ApiWechatBindPhonePlatform = ( body: ReqWechatBindPhoneUpdate, headers?: Record<string, any>, ) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-platform-uaa/user/phone/update`; return http.post<ResWechatBindPhone>(api, body, { headers: { Authorization: null, ...headers }, }); }; /** 租户端-通过手机号绑定微信扫码登录 */ export const ApiWechatBindPhone = ( body: ReqWechatBindPhoneUpdate, headers?: Record<string, any>, ) => { const { url, http } = getInitializedApiConfig(); const api = `${url}/service-uaa/user/phone/update`; return http.post<ResWechatBindPhone>(api, body, { headers: { Authorization: null, ...headers }, }); };