Commit 20c75a3a by chunhong.mu

feat(@common/sdk): 登录复用逻辑

parent 5999c413
...@@ -21,17 +21,29 @@ export { default as HttpRequest } from "./http-request" ...@@ -21,17 +21,29 @@ export { default as HttpRequest } from "./http-request"
// 图片/文件 URL 构建 // 图片/文件 URL 构建
export { buildFilePath, buildImage } from "./image-builder" export { buildFilePath, buildImage } from "./image-builder"
// 登录服务
export { loginService } from "./login-service"
export type { LoginRegisterResult, RegisterChannel } from "./login-service"
// 小程序工具 // 小程序工具
export { export {
MiniProgramVersion, MiniProgramVersion,
navigateBackMiniProgram, navigateBackMiniProgram,
navigateToMiniProgram, navigateToMiniProgram,
} from "./mini-program" } from "./mini-program"
export type { MiniProgramNavConfig } from "./mini-program"
export type { MiniProgramNavConfig } from "./mini-program"
// 监控 // 监控
export { monitor, Product } from "./monitor" export { monitor, Product } from "./monitor"
export type { SdkMonitorOption } from "./monitor" export type { SdkMonitorOption } from "./monitor"
// Passport 公共常量
export {
PASSPORT_ANONYMOUS_SERVICE,
PASSPORT_SERVICE,
PASSPORT_SUB_PROJECT,
} from "./passport-constants"
// SDK 控制器 // SDK 控制器
export { SdkController } from "./sdk-controller" export { SdkController } from "./sdk-controller"
...@@ -46,8 +58,6 @@ export { createShareMixin, finalShareData, setShareData } from "./share" ...@@ -46,8 +58,6 @@ export { createShareMixin, finalShareData, setShareData } from "./share"
// Token 管理 // Token 管理
export { decodeToken, PassportTokenController } from "./token-manager" export { decodeToken, PassportTokenController } from "./token-manager"
export { LoginApiName } from "./token-manager"
// 核心配置 // 核心配置
export { ClientId, Environment, UICore, wxDistributerUrlConfig } from "./ui-core" export { ClientId, Environment, UICore, wxDistributerUrlConfig } from "./ui-core"
...@@ -55,3 +65,13 @@ export { ClientId, Environment, UICore, wxDistributerUrlConfig } from "./ui-core ...@@ -55,3 +65,13 @@ export { ClientId, Environment, UICore, wxDistributerUrlConfig } from "./ui-core
export { uniAdapter } from "./uni-adapter" export { uniAdapter } from "./uni-adapter"
// 小程序版本更新 // 小程序版本更新
export { updateManager } from "./update-manager" export { updateManager } from "./update-manager"
// 验证码服务
export { verifyService } from "./verify-service"
export type { VerifyImageResult } from "./verify-service"
// 微信登录服务
export { wechatLoginService } from "./wechat-login-service"
export type { RegisterChannel, WechatLoginResult } from "./wechat-login-service"
import {
PASSPORT_ANONYMOUS_SERVICE,
PASSPORT_SERVICE,
PASSPORT_SUB_PROJECT,
} from "./passport-constants"
/**
* 登录服务
* 提供密码登录、验证码登录等功能
*/
import { UICore } from "./ui-core"
export enum LoginApiName {
LoginWithPassword = "loginWithPassword",
LoginWithVerifyCode = "loginWithVerifyCode",
UpdatePersonal = "update_personal",
SendVerifyCode = "sendVerifyCode",
VerifyMobile = "verifyMobile",
Info = "info",
LoginHistory = "login_history",
MobileCheck = "mobileCheck",
Register = "register",
BindMobile = "bind_mobile",
ResetPassword = "reset_password",
RefreshToken = "refresh_token",
}
export interface LoginRegisterResult {
jwt: string
user_id?: string
mobile?: string
[key: string]: any
}
export interface RegisterChannel {
app_store?: string
activity_type?: string
activity_id?: number | string
invited_by?: number | string
app?: string
channel?: string
scene?: string
sendMessage?: boolean
[key: string]: any
}
export class LoginService {
/**
* 密码登录
*/
public login(username: string, password: string) {
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_ANONYMOUS_SERVICE.SystemUser,
LoginApiName.LoginWithPassword,
)
.post<LoginRegisterResult>({
password,
username,
client_id: UICore.clientId,
})
}
/**
* 验证码登录
*/
public verifyCodeLogin(
mobile: string,
verifycode: string,
optional?: RegisterChannel,
) {
const data: Record<string, any> = {
mobile,
verifycode,
client_id: UICore.clientId,
}
if (optional) {
Object.assign(data, this.buildNotNullProperty(optional))
}
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_ANONYMOUS_SERVICE.SystemUser,
LoginApiName.LoginWithVerifyCode,
)
.post<LoginRegisterResult>(data)
}
/**
* 刷新登录
*/
public refreshLogin(
optional?: RegisterChannel,
deviceInfo?: Record<string, string>,
) {
const p = optional || {}
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_SERVICE.SystemUser,
LoginApiName.RefreshToken,
)
.post(p, { client_id: UICore.clientId, ...deviceInfo })
}
/**
* 移除 value 为 0、null、"" 的值
* 注册接口对一些值有要求,比如活动 id 要么不传要么>0 不能传""
*/
private buildNotNullProperty(o: Record<string, any>) {
const t = {} as Record<string, string | number | boolean>
for (const p in o) {
const v = o[p]
if (v || v === true || v === false) {
t[p] = v
}
}
return t
}
}
export const loginService = new LoginService()
/**
* Passport 公共常量
* 定义子项目名称和服务名称
*/
/** 子项目名称 */
export const PASSPORT_SUB_PROJECT = "uniplat_base"
/** 匿名服务名称 */
export const PASSPORT_ANONYMOUS_SERVICE = {
SystemUser: "anonymous/system.user",
SystemAuth: "anonymous/system.auth",
SystemWechat: "anonymous/system.wechat",
} as const
/** 认证服务名称 */
export const PASSPORT_SERVICE = {
SystemUser: "system.user",
SystemAuth: "system.auth",
SystemWechat: "system.wechat",
} as const
import {
PASSPORT_ANONYMOUS_SERVICE,
PASSPORT_SUB_PROJECT,
} from "./passport-constants"
/**
* 验证码服务
* 提供图形验证码生成、短信验证码发送等功能
*/
import { UICore } from "./ui-core"
export enum VerifyApiName {
SendVerifyCode = "sendVerifyCode",
VerifyMobile = "verifyMobile",
}
export interface VerifyImageResult {
img: string
seed: string
}
export class VerifyService {
/**
* 生成图形验证码
*/
public generateImage(): VerifyImageResult {
return UICore.core.getVerifyImageAndSeed()
}
/**
* 发送短信验证码(需要图形验证码)
*/
public sendVerifyCode(mobile: string, seed: string, verifycode: string) {
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_ANONYMOUS_SERVICE.SystemUser,
VerifyApiName.SendVerifyCode,
)
.request<
Record<string, unknown>,
Record<string, unknown>,
void
>("post", { data: { mobile, seed, verifycode } })
}
/**
* 发送短信验证码(使用 token)
*/
public sendVerifyCodeWithToken(mobile: string, token: string) {
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_ANONYMOUS_SERVICE.SystemUser,
VerifyApiName.SendVerifyCode,
)
.request<
Record<string, unknown>,
Record<string, unknown>,
void
>("post", { data: { mobile, token } })
}
/**
* 验证手机号
*/
public verifyMobile(mobile: string, verifycode: string) {
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_ANONYMOUS_SERVICE.SystemUser,
VerifyApiName.VerifyMobile,
)
.request<
Record<string, unknown>,
Record<string, unknown>,
{ check_result: boolean }
>("post", {
data: { mobile, verifycode, client_id: UICore.clientId },
})
}
}
export const verifyService = new VerifyService()
import {
PASSPORT_ANONYMOUS_SERVICE,
PASSPORT_SUB_PROJECT,
} from "./passport-constants"
/**
* 微信登录服务
* 提供微信小程序绑定手机号登录等功能
*/
import { UICore } from "./ui-core"
export enum WechatLoginApiName {
BindPhone = "bind_phone",
}
export interface RegisterChannel {
app?: string
scene?: string
channel?: string
app_store?: string
[key: string]: any
}
export interface WechatLoginResult {
jwt: string
user_id?: string
mobile?: string
[key: string]: any
}
export class WechatLoginService {
/**
* 微信小程序绑定手机号登录
*/
public miniProgramBindPhone(
appid: string,
code: string,
encryptedData: string,
iv: string,
registerChannel: RegisterChannel = {},
) {
return UICore.core
.domainService(
PASSPORT_SUB_PROJECT,
PASSPORT_ANONYMOUS_SERVICE.SystemWechat,
WechatLoginApiName.BindPhone,
)
.request<
Record<string, unknown>,
Record<string, unknown>,
WechatLoginResult
>("post", {
data: {
appid,
code,
client_id: UICore.clientId,
encryptedData,
iv,
...registerChannel,
},
})
}
}
export const wechatLoginService = new WechatLoginService()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment