跳到主要内容

账号功能

更新时间:2026-10-28 11:07:34

账号和用户SDK提供了账号能力,包括注册、登录、登出等功能。建议在SDK初始化完成以后再调用登录接口,保证SDK初始化与登录时序性。

内置账号体系​

内置账号为AIRTC平台提供的账号能力,包括手机号注册、邮箱注册、验证码登录、自动登录、退出登录、发送验证码、重置密码、修改密码、注销。您在客户端集成后即可使用。

// 导入账号管理头文件
import { RTCXLogger, RTCXAccount } from 'rtcxsdk'
/**
* 帐号密码登录
* @param account - 帐号
* @param password - 密码
* @public
*/
export declare function loginByPassword(account: string, password: string): Promise<IUserLoginResp>;
/**
* 验证码登录
* @param account - 帐号
* @param verifyCode - 验证码
* @public
*/
export declare function loginByVerifyCode(account: string, verifyCode: string): Promise<IUserLoginResp>;
/**
* 退出登录
* @public
*/
export declare function logout(): Promise<void>;
/**
* 自动登录
*/
export declare function autoLogin(): Promise<boolean>;


/**
* 发送验证码
* @param sendCodeReq - sendCodeReq
* @public
*/
declare function sendCode(sendCodeReq: SendCodeReq): Promise<void>;

/**
* 内置帐号注册
*
* @param userRegisterReq - [UserRegisterReq]
* @public
*/
declare function register(userRegisterReq: UserRegisterReq): Promise<void>;

/**
* 获取登录用户信息
* @public
*/
declare function getLoginUserInfo(): Promise<UserProfileRsp>;

/**
* 用户重置密码
*
* @param userResetPwdReq - [UserModifyPwdReq]
* @public
*/
declare function resetPassword(userResetPwdReq: UserResetPwdReq): Promise<UserResetPwdRsp>;

/**
* 用户登录后使用旧密码修改新密码
*
* @param oldPassword - 旧密码
* @param newPassword - 新密码
* @public
*/
declare function modifyPassword(oldPassword: string, newPassword: string): Promise<UserCommonRsp>;

/**
* 注销用户
*
* @param verifyCode - 验证码
* @public
*/
declare function userDelete(verifyCode: string): Promise<UserCommonRsp>;

/**
* 更新用户昵称
*
* @param nickname - 用户昵称
* @public
*/
declare function updateUserNickname(nickname: string): Promise<UserCommonRsp>;
/**
* 更新用户头像
*
* @param fileUri - 头像图片地址
* @public
*/
declare function updateUserHeadImg(fileUri: string, picType: ImageFormat): Promise<string | undefined>
/**
* 更新用户头像
*
* @param fileBuffer - 头像图片ArrayBuffer
* @public
*/
declare function updateUserHeadImgBuffer(fileBuffer: ArrayBuffer, picType: ImageFormat): Promise<string | undefined>

调用示例如下:

发送验证码​

/**
* 发送验证码
* @param sendCodeReq - sendCodeReq
* @public
*/
declare function sendCode(sendCodeReq: SendCodeReq): Promise<void>;

示例

import { RTCXAccount,RTCXAccountTypes} from 'rtcxsdk'

async function sendAccountVerifyCode(accout: string) {
const params: RTCXAccountTypes.SendCodeReq = {
account: accout,
verifyCodeType: RTCXAccountTypes.VerifyCodeTypeEnum.LOGIN,
}
return RTCXAccount.sendCode(params)
}

用户注册​

/**
* 内置帐号注册
*
* @param userRegisterReq - [UserRegisterReq]
* @public
*/
declare function register(userRegisterReq: UserRegisterReq): Promise<void>;

示例

import { RTCXAccount, RTCXAccountTypes} from 'rtcxsdk'
async function handleRegister() {

try {
const isEmail = form.account.includes('@')
const params: RTCXAccountTypes.UserRegisterReq = {
account: form.account,
verifyCode: form.verifyCode,
password: form.password,
accountType: isEmail
? RTCXAccountTypes.AccountTypeEnum.EMAIL
:RTCXAccountTypes.AccountTypeEnum.MOBILE,
pwdType: isEmail
? RTCXAccountTypes.PasswordTypeEnum.EMAIL_CODE
:RTCXAccountTypes.PasswordTypeEnum.MOBILE_CODE,
}
await RTCXAccount.register(params)
console.info('注册成功,即将跳转登录')

} catch (e: any) {
console.error(e.message || '注册失败')
}
}

自动登录​

当调用登录接口完成登录以后,SDK会自动缓存登录用户信息,下次可以直接调用自动登录接口完成登录,切记:自动登录接口需要在SDK初始化完成以后调用,否则会出现异常

const success = await RTCXAccount.autoLogin()
if (success) {
this.pathStack.replacePathByName('PageMain', null);
} else {
this.pathStack.replacePathByName('PageLogin', null);
}

账号密码登录​

切记:该接口需要在SDK初始化完成以后调用,否则会出现异常

try {
const loginRsp = await RTCXAccount.loginByPassword(this.username, this.password)
if (loginRsp) {
showToast(this, "登录成功")
this.pathStack.replacePathByName("PageMain", null)
}
} catch (e) {
showToast(this, `登录失败[${e.code ?? e.message}]`)
} finally {
hideLoading()
}

验证码登录​

切记:该接口需要在SDK初始化完成以后调用,否则会出现异常

try {
const loginRsp = await RTCXAccount.loginByVerifyCode(this.username, this.password)
if (loginRsp) {
showToast(this, "登录成功")
this.pathStack.replacePathByName("PageMain", null)
}
} catch (e) {
showToast(this, `登录失败[${e.code ?? e.message}]`)
} finally {
hideLoading()
}

