phone_login.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. confirmColor:'#00AADD',
  28. });
  29. },
  30. inputPhone(e) {
  31. this.setData({
  32. phone: e.detail.value
  33. });
  34. this.checkAll();
  35. },
  36. inputVerificatrCode(e) {
  37. this.setData({
  38. vcode: e.detail.value
  39. });
  40. this.checkAll();
  41. },
  42. checkAll() {
  43. if (!this.checkPhoneNumber()) {
  44. this.setData({
  45. errorMsg: '手机号输入有误,请重新输入',
  46. checkPhone: false,
  47. checkCode: false
  48. });
  49. return;
  50. } else {
  51. this.setData({
  52. checkPhone: true,
  53. errorMsg: ''
  54. });
  55. }
  56. if (!this.checkVcode()) {
  57. this.setData({
  58. errorMsg: '验证码格式不正确'
  59. });
  60. this.setData({
  61. checkCode: false
  62. });
  63. return;
  64. } else {
  65. this.setData({
  66. errorMsg: ''
  67. });
  68. }
  69. if (this.data.phone && this.data.vcode) {
  70. this.setData({
  71. checkCode: true
  72. });
  73. } else {
  74. this.setData({
  75. checkCode: false
  76. });
  77. }
  78. },
  79. checkPhoneNumber() {
  80. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  81. return false;
  82. }
  83. return true;
  84. },
  85. checkVcode() {
  86. if (this.data.vcode==undefined || this.data.vcode=="") {
  87. return false;
  88. }
  89. if(this.data.vcode.length==this.data.vcodeLen){
  90. return true;
  91. }
  92. return false;
  93. },
  94. getVerificateCode(){
  95. let vcodeFlag = this.data.vcodeFlag;
  96. let checkPhone = this.data.checkPhone;
  97. let phone = this.data.phone;
  98. let that = this;
  99. if (!vcodeFlag) {
  100. return;
  101. }
  102. if (!checkPhone) {
  103. that.setData({
  104. errorMsg: '手机号输入有误,请重新输入',
  105. checkCode: false
  106. });
  107. return;
  108. } else {
  109. that.setData({
  110. errorMsg: ''
  111. });
  112. }
  113. this.setData({
  114. vcodeTimeOut:this.data.vcodeTimeOutDefault
  115. })
  116. wx.request({
  117. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcodeNoUnique',
  118. data: {
  119. scene: that.data.scene,
  120. terminal: phone,
  121. },
  122. method: 'POST',
  123. success(res) {
  124. console.info(res.data.code);
  125. if (res.data.code == 1) {
  126. wx.showModal({
  127. title: '提示',
  128. content:res.msg,
  129. showCancel:false,
  130. confirmColor:'#00AADD',
  131. });
  132. }else if (res.data.code == 2) {
  133. // 跳转
  134. wx.showModal({
  135. title: '提示',
  136. content: '手机号未绑定账户。若您已注册,请通过账号密码方式登录,登陆后根据系统提示绑定手机号或联系客服处理',
  137. confirmText: '密码登录',
  138. cancelText:"确定",
  139. showCancel:true,
  140. confirmColor:'#00AADD',
  141. success: function (res1) {
  142. if (res1.confirm) {
  143. let url = '/pages/login/username_login/username_login';
  144. wx.redirectTo({
  145. url
  146. })
  147. }
  148. }
  149. });
  150. }else if (res.data.code == 200) {
  151. wx.showToast({
  152. title: '验证码已发送',
  153. icon: 'success',
  154. duration: 1000 //持续的时间
  155. })
  156. let vcodeInterval = setInterval(function () {
  157. if (that.data.vcodeTimeOut <= 0) {
  158. that.setData({
  159. vcodeFlag: true,
  160. vcode_button_text: '获取验证码'
  161. })
  162. clearInterval(vcodeInterval);
  163. }else{
  164. let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
  165. that.setData({
  166. vcodeTimeOut:that.data.vcodeTimeOut-1,
  167. vcodeFlag: false,
  168. vcode_button_text: str
  169. })
  170. }
  171. }, 1000);
  172. }
  173. }
  174. });
  175. },
  176. loginUser(e) {
  177. let that = this;
  178. let { phone, vcode } = this.data;
  179. log.info('[登录]', '[登录]', '[请求]', { phone, vcode });
  180. wx.request({
  181. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/phoneLogin',
  182. data: {
  183. phonenumber: phone,
  184. vcode
  185. },
  186. method: 'POST',
  187. success(res) {
  188. console.log(res);
  189. log.info('[登录]', '[登录]', '[响应]', res.data);
  190. if (res.data.code == 1) {
  191. that.setData({
  192. vcode: null,
  193. passwordFocus:true//,
  194. //errorMsg: res.data.msg
  195. });
  196. wx.showModal({
  197. title: '提示',
  198. content:res.data.msg,
  199. showCancel:false,
  200. confirmColor:'#00AADD',
  201. });
  202. log.info('[登录]', '[登录]', '[失败code==1]', res.data);
  203. } else {
  204. //返回该用户
  205. //跳转到上一页
  206. wx.setStorageSync("userInfo", res.data);
  207. wx.setStorageSync("isLogin", true);
  208. //let url = `/pages/ucenter/index/index`;
  209. log.info('[登录]', '[登录]', '[成功返回上一个界面]', res.data);
  210. wx.navigateBack();
  211. //wx.redirectTo({
  212. // url
  213. //});
  214. }
  215. },
  216. fail(e){
  217. getApp().showNetworkError();
  218. }
  219. });
  220. },
  221. phoneCall(){
  222. wx.makePhoneCall({
  223. phoneNumber: this.data.servicetel
  224. })
  225. },
  226. goToAccountLogin(e){
  227. let url = '/pages/login/username_login/username_login';
  228. wx.redirectTo({
  229. url
  230. })
  231. },
  232. goToRegister(e) {
  233. let url = '/pages/register/register';
  234. wx.redirectTo({
  235. url
  236. })
  237. },
  238. goToForget(e){
  239. let url = '/pages/forget/forget';
  240. wx.redirectTo({
  241. url
  242. })
  243. },
  244. /**
  245. * 生命周期函数--监听页面加载
  246. */
  247. onLoad: function (options) {
  248. let scene = this.data.scene;
  249. let that = this
  250. wx.request({
  251. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  252. data: {
  253. scene,
  254. },
  255. method: 'POST',
  256. success(res) {
  257. that.setData({
  258. vcodeInfo:false,
  259. vcodeTimeOut:res.data.result.expire,
  260. vcodeTimeOutDefault:res.data.result.expire,
  261. vcodeLen:res.data.result.len
  262. })
  263. }
  264. });
  265. },
  266. /**
  267. * 生命周期函数--监听页面初次渲染完成
  268. */
  269. onReady: function () {
  270. },
  271. /**
  272. * 生命周期函数--监听页面显示
  273. */
  274. onShow: function () {
  275. },
  276. /**
  277. * 生命周期函数--监听页面隐藏
  278. */
  279. onHide: function () {
  280. },
  281. /**
  282. * 生命周期函数--监听页面卸载
  283. */
  284. onUnload: function () {
  285. },
  286. /**
  287. * 页面相关事件处理函数--监听用户下拉动作
  288. */
  289. onPullDownRefresh: function () {
  290. },
  291. /**
  292. * 页面上拉触底事件的处理函数
  293. */
  294. onReachBottom: function () {
  295. },
  296. /**
  297. * 用户点击右上角分享
  298. */
  299. onShareAppMessage: function () {
  300. }
  301. })