unbindphone.js 8.5 KB

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