UserApi.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { DataModel } from '@imengyu/js-request-transform';
  2. import { AppServerRequestModule } from '../RequestModules';
  3. import AppCofig from '@/common/config/AppCofig';
  4. import { VolunteerInfo } from '../inhert/VillageApi';
  5. export class LoginResult extends DataModel<LoginResult> {
  6. constructor() {
  7. super(LoginResult, "登录结果");
  8. this._convertTable = {
  9. token: { clientSide: 'string', clientSideRequired: true },
  10. userInfo: { clientSide: 'object', clientSideChildDataModel: UserInfo },
  11. villageVolunteer: { clientSide: 'object', clientSideChildDataModel: VolunteerInfo },
  12. };
  13. this._nameMapperServer = {
  14. 'userinfo': 'userInfo',
  15. }
  16. this._beforeSolveServer = (data, self) => {
  17. if (data.userinfo)
  18. data.token = (data.userinfo as any).token;
  19. return data;
  20. }
  21. this._afterSolveServer = () => {
  22. if (!this.userInfo.id)
  23. this.userInfo.id = this.id;
  24. if (!this.userInfo.mobile)
  25. this.userInfo.mobile = this.mobile;
  26. if (!this.userInfo.nickname)
  27. this.userInfo.nickname = this.nickname;
  28. if (!this.userInfo.avatar)
  29. this.userInfo.avatar = this.avatar;
  30. if (!this.userInfo.username)
  31. this.userInfo.username = this.username;
  32. }
  33. }
  34. id = 0;
  35. username = '';
  36. nickname = '';
  37. mobile = '';
  38. avatar = '';
  39. bio = '';
  40. score = null as number|null;
  41. token = '';
  42. userId = null as number|null;
  43. createtime = null as number|null;
  44. expiretime = null as number|null;
  45. expiresIn = null as number|null;
  46. inheritorId = null as number|null;
  47. loginType = 0;
  48. userInfo = new UserInfo();
  49. villageVolunteer : VolunteerInfo|null = null;
  50. }
  51. export class UserInfo extends DataModel<UserInfo> {
  52. constructor() {
  53. super(UserInfo, "用户信息");
  54. this.setNameMapperCase('Camel', 'Snake');
  55. this._nameMapperServer = {
  56. 'userid': 'userId',
  57. 'openid': 'openId',
  58. }
  59. this._convertTable = {
  60. id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
  61. }
  62. }
  63. id = 0;
  64. userId = 0;
  65. mobile = '';
  66. nickname = '';
  67. avatar = '';
  68. username = '';
  69. regionId = 0;
  70. openId = '';
  71. isReviewer = false;
  72. }
  73. export class UserApi extends AppServerRequestModule<DataModel> {
  74. constructor() {
  75. super();
  76. }
  77. static LOGIN_TYPE_ADMIN = 1;
  78. static LOGIN_TYPE_USER = 0;
  79. async loginThird(data?: {
  80. code: string,
  81. platform: 'wechat',
  82. encrypted_data: string,
  83. iv: string,
  84. raw_data: string,
  85. signature: string,
  86. }) {
  87. return (await this.post('/village/volunteer/third', '登录', {
  88. appid: AppCofig.appId,
  89. ...data,
  90. }, undefined, LoginResult)).data as LoginResult;
  91. }
  92. async login(data: {
  93. account: string,
  94. password: string,
  95. }) {
  96. return (await this.post('/user/login', '登录', data, undefined, LoginResult)).data as LoginResult;
  97. }
  98. async loginAdmin(data: {
  99. account: string,
  100. password: string,
  101. }) {
  102. return (await this.post('/user/adminLogin', '登录', {
  103. account: data?.account,
  104. password: data?.password,
  105. }, undefined, LoginResult)).data as LoginResult;
  106. }
  107. async updatePassword(data: {
  108. newpassword: string,
  109. oldpassword: string,
  110. }) {
  111. return (await this.post('/content/main_body_user/changepwd', '更新密码', data))
  112. }
  113. async getUserInfo() {
  114. return (await this.post('/content/main_body_user/getMainBodyUser', '获取用户信息', {}, undefined, UserInfo)).data as UserInfo;
  115. }
  116. async updateUserInfo(data: {
  117. nickname?: string,
  118. avatar?: string,
  119. intro?: string,
  120. password?: string,
  121. }) {
  122. return (await this.post('/content/main_body_user/editMainBodyUser', '更新用户信息', data))
  123. }
  124. async updateSystemUserInfo(data: {
  125. username?: string,
  126. nickname?: string,
  127. avatar?: string,
  128. bio?: string,
  129. }) {
  130. return (await this.post('/user/profile', '更新用户信息', {
  131. username: data.username,
  132. nickname: data?.nickname,
  133. avatar: data?.avatar,
  134. bio: data?.bio,
  135. }))
  136. }
  137. async refresh() {
  138. return (await this.post('/ich/inheritor/refresh', '刷新token', {}, undefined, LoginResult)).data as LoginResult;
  139. }
  140. async checkUserAuthed() {
  141. return await this.post('/village/village/getVillageList', '检查用户是否登录', {});
  142. }
  143. }
  144. export default new UserApi();