JM-MiniApp/pages/user/index.vue

121 lines
3.3 KiB
Vue
Raw Normal View History

2023-10-11 22:20:34 +08:00
<script setup>
// 引入依赖
2023-10-18 21:00:42 +08:00
import { onLoad,onShow } from "@dcloudio/uni-app"
2023-10-11 22:20:34 +08:00
import mySwiper from "@/components/mySwiper.vue"
import {ref,onMounted,nextTick} from "vue"
import {useStore} from '@/store/index.js'
const store = useStore()
import api from "@/api/index"
2023-10-18 21:00:42 +08:00
import custom from "@/utils/index.js"
onShow(() => {
is_login.value = custom.checkLogin()
// console.log(is_login.value)
if(is_login.value) {
userinfo.value = uni.getStorageSync('userInfo')
avatar.value = uni.getStorageSync('avatarUrl')
userinfo.value = JSON.parse(userinfo.value)
// console.log(userinfo.value)
}
})
// 变量
const avatar = ref('')
const res_form = ref(null)
const is_login = ref(null)
const userinfo = ref(null)
2023-10-11 22:20:34 +08:00
// 函数
function getPhoneNumber (e) {
// tel_code.value = e.detail.code
2023-10-18 21:00:42 +08:00
// console.log(e)
2023-10-11 22:20:34 +08:00
getAccess_token({code:e.detail.code})
// console.log(e.detail.code) // 动态令牌
// console.log(e.detail.errMsg) // 回调信息(成功失败都会返回)
// console.log(e.detail.errno) // 错误码(失败时返回)
}
async function getAccess_token(params={}){//获取token 获取手机号
//清除token缓存
// wx.clearStorageSync();
//访问接口
const data={
username:params.code,//手机号按钮获取的code
type:'wechat_mini_app',
identity:'user',
password:'111'
}
// console.log('登录参数:',data)
const res = await api.getToken(data)
2023-10-18 21:00:42 +08:00
if(res.code === 200) {
res_form.value = res.data
res_form.value.access_token = 'Bearer '+res.data.access_token
let userId = res.data.userId
uni.setStorageSync('access_token',res_form.value.access_token);// token单独缓存一次
wx_login(userId)
}
2023-10-11 22:20:34 +08:00
}
2023-10-18 21:00:42 +08:00
async function wx_login(userId){
2023-10-11 22:20:34 +08:00
let res=await new Promise(resolve=>{
uni.login({
provider: 'weixin', //使用微信登录
success: function (loginRes) {
resolve(loginRes);
console.log(loginRes,'微信登录返回信息');
}
});
});
let res2=await api.wx_login({code:res.code},userId);
2023-10-18 21:00:42 +08:00
if(res2.code === 0) {
// for(let prop in res_form.value) {
// uni.setStorageSync(prop,res_form.value[prop])
// }
uni.setStorageSync(userInfo,JSON.stringify(res_form.value))
}
else{
uni.clearStorage();
}
}
function toLogin() {
uni.navigateTo({
url:"/pages/login/login"
})
}
2023-10-08 21:21:21 +08:00
</script>
2023-10-11 22:20:34 +08:00
<template>
<view class="user-box page-box">
2023-10-18 21:00:42 +08:00
<view class="userinfo-box">
<image :src="is_login?avatar:'../../static/avatar.png'" mode=""></image>
<view class="">
{{is_login?userinfo.nickname:'未登录'}}
</view>
</view>
<view class="btn-box" @tap="toLogin" v-if="!is_login">
<view class="btn">登录</view>
</view>
2023-10-11 22:20:34 +08:00
</view>
</template>
2023-10-18 21:00:42 +08:00
<style scoped lang="scss">
.btn-box{
text-align: center;
margin-top: 40rpx;
.btn{
display: inline-block;
padding: 10px 20px;
background: #e2e2e2;
color: #26758d;
border-radius: 10px;
width: 50%;
}
}
.userinfo-box{
image{
width: 120rpx;
height: 120rpx;
border-radius: 50%;
background: white;
margin-right: 20rpx;
}
color: white;
background-color: #26758d;
display: flex;
align-items: center;
padding: 40rpx 20rpx;
}
2023-10-08 21:21:21 +08:00
</style>