login.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { queenApi } from "queenjs";
  2. import { AuthModule } from "../..";
  3. import { User_Config } from "../config";
  4. export const loginActions = AuthModule.action({
  5. async pwdLogin(data: any) {
  6. const ret = await this.https.personPwdLogin({
  7. ...data,
  8. key: this.config.key,
  9. });
  10. this.actions.getUserInfo();
  11. this.actions.saveToken(ret.result.token);
  12. this.actions.jump("loginSucc");
  13. },
  14. async wechatLogin(code: string) {
  15. const res = await this.https.personWechatLogin({ code });
  16. this.actions.getUserInfo();
  17. this.actions.saveToken(res.result.token);
  18. this.actions.jump("loginSucc");
  19. },
  20. async checkLoginExpire(result: any) {
  21. if (result.errorNo === 401) {
  22. this.store.clearUserInfo();
  23. if (!this.helper.isLoginPage()) {
  24. this.actions.jump("tokenExpire");
  25. }
  26. }
  27. },
  28. logout() {
  29. this.store.clearUserInfo();
  30. this.actions.jump("logout");
  31. },
  32. async getSmsCode(data: {
  33. email?: string;
  34. phone?: string;
  35. use: "register" | "resetPasswd" | "login";
  36. }) {
  37. await this.https.getSmsCode(data);
  38. },
  39. async personRegister(data: any) {
  40. const ret = await this.https.personRegister({
  41. ...data,
  42. regInfo: this.config.regInfo,
  43. });
  44. this.actions.getUserInfo();
  45. this.actions.saveToken(ret.result.token);
  46. this.actions.jump("loginSucc");
  47. },
  48. async emailRegister(data: any) {
  49. const ret = await this.https.emailRegist({
  50. ...data,
  51. regInfo: this.config.regInfo,
  52. });
  53. this.actions.getUserInfo();
  54. this.actions.saveToken(ret.result.token);
  55. this.actions.jump("loginSucc");
  56. },
  57. async resetPassword(data: {
  58. email?: string;
  59. phone?: string;
  60. password: string;
  61. code: string;
  62. }) {
  63. await this.https.resetPwd(data);
  64. queenApi.messageSuccess("修改成功");
  65. if (this.helper.isLoginPage()) {
  66. setTimeout(() => {
  67. queenApi.router.back();
  68. }, 1000);
  69. }
  70. },
  71. saveToken(token: string) {
  72. this.store.setToken(token);
  73. },
  74. jump(jumpType: keyof typeof User_Config["jumpOptions"]) {
  75. this.config.jumpOptions[jumpType].action.call(this);
  76. },
  77. });