微信小程序(黑马优购:登录)

1.点击结算进行条件判断

user.js

//数据

state: () =>({

// address: {}

address: JSON.parse(uni.getStorageSync('address') || '{}'),

token: ''

}),
my-settle.vue

computed: {

...mapGetters('m_cart','checkedCount','total','checkedGoodsAmount'),

...mapGetters('m_user','addstr'),

...mapState('m_user','token')
//用户点击了结算按钮

settlement(){

if(!this.checkedCount) return uni.$showMsg('请选择要结算的商品!')

if(!this.addstr) return uni.$showMsg('请选择收货地址')

if(!this.token) return uni.$showMsg('请先登录!')

}

2.创建登录(my-login)和用户信息组件(my-userinfo)

my-login.vue

//绘制底部半圆的造型

&::after{

content: ' ';

display: block;

width: 100%;

height: 40px;

background-color: white;

position: absolute;

bottom: 0;

left: 0;

border-radius: 100%;

//往下移50%

transform: translateY(50%);

}

3.登录授权

如果没有显示下面的弹框,基础库设置为最低版本即可

methods:{

//用户授权之后,获取用户的基本信息

getUserinfo(e){

console.log(e);

if(e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权!')

}

}

3.将用户的基本信息存储到Vuex

//数据

state: () =>({

// address: {}

address: JSON.parse(uni.getStorageSync('address') || '{}'),

token: '',

//用户的信息对象

userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}')

}),
saveUserInfoToStorage(state){

uni.setStorageSync('userinfo',JSON.stringify(state.userinfo))

}

my-login.vue

<script>

import {mapMutations} from 'vuex'

export default {

data() {

return {

};

},

methods:{

...mapMutations('m_user','updateUserInfo'),

//用户授权之后,获取用户的基本信息

getUserinfo(e){

if(e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权!')

console.log(e.detail.userInfo);

this.updateUserInfo(e.detail.userInfo)

}

}

}

</script>

4.调用uni.login

1)拿到code值

//用户授权之后,获取用户的基本信息

getUserinfo(e){

if(e.detail.errMsg === 'getUserInfo:fail auth deny') return uni.$showMsg('您取消了登录授权!')

console.log(e.detail.userInfo);

this.updateUserInfo(e.detail.userInfo)

this.getToken(e.detail)

},

async getToken(info){

//获取code对应的值

const err,res = await uni.login().catch(err => err)

console.log(res);

if( err || res.errMsg !== 'login:ok'){

return uni.$showMsg('登录失败!')

}

console.log(res.code);

console.log(info);

}

user.js

token: uni.getStorageInfoSync('token') || '',

updateToken(state,token){

state.token = token

this.commit('m_user/saveTokenToStorage')

},

saveTokenToStorage(state){

uni.setStorage('token',state.token)

}
...mapMutations('m_user','updateUserInfo','updateToken'),

2)持久化存储token

await uni.$http.post('/api/public/v1/users/wxlogin',query)这里的接口不能获取到token值,

直接把token写死

my-login.vue

