添加登录代码

This commit is contained in:
2023-05-15 14:30:16 +08:00
parent 8e3beb44d9
commit 9cb8aa0bac
16 changed files with 330 additions and 21 deletions

View File

@@ -1,5 +1,6 @@
// pages/home/home.ts
const api = require("../../api/index")
const app = getApp<IAppOption>()
Page({
/**
* 页面的初始数据
@@ -97,6 +98,8 @@ Page({
})
},
async getList() {//获取列表
app.globalData.isLogin = api.checkLogin(1)
// app.globalData.isLogin = login_state
const res = await api.getWineList({
current:1,
size:10

View File

@@ -0,0 +1,4 @@
{
"usingComponents": {},
"navigationBarTitleText": "登录"
}

View File

@@ -0,0 +1,42 @@
/* pages/login/login.wxss */
page{
height: 100%;
}
.container{
align-items: center;
}
.title{
font-weight: bolder;
font-size: 40rpx;
}
.more-info{
color: gray;
font-size: 24rpx;
margin-top: 1rem;
}
.buy-btn{
margin-top:1rem;
background-color: #000;
color: white;
border-radius: 50rpx;
font-weight: 200;
font-size: 24rpx;
}
.title-part{
text-align: center;
}
.avatar-wrapper{
width: 80rpx !important;
height: 80rpx;
padding: 0px;
margin: 0 auto;
overflow: hidden;
margin-bottom: 40rpx;
}
.avatar{
width: 80rpx;
height: 80rpx;
}
.weui-input{
}

View File

@@ -0,0 +1,154 @@
// pages/login/login.ts
const defaultAvatarUrl = '../../assets/avatar.png'
const api = require("../../api/index")
const app = getApp<IAppOption>()
Page({
/**
* 页面的初始数据
*/
data: {
avatarUrl:defaultAvatarUrl ,
nickName:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad() {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
},
onChooseAvatar(e:any) {
console.log(e);
const { avatarUrl } = e.detail
this.setData({
avatarUrl,
})
},
async toWXLogin() {
console.log(app.globalData.code)
wx.showLoading({
title: '加载中',
})
if(!this.data.nickName) {
wx.showToast({
title:'请填写昵称',
icon: 'error',
duration: 2000
})
return
}
const token_res = await api.getAccess_token({code:app.globalData.code,nickName:this.data.nickName,avatarUrl:this.data.avatarUrl});
if(token_res.code == 200){
// 设置缓存
wx.setStorageSync('token', token_res.data.access_token);//token
wx.setStorageSync('userId', token_res.data.userId);//userId
wx.setStorageSync('nickName', this.data.nickName);//nickName
wx.setStorageSync('avatarUrl', this.data.avatarUrl);//avatarUrl
//绑定微信
const login_res =await api.bindingWX({code:app.globalData.code});
if(login_res.code == 200){
wx.hideLoading()
wx.navigateBack({
delta: -1
})
}
}
else{
wx.hideLoading()
wx.showToast({
title:token_res.message,
icon: 'error',
duration: 2000
})
}
},
async getPhoneNumber (e:any) {//获取手机号
// var _this=this;
console.log(e)
wx.showLoading({
title: '加载中',
})
if(!this.data.nickName) {
wx.showToast({
title:'请填写昵称',
icon: 'error',
duration: 2000
})
return
}
const token_res = await api.getAccess_token({code:e.detail.code,nickName:this.data.nickName,avatarUrl:this.data.avatarUrl});
if(token_res.code == 200){
// 设置缓存
wx.setStorageSync('token', token_res.data.access_token);//token
wx.setStorageSync('userId', token_res.data.userId);//userId
wx.setStorageSync('nickName', this.data.nickName);//nickName
wx.setStorageSync('avatarUrl', this.data.avatarUrl);//avatarUrl
//绑定微信
const login_res =await api.toLogin();
if(login_res.code == 200){
wx.hideLoading()
wx.navigateBack({
delta: -1
})
}
}
else{
wx.hideLoading()
wx.showToast({
title:token_res.message,
icon: 'error',
duration: 2000
})
}
},
})

View File

@@ -0,0 +1,24 @@
<!--pages/login/login.wxml-->
<view class="page-box" style="margin-top: 50%;">
<!-- <view class="img-part card-part" style="text-align:center">
<image mode="aspectFit" style="width: 250rpx;height: 250rpx;" src="../../assets/ico.jpg"></image>
</view>
<view class="title-part card-part">
<view class="title">东方蝴蝶</view>
<view class="more-info">使用当前微信登录</view>
</view> -->
<view class="img-part card-part" style="text-align:center">
<!-- <image mode="aspectFit" style="width: 250rpx;height: 250rpx;" src="../../assets/ico.jpg"></image> -->
<view class="tip">
点击下方头像位置获取微信头像
</view>
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar" style="border-radius: 50%;">
<image class="avatar" mode="aspectFill" src="{{avatarUrl}}"></image>
</button>
<input type="nickname" class="weui-input" placeholder="点击获取微信昵称" model:value="{{nickName}}" />
</view>
<view class="btn-part">
<!-- <button class="buy-btn" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">立即登录</button> -->
<button class="buy-btn" bindtap="toWXLogin">立即登录</button>
</view>
</view>

View File

@@ -1,5 +1,6 @@
// pages/star/star.ts
const api = require("../../api/index")
const app = getApp<IAppOption>()
Page({
/**
@@ -78,12 +79,21 @@ Page({
},
async getList() {
const res = await api.getMyWineList({
current:1,
size:10
})
if(res.code === 200) {
console.log(app.globalData.isLogin)
if(app.globalData.isLogin) {
const res = await api.getMyWineList({
current:1,
size:10
})
if(res.code === 200) {
}
}
else{ // 跳转登录页
wx.navigateTo({
url:'../login/login'
})
}
}
})

View File

@@ -1,5 +1,5 @@
<view class="container" style="padding: 10px;">
<view class="list-part c-part">
<view class="list-part c-part" wx:if="isLogin">
<view class="lp-card" wx:for="{{list}}" wx:for-item="item" wx:key="id">
<view class="lpc-name-box">
<view class="lpc-name text-ellipsis-1" style="margin-bottom: 5px;"> {{item.name}}</view>