123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- // 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,
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 页面方法集合
- */
- //表单栏位值发生变化时
- 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
- }
- wx.showLoading({
- title: '加载中',
- })
- api.loginByPhone(data).then(res=>{
- console.log(res)
- if(res.code == 200){
- this.isLogin(res.data)
- }
- }).catch(error=>{
- })
- }
- })
- },
- //登录后操作
- isLogin({token,id,isFirst,phone}){
- wx.setStorageSync('token',token)
- this.getPersonRights()
- wx.navigateBack()
-
- },
- getPersonRights(){
- api.getPersonRights({},false).then(res=>{
- if(res.code == 200){
- var data = JSON.stringify(res.data.data)
- wx.setStorageSync('vip', data)
- }
- })
- },
- //跳转到注册界面
- 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
- }
- wx.login({
- success: (res) => {
- if(res.errMsg == 'login:ok'){
- data.code = res.code
- wx.showLoading({
- title: '加载中',
- })
- api.loginByWeChat(data).then(res=>{
- if(res.code == 200){
- this.isLogin(res.data)
- }
- }).catch(error=>{
-
- })
- }
-
- },
- })
-
-
- } else if (e.detail.errMsg === 'getPhoneNumber:fail') {
- // 用户拒绝授权
- console.log('获取手机号失败');
- }
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|