unbindphone.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // pages/ucenter/accountsecurity/unbindphone/unbindphone.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: "UN_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. showCancel: false,
  26. confirmColor:'#00AADD',
  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. scene: that.data.scene,
  119. terminal: phone,
  120. },
  121. method: 'POST',
  122. success(res) {
  123. if (res.data.code == 1) {
  124. wx.showModal({
  125. title: '提示',
  126. content:res.data.msg,
  127. showCancel:false,
  128. confirmColor:'#00AADD',
  129. });
  130. }else if (res.data.code == 2) {
  131. // 跳转
  132. wx.showModal({
  133. title: '提示',
  134. content: '手机已绑定账户,请检查手机号是否填写正确或联系客服',
  135. showCancel:false,
  136. confirmColor:'#00AADD',
  137. });
  138. }else if (res.data.code == 200) {
  139. wx.showToast({
  140. title: '验证码已发送',
  141. icon: 'success',
  142. duration: 1000 //持续的时间
  143. })
  144. let vcodeInterval = setInterval(function () {
  145. if (that.data.vcodeTimeOut <= 0) {
  146. that.setData({
  147. vcodeFlag: true,
  148. vcode_button_text: '获取验证码'
  149. })
  150. clearInterval(vcodeInterval);
  151. }else{
  152. let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
  153. that.setData({
  154. vcodeTimeOut:that.data.vcodeTimeOut-1,
  155. vcodeFlag: false,
  156. vcode_button_text: str
  157. })
  158. }
  159. }, 1000);
  160. }
  161. }
  162. });
  163. },
  164. bindPhone(e) {
  165. let that = this;
  166. let loginName = this.data.userInfo.loginName;
  167. let { phone, vcode } = this.data;
  168. log.info('[绑定手机]', '[绑定手机]', '[请求]', { phone, vcode });
  169. wx.request({
  170. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/unbindPhone',
  171. data: {
  172. loginName,
  173. phonenumber: phone,
  174. vcode
  175. },
  176. method: 'POST',
  177. success(res) {
  178. if (res.data.code == 1) {
  179. that.setData({
  180. vcode: null,
  181. });
  182. wx.showModal({
  183. title: '提示',
  184. content:res.data.msg,
  185. showCancel:false,
  186. confirmColor:'#00AADD',
  187. });
  188. log.info('[绑定手机号]', '[绑定手机号]', '[失败code==1]', res.data);
  189. }else if (res.data.code == 2) {
  190. wx.showModal({
  191. title: '提示',
  192. content: '您未绑定手机号,请重新登录后自动刷新绑定信息',
  193. confirmText: '去登录',
  194. showCancel:false,
  195. confirmColor:'#00AADD',
  196. success: function (res1) {
  197. if (res1.confirm) {
  198. wx.removeStorageSync('userInfo');
  199. wx.removeStorageSync('isLogin');
  200. let url = '/pages/login/phone_login/phone_login';
  201. wx.redirectTo({
  202. url
  203. })
  204. }
  205. }
  206. });
  207. log.info('[绑定手机号]', '[绑定手机号]', '[失败code==2]', res.data);
  208. } else {
  209. //跳转到上一页
  210. wx.setStorageSync("userInfo", res.data);
  211. wx.setStorageSync("isLogin", true);
  212. log.info('[绑定手机号]', '[绑定手机号]', '[成功返回首页]');
  213. wx.redirectTo({
  214. url:"/pages/ucenter/index/index"
  215. })
  216. }
  217. },
  218. fail(e){
  219. getApp().showNetworkError();
  220. }
  221. });
  222. },
  223. /**
  224. * 生命周期函数--监听页面加载
  225. */
  226. onLoad: function (options) {
  227. let userInfo = wx.getStorageSync('userInfo');
  228. let isLogin = wx.getStorageSync('isLogin');
  229. // 页面显示
  230. if (userInfo && isLogin) {
  231. this.setData({
  232. userInfo: userInfo,
  233. isLogin: isLogin
  234. });
  235. if ((!userInfo.userId && userInfo.userId != 0)){
  236. log.info('[首页]', '[已登陆未有用户ID跳转登录界面]');
  237. let url = `/pages/login/phone_login/phone_login`;
  238. wx.navigateTo({
  239. url
  240. });
  241. return;
  242. }
  243. } else {
  244. //未登录信息
  245. this.setData({
  246. userInfo: {}
  247. });
  248. let url = `/pages/login/phone_login/phone_login`;
  249. wx.navigateTo({
  250. url
  251. });
  252. return;
  253. }
  254. let scene = this.data.scene;
  255. let that = this
  256. wx.request({
  257. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  258. data: {
  259. scene,
  260. },
  261. method: 'POST',
  262. success(res) {
  263. that.setData({
  264. vcodeInfo:false,
  265. vcodeTimeOut:res.data.result.expire,
  266. vcodeTimeOutDefault:res.data.result.expire,
  267. vcodeLen:res.data.result.len
  268. })
  269. }
  270. });
  271. },
  272. /**
  273. * 生命周期函数--监听页面初次渲染完成
  274. */
  275. onReady() {
  276. },
  277. /**
  278. * 生命周期函数--监听页面显示
  279. */
  280. onShow() {
  281. },
  282. /**
  283. * 生命周期函数--监听页面隐藏
  284. */
  285. onHide() {
  286. },
  287. /**
  288. * 生命周期函数--监听页面卸载
  289. */
  290. onUnload() {
  291. },
  292. /**
  293. * 页面相关事件处理函数--监听用户下拉动作
  294. */
  295. onPullDownRefresh() {
  296. },
  297. /**
  298. * 页面上拉触底事件的处理函数
  299. */
  300. onReachBottom() {
  301. },
  302. /**
  303. * 用户点击右上角分享
  304. */
  305. onShareAppMessage() {
  306. }
  307. })