bindPhone.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // pages/bindphone/bindPhone.js
  2. let log = require('../../utils/log.js');
  3. Page({
  4. /**
  5. * 组件的初始数据
  6. */
  7. data: {
  8. userInfo: {},
  9. isLogin: false,
  10. phone: null,
  11. errorMsg: '',
  12. vcodeFlag: true,
  13. vcodeTimeOut: 0,
  14. vcodeTimeOutDefault: 0,
  15. scene: "LN_PHONE",
  16. vcodeLen:2,
  17. vcodeInterval:null,
  18. vcode_button_text:'获取验证码',
  19. servicetel: '4009608068'
  20. },
  21. onError(e) {
  22. wx.showModal({
  23. title: '温馨提示',
  24. content: '当前网络环境较差,无法连接服务器,请稍后重试。有问题请联系客服电话4009608068,接听时段08:30-17:00。',
  25. confirmColor:'#00AADD',
  26. showCancel: false
  27. });
  28. },
  29. inputPhone(e) {
  30. this.setData({
  31. phone: e.detail.value
  32. });
  33. this.checkAll();
  34. },
  35. inputVerificatrCode(e) {
  36. this.setData({
  37. vcode: e.detail.value
  38. });
  39. this.checkAll();
  40. },
  41. checkAll() {
  42. if (!this.checkPhoneNumber()) {
  43. this.setData({
  44. errorMsg: '手机号输入有误,请重新输入',
  45. checkPhone: false,
  46. checkCode: false
  47. });
  48. return;
  49. } else {
  50. this.setData({
  51. checkPhone: true,
  52. errorMsg: ''
  53. });
  54. }
  55. if (!this.checkVcode()) {
  56. this.setData({
  57. errorMsg: '验证码格式不正确'
  58. });
  59. this.setData({
  60. checkCode: false
  61. });
  62. return;
  63. } else {
  64. this.setData({
  65. errorMsg: ''
  66. });
  67. }
  68. if (this.data.phone && this.data.vcode) {
  69. this.setData({
  70. checkCode: true
  71. });
  72. } else {
  73. this.setData({
  74. checkCode: false
  75. });
  76. }
  77. },
  78. checkPhoneNumber() {
  79. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  80. return false;
  81. }
  82. return true;
  83. },
  84. checkVcode() {
  85. if (this.data.vcode==undefined || this.data.vcode=="") {
  86. return false;
  87. }
  88. if(this.data.vcode.length==this.data.vcodeLen){
  89. return true;
  90. }
  91. return false;
  92. },
  93. getVerificateCode(){
  94. let vcodeFlag = this.data.vcodeFlag;
  95. let checkPhone = this.data.checkPhone;
  96. let phone = this.data.phone;
  97. let that = this;
  98. if (!vcodeFlag) {
  99. return;
  100. }
  101. if (!checkPhone) {
  102. that.setData({
  103. errorMsg: '手机号输入有误,请重新输入',
  104. checkCode: false
  105. });
  106. return;
  107. } else {
  108. that.setData({
  109. errorMsg: ''
  110. });
  111. }
  112. this.setData({
  113. vcodeTimeOut:this.data.vcodeTimeOutDefault
  114. })
  115. wx.request({
  116. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcodeUnique',
  117. data: {
  118. userId: that.data.userInfo.userId,
  119. scene: that.data.scene,
  120. terminal: phone,
  121. },
  122. method: 'POST',
  123. success(res) {
  124. console.info(res)
  125. if (res.data.code == 1) {
  126. wx.showModal({
  127. title: '提示',
  128. content:res.data.msg,
  129. showCancel:false,
  130. confirmColor:'#00AADD'
  131. });
  132. }else if (res.data.code == 2) {
  133. // 跳转
  134. wx.showModal({
  135. title: '提示',
  136. content: '手机已绑定账户,请检查手机号是否填写正确或联系客服',
  137. showCancel:false,
  138. confirmColor:'#00AADD',
  139. });
  140. }else if (res.data.code == 200) {
  141. wx.showToast({
  142. title: '验证码已发送',
  143. icon: 'success',
  144. duration: 1000 //持续的时间
  145. })
  146. let vcodeInterval = setInterval(function () {
  147. if (that.data.vcodeTimeOut <= 0) {
  148. that.setData({
  149. vcodeFlag: true,
  150. vcode_button_text: '获取验证码'
  151. })
  152. clearInterval(vcodeInterval);
  153. }else{
  154. let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
  155. that.setData({
  156. vcodeTimeOut:that.data.vcodeTimeOut-1,
  157. vcodeFlag: false,
  158. vcode_button_text: str
  159. })
  160. }
  161. }, 1000);
  162. }
  163. }
  164. });
  165. },
  166. bindPhone(e) {
  167. let that = this;
  168. let loginName = this.data.userInfo.loginName;
  169. let { phone, vcode } = this.data;
  170. log.info('[绑定手机]', '[绑定手机]', '[请求]', { phone, vcode });
  171. wx.request({
  172. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/bindPhone',
  173. data: {
  174. loginName,
  175. phonenumber: phone,
  176. vcode
  177. },
  178. method: 'POST',
  179. success(res) {
  180. if (res.data.code == 1) {
  181. that.setData({
  182. vcode: null,
  183. });
  184. wx.showModal({
  185. title: '提示',
  186. content:res.data.msg,
  187. showCancel:false,
  188. confirmColor:'#00AADD',
  189. });
  190. log.info('[绑定手机号]', '[绑定手机号]', '[失败code==1]', res.data);
  191. } else {
  192. wx.setStorageSync("userInfo", res.data);
  193. wx.setStorageSync("isLogin", true);
  194. //跳转到上一页
  195. log.info('[绑定手机号]', '[绑定手机号]', '[成功返回上一个界面]', res.data);
  196. wx.navigateBack();
  197. }
  198. },
  199. fail(e){
  200. getApp().showNetworkError();
  201. }
  202. });
  203. },
  204. /**
  205. * 生命周期函数--监听页面加载
  206. */
  207. onLoad: function (options) {
  208. let userInfo = wx.getStorageSync('userInfo');
  209. let isLogin = wx.getStorageSync('isLogin');
  210. // 页面显示
  211. if (userInfo && isLogin) {
  212. this.setData({
  213. userInfo: userInfo,
  214. isLogin: isLogin
  215. });
  216. if ((!userInfo.userId && userInfo.userId != 0)){
  217. log.info('[首页]', '[已登陆未有用户ID跳转登录界面]');
  218. let url = `/pages/login/phone_login/phone_login`;
  219. wx.navigateTo({
  220. url
  221. });
  222. return;
  223. }
  224. } else {
  225. //未登录信息
  226. this.setData({
  227. userInfo: {}
  228. });
  229. let url = `/pages/login/phone_login/phone_login`;
  230. wx.navigateTo({
  231. url
  232. });
  233. return;
  234. }
  235. let scene = this.data.scene;
  236. let that = this
  237. wx.request({
  238. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  239. data: {
  240. scene,
  241. },
  242. method: 'POST',
  243. success(res) {
  244. that.setData({
  245. vcodeInfo:false,
  246. vcodeTimeOut:res.data.result.expire,
  247. vcodeTimeOutDefault:res.data.result.expire,
  248. vcodeLen:res.data.result.len
  249. })
  250. }
  251. });
  252. },
  253. /**
  254. * 生命周期函数--监听页面初次渲染完成
  255. */
  256. onReady: function () {
  257. },
  258. /**
  259. * 生命周期函数--监听页面显示
  260. */
  261. onShow: function () {
  262. },
  263. /**
  264. * 生命周期函数--监听页面隐藏
  265. */
  266. onHide: function () {
  267. },
  268. /**
  269. * 生命周期函数--监听页面卸载
  270. */
  271. onUnload: function () {
  272. },
  273. /**
  274. * 页面相关事件处理函数--监听用户下拉动作
  275. */
  276. onPullDownRefresh: function () {
  277. },
  278. /**
  279. * 页面上拉触底事件的处理函数
  280. */
  281. onReachBottom: function () {
  282. },
  283. /**
  284. * 用户点击右上角分享
  285. */
  286. onShareAppMessage: function () {
  287. }
  288. })