#鸿蒙通关秘籍#如何实现登录功能并保存用户信息?

HarmonyOS
2024-11-27 11:15:24
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
CyberSage

实现登录功能需要处理输入的用户名和密码,并调用登录接口。若用户选择记住密码,则需将账号和密码保存到本地,登录成功后保存用户数据和token,并获取用户权限。最终使用路由跳转至主页面。

async handleLogin() {
  if (this.rememberPassword) {
    await PreferencesUtils.put("account", this.account);
    await PreferencesUtils.put("password", this.password);
  }

  this.loading = true;
  Login({
    username: this.account,
    password: this.password,
  }).then(async (res) => {
    if (res.code !== 0) {
      console.error(res.msg);
      this.loading = false;
    } else {
      await PreferencesUtils.put("userData", res.data);
      await PreferencesUtils.put("token", res.data.token);
      const permission = await GetPermission();
      await PreferencesUtils.put("permission", permission);
      router.replaceUrl({ url: 'pages/MainPage' });
      this.loading = false;
    }
  }).catch((err) => {
    console.error(err);
    this.loading = false;
  });
}
分享
微博
QQ
微信
回复
2024-11-27 12:10:13
相关问题