Aucune description

bindphone.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //绑定手机
  2. mui.ready(function() {
  3. /*定义全局变量*/
  4. var phoneName = document.getElementById("username");
  5. var setCode = document.getElementById("set-code");
  6. var obtainCode = document.getElementById("obtain-code");
  7. var bdButtn = document.getElementById("bdbuttn");
  8. var phoneCode = false;
  9. var state;
  10. var userId;
  11. mui.plusReady(function() {
  12. /*校验提交按钮显示状态*/
  13. mui('.maincon').on('keyup', "#username,#set-code", function() {
  14. hideButtn(phoneName,setCode,bdButtn,"frmactiveok");
  15. });
  16. /*点击获取验证码*/
  17. obtainCode.addEventListener('tap', function() {
  18. phoneVal();
  19. })
  20. /*注册按钮*/
  21. bdButtn.addEventListener('tap', function() {
  22. codeVal();
  23. })
  24. /*校验手机号*/
  25. function phoneVal() {
  26. var hunPhone = /^1[3|4|5|7|8]\d{9}$/;
  27. if(hunPhone.test(phoneName.value)) {
  28. isReg();
  29. } else {
  30. plus.nativeUI.toast("请输入正确的手机号码", toastStyle);
  31. return;
  32. }
  33. }
  34. /*校验用户名是否注册*/
  35. function isReg() {
  36. mui.ajax(baseUrl + '/ajax/isReg?key=' + phoneName.value, {
  37. dataType: 'json', //数据格式类型
  38. type: 'GET', //http请求类型
  39. timeout: 10000, //超时设置
  40. async: false,
  41. success: function(data) {
  42. console.log(data.data);
  43. if(data.data == false) {
  44. plus.nativeUI.toast("您的手机已被绑定", toastStyle);
  45. return;
  46. } else {
  47. phoneCode = true;
  48. if(phoneCode){
  49. sendAuthentication();
  50. }
  51. }
  52. },
  53. error: function() {
  54. plus.nativeUI.toast("服务器链接超时", toastStyle);
  55. }
  56. });
  57. }
  58. /*手机发送验证码*/
  59. function sendAuthentication() {
  60. userId = plus.storage.getItem('userid');
  61. mui.ajax(baseUrl + '/ajax/vcWithBind', {
  62. data: {
  63. mobilePhone: phoneName.value,
  64. userid:userId
  65. },
  66. dataType: 'json', //数据格式类型
  67. type: 'get', //http请求类型
  68. async: false,
  69. timeout: 10000, //超时设置
  70. success: function(data) {
  71. console.log(data.success);
  72. if(data.success) {
  73. state = data.data;
  74. doClick();
  75. }
  76. },
  77. error: function() {
  78. plus.nativeUI.toast("服务器链接超时", toastStyle);
  79. return;
  80. }
  81. })
  82. }
  83. /*30s后重新获取验证码*/
  84. function doClick() {
  85. var getCodeOff = document.getElementById("getcodeoff");
  86. obtainCode.style.display = "none";
  87. getCodeOff.style.display = "block";
  88. getCodeOff.innerHTML = "30s后重新获取";
  89. var clickTime = new Date().getTime();
  90. var Timer = setInterval(function() {
  91. var nowTime = new Date().getTime();
  92. var second = Math.ceil(30 - (nowTime - clickTime) / 1000);
  93. if(second > 0) {
  94. getCodeOff.innerHTML = second + "s后重新获取";
  95. } else {
  96. clearInterval(Timer);
  97. obtainCode.style.display = "block";
  98. getCodeOff.style.display = "none";
  99. obtainCode.innerHTML = "获取验证码";
  100. }
  101. }, 1000);
  102. }
  103. /*校验验证码*/
  104. function codeVal() {
  105. mui.ajax(baseUrl + '/ajax/validCode', {
  106. data: {
  107. "state": state,
  108. "vc": setCode.value
  109. },
  110. dataType: 'json', //数据格式类型
  111. async: false,
  112. type: 'POST', //http请求类型
  113. timeout: 10000, //超时设置
  114. success: function(data) {
  115. if(data.success) {
  116. if(data.data) {
  117. bdOK();
  118. }else{
  119. plus.nativeUI.toast("验证码不正确", toastStyle);
  120. return;
  121. }
  122. }else{
  123. console.log(data.msg);
  124. if(data.msg=="验证超时"){
  125. plus.nativeUI.toast("验证码超时", toastStyle);
  126. return;
  127. }else{
  128. plus.nativeUI.toast("请填写正确的手机号,验证码", toastStyle);
  129. return;
  130. }
  131. }
  132. },
  133. error: function() {
  134. plus.nativeUI.toast("服务器链接超时", toastStyle);
  135. return;
  136. }
  137. })
  138. }
  139. /*发送后台信息*/
  140. function bdOK(){
  141. console.log(state);
  142. console.log(userId);
  143. console.log(phoneName.value);
  144. console.log(setCode.value);
  145. mui.ajax(baseUrl + '/ajax/bindMobilePhone', {
  146. data: {
  147. state:state,
  148. userid:userId,
  149. mobilePhone:phoneName.value,
  150. validateCode:setCode.value
  151. },
  152. dataType: 'json', //数据格式类型
  153. type: 'POST', //http请求类型
  154. async: false,
  155. timeout: 10000, //超时设置
  156. success: function(data) {
  157. console.log(data.success);
  158. console.log(data.data);
  159. if(data.success && data.data) {
  160. plus.nativeUI.toast("手机绑定成功", toastStyle);
  161. var securityPage = plus.webview.getWebviewById('../html/security.html');
  162. mui.fire(securityPage,'xsphone', {
  163. phonetel:phoneName.value
  164. });
  165. mui.back();
  166. }else{
  167. plus.nativeUI.toast("手机绑定失败", toastStyle);
  168. return;
  169. }
  170. },
  171. error: function() {
  172. plus.nativeUI.toast("服务器链接超时", toastStyle);
  173. return;
  174. }
  175. })
  176. }
  177. });
  178. });