phone_login.js 9.1 KB

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