UserApi.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { DataModel } from '@imengyu/js-request-transform';
  2. import { AppServerRequestModule } from '../RequestModules';
  3. import AppCofig from '@/common/config/AppCofig';
  4. export class LoginResult extends DataModel<LoginResult> {
  5. constructor() {
  6. super(LoginResult, "登录结果");
  7. //this.setNameMapperCase('Camel', 'Snake');
  8. this._beforeSolveServer = (data, self) => {
  9. if (!data.userinfo && data.user)
  10. data.userinfo = data.user;
  11. if (!data.auth && data.userinfo)
  12. data.auth = data.userinfo;
  13. if (!data.mainBodyUserInfo && data.userinfo)
  14. data.mainBodyUserInfo = data.userinfo;
  15. return data;
  16. };
  17. this._convertTable = {
  18. mainBodyUserInfo: { clientSide: 'object', clientSideRequired: true, clientSideChildDataModel: UserInfo },
  19. auth: {
  20. clientSide: 'object',
  21. clientSideRequired: true,
  22. clientSideChildDataModel: {
  23. nameMapperServer: {
  24. user_id: 'userId',
  25. expires_in: 'expiresIn',
  26. },
  27. convertTable: {
  28. token: { clientSide: 'string', clientSideRequired: true },
  29. userId: { clientSide: 'number' },
  30. expiresIn: { clientSide: 'number' },
  31. }
  32. }
  33. },
  34. }
  35. }
  36. auth !: {
  37. id: number,
  38. username: string,
  39. nickname: string,
  40. mobile: string,
  41. avatar: string,
  42. score: number,
  43. token: string,
  44. userId: number,
  45. createtime: Date,
  46. expiretime: Date,
  47. expiresIn: number,
  48. };
  49. mainBodyUserInfo !:UserInfo;
  50. }
  51. export class UserInfo extends DataModel<UserInfo> {
  52. constructor() {
  53. super(UserInfo, "用户信息");
  54. this.setNameMapperCase('Camel', 'Snake');
  55. this._convertTable = {
  56. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  57. userId: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  58. mainBodyId: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  59. loginTime: { clientSide: 'date', serverSide: 'string' },
  60. }
  61. }
  62. id = 0;
  63. userId = 0;
  64. mainBodyId = 0;
  65. nickname = '';
  66. avatar = '';
  67. intro = '';
  68. fans = '';
  69. score = '';
  70. loginTime = null as null|Date;
  71. diyname = '';
  72. }
  73. export class UserApi extends AppServerRequestModule<DataModel> {
  74. constructor() {
  75. super();
  76. }
  77. async loginThird(data?: {
  78. code: string,
  79. platform: 'wechat',
  80. encrypted_data: string,
  81. iv: string,
  82. raw_data: string,
  83. signature: string,
  84. }) {
  85. return (await this.post('/content/main_body_user/third', {
  86. appid: AppCofig.appId,
  87. main_body_id: 2, //AppCofig.mainBodyId,
  88. ...data,
  89. }, '登录', undefined, LoginResult)).data as LoginResult;
  90. }
  91. async login(data: {
  92. account: string,
  93. password: string,
  94. }) {
  95. return (await this.post('/user/login', data, '登录', undefined, LoginResult)).data as LoginResult;
  96. }
  97. async loginAdmin(data: {
  98. account: string,
  99. password: string,
  100. }) {
  101. return (await this.post('/user/adminLogin', {
  102. account: data?.account,
  103. password: data?.password,
  104. }, '登录', undefined, LoginResult)).data as LoginResult;
  105. }
  106. async getUserInfo(main_body_user_id: number) {
  107. return (await this.post('/content/main_body_user/getMainBodyUser', {
  108. main_body_user_id,
  109. }, '获取用户信息', undefined, UserInfo)).data as UserInfo;
  110. }
  111. async refresh() {
  112. return (await this.post('/content/main_body_user/refreshUser', {
  113. }, '刷新用户', undefined, LoginResult)).data as LoginResult;
  114. }
  115. }
  116. export default new UserApi();