UserAuthModel.ets 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import relationalStore from '@ohos.data.relationalStore';
  2. import CommonConstants from '../../common/constants/CommonConstants';
  3. import {UserAuthInfo } from '../../viewmodel/wms/UserInfo';
  4. class UserModel {
  5. private rdbStore: relationalStore.RdbStore |undefined=undefined
  6. private tableName: string = 'USER'
  7. /**
  8. * 初始化表
  9. */
  10. async initTaskDB(context:Context){
  11. // 1.rdb配置
  12. const config = {
  13. name: CommonConstants.RDB_NAME,
  14. securityLevel: relationalStore.SecurityLevel.S1
  15. }as relationalStore.StoreConfig
  16. try {
  17. // 获取数据库存储对象
  18. this.rdbStore = await relationalStore.getRdbStore(context,config);
  19. } catch (err) {
  20. console.info(`getRdbStore err ${JSON.stringify(err)}`);
  21. }
  22. // 2.初始化SQL语句
  23. const sql = `CREATE TABLE IF NOT EXISTS USER (
  24. ID INTEGER PRIMARY KEY AUTOINCREMENT,
  25. USER_ID INTEGER NOT NULL,
  26. USER_NAME VARCHAR NOT NULL,
  27. PASSWORD VARCHAR NOT NULL,
  28. ORG_ID INTEGER NOT NULL,
  29. STATION_ID INTEGER NOT NULL,
  30. STATION_NAME VARCHAR NULL,
  31. STATION_DICE_VALUE VARCHAR NULL,
  32. AVATAR VARCHAR NULL,
  33. MAINTAIN_LOGIN_STATUS INTEGER NOT NULL,
  34. UPDATE_TIME INTEGER NOT NULL
  35. )`
  36. // 3.获取rdb
  37. relationalStore.getRdbStore(context, config, (err, rdbStore) => {
  38. if(err){
  39. console.log('testTag', '获取rdbStore失败!')
  40. return
  41. }
  42. // 执行Sql
  43. rdbStore.executeSql(sql).then(()=>{
  44. console.log('testTag', '创建用户设置表成功!')
  45. }).catch((err: object)=>{
  46. console.log('testTag', '创建失败')
  47. })
  48. // 保存rdbStore
  49. this.rdbStore = rdbStore
  50. })
  51. }
  52. // 查询最后一次用户登录的数据
  53. async getLast(): Promise<UserAuthInfo | undefined> {
  54. // 1.构建查询条件
  55. let predicates = new relationalStore.RdbPredicates(this.tableName)
  56. predicates.orderByDesc('UPDATE_TIME')
  57. predicates.limitAs(1)
  58. // 2.查询
  59. let result = await this.rdbStore?.query(predicates, ['ID', 'USER_ID', 'USER_NAME', 'PASSWORD', 'ORG_ID', 'STATION_ID', 'STATION_NAME', 'STATION_DICE_VALUE', 'AVATAR', 'MAINTAIN_LOGIN_STATUS', 'UPDATE_TIME'])
  60. // 3.2.遍历封装 只取第一条
  61. while(!result?.isAtLastRow){
  62. // 3.3.指针移动到下一行
  63. result?.goToNextRow()
  64. // 3.4.获取数据
  65. let id = result?.getLong(result?.getColumnIndex('ID'))
  66. let userId = result?.getLong(result?.getColumnIndex('USER_ID'))
  67. let userName = result?.getString(result?.getColumnIndex('USER_NAME'))
  68. let password = result?.getString(result?.getColumnIndex('PASSWORD'))
  69. let orgId = result?.getLong(result?.getColumnIndex('ORG_ID'))
  70. let stationId = result?.getLong(result?.getColumnIndex('STATION_ID'))
  71. let stationName = result?.getString(result?.getColumnIndex('STATION_NAME'))
  72. let stationDictValue = result?.getString(result?.getColumnIndex('STATION_DICE_VALUE'))
  73. let avatar = result?.getString(result?.getColumnIndex('AVATAR'))
  74. let maintainLoginStatus = result?.getLong(result?.getColumnIndex('MAINTAIN_LOGIN_STATUS'))
  75. let updateTime = result?.getLong(result?.getColumnIndex('UPDATE_TIME'))
  76. return {id: id, userId: userId, userName: userName, password: password, orgId: orgId, stationId: stationId, stationName: stationName, stationDictValue: stationDictValue, avatar: avatar, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime}
  77. }
  78. return
  79. }
  80. // 查询最后一次用户登录的数据
  81. async getByUserId(userId: number): Promise<UserAuthInfo | undefined> {
  82. if (!userId) {
  83. return
  84. }
  85. // 1.构建查询条件
  86. let predicates = new relationalStore.RdbPredicates(this.tableName)
  87. predicates.equalTo('USER_ID', userId)
  88. predicates.orderByDesc('UPDATE_TIME')
  89. predicates.limitAs(1)
  90. // 2.查询
  91. let result: relationalStore.ResultSet | undefined = await this.rdbStore?.query(predicates, ['ID', 'USER_ID', 'USER_NAME', 'PASSWORD', 'ORG_ID', 'STATION_ID', 'STATION_NAME', 'STATION_DICE_VALUE', 'AVATAR', 'MAINTAIN_LOGIN_STATUS', 'UPDATE_TIME'])
  92. // 3.2.遍历封装 只取第一条
  93. while(result && !result?.isAtLastRow){
  94. // 3.3.指针移动到下一行
  95. result.goToNextRow()
  96. // 3.4.获取数据
  97. let id = result.getLong(result.getColumnIndex('ID'))
  98. let userId = result.getLong(result.getColumnIndex('USER_ID'))
  99. let userName = result.getString(result.getColumnIndex('USER_NAME'))
  100. let password = result.getString(result.getColumnIndex('PASSWORD'))
  101. let orgId = result.getLong(result.getColumnIndex('ORG_ID'))
  102. let stationId = result.getLong(result.getColumnIndex('STATION_ID'))
  103. let stationName = result?.getString(result?.getColumnIndex('STATION_NAME'))
  104. let stationDictValue = result?.getString(result?.getColumnIndex('STATION_DICE_VALUE'))
  105. let avatar = result.getString(result.getColumnIndex('AVATAR'))
  106. let maintainLoginStatus = result.getLong(result.getColumnIndex('MAINTAIN_LOGIN_STATUS'))
  107. let updateTime = result.getLong(result.getColumnIndex('UPDATE_TIME'))
  108. return {id: id, userId: userId, userName: userName, password: password, orgId: orgId, stationId: stationId, stationName: stationName, stationDictValue: stationDictValue, avatar: avatar, maintainLoginStatus: maintainLoginStatus, updateTime: updateTime}
  109. }
  110. return
  111. }
  112. /**
  113. * 添加用户设置
  114. * @param user 用户信息
  115. * @returns 用户信息id
  116. */
  117. addUser(user: UserAuthInfo): Promise<number> | undefined {
  118. return this.rdbStore?.insert(this.tableName, {USER_ID: user.userId, USER_NAME: user.userName, PASSWORD: user.password, ORG_ID: user.orgId, STATION_ID: user.stationId, STATION_NAME: user.stationName, STATION_DICE_VALUE: user.stationDictValue, AVATAR: user.avatar, MAINTAIN_LOGIN_STATUS: user.maintainLoginStatus, UPDATE_TIME: user.updateTime} as relationalStore.ValuesBucket)
  119. }
  120. /**
  121. * 根据id更新用户设置状态
  122. * @param id 任务id
  123. * @param finished 任务是否完成
  124. */
  125. updateUser(user: UserAuthInfo) {
  126. // 1.要更新的数据
  127. let data: relationalStore.ValuesBucket = {USER_ID: user.userId as number, USER_NAME: user.userName as string, PASSWORD: user.password as string, ORG_ID: user.orgId as number, STATION_ID: user.stationId as number, STATION_NAME: user.stationName as string, STATION_DICE_VALUE: user.stationDictValue as string, AVATAR: user.avatar as string, MAINTAIN_LOGIN_STATUS: user.maintainLoginStatus as number, UPDATE_TIME: user.updateTime as number}
  128. // 2.更新的条件
  129. let predicates = new relationalStore.RdbPredicates(this.tableName)
  130. predicates.equalTo('ID', user.id)
  131. // 3.更新操作
  132. return this.rdbStore?.update(data, predicates)
  133. }
  134. /**
  135. * 根据id删除任务
  136. * @param id 任务id
  137. */
  138. deleteUserById(id: number){
  139. // 1.删除的条件
  140. let predicates = new relationalStore.RdbPredicates(this.tableName)
  141. predicates.equalTo('ID', id)
  142. // 2.删除操作
  143. return this.rdbStore?.delete(predicates)
  144. }
  145. }
  146. let userModel = new UserModel();
  147. export default userModel as UserModel;