data() {

return {

token : 'abc147258369jkl'

};

},
async getToken(info){

//获取code对应的值

const err,res = await uni.login().catch(err => err)

console.log(info);

if( err || res.errMsg !== 'login:ok'){

return uni.$showMsg('登录失败!')

}

console.log(res);

//准备参数

const query = {

code: res.code,

encryptedData: info.encryptedData,

iv: info.iv,

rawData: info.rawData,

signature: info.signature

}

const { data: loginResult } = await uni.$http.post('/api/public/v1/users/wxlogin',query)

console.log(loginResult);

if(loginResult.meta.status !== 200 ) {
uni.$showMsg('登录成功')
uni.setStorageSync('token',this.token);
this.updateToken(this.token)
}

4.获取用户信息

渲染头像和名称

<view class="top-box">

<image :src="userinfo.avatarUrl" class="avatar"></image>

<view class="nickname">{{userinfo.nickname}}</view>

</view>
import { mapState } from 'vuex'

computed:{

...mapState('m_user','userinfo')

}

5.退出登录

methods:{

...mapMutations('m_user','updateAddress','updateUserInfo','updateToken'),

async logout(){

const err,succ = await uni.showModal({

title: '提示',

content: '确认退出登录吗?'

}).catch(err => err)

if(succ && succ.confirm){

this.updateAddress({})

this.updateUserInfo({})

this.updateToken('')

}

}

6.如果用户没有登录,则3秒后自动跳转到登录页面

my-settle.vue

return {

//倒计时的秒数

seconds: 3,

//定时器的Id

timer: null

};
//延时导航到my页面

delayNavigate(){

this.seconds = 3

this.showTips(this.seconds)

this.timer = setInterval(()=>{

this.seconds--

if(this.seconds <= 0){

clearInterval(this.timer)

uni.switchTab({

url: '/pages/my/my'

})

return

}

this.showTips(this.seconds)

},1000)

},
//展示倒计时的提示消息

showTips(n){

uni.showToast({

icon: 'none',

title: '请登录后再结算! '+n+' 秒之后自动跳转到登录页',

mask: true,

duration: 1500

})

7.登录成功之后再返回之前的页面

user.js

//重定向的Object对象

redirectInfo: null
updateRedirectInfo(state,info){

state.redirectInfo = info

console.log(state.redirectInfo);

}

my-login.vue

computed:{

...mapState('m_user','redirectInfo')

},

.methods:{

...mapMutations('m_user','updateUserInfo','updateToken','updateRedirectInfo'),

if(loginResult.meta.status !== 200 ) {

uni.$showMsg('登录成功')

this.updateToken('abc147258369jkl')

this.navigateBack()

}
navigateBack(){

// this.redirectInfo.openType === 'switchTab':重定向的方式是switchTab

if(this.redirectInfo && this.redirectInfo.openType === 'switchTab'){

uni.switchTab({

url:this.redirectInfo.from,

complete: ()=>{

this.updateRedirectInfo(null)

}

})

}

}

my-settle.vue

//延时导航到my页面

delayNavigate(){

this.seconds = 3

this.showTips(this.seconds)

this.timer = setInterval(()=>{

this.seconds--

if(this.seconds <= 0){

clearInterval(this.timer)

uni.switchTab({
url: '/pages/my/my',
success: () => {
this.updateRedirectInfo({
openType: 'switchTab',
from: '/pages/cart/cart'
})

}

相关推荐
海鸥两三5 小时前
小程序文档预览 · 进阶面试题
小程序·uni-app·uniapp
m0_587383005 小时前
上海24小时自助健身房系统软件开发实战指南:从架构到部署
人工智能·小程序·数据挖掘·系统架构·需求分析
海鸥两三6 小时前
微信小程序 PDF 预览实践(uni.downloadFile → uni.openDocument)
小程序·uni-app
河北清兮网络科技18 小时前
开发软件怎么找靠谱的公司?普通人最全筛选避坑指南
小程序·app·短剧·短剧app·广告联盟
拖孩1 天前
这个小程序是 AI 帮我写的,可它里面一个 AI 功能都没有
前端·后端·微信小程序
奶油喜多多1 天前
深度测评2026培训机构消课系统,课时核算落地经验
小程序·需求分析
克里斯蒂亚诺更新1 天前
小程序自定义导航栏怎么写:从配置到实战
微信小程序
liyinchi19881 天前
微信小程序支付遇到“由于小程序违规,支付功能暂时无法使用” 解决办法
java·微信小程序·go
silianpan1 天前
Office 文档预览 UTS 插件
android·微信小程序·harmonyos
微笑的曙光1 天前
第三期 · 账号体系与双 Token 鉴权:让用户「无感登录,有感安全」
微信小程序