更新时间:2025-12-16 07:19:02下载pdf
涂鸿蒙智能生活 App SDK 支持手机号码、邮箱、App UID 等多种用户账号类型。
用户账号支撑的具体能力包括:
在该模块中,您将频繁地调用对象 TSmartUser。它是一个静态类,存储了当前用户的所有信息及相关的登录注册方法。
import { TSmartUser } from '@thingsmart/userlib';
智能生活 App SDK 提供了手机号码和密码的注册和登录能力。
为了加强用户信息的数据安全,涂鸦优化了验证码发送流程,并添加了账号限制。只有验证码服务可用的地区,才可以发送验证码。
如果您想为 App 启用手机号码验证服务,那您需要开通和配置 手机号码短信验证服务。该服务让您的 App 用户可以通过手机号码直接注册账号或绑定已有的 App 账号,并可以直接通过手机号码完成登录 App、找回密码等操作。
接口说明
static getWhiteListCanSendMobileCode(): Promise<TSmartUserResponse<ITSmartUserRegionWhiteListBean>>
返回值说明
| 参数 | 说明 |
|---|---|
| success | 是否成功 |
| result | 返回数据,包含可发送验证码的国家码列表 |
| errorMsg | 错误信息 |
| errorCode | 错误码 |
ITSmartUserRegionWhiteListBean 结构
| 字段 | 类型 | 说明 |
|---|---|---|
| countryCodes | string[] | 可发送短信验证码的国家码数组 |
示例代码
try {
const result = await TSmartUser.getWhiteListCanSendMobileCode();
if (result.success) {
console.log('可用地区:', result.result?.countryCodes);
} else {
console.error('查询失败:', result.errorMsg);
}
} catch (error) {
console.error('查询异常:', error);
}
发送验证码,用于注册、登录、密码重置等场景。该接口同时支持手机号码和邮箱地址两种账号类型。
如果使用手机号码发送验证码,需要先调用 getWhiteListCanSendMobileCode 查看使用权限。
接口说明
static sendAuthCode(params: ITSmartUserSendAuthCodeParams): Promise<TSmartUserResponse<boolean>>
参数说明
ITSmartUserSendAuthCodeParams 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| account | string | 手机号码或邮箱地址 |
| countryCode | string | 国家码,例如 86 |
| codeType | TSmartUserAuthCodeType | 发送验证码类型。取值:
|
| region | string (可选) | 用户注册地区 |
示例代码
手机号码示例:
try {
const result = await TSmartUser.sendAuthCode({
account: '13800138000',
countryCode: '86',
codeType: TSmartUserAuthCodeType.REGISTER
});
if (result.success) {
console.log('验证码发送成功');
} else {
console.error('验证码发送失败:', result.errorMsg);
}
} catch (error) {
console.error('发送验证码异常:', error);
}
邮箱地址示例:
try {
const result = await TSmartUser.sendAuthCode({
account: 'user@example.com',
countryCode: '86',
codeType: TSmartUserAuthCodeType.REGISTER
});
if (result.success) {
console.log('邮箱验证码发送成功');
} else {
console.error('邮箱验证码发送失败:', result.errorMsg);
}
} catch (error) {
console.error('发送邮箱验证码异常:', error);
}
接口说明
校验验证码是否有效,用于注册、登录、重设密码等场景。该接口同时支持手机号码和邮箱地址两种账号类型。
static verifyAuthCode(params: ITSmartUserVerifyAuthCodeParams): Promise<TSmartUserResponse<boolean>>
参数说明
ITSmartUserVerifyAuthCodeParams 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| account | string | 手机号码或邮箱地址 |
| countryCode | string | 国家码,例如 86 |
| codeType | TSmartUserAuthCodeType | 验证码类型。取值:
|
| authCode | string | 经过验证码发送接口,收到的验证码 |
| region | string (可选) | 区域,默认填空 |
| sid | string (可选) | 会话ID,仅在特定场景需要 |
示例代码
手机号码示例:
try {
const result = await TSmartUser.verifyAuthCode({
account: '13800138000',
region: 'CN',
countryCode: '86',
codeType: TSmartUserAuthCodeType.REGISTER,
authCode: '123456'
});
if (result.success && result.result) {
console.log('验证码有效');
} else {
console.log('验证码无效');
}
} catch (error) {
console.error('验证码校验失败:', error);
}
邮箱地址示例:
try {
const result = await TSmartUser.verifyAuthCode({
account: 'user@example.com',
region: 'CN',
countryCode: '86',
codeType: TSmartUserAuthCodeType.REGISTER,
authCode: '123456'
});
if (result.success && result.result) {
console.log('邮箱验证码有效');
} else {
console.log('邮箱验证码无效');
}
} catch (error) {
console.error('邮箱验证码校验失败:', error);
}
注册账号前,您需要先获取验证码并进行校验。该接口同时支持手机号码和邮箱地址两种账号类型。
接口说明
static registerAccount(params: ITSmartUserRegisterParams): Promise<TSmartUserResponse<boolean>>
参数说明
ITSmartUserRegisterParams 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| account | string | 手机号码或邮箱地址 |
| countryCode | string | 国家码,例如 86 |
| authCode | string | 经过验证码发送接口,收到的验证码 |
| password | string | 密码 |
| accountType | TSmartUserAccountType | 账号类型。取值:
|
| region | string (可选) | 地区 |
示例代码
手机号码注册示例:
try {
const result = await TSmartUser.registerAccount({
account: '13800138000',
region: 'CN',
countryCode: '86',
authCode: '123456',
password: 'your_password',
accountType: TSmartUserAccountType.PHONE
});
if (result.success) {
console.log('注册成功');
} else {
console.error('注册失败:', result.errorMsg);
}
} catch (error) {
console.error('注册异常:', error);
}
邮箱地址注册示例:
try {
const result = await TSmartUser.registerAccount({
account: 'user@example.com',
region: 'CN',
countryCode: '86',
authCode: '123456',
password: 'your_password',
accountType: TSmartUserAccountType.EMAIL
});
if (result.success) {
console.log('邮箱注册成功');
} else {
console.error('邮箱注册失败:', result.errorMsg);
}
} catch (error) {
console.error('邮箱注册异常:', error);
}
使用账号和密码登录,该接口同时支持手机号码和邮箱地址两种账号类型。
接口说明
static loginAccount(params: ITSmartUserLoginParams): Promise<TSmartUserResponse<boolean>>
参数说明
ITSmartUserLoginParams 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| account | string | 手机号码或邮箱地址 |
| countryCode | string | 国家码,例如 86 |
| password | string | 密码 |
| accountType | TSmartUserAccountType | 账号类型。取值:
|
| twiceCode | string (可选) | 二次验证码 |
示例代码
手机号码登录示例:
try {
const result = await TSmartUser.loginAccount({
account: '13800138000',
countryCode: '86',
password: 'your_password',
accountType: TSmartUserAccountType.PHONE
});
if (result.success) {
console.log('登录成功');
} else {
console.error('登录失败:', result.errorMsg);
}
} catch (error) {
console.error('登录异常:', error);
}
邮箱地址登录示例:
try {
const result = await TSmartUser.loginAccount({
account: 'user@example.com',
countryCode: '86',
password: 'your_password',
accountType: TSmartUserAccountType.EMAIL
});
if (result.success) {
console.log('邮箱登录成功');
} else {
console.error('邮箱登录失败:', result.errorMsg);
}
} catch (error) {
console.error('邮箱登录异常:', error);
}
重置密码前,您需要先获取验证码并进行校验。该接口同时支持手机号码和邮箱地址两种账号类型。
接口说明
static resetPassword(params: ITSmartUserResetPasswordParams): Promise<TSmartUserResponse<boolean>>
参数说明
ITSmartUserResetPasswordParams 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| account | string | 手机号码或邮箱地址 |
| countryCode | string | 国家码,例如 86 |
| authCode | string | 经过验证码发送接口,收到的验证码 |
| newPassword | string | 新密码 |
| accountType | TSmartUserAccountType | 账号类型。取值:
|
| region | string (可选) | 地区 |
示例代码
手机号码重置密码示例:
try {
const result = await TSmartUser.resetPassword({
account: '13800138000',
region: 'CN',
countryCode: '86',
authCode: '123456',
newPassword: 'your_new_password',
accountType: TSmartUserAccountType.PHONE
});
if (result.success) {
console.log('密码重置成功');
} else {
console.error('密码重置失败:', result.errorMsg);
}
} catch (error) {
console.error('密码重置异常:', error);
}
邮箱地址重置密码示例:
try {
const result = await TSmartUser.resetPassword({
account: 'user@example.com',
region: 'CN',
countryCode: '86',
authCode: '123456',
newPassword: 'your_new_password',
accountType: TSmartUserAccountType.EMAIL
});
if (result.success) {
console.log('邮箱密码重置成功');
} else {
console.error('邮箱密码重置失败:', result.errorMsg);
}
} catch (error) {
console.error('邮箱密码重置异常:', error);
}
用户 UID 登录采用注册和登录为一体的接口,适用于已经拥有账号体系的场景。UID 是在您的用户系统中维护的用户唯一标识。
您可能误解 UID 是指涂鸦系统中的 UID,实际上,UID 应为您自己系统中的用户唯一标识。通过 UID,您可以建立您自己系统数据库与涂鸦数据库之间的连接。
接口说明
如果注册了账号就自动登录,如果没有注册账号就自动注册并且登录。
static loginOrRegisterByUid(params: ITSmartUserAccountUidParams): Promise<TSmartUserResponse<Record<string, Object>> | TSmartUserResponse<undefined>>
参数说明
ITSmartUserAccountUidParams 结构
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | string | 匿名 ID,用户唯一标识,没有格式要求 |
| countryCode | string | 国家码,例如 86 |
| password | string | 与账户 ID 对应的随机标识,同一个账户 ID 下保持同一个标识,而非用户的实际密码 |
| createHome | boolean | 是否创建默认家庭 |
示例代码
try {
const result = await TSmartUser.loginOrRegisterByUid({
uid: 'your_uid',
countryCode: '86',
password: 'your_password',
createHome: true
});
if (result.success) {
console.log('UID登录成功:', result.result);
} else {
console.error('UID登录失败:', result.errorMsg);
}
} catch (error) {
console.error('UID登录异常:', error);
}
TSmartUser 提供了一系列静态方法来获取当前登录用户的信息。
接口说明
static isLogin(): boolean
返回值
| 返回值 | 说明 |
|---|---|
| boolean | true 表示已登录,false 表示未登录 |
示例代码
const isLoggedIn = TSmartUser.isLogin();
if (isLoggedIn) {
console.log('用户已登录');
} else {
console.log('用户未登录');
}
接口说明
// 获取用户名
static getUserName(): string | undefined
// 获取手机号
static getPhoneNumber(): string | undefined
// 获取邮箱
static getEmail(): string | undefined
// 获取国家码
static getCountryCode(): string | undefined
// 获取区域码
static getRegionCode(): string | undefined
// 获取用户ID
static getUid(): string | undefined
// 获取会话ID
static getSid(): string | undefined
// 获取昵称
static getNickname(): string | undefined
// 获取头像URL
static getHeadIconUrl(): string | undefined
// 获取时区ID
static getTimezoneId(): string | undefined
// 获取温度单位
static getTempUnit(): TSmartUserTempUnit | undefined
// 获取合作伙伴身份
static getPartnerIdentity(): string | undefined
// 获取电子码
static getEcode(): string | undefined
// 获取注册来源
static getRegFrom(): ThingRegType
// 获取用户别名
static getUserAlias(): string | undefined
// 获取额外信息
static getExtras(): Map<string, string>
示例代码
// 获取用户完整信息
const userInfo = {
isLoggedIn: TSmartUser.isLogin(),
username: TSmartUser.getUserName(),
phoneNumber: TSmartUser.getPhoneNumber(),
email: TSmartUser.getEmail(),
countryCode: TSmartUser.getCountryCode(),
regionCode: TSmartUser.getRegionCode(),
uid: TSmartUser.getUid(),
sid: TSmartUser.getSid(),
nickname: TSmartUser.getNickname(),
headIconUrl: TSmartUser.getHeadIconUrl(),
timezoneId: TSmartUser.getTimezoneId(),
tempUnit: TSmartUser.getTempUnit(),
partnerIdentity: TSmartUser.getPartnerIdentity(),
ecode: TSmartUser.getEcode(),
regFrom: TSmartUser.getRegFrom(),
userAlias: TSmartUser.getUserAlias(),
extras: TSmartUser.getExtras()
};
console.log('用户信息:', userInfo);
接口说明
刷新并更新用户信息。
static updateUserInfo(): Promise<TSmartUserResponse<boolean>>
示例代码
try {
const result = await TSmartUser.updateUserInfo();
if (result.success) {
console.log('用户信息更新成功');
} else {
console.error('用户信息更新失败:', result.errorMsg);
}
} catch (error) {
console.error('更新用户信息异常:', error);
}
接口说明
static updateHeadIcon(url: string): Promise<TSmartUserResponse<boolean>>
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| url | string | 头像图片URL地址 |
示例代码
try {
const result = await TSmartUser.updateHeadIcon('https://example.com/avatar.jpg');
if (result.success) {
console.log('头像更新成功');
} else {
console.error('头像更新失败:', result.errorMsg);
}
} catch (error) {
console.error('更新头像异常:', error);
}
接口说明
static updateTempUnit(tempUnit: TSmartUserTempUnit): Promise<TSmartUserResponse<boolean>>
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| tempUnit | TSmartUserTempUnit | 温度单位
|
示例代码
try {
const result = await TSmartUser.updateTempUnit(TSmartUserTempUnit.CELSIUS);
if (result.success) {
console.log('温度单位更新成功');
} else {
console.error('温度单位更新失败:', result.errorMsg);
}
} catch (error) {
console.error('更新温度单位异常:', error);
}
接口说明
static updateNickName(nickName: string): Promise<TSmartUserResponse<boolean>>
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| nickName | string | 昵称 |
示例代码
try {
const result = await TSmartUser.updateNickName('新昵称');
if (result.success) {
console.log('昵称更新成功');
} else {
console.error('昵称更新失败:', result.errorMsg);
}
} catch (error) {
console.error('更新昵称异常:', error);
}
接口说明
static updateTimezondId(timezondId: string): Promise<TSmartUserResponse<boolean>>
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| timezondId | string | 时区 ID,例如 Asia/Shanghai |
示例代码
try {
const result = await TSmartUser.updateTimezondId('Asia/Shanghai');
if (result.success) {
console.log('时区更新成功');
} else {
console.error('时区更新失败:', result.errorMsg);
}
} catch (error) {
console.error('更新时区异常:', error);
}
接口说明
static logout(): Promise<TSmartUserResponse<boolean>>
示例代码
try {
const result = await TSmartUser.logout();
if (result.success) {
console.log('退出登录成功');
// 跳转到登录页面
} else {
console.error('退出登录失败:', result.errorMsg);
}
} catch (error) {
console.error('退出登录异常:', error);
}
接口说明
注销账号后,一周后才会永久注销并删除用户账户中的所有信息。但是,如果用户在永久注销之前重新登录的话,则注销请求会被取消。
static cancelAccount(): Promise<TSmartUserResponse<boolean>>
示例代码
try {
const result = await TSmartUser.cancelAccount();
if (result.success) {
console.log('账号注销成功');
} else {
console.error('账号注销失败:', result.errorMsg);
}
} catch (error) {
console.error('账号注销异常:', error);
}
接口说明
初始化用户代理信息,用于配置设备/用户特定设置。
static initUserAgent(): void
示例代码
TSmartUser.initUserAgent();
接口说明
清除用户代理信息。
static clearUserAgent(): void
示例代码
TSmartUser.clearUserAgent();
SDK 提供了多个事件通知,您可以使用 @ohos.events.emitter 监听这些事件。
| 事件常量 | 说明 |
|---|---|
| TSMART_USER_LOGIN_EVENT | 用户登录通知 |
| TSMART_USER_AUTO_LOGIN_EVENT | 用户自动登录通知 |
| TSMART_USER_LOGOUT_EVENT | 用户登出通知 |
| TSMART_USER_SESSION_INVALID | 用户会话失效通知 |
| TSMART_USER_TEMPERATURE_UNIT_CHANGE_EVENT | 温度单位变化通知 |
为了确保良好的用户体验,妥善处理登录会话过期问题至关重要。以下是可能导致会话过期的常见情况:
在这些情况下,您需要监听 TSMART_USER_SESSION_INVALID 事件,并引导用户跳转至登录页面,以便重新登录。
示例代码
import emitter from '@ohos.events.emitter';
import { TSMART_USER_SESSION_INVALID } from '@thingsmart/userlib';
// 监听会话失效事件
const sessionInvalidCallback = (eventData: emitter.EventData): void => {
console.log('用户会话失效,需要重新登录');
// 跳转到登录页面
// router.pushUrl({ url: 'pages/LoginPage' });
};
// 订阅事件
emitter.on(TSMART_USER_SESSION_INVALID, sessionInvalidCallback);
// 在适当时机取消订阅
emitter.off(TSMART_USER_SESSION_INVALID sessionInvalidCallback);
示例代码
import emitter from '@ohos.events.emitter';
import { TSMART_USER_LOGIN_EVENT } from '@thingsmart/userlib';
const loginCallback = (eventData: emitter.EventData): void => {
console.log('用户登录成功');
// 处理登录后的逻辑
};
emitter.on(TSMART_USER_LOGIN_EVENT, loginCallback);
示例代码
import emitter from '@ohos.events.emitter';
import { TSMART_USER_LOGOUT_EVENT } from '@thingsmart/userlib';
const logoutCallback = (eventData: emitter.EventData): void => {
console.log('用户已登出');
// 清理用户相关数据
// 跳转到登录页面
};
emitter.on(TSMART_USER_LOGOUT_EVENT, logoutCallback);
示例代码
import emitter from '@ohos.events.emitter';
import { TSMART_USER_TEMPERATURE_UNIT_CHANGE_EVENT } from '@thingsmart/userlib';
const tempUnitChangeCallback = (eventData: emitter.EventData): void => {
console.log('温度单位已变化');
const newTempUnit = TSmartUser.getTempUnit();
console.log('新的温度单位:', newTempUnit);
// 更新UI显示
};
emitter.on(TSMART_USER_TEMPERATURE_UNIT_CHANGE_EVENT, tempUnitChangeCallback);
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