login.uts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import request from '@/utils/request'
  2. import { getToken, removeToken, removeApiToken, getCurrentUserNameSync } from '@/utils/auth';
  3. import { constant } from '@/utils/constant';
  4. // #ifdef APP-ANDROID
  5. import {selectTableData, insertTableData} from '@/utils/sqlite'
  6. // #endif
  7. export function offlineData(data:UTSJSONObject):Promise<UTSJSONObject> {
  8. return new Promise((resolve, reject) => {
  9. let result = JSON.parseObject(JSON.stringify(data)) as UTSJSONObject
  10. let ret = {} as UTSJSONObject
  11. ret['errMsg']= 'ok'
  12. ret['data'] = [result]
  13. resolve(ret)
  14. });
  15. }
  16. // 登录方法
  17. export function login(username:string, password:string, code:string, uuid:string):Promise<UTSJSONObject> {
  18. const data = {
  19. username,
  20. password,
  21. code,
  22. uuid
  23. } as UTSJSONObject
  24. let data2 = {
  25. username: "admin",
  26. password: "admin123",
  27. code: "",
  28. uuid: '1',
  29. token: '123456',
  30. roles:['admin']
  31. } as UTSJSONObject
  32. // #ifdef APP-ANDROID
  33. const result = selectTableData('app_user', 'username', username, 'password', password);
  34. console.log(result);
  35. return result;
  36. // #endif
  37. // #ifdef H5
  38. return offlineData(data2)
  39. // #endif
  40. /*
  41. return request({
  42. 'url': '/login',
  43. headers: {
  44. isToken: false
  45. },
  46. 'method': 'post',
  47. 'data': data
  48. })*/
  49. }
  50. export type RegForm={
  51. username: string,
  52. name: string,
  53. password: string,
  54. token: string,
  55. confirmPassword: string,
  56. code: string,
  57. uuid: string
  58. }
  59. // 注册方法
  60. export function register(data:RegForm):Promise<UTSJSONObject> {
  61. /*
  62. return request({
  63. url: '/register',
  64. headers: {
  65. isToken: false
  66. },
  67. method: 'post',
  68. data
  69. }) */
  70. // #ifdef APP-ANDROID
  71. let condition = 'username,name,password,role,status,createtime,updatetime'
  72. let date = "strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime')"
  73. let dataStr = `'${data.username}','${data.name}','${data.password}','user',1, ${date}, ${date}`;
  74. console.log(dataStr)
  75. const result = insertTableData('app_user', dataStr, condition);
  76. console.log(result);
  77. return result;
  78. // #endif
  79. // #ifdef H5
  80. return offlineData(data)
  81. // #endif
  82. }
  83. // 获取用户详细信息
  84. export function getInfo():Promise<UTSJSONObject> {
  85. /*
  86. return request({
  87. 'url': '/getInfo',
  88. 'method': 'get'
  89. })*/
  90. let data = {
  91. username: "admin",
  92. name: "管理员"
  93. password: "admin123",
  94. code: "",
  95. uuid: '1',
  96. token: '123456',
  97. roles:[] as string[]
  98. } as UTSJSONObject
  99. // #ifdef APP-ANDROID
  100. console.log('getCurrentUserNameSync', getCurrentUserNameSync())
  101. const result = selectTableData('app_user', 'username', getCurrentUserNameSync(), null, null);
  102. console.log(result);
  103. return result;
  104. // #endif
  105. // #ifdef H5
  106. return offlineData(data)
  107. // #endif
  108. }
  109. // 退出方法
  110. export function logout():Promise<UTSJSONObject> {
  111. return new Promise((resolve, reject) => {
  112. try {
  113. // 移除用户token
  114. removeToken();
  115. removeApiToken();
  116. // 清除用户相关信息
  117. uni.removeStorageSync(constant.name);
  118. uni.removeStorageSync(constant.username);
  119. uni.removeStorageSync(constant.roles);
  120. uni.removeStorageSync(constant.permissions);
  121. uni.removeStorageSync(constant.avatar);
  122. // 直接返回成功,因为本地已经清理了用户数据
  123. resolve({ code: 200, message: '退出成功' });
  124. } catch (error) {
  125. console.error('退出登录时发生错误:', error);
  126. reject({ code: 500, message: '退出失败' });
  127. }
  128. });
  129. }
  130. // 获取验证码
  131. export function getCodeImg():Promise<UTSJSONObject> {
  132. return request({
  133. 'url': '/captchaImage',
  134. headers: {
  135. isToken: false
  136. },
  137. method: 'get',
  138. timeout: 20000
  139. })
  140. }