LoginCollect.ets 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { YTLog } from '../../../../Index';
  2. import { IBestToast } from '@ibestservices/ibest-ui';
  3. import { YTToast } from '../utils/YTToast';
  4. import { AppStorageKeyCollect } from '../constants';
  5. import { YTUserRequest } from '../apis/YTUserRequest';
  6. export class LoginCollect {
  7. phonenumber?: string; // 用户输入的手机号
  8. password?: string; // 密码(密码登录/注册/重置时使用)
  9. smsCode?: string; // 验证码(验证码登录/注册/重置时使用)
  10. confirmPassword?: string; // 确认密码(注册/重置时使用)
  11. isAgreePrivacy: boolean = false; // 是否同意隐私政策
  12. isPassword: boolean = false; // 当前操作类型:密码登录/验证码登录
  13. private code?: string //华为登录code
  14. private operation: 'register' | 'reset' | 'login'; // 当前操作类型:注册/重置/登录
  15. private uuid?: string //验证码登录防伪标识
  16. private isRequesting: boolean = false //是否已经发送一个请求了
  17. private loginMethod: 'harmony' | 'common' = 'common'
  18. private countDown: number = 0
  19. constructor(registerOrReset: 'register' | 'reset' | 'login') {
  20. this.operation = registerOrReset
  21. const storageUUID = AppStorage.get<string>(AppStorageKeyCollect.UUID)
  22. if (storageUUID) {
  23. this.uuid = storageUUID
  24. }
  25. }
  26. /**
  27. * 执行登录/注册/重置操作,根据当前场景校验必要字段并处理逻辑
  28. * @param harmony表示华为登录,common表示其它普通登录
  29. */
  30. executeLogin(loginMethod: 'harmony' | 'common' = this.loginMethod) {
  31. if (loginMethod == undefined) {
  32. throw Error("请传入登录方法")
  33. }
  34. //记录弹出隐私政策时的登录方法
  35. this.loginMethod = loginMethod
  36. // 基础校验:必须同意隐私政策
  37. if (!this.isAgreePrivacy) {
  38. YTToast.getInstance().agreePrivacy({ loginType: this })
  39. return
  40. }
  41. //已有请求则拦截多于请求
  42. if (this.isRequesting) {
  43. return
  44. }
  45. if (loginMethod == 'harmony') {
  46. YTUserRequest.harmonyLogin(this)
  47. return
  48. }
  49. // 根据当前操作类型分支处理
  50. switch (this.operation) {
  51. case 'register':
  52. this.handleRegister(); // 处理注册逻辑
  53. break
  54. case 'reset':
  55. this.handleResetPassword(); // 处理重置密码逻辑
  56. break
  57. case 'login':
  58. this.handleLogin(); // 默认处理登录逻辑(密码/验证码登录)
  59. break
  60. }
  61. }
  62. //将所有属性重置为初始态
  63. clearAll() {
  64. this.code = undefined
  65. this.phonenumber = undefined
  66. this.password = undefined
  67. this.smsCode = undefined
  68. this.confirmPassword = undefined
  69. this.isAgreePrivacy = false
  70. this.uuid = undefined
  71. this.isRequesting = false
  72. }
  73. //如获取验证码或华为登录后,后端返回的字段使用此方法接收
  74. updateUsefulProperty(instance: LoginCollect) {
  75. //判断不为空再赋值
  76. if (instance.code) {
  77. this.code = instance.code
  78. }
  79. if (instance.phonenumber) {
  80. this.phonenumber = instance.phonenumber
  81. }
  82. if (instance.password) {
  83. this.password = instance.password
  84. }
  85. if (instance.operation) {
  86. this.operation = instance.operation
  87. }
  88. if (instance.smsCode) {
  89. this.smsCode = instance.smsCode
  90. }
  91. if (instance.confirmPassword) {
  92. this.confirmPassword = instance.confirmPassword
  93. }
  94. if (instance.isAgreePrivacy) {
  95. this.isAgreePrivacy = instance.isAgreePrivacy
  96. }
  97. if (instance.uuid) {
  98. this.uuid = instance.uuid
  99. }
  100. return this
  101. }
  102. //发送验证码
  103. requestSmsCode(success: (res: LoginCollect) => void) {
  104. if (this.phonenumber?.match(new RegExp('^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$'))) {
  105. YTUserRequest.getLoginCaptcha(this.phonenumber, (res, err) => {
  106. if (!err && !res) {
  107. IBestToast.show({ message: '请勿重复发送验证码' })
  108. return
  109. } else if (res) {
  110. //防止退出登录页导致uuid直接失效
  111. AppStorage.setOrCreate<string>(AppStorageKeyCollect.UUID, this.uuid)
  112. IBestToast.show({ message: '验证码发送成功' })
  113. this.updateUsefulProperty(res)
  114. success(this)
  115. } else {
  116. YTLog.error(err)
  117. }
  118. })
  119. } else {
  120. IBestToast.show({ message: '请输入正确的手机号' })
  121. }
  122. }
  123. getCode(): string | undefined {
  124. return this.code
  125. }
  126. setCode(code: string) {
  127. this.code = code
  128. return this
  129. }
  130. getPhonenumber(): string | undefined {
  131. return this.phonenumber
  132. }
  133. setPhonenumber(phonenumber: string) {
  134. this.phonenumber = phonenumber
  135. return this
  136. }
  137. getPassword(): string | undefined {
  138. return this.password
  139. }
  140. setPassword(password: string) {
  141. this.password = password
  142. return this
  143. }
  144. getOperation(): 'register' | 'reset' | 'login' {
  145. return this.operation
  146. }
  147. setOperation(registerOrReset: 'register' | 'reset' | 'login') {
  148. this.operation = registerOrReset
  149. return this
  150. }
  151. getSmsCode(): string | undefined {
  152. return this.smsCode
  153. }
  154. setSmsCode(smsCode: string) {
  155. this.smsCode = smsCode
  156. return this
  157. }
  158. getConfirmPassword(): string | undefined {
  159. return this.confirmPassword
  160. }
  161. setConfirmPassword(confirmPassword: string) {
  162. this.confirmPassword = confirmPassword
  163. return this
  164. }
  165. getIsAgreePrivacy(): boolean {
  166. return this.isAgreePrivacy
  167. }
  168. setIsAgreePrivacy(isAgreePrivacy: boolean) {
  169. this.isAgreePrivacy = isAgreePrivacy
  170. return this
  171. }
  172. getUuid(): string | undefined {
  173. return this.uuid
  174. }
  175. setUuid(uuid: string) {
  176. this.uuid = uuid
  177. return this
  178. }
  179. getCountDown() {
  180. return this.countDown
  181. }
  182. /**
  183. * 处理登录逻辑(支持密码登录或验证码登录)
  184. */
  185. private handleLogin() {
  186. // 核心校验:手机号必填
  187. if (!this.phonenumber) {
  188. IBestToast.show({ message: '登录失败:请输入手机号码', type: "warning" })
  189. return
  190. }
  191. // 场景分支:密码登录 或 验证码登录
  192. // 补充: 必须是在密码登录状态下才校验密码的合法性
  193. if (this.isPassword && this.password) {
  194. // 密码登录:校验密码非空
  195. if (!this.password.trim()) {
  196. IBestToast.show({ message: '登录失败:请输入密码', type: "warning" })
  197. return
  198. }
  199. // 调用密码登录接口
  200. YTUserRequest.passwordLogin(this)
  201. return
  202. } else if (this.smsCode) {
  203. // 验证码登录:校验验证码非空
  204. if (!this.smsCode.trim()) {
  205. IBestToast.show({ message: '登录失败:请输入验证码', type: "warning" })
  206. return
  207. }
  208. // 验证码登录逻辑
  209. YTUserRequest.phonenumberLogin(this)
  210. return
  211. } else {
  212. // 无密码/验证码时提示
  213. IBestToast.show({ message: '登录失败:请输入密码或验证码', type: "warning" })
  214. return
  215. }
  216. }
  217. /**
  218. * 处理注册逻辑(手机号+验证码+密码+确认密码)
  219. */
  220. private handleRegister() {
  221. // 核心校验:手机号必填
  222. if (!this.phonenumber) {
  223. IBestToast.show({ message: '注册失败:请输入手机号码', type: "warning" })
  224. return
  225. }
  226. // 场景分支:注册需要验证码
  227. if (!this.smsCode?.trim()) {
  228. IBestToast.show({ message: '注册失败:请输入验证码', type: "warning" })
  229. return
  230. }
  231. // 密码相关校验
  232. if (!this.password?.trim()) {
  233. IBestToast.show({ message: '注册失败:请设置密码', type: "warning" })
  234. return
  235. }
  236. if (!this.confirmPassword?.trim()) {
  237. IBestToast.show({ message: '注册失败:请确认密码', type: "warning" })
  238. return
  239. }
  240. if (!this.validatePassword(this.password)) {
  241. IBestToast.show({ message: '注册失败:密码必须包含字母、数字和特殊符号,并且长度在8到20位之间', type: "warning" })
  242. return
  243. }
  244. if (this.password !== this.confirmPassword) {
  245. IBestToast.show({ message: '注册失败:两次输入的密码不一致', type: "warning" })
  246. return
  247. }
  248. // 实际场景可调用注册接口(示例)
  249. YTUserRequest.register(this)
  250. return
  251. }
  252. /**
  253. * 处理重置密码逻辑(手机号+验证码+新密码+确认密码)
  254. */
  255. private handleResetPassword() {
  256. // 核心校验:手机号必填
  257. if (!this.phonenumber) {
  258. IBestToast.show({ message: '重置密码失败:请输入手机号码', type: "warning" })
  259. return
  260. }
  261. // 场景分支:重置需要验证码
  262. if (!this.smsCode?.trim()) {
  263. IBestToast.show({ message: '重置密码失败:请输入验证码', type: "warning" })
  264. return
  265. }
  266. // 新密码相关校验
  267. if (!this.password?.trim()) {
  268. IBestToast.show({ message: '重置密码失败:请设置新密码', type: "warning" })
  269. return
  270. }
  271. if (!this.confirmPassword?.trim()) {
  272. IBestToast.show({ message: '重置密码失败:请确认新密码', type: "warning" })
  273. return
  274. }
  275. if (this.password !== this.confirmPassword) {
  276. IBestToast.show({ message: '重置密码失败:两次输入的新密码不一致', type: "warning" })
  277. return
  278. }
  279. // 实际场景可调用重置密码接口
  280. YTUserRequest.resetPassword(this, () => {
  281. this.handleLogin()
  282. })
  283. return
  284. }
  285. // 密码校验函数
  286. private validatePassword(password: string): boolean {
  287. // 正则表达式说明:
  288. // ^ 表示字符串开始
  289. // (?=.*[A-Za-z]) 至少包含一个字母(大小写均可)
  290. // (?=.*\d) 至少包含一个数字
  291. // (?=.*[^A-Za-z0-9]) 至少包含一个非字母数字的特殊符号(如@#$%^&*等)
  292. // .{8,20} 长度限制 8-20 位
  293. // $ 表示字符串结束
  294. const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,20}$/;
  295. return passwordRegex.test(password);
  296. }
  297. }