phone_login.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // pages/login/phone_login/phone_login.js
  2. let log = require('../../../utils/log.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. phone: null,
  9. vcode: null,
  10. checkCode: false,
  11. checkPhone: false,
  12. vcodeFlag: true,
  13. errorMsg: '',
  14. varificatCodeFocus:false,
  15. vcode_button_text: '获取验证码',
  16. scene: "LOGIN",
  17. vcodeTimeOut: 0,
  18. vcodeTimeOutDefault: 0,
  19. vcodeLen: 2,
  20. servicetel: getApp().globalData.helpPhoneNum
  21. },
  22. onError(e) {
  23. wx.showModal({
  24. title: '温馨提示',
  25. content: '当前网络环境较差,无法连接服务器,请稍后重试。有问题请联系客服电话4009608068,接听时段08:30-17:00。',
  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/sendPhoneVcodeNoUnique',
  117. data: {
  118. scene: that.data.scene,
  119. terminal: phone,
  120. },
  121. method: 'POST',
  122. success(res) {
  123. console.info(res.data.code);
  124. if (res.data.code == 1) {
  125. wx.showModal({
  126. title: '提示',
  127. content:res.msg,
  128. showCancel:false,
  129. confirmColor:'#4359b5'
  130. });
  131. }else if (res.data.code == 2) {
  132. // 跳转
  133. wx.showModal({
  134. title: '提示',
  135. content: '手机号未绑定账户。若您已注册,请通过账号密码方式登录,登陆后根据系统提示绑定手机号或联系客服处理',
  136. confirmText: '账号登录',
  137. showCancel:true,
  138. confirmColor:'#36a9a6',
  139. success: function (res1) {
  140. if (res1.confirm) {
  141. let url = '/pages/login/username_login/username_login';
  142. wx.redirectTo({
  143. url
  144. })
  145. }
  146. }
  147. });
  148. }else if (res.data.code == 200) {
  149. wx.showToast({
  150. title: '验证码已发送',
  151. icon: 'success',
  152. duration: 1000 //持续的时间
  153. })
  154. let vcodeInterval = setInterval(function () {
  155. if (that.data.vcodeTimeOut <= 0) {
  156. that.setData({
  157. vcodeFlag: true,
  158. vcode_button_text: '获取验证码'
  159. })
  160. clearInterval(vcodeInterval);
  161. }else{
  162. let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
  163. that.setData({
  164. vcodeTimeOut:that.data.vcodeTimeOut-1,
  165. vcodeFlag: false,
  166. vcode_button_text: str
  167. })
  168. }
  169. }, 1000);
  170. }
  171. }
  172. });
  173. },
  174. loginUser(e) {
  175. let that = this;
  176. let { phone, vcode } = this.data;
  177. log.info('[登录]', '[登录]', '[请求]', { phone, vcode });
  178. wx.request({
  179. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/phoneLogin',
  180. data: {
  181. phonenumber: phone,
  182. vcode
  183. },
  184. method: 'POST',
  185. success(res) {
  186. console.log(res);
  187. log.info('[登录]', '[登录]', '[响应]', res.data);
  188. if (res.data.code == 1) {
  189. that.setData({
  190. vcode: null,
  191. passwordFocus:true//,
  192. //errorMsg: res.data.msg
  193. });
  194. wx.showModal({
  195. title: '提示',
  196. content:res.data.msg,
  197. showCancel:false,
  198. confirmColor:'#4359b5'
  199. });
  200. log.info('[登录]', '[登录]', '[失败code==1]', res.data);
  201. } else {
  202. //返回该用户
  203. //跳转到上一页
  204. wx.setStorageSync("userInfo", res.data);
  205. wx.setStorageSync("isBindPhone", res.data.bindingPhone);
  206. wx.setStorageSync("isLogin", true);
  207. //let url = `/pages/ucenter/index/index`;
  208. log.info('[登录]', '[登录]', '[成功返回上一个界面]', res.data);
  209. wx.navigateBack();
  210. //wx.redirectTo({
  211. // url
  212. //});
  213. }
  214. },
  215. fail(e){
  216. getApp().showNetworkError();
  217. }
  218. });
  219. },
  220. phoneCall(){
  221. wx.makePhoneCall({
  222. phoneNumber: this.data.servicetel
  223. })
  224. },
  225. goToAccountLogin(e){
  226. let url = '/pages/login/username_login/username_login';
  227. wx.redirectTo({
  228. url
  229. })
  230. },
  231. goToRegister(e) {
  232. let url = '/pages/register/register';
  233. wx.redirectTo({
  234. url
  235. })
  236. },
  237. goToForget(e){
  238. let url = '/pages/forget/forget';
  239. wx.redirectTo({
  240. url
  241. })
  242. },
  243. /**
  244. * 生命周期函数--监听页面加载
  245. */
  246. onLoad: function (options) {
  247. let scene = this.data.scene;
  248. let that = this
  249. wx.request({
  250. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  251. data: {
  252. scene,
  253. },
  254. method: 'POST',
  255. success(res) {
  256. that.setData({
  257. vcodeInfo:false,
  258. vcodeTimeOut:res.data.result.expire,
  259. vcodeTimeOutDefault:res.data.result.expire,
  260. vcodeLen:res.data.result.len
  261. })
  262. }
  263. });
  264. },
  265. /**
  266. * 生命周期函数--监听页面初次渲染完成
  267. */
  268. onReady: function () {
  269. },
  270. /**
  271. * 生命周期函数--监听页面显示
  272. */
  273. onShow: function () {
  274. },
  275. /**
  276. * 生命周期函数--监听页面隐藏
  277. */
  278. onHide: function () {
  279. },
  280. /**
  281. * 生命周期函数--监听页面卸载
  282. */
  283. onUnload: function () {
  284. },
  285. /**
  286. * 页面相关事件处理函数--监听用户下拉动作
  287. */
  288. onPullDownRefresh: function () {
  289. },
  290. /**
  291. * 页面上拉触底事件的处理函数
  292. */
  293. onReachBottom: function () {
  294. },
  295. /**
  296. * 用户点击右上角分享
  297. */
  298. onShareAppMessage: function () {
  299. }
  300. })