Login.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import HTTPS, { NetGet, NetPost } from "./HTTPS";
  8. import Tips from "./Tips";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class Login extends cc.Component {
  12. @property(cc.EditBox)
  13. Iphone: cc.EditBox = null;
  14. @property(cc.EditBox)
  15. Code: cc.EditBox = null;
  16. protected onLoad(): void {
  17. }
  18. CountDown = 0
  19. SendCode() {
  20. if (!this.checkIphone()) {
  21. Tips.Instance.show('手机号码不正确')
  22. return
  23. }
  24. if (this.CountDown != 0) {
  25. return
  26. }
  27. HTTPS.Instance.post(NetPost.SendIphoneCode, {
  28. mobile: this.Iphone.string
  29. }).then(res => {
  30. if (res.code == 200) {
  31. Tips.Instance.show(res.msg)
  32. let Label = this.node.getChildByName("SendCode").getChildByName("Label").getComponent(cc.Label)
  33. this.CountDown = 60
  34. Label.string = `(${this.CountDown}s)`
  35. this.schedule(() => {
  36. Label.string = `(${--this.CountDown}s)`
  37. if (this.CountDown == 0) {
  38. this.unscheduleAllCallbacks()
  39. Label.string = '验证码'
  40. }
  41. }, 1, 59, 1)
  42. } else {
  43. Tips.Instance.show(res.msg)
  44. }
  45. })
  46. }
  47. login() {
  48. if (!this.checkIphone()) {
  49. Tips.Instance.show('手机号码不正确')
  50. return
  51. }
  52. HTTPS.Instance.post(NetPost.IphoneLogin, {
  53. mobile: this.Iphone.string,
  54. code: this.Code.string
  55. }).then(res => {
  56. if (res.code == 200) {
  57. // "token": "xxx",
  58. // "user_info": {
  59. // "id": 1,
  60. // "username": "13800138000",
  61. // "nickname": "138****8000",
  62. // "avatar": "",
  63. // "balance": "0.00"
  64. // }
  65. res.data.user_info
  66. HTTPS.Instance.token = res.data.token
  67. HTTPS.Instance.get(NetGet.Task).then(res => {
  68. })
  69. }
  70. })
  71. }
  72. // update (dt) {}
  73. checkIphone() {
  74. function isValidPhoneNumber(phoneNumber: string): boolean {
  75. // 正则表达式,适用于中国手机号码
  76. const phonePattern = /^1[3-9]\d{9}$/;
  77. return phonePattern.test(phoneNumber);
  78. }
  79. return isValidPhoneNumber(this.Iphone.string)
  80. }
  81. }