Login.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. @property(cc.Node)
  17. Login: cc.Node = null;
  18. clickLogin() {
  19. this.Login.active = true
  20. }
  21. protected onLoad(): void {
  22. cc.director.preloadScene('scene')
  23. }
  24. CountDown = 0
  25. SendCode() {
  26. if (!this.checkIphone()) {
  27. Tips.Instance.show('手机号码不正确')
  28. return
  29. }
  30. if (this.CountDown != 0) {
  31. return
  32. }
  33. HTTPS.Instance.post(NetPost.SendIphoneCode, {
  34. mobile: this.Iphone.string
  35. }).then(res => {
  36. if (res.code == 200) {
  37. Tips.Instance.show(res.msg)
  38. let Label = this.node.getChildByName("SendCode").getChildByName("Label").getComponent(cc.Label)
  39. this.CountDown = 60
  40. Label.string = `(${this.CountDown}s)`
  41. this.schedule(() => {
  42. Label.string = `(${--this.CountDown}s)`
  43. if (this.CountDown == 0) {
  44. this.unscheduleAllCallbacks()
  45. Label.string = '验证码'
  46. }
  47. }, 1, 59, 1)
  48. } else {
  49. Tips.Instance.show(res.msg)
  50. }
  51. })
  52. }
  53. login() {
  54. if (!this.checkIphone()) {
  55. Tips.Instance.show('手机号码不正确')
  56. return
  57. }
  58. HTTPS.Instance.post(NetPost.IphoneLogin, {
  59. mobile: this.Iphone.string,
  60. code: this.Code.string
  61. }).then(res => {
  62. if (res.code != 200) {
  63. Tips.Instance.show(res.msg)
  64. return
  65. }
  66. Tips.Instance.show('登录成功')
  67. this.Login.active = false
  68. HTTPS.Instance.user_info = res.data.user_info
  69. HTTPS.Instance.token = res.data.token
  70. let startBtn = cc.Canvas.instance.node.getChildByName("0startPage").getChildByName("startBtn")
  71. let login = cc.Canvas.instance.node.getChildByName("0startPage").getChildByName("login")
  72. startBtn.active = true
  73. login.active = false
  74. })
  75. }
  76. // update (dt) {}
  77. checkIphone() {
  78. function isValidPhoneNumber(phoneNumber: string): boolean {
  79. // 正则表达式,适用于中国手机号码
  80. const phonePattern = /^1[3-9]\d{9}$/;
  81. return phonePattern.test(phoneNumber);
  82. }
  83. return isValidPhoneNumber(this.Iphone.string)
  84. }
  85. //记录广告观看
  86. Setrecord() {
  87. HTTPS.Instance.post(NetPost.record, {
  88. ad_type: 'reward',//广告类型(默认reward)
  89. duration: 'sign_in',//观看时长,单位秒(默认30)
  90. }).then(res => {
  91. console.error(res);
  92. if (res.code == 200) {
  93. }
  94. })
  95. }
  96. openTask() {
  97. let Task = this.node.getChildByName("Task")
  98. Task.active = true
  99. }
  100. openwithdraw() {
  101. let Task = this.node.getChildByName("withdraw")
  102. Task.active = true
  103. }
  104. }