10.23修改

This commit is contained in:
2023-10-23 23:57:15 +08:00
parent 23f37bc1a5
commit f55439859b
6 changed files with 163 additions and 93 deletions

View File

@@ -8,68 +8,22 @@
import api from "@/api/index"
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)
}
getInfo()
})
// 变量
const avatar = ref('')
const res_form = ref(null)
const is_login = ref(null)
const userinfo = ref(null)
// const avatarUrl = ref(null)
const nickname = ref('')
// 函数
function getPhoneNumber (e) {
// tel_code.value = e.detail.code
// console.log(e)
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)
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)
}
}
async function wx_login(userId){
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);
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 getInfo(){
is_login.value = custom.checkLogin()
// console.log(is_login.value)
if(is_login.value) {
avatar.value = uni.getStorageSync('avatarUrl')
userinfo.value = JSON.parse(uni.getStorageSync('userInfo'))
}
}
function toLogin() {
@@ -105,6 +59,60 @@
url:"/pages/user/userInfo"
})
}
function getPhoneNumber (e) {
// tel_code.value = e.detail.code
// console.log(e)
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);return;
const res = await api.getToken(data)
if(res.code === 200) {
if(res.data.newUser) { // 新用户,跳转登录页
toUserInfo()
}
else{
uni.setStorageSync('avatarUrl', res.data.avatar);//avatarUrl
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)
}
}
}
async function wx_login(userId){
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);
if(res2.code === 0) {
uni.setStorageSync('userInfo',JSON.stringify(res_form.value))
// is_login.value = custom.checkLogin()
getInfo()
}
else{
uni.clearStorage();
}
}
</script>
<template>
<view class="user-box page-box">
@@ -115,8 +123,12 @@
</view>
<uni-icons style="position: absolute;right:20rpx;top: 40%;" v-if="is_login" type="forward" color="white" size="20"></uni-icons>
</view>
<view class="btn-box" @tap="toLogin" v-if="!is_login">
<view class="btn">登录</view>
<!-- <view class="btn-box" @tap="toLogin" v-if="!is_login"> -->
<view class="btn-box" v-if="!is_login">
<!-- <view class="btn">登录</view> -->
<view class="btn-part">
<button class="buy-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">立即登录</button>
</view>
</view>
<view class="row-box" v-else>
<view class="rb-card" @tap="toDetail(1)" style="margin-bottom: 10px;">
@@ -166,6 +178,17 @@
</view>
</template>
<style scoped lang="scss">
.buy-btn{
display: inline-block;
margin-top:1rem;
background-color: #26758d;
color: white;
border-radius: 50rpx;
font-weight: 200;
font-size: 24rpx;
width: 50%;
padding: 10rpx 0;
}
.rbc-content-left-box{
uni-icons{
margin-right: 20rpx;