VerificationCtrl.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. properties: {
  4. m_EdPhone: cc.EditBox,
  5. m_EdCode: cc.EditBox,
  6. m_BtGetCode: cc.Button,
  7. },
  8. ctor: function () {
  9. this.m_nNeedUpdate = 0;
  10. this.m_cbCheckState = 1; // 需要的手机绑定状态 1:需要校验输入手机号没有绑定 2:需要校验手机号已绑定 3:需要校验与当前账号绑定相同手机号
  11. this.m_BindPNum = 0; // 当前绑定手机
  12. this.m_keyPhoneCode = window.Key_PhoneCode;
  13. this.m_keyPhoneCodeTime = window.Key_PhoneCodeTime;
  14. },
  15. onLoad: function () {
  16. },
  17. OnShowView: function () {
  18. ShowO2I(this.node);
  19. },
  20. OnHideView: function () {
  21. HideI2O(this.node);
  22. },
  23. SetHook: function (Hook, callFunc) {
  24. this.m_Hook = Hook;
  25. this.m_callFunc = callFunc;
  26. },
  27. SetKey: function(Key) {
  28. this.m_keyPhoneCode = `${window.Key_PhoneCode}_${Key}`;
  29. this.m_keyPhoneCodeTime = `${window.Key_PhoneCodeTime}_${Key}`;
  30. },
  31. SetCheckState: function(cbState) {
  32. this.m_cbCheckState = cbState;
  33. },
  34. SetPhoneNum: function(PhoneNum) {
  35. this.m_EdPhone.string = PhoneNum;
  36. },
  37. CheckPhoneBind: function (Callback) {
  38. var resPhone = this._CheckInput_Phone();
  39. if(resPhone.code != 0) return false;
  40. var webUrl = `${window.PHP_HOME}/UserFunc.php?GetMark=29&PhoneNum=${resPhone.PhoneNum}`;
  41. WebCenter.GetData(webUrl, 3, function (data) {
  42. var res = JSON.parse(data);
  43. if(Callback) Callback(res.UserID);
  44. }.bind(this));
  45. return true;
  46. },
  47. GetAllowTimeOfVerificationCode: function(Key) {
  48. if(window.VCODE_GET) {
  49. var now = new Date().getTime();
  50. var codeTime = parseInt(cc.sys.localStorage.getItem(Key));
  51. var cntdown = (60000 + codeTime - now) / 1000
  52. if (codeTime != null && cntdown > 0) {
  53. this.m_Hook.ShowTips('操作頻繁,請在等待' + parseInt(cntdown) + '秒');
  54. return cntdown;
  55. }
  56. }
  57. return 0;
  58. },
  59. GetVerificationCode: function(CallBack) {
  60. var resPhone = this._CheckInput_Phone();
  61. if(resPhone.code != 0) {
  62. this.m_Hook.ShowTips(resPhone.describe);
  63. return;
  64. }
  65. if(this.GetAllowTimeOfVerificationCode(this.m_keyPhoneCodeTime) > 0) return;
  66. this.CheckPhoneBind(function(UserID) {
  67. if(this.m_cbCheckState == 1) { // 需要校验输入手机号没有绑定
  68. if(UserID > 0) {
  69. this.m_Hook.ShowTips('該手機號已被佔用!');
  70. return;
  71. }
  72. } else if(this.m_cbCheckState == 2) { // 需要校验手机号已绑定
  73. if(UserID == 0) {
  74. this.m_Hook.ShowTips('該手機未綁定任何帳號!');
  75. return;
  76. }
  77. } else if(this.m_cbCheckState == 3) { // 需要校验与当前账号绑定相同手机号
  78. var pGlobalUserData = g_GlobalUserInfo.GetGlobalUserData();
  79. if(UserID > 0) {
  80. if(UserID != pGlobalUserData.dwUserID) {
  81. this.m_Hook.ShowTips('手機號輸入錯誤!');
  82. return;
  83. }
  84. } else {
  85. this.m_Hook.ShowTips('該手機未綁定任何帳號!');
  86. return;
  87. }
  88. }
  89. if(window.VCODE_GET) {
  90. var webUrl = window.PHP_HOME + '/UserFunc.php?&GetMark=3&Phone=' + resPhone.PhoneNum;
  91. WebCenter.GetData(webUrl, null, function (data) {
  92. if (this.m_Hook) this.m_Hook.StopLoading();
  93. if (data === -1) {
  94. this.m_Hook.ShowTips('請檢查網絡!');
  95. } else {
  96. var res = JSON.parse(data);
  97. this.SetNewCode(res.code, res.code == 0 ? res.cmscode : null, res.info);
  98. if(CallBack) CallBack(res.code == 0 ? res.cmscode : null);
  99. }
  100. }.bind(this));
  101. } else {
  102. this.SetNewCode(0, 1234, '');
  103. }
  104. }.bind(this));
  105. },
  106. SetNewCode: function (code, PhoneCode, describe) {
  107. if(code == 0) {
  108. if (window.LOG_NET_DATA) console.log('SetNewCode' + PhoneCode)
  109. cc.sys.localStorage.setItem(this.m_keyPhoneCode, PhoneCode);
  110. cc.sys.localStorage.setItem(this.m_keyPhoneCodeTime, new Date().getTime());
  111. this.m_Hook.ShowTips("驗證碼已發送到您的手機");
  112. return;
  113. }
  114. this.m_Hook.ShowAlert((!describe || describe == '') ? "輸入手機號不正確!" : str);
  115. this.Clear();
  116. },
  117. OnClicked_GetCode: function () {
  118. this.GetVerificationCode();
  119. },
  120. OnEditBoxInput_Began: function (pEditBox) {
  121. this.m_TargetEB = pEditBox;
  122. },
  123. OnEditBoxInput_Changed: function (pEditBox) {
  124. if (this.m_TargetEB == this.m_EdPhone) {
  125. this.m_nNeedUpdate = 1;
  126. } else if (this.m_TargetEB == this.m_EdCode) {
  127. this.OnCheckInputVCode();
  128. }
  129. },
  130. OnEditBoxInput_Ended: function (strText) {
  131. if (this.m_TargetEB == this.m_EdPhone) {
  132. // this.m_nNeedUpdate = 1;
  133. } else if (this.m_TargetEB == this.m_EdCode) {
  134. this.OnCheckInputVCode();
  135. }
  136. },
  137. OnEditBoxInput_Return: function (pEditBox) {
  138. },
  139. _CheckInput_Phone: function() {
  140. if (this.m_EdPhone.string.length < 11) {
  141. return {code:1, describe: "請填寫正確手機號碼!", PhoneNum: null};
  142. }
  143. return {code:0, describe: "手機號碼正確!", PhoneNum: this.m_EdPhone.string};
  144. },
  145. _CheckInput_Code: function() {
  146. var now = new Date().getTime();
  147. var pcode = cc.sys.localStorage.getItem(this.m_keyPhoneCode);
  148. var codeTime = cc.sys.localStorage.getItem(this.m_keyPhoneCodeTime);
  149. if (!pcode == null) {
  150. return {code:2, describe: "請先獲取驗證碼!", PhoneCode: null};
  151. }
  152. if (now - codeTime > 3600000) {
  153. return {code:3, describe: "操作超時,請重新獲取!", PhoneCode: null};
  154. }
  155. if (this.m_EdCode.string < this.m_EdCode.string.length) {
  156. return {code:4, describe: "請輸入驗證碼!", PhoneCode: null};
  157. }
  158. if (pcode != this.m_EdCode.string) {
  159. return {code:5, describe: "驗證碼錯誤,請重新輸入!", PhoneCode: null};
  160. }
  161. return {code:0, describe: "驗證碼正確!", PhoneCode: pcode};
  162. },
  163. OnCheckInputVCode: function () {
  164. if (this.m_Hook && this.m_callFunc) this.m_callFunc(false);
  165. var resCode = this._CheckInput_Code();
  166. if (resCode.code != 0) {
  167. if(resCode.code == 5) {
  168. this.m_Hook.ShowTips(resCode.describe);
  169. }
  170. return false;
  171. }
  172. if (this.m_Hook && this.m_callFunc) this.m_callFunc(true);
  173. return true;
  174. },
  175. Check: function (bReset) {
  176. var res = {code:0, describe: "", PhoneNum: 0, PhoneCode: 0};
  177. var resPhone = this._CheckInput_Phone();
  178. if (resPhone.code != 0) {
  179. res.code = resPhone.code;
  180. res.describe = resPhone.describe;
  181. this.m_Hook.ShowTips(res.describe);
  182. return res;
  183. }
  184. var resCode = this._CheckInput_Code();
  185. if (resCode.code != 0) {
  186. res.code = resCode.code;
  187. res.describe = resCode.describe;
  188. this.m_Hook.ShowTips(res.describe);
  189. return res;
  190. }
  191. res.code = 0;
  192. res.describe = "填寫正確";
  193. res.PhoneNum = resPhone.PhoneNum;
  194. res.PhoneCode = resCode.PhoneCode;
  195. if (bReset) this.Reset();
  196. return res;
  197. },
  198. Reset: function () {
  199. //重置界面
  200. this.m_EdPhone.string = '';
  201. this.m_EdCode.string = '';
  202. },
  203. Clear: function() {
  204. cc.sys.localStorage.setItem(this.m_keyPhoneCode, null);
  205. },
  206. });