退出登录​

try {
const rsp = await RTCXAccount.logout()
if (rsp) {
showToast(this, "登出成功")
}
} catch (e) {
showToast(this, `登出失败[${e.code ?? e.message}]`)
} finally {
hideLoading()
}

用户信息​

/**
* 获取登录用户信息
* @public
*/
declare function getLoginUserInfo(): Promise<UserProfileRsp>;

示例

import {
RTCXAccount,RTCXAccountTypes
} from 'rtcxsdk'

const fetchUserInfo = async () => {
try {
const rsp = await RTCXAccount.getLoginUserInfo()
const userAccount = rsp.userAccountList[0]?.account ?? ''

const nickname = rsp.loginName ?? userAccount
const avatarUrl = rsp.avatarUrl ?? ''
const userId = rsp.userId ?? 0
} catch (e) {
console.error('获取用户信息失败')
}
}

重置密码​

需要先发送重置密码验证码

/**
* 用户重置密码
*
* @param userResetPwdReq - [UserModifyPwdReq]
* @public
*/
declare function resetPassword(userResetPwdReq: UserResetPwdReq): Promise<UserResetPwdRsp>;

示例

import {
RTCXAccount,RTCXAccountTypes
} from 'rtcxsdk'

async function handleReset(account:string, verifyCode:string, password:string) {

try {
const params: RTCXAccountTypes.UserResetPwdReq = {
account: account,
verifyCode: verifyCode,
password: password,
}
await RTCXAccount.resetPassword(params)
console.info('密码重置成功,请重新登录')

} catch (e: any) {
console.error(e.message || '重置失败')
}
}

修改密码​

/**
* 用户登录后使用旧密码修改新密码
*
* @param oldPassword - 旧密码
* @param newPassword - 新密码
* @public
*/
declare function modifyPassword(oldPassword: string, newPassword: string): Promise<UserCommonRsp>;

示例

import {
RTCXAccount,
} from 'rtcxsdk'

const submitModifyPwd = async (oldPassword: string, newPassword: string) => {
try {
// 请根据实际的 UserModifyPwdReq 类型定义调整字段名
await RTCXAccount.modifyPassword(oldPassword, newPassword)

console.info('密码修改成功,请重新登录')

// 修改密码后通常需要重新登录
await RTCXAccount.logout()

} catch (e: any) {
console.error(e.message || '密码修改失败')
}
}

用户注销​

需要先发送用户注销验证码

/**
* 注销用户
*
* @param verifyCode - 验证码
* @public
*/
declare function userDelete(verifyCode: string): Promise<UserCommonRsp>;

示例

import {
RTCXAccount,
} from 'rtcxsdk'


const handleDeleteAccount = async (deleteVerifyCode: string) => {
await RTCXAccount.userDelete(deleteVerifyCode)
console.success('账号已注销')
await RTCXAccount.logout()
// 清理本地状态并跳转登录页
} catch (e: any) {
console.error(e.message || '注销失败')
}
}

修改用户昵称​

/**
* 更新用户昵称
*
* @param nickname - 用户昵称
* @public
*/
declare function updateUserNickname(nickname: string): Promise<UserCommonRsp>;

示例

import {
RTCXAccount, RTCXAccountTypes
} from 'rtcxsdk'

const submitNickname = async (newNickname: string) => {
try {
await RTCXAccount.updateUserNickname(newNickname)

console.info('昵称修改成功')
} catch (e: any) {
console.error(e.message || '昵称修改失败')
}
}

修改用户头像​

/**
* 更新用户头像
*
* @param fileUri - 头像图片地址
* @public
*/
declare function updateUserHeadImg(fileUri: string, picType: ImageFormat): Promise<string | undefined>
/**
* 更新用户头像
*
* @param fileBuffer - 头像图片ArrayBuffer
* @public
*/
declare function updateUserHeadImgBuffer(fileBuffer: ArrayBuffer, picType: ImageFormat): Promise<string | undefined>

示例

import {
RTCXAccount, RTCXAccountTypes
} from 'rtcxsdk'

async uploadAvatar(photoUri: string) {

try {
const fileName = photoUri.substring(photoUri.lastIndexOf('/') + 1);
const ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
let imageFormat: RTCXCommonTypes.ImageFormat = RTCXCommonTypes.ImageFormat.JPEG;
if (ext === 'png') {
imageFormat = RTCXCommonTypes.ImageFormat.PNG;
} else if (ext === 'webp') {
imageFormat = RTCXCommonTypes.ImageFormat.WEBP;
}

const newAvatarUrl = await RTCXAccount.updateUserHeadImg(photoUri, imageFormat);
if (newAvatarUrl) {
this.avatarUrl = newAvatarUrl;
}
showToast(this, "头像上传成功")
} catch (e) {
RTCXLogger.e(this.TAG, 'uploadAvatar error', e);
showToast(this, `头像上传失败[${e.code ?? e.message}]`)
}
}

自有账号体系​

登录​

通过 authCode 授权码登录,切记:该接口需要在SDK初始化完成以后调用,否则会出现异常

// 导入账号管理头文件
import { RTCXLogger, RTCXAccount } from 'rtcxsdk'
/**
* 自建帐号OAuth2协议登录
* @param token - 自建帐号用户token
* @returns
* @public
*/
export declare function loginByAuth2(authCode: string): Promise<IUserLoginResp>;

代码调用示例如下

try {
const loginRsp = await RTCXAccount.loginByAuth2('xxx')
if (loginRsp) {
showToast(this, "登录成功")
this.pathStack.replacePathByName("PageMain", null)
}
} catch (e) {
showToast(this, `登录失败[${e.code ?? e.message}]`)
} finally {
hideLoading()
}