LoginCollect.ets 10.0 KB

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