|
@@ -1,11 +1,23 @@
|
|
|
// pages/login/login.js
|
|
|
+import { $wuxCountDown, $wuxForm,$wuxToptips} from '../../miniprogram_npm/wux-weapp/index'
|
|
|
+const api = require('../../api/index')
|
|
|
Page({
|
|
|
|
|
|
/**
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
-
|
|
|
+ layout: 'horizontal',
|
|
|
+ validateMessages: {
|
|
|
+ required: '%s 字段为必填',
|
|
|
+ },
|
|
|
+ loginMessage:{
|
|
|
+ phone:'',
|
|
|
+ code:'',
|
|
|
+ checkCode:''
|
|
|
+ },
|
|
|
+ showDialog:false,
|
|
|
+ imgURL:null,
|
|
|
},
|
|
|
|
|
|
/**
|
|
@@ -29,6 +41,196 @@ Page({
|
|
|
|
|
|
},
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 页面方法集合
|
|
|
+ */
|
|
|
+ //表单栏位值发生变化时
|
|
|
+ onValuesChange(e) {
|
|
|
+ console.log(e)
|
|
|
+ const { changedValues, allValues } = e.detail
|
|
|
+ // this.setValues(changedValues)
|
|
|
+ Object.keys(changedValues).forEach((field) => {
|
|
|
+ this.setData({
|
|
|
+ ['loginMessage.'+field]: changedValues[field],
|
|
|
+ })
|
|
|
+ })
|
|
|
+ console.log('onValuesChange \n', changedValues, allValues)
|
|
|
+ },
|
|
|
+ //获取手机号并打开弹窗
|
|
|
+ onSendCode(){
|
|
|
+ var value = this.data.loginMessage.phone
|
|
|
+ if(!value){
|
|
|
+ $wuxToptips().error({
|
|
|
+ hidden: false,
|
|
|
+ text: '请输入手机号',
|
|
|
+ duration: 3000,
|
|
|
+ success() {},
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const isTel = (value) => /^1[34578]\d{9}$/.test(value)
|
|
|
+ if(!isTel(value)){
|
|
|
+ $wuxToptips().error({
|
|
|
+ hidden: false,
|
|
|
+ text: '手机号格式不正确',
|
|
|
+ duration: 3000,
|
|
|
+ success() {},
|
|
|
+ })
|
|
|
+ return
|
|
|
+ }
|
|
|
+ this.changeVerifyCode()
|
|
|
+ },
|
|
|
+ //获取输入的校验码
|
|
|
+ changeInput(e) {
|
|
|
+ this.setData({
|
|
|
+ ['loginMessage.checkCode']: e.detail.value,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //关闭弹窗
|
|
|
+ closeDialog(){
|
|
|
+ this.setData(
|
|
|
+ {
|
|
|
+ showDialog:false
|
|
|
+ }
|
|
|
+ )
|
|
|
+ },
|
|
|
+ //获取验证码
|
|
|
+ getCheckCode(){
|
|
|
+ if (this.c2 && this.c2.interval) return !1
|
|
|
+ var data = {
|
|
|
+ phoneNum:this.data.loginMessage.phone,
|
|
|
+ checkCode:this.data.loginMessage.checkCode
|
|
|
+ }
|
|
|
+ api.sendCode(data).then(res=>{
|
|
|
+ if(res.code == 200){
|
|
|
+ this.CountDown()
|
|
|
+ this.closeDialog()
|
|
|
+ }
|
|
|
+ }).catch(error=>{
|
|
|
+ console.log(error)
|
|
|
+ // $wuxToptips().error({
|
|
|
+ // hidden: false,
|
|
|
+ // text: '获取验证码失败',
|
|
|
+ // duration: 3000,
|
|
|
+ // success() {},
|
|
|
+ // })
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //获取校验码
|
|
|
+ changeVerifyCode(){
|
|
|
+ var data = {
|
|
|
+ phoneNum:this.data.loginMessage.phone
|
|
|
+ }
|
|
|
+ api.verifyCode(data).then(res=>{
|
|
|
+ if(res.code == 200){
|
|
|
+ console.log(res)
|
|
|
+ this.setData(
|
|
|
+ {
|
|
|
+ imgURL:res.data.captcha,
|
|
|
+ showDialog:true
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }).catch(error=>{
|
|
|
+
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //倒计时
|
|
|
+ CountDown(phoneCode='1234'){
|
|
|
+ this.c2 = new $wuxCountDown({
|
|
|
+ date: +new Date() + 60000,
|
|
|
+ onEnd() {
|
|
|
+ this.setData({
|
|
|
+ c2: '重新获取验证码',
|
|
|
+ })
|
|
|
+ },
|
|
|
+ render(date) {
|
|
|
+ const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
|
|
|
+ date.sec !== 0 &&
|
|
|
+ this.setData({
|
|
|
+ c2: sec,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //手机号校验
|
|
|
+ checkPhone(rule, value){
|
|
|
+ if(!value){
|
|
|
+ return Promise.reject(new Error('手机号不能为空!'))
|
|
|
+ }
|
|
|
+ const isTel = (value) => /^1[34578]\d{9}$/.test(value)
|
|
|
+ if(!isTel(value)){
|
|
|
+ return Promise.reject(new Error('手机号输入错误!'))
|
|
|
+ }
|
|
|
+ return Promise.resolve()
|
|
|
+ },
|
|
|
+ //验证码校验
|
|
|
+ checkCode(rule, value){
|
|
|
+ if(!value){
|
|
|
+ return Promise.reject(new Error('验证码不能为空!'))
|
|
|
+ }
|
|
|
+ return Promise.resolve()
|
|
|
+ },
|
|
|
+ //登录
|
|
|
+ onSubmit(){
|
|
|
+ console.log(this.data.loginMessage)
|
|
|
+ const { validateFields } = $wuxForm()
|
|
|
+
|
|
|
+ validateFields((err, values) => {
|
|
|
+ if (!err) {
|
|
|
+ var data = {
|
|
|
+ phoneNum:this.data.loginMessage.phone,
|
|
|
+ phoneCode:this.data.loginMessage.code
|
|
|
+ }
|
|
|
+ api.loginByPhone(data).then(res=>{
|
|
|
+ console.log(res)
|
|
|
+ if(res.code == 200){
|
|
|
+ this.isLogin(res.data)
|
|
|
+ }
|
|
|
+ }).catch(error=>{
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //登录后操作
|
|
|
+ isLogin({token,id,isFirst,phone}){
|
|
|
+ wx.navigateBack()
|
|
|
+ wx.setStorageSync('token',token)
|
|
|
+ },
|
|
|
+ //跳转到注册界面
|
|
|
+ register(){
|
|
|
+ wx.navigateTo({
|
|
|
+ url: '/pages/register/register',
|
|
|
+ })
|
|
|
+ },
|
|
|
+ //微信一键登录
|
|
|
+ fastLogin(e){
|
|
|
+ if (e.detail.errMsg === 'getPhoneNumber:ok') {
|
|
|
+ // 用户同意授权,获取加密信息
|
|
|
+ const encryptedData = e.detail.encryptedData;
|
|
|
+ const iv = e.detail.iv;
|
|
|
+ const code = e.detail.code
|
|
|
+ // 将 encryptedData 和 iv 发送到后端服务器
|
|
|
+ var data= {
|
|
|
+ encryptedData: encryptedData,
|
|
|
+ iv: iv,
|
|
|
+ code:code
|
|
|
+ }
|
|
|
+ api.loginByWeChat(data).then(res=>{
|
|
|
+ if(res.code == 200){
|
|
|
+ this.isLogin(res.data)
|
|
|
+ }
|
|
|
+ }).catch(error=>{
|
|
|
+
|
|
|
+ })
|
|
|
+
|
|
|
+ } else if (e.detail.errMsg === 'getPhoneNumber:fail') {
|
|
|
+ // 用户拒绝授权
|
|
|
+ console.log('获取手机号失败');
|
|
|
+ }
|
|
|
+ },
|
|
|
/**
|
|
|
* 生命周期函数--监听页面隐藏
|
|
|
*/
|