赞
踩
注意目前微信小程序防止强制性登陆,应当在用户查看小程序功能后才能让用户登陆,否则微信审核会不通过
前端界面如下:
wxml代码
<!--pages/login/login.wxml-->
<view class="login_container">
<view class="content" wx:if="{{ !agreementShow && !AntiFraudShow && !illegalShow}}">
<image src="{{logoUrl}}"></image>
<button class="login" bindtap="login">登录/注册</button>
<button open-type="getPhoneNumber" class="get_phone" bindgetphonenumber="handlePhoneLogin">微信手机号快捷登录</button>
</view>
</view>
js代码 // pages/login/login.js import {get_phone,login} from '../../assets/api/login' import APIConfig from "../../config/api"; Page({ /** * 页面的初始数据 */ data: { code:'', phone:'', value:'', checked:false, team_code:'',// 团队cede company_code:'',// 承保公司code login_mode:'wx-phone',// 登陆方式--本页面为微信登陆 logoUrl:`${APIConfig.baseUrl1}/logo/bjtp.png` }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 判断是否存在token--存在则直接跳转到到业务模块页面 console.log(wx.getStorageSync("token"),'得到的token值'); if (wx.getStorageSync("token") && wx.getStorageSync("token") != '') return wx.redirectTo({ url: "/pages/questionList/list", }); }, handlePhoneLogin:function(e){ let that = this; let token = wx.getStorageSync('token'); if (token) { wx.redirectTo({ url: "/pages/questionList/list" }) } else { that.data.code = e.detail.code console.log(e,'获取手机号'); let param = { code:that.data.code } get_phone(param).then(res=>{ let phone = res.phone_number this.setData({ phone }) // const form = new FormData(); // form.append('username',phone) // form.append('password',phone) let obj = { username:that.data.phone, password:that.data.phone } login(obj).then(res=>{ let token = res.token_type + ' ' + res.access_token console.log(token,'存下来的token值'); wx.setStorageSync('token',token); wx.redirectTo({ url: `/pages/questionList/list` }) }) }) } }, //去往注册页面 login(){ wx.redirectTo({ url: '/pages/register/register', }) }, })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。