登录优化

This commit is contained in:
Xubx 2024-09-03 00:16:51 +08:00
parent b4e1cbbb4a
commit 6de50cf8b9
12 changed files with 221 additions and 112 deletions

View File

@ -12,7 +12,8 @@
"page/component/myOrders/myOrder",
"page/component/refund/refund",
"page/component/invoice/invoice",
"page/component/myOrders2/myOrder2"
"page/component/myOrders2/myOrder2",
"page/component/login/login"
],
"window": {
"navigationBarTextStyle": "white",

View File

@ -43,7 +43,7 @@ Page({
method: "POST",
data: {
...value,
openId: "123123dasdas",
openId: wx.getStorageSync('openid'),
},
success(res) {
console.log(res)

View File

@ -18,14 +18,6 @@ Page({
goodImage: decodeURIComponent(options.goodImage).split(','),
};
const specList = [
{
specId: 1,
title: "枕头类型",
specValueList: [
{ specValueId: 1, specValue: "普通蓬松枕", isSelected: false, goodImage: "/image/zhentou1.jpg" },
{ specValueId: 2, specValue: "压缩高级枕", isSelected: false, goodImage: "/image/zhentou2.jpg" }
]
},
{
specId: 2,
title: "枕被套 、床单颜色",

View File

@ -0,0 +1,105 @@
const app = getApp()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
/**
* 页面的初始数据
*/
data: {
avatarUrl: defaultAvatarUrl,
theme: "",
},
onChooseAvatar(e) {
const { avatarUrl } = e.detail;
wx.uploadFile({
url: app.globalData.baseUrl + "/user/upload", // 替换为你服务器的上传接口
filePath: avatarUrl,
name: 'file',
success: (res) => {
const data = JSON.parse(res.data);
const permanentAvatarUrl = data.fileDownloadUri; // 假设服务器返回上传后的URL
this.setData({
avatarUrl: permanentAvatarUrl,
});
},
fail: (err) => {
console.error('上传失败', err);
}
});
},
formSubmit(e) {
const openid = wx.getStorageSync('openid'); // 从本地存储中获取 openid
const avatarUrl = this.data.avatarUrl; // 从 data 中获取头像 URL
const nickname = e.detail.value.nickname; // 从表单中获取用户输入的昵称
// 检查是否获取到所有需要的数据
if (!openid || !avatarUrl || !nickname) {
wx.showToast({
title: '信息不完整',
icon: 'none',
});
return;
}
//将用户信息存到StorageSync
// 将用户信息存到 StorageSync
const userInfo = {
avatarUrl: avatarUrl,
nickName: nickname
};
wx.setStorageSync('userInfo', userInfo); // 存储到本地
// 将数据发送到后端
wx.request({
url: app.globalData.baseUrl + "/user/addOrUpdate", // 替换为你的后端接口地址
method: 'POST',
data: {
openId: openid,
avatarUrl: avatarUrl,
userName: nickname,
},
header: {
'Content-Type': 'application/json', // 设置请求头
},
success(res) {
if (res.statusCode === 200) {
wx.showToast({
title: '信息提交成功',
icon: 'success',
});
// 提交成功后,跳转到首页或其他页面
wx.switchTab({
url: '/page/component/index', // 替换为你的首页路径
});
} else {
wx.showToast({
title: '提交失败',
icon: 'none',
});
}
},
fail(err) {
wx.showToast({
title: '请求失败',
icon: 'none',
});
console.error('请求失败', err);
}
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
wx.onThemeChange((result) => {
this.setData({
theme: result.theme
})
})
}
})

View File

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

View File

@ -0,0 +1,12 @@
<view data-weui-theme="{{theme}}">
<button class="avatar-wrapper" open-type="chooseAvatar" bindchooseavatar="onChooseAvatar">
<image class="avatar" src="{{avatarUrl}}"></image>
</button>
<form catchsubmit="formSubmit">
<view class="row">
<view class="text1">昵称:</view>
<input type="nickname" class="weui-input" name="nickname" placeholder="请输入昵称"/>
</view>
<button type="primary" style="margin-top: 40rpx;margin-bottom: 20rpx;" form-type="submit">提交</button>
</form>
</view>

View File

@ -0,0 +1,32 @@
.avatar-wrapper {
padding: 0;
width: 56px !important;
border-radius: 8px;
margin-top: 40px;
margin-bottom: 40px;
background-color: #fff;
}
.avatar {
display: block;
width: 56px;
height: 56px;
}
.container {
display: flex;
}
.row{
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
display: flex;
align-items: center;
height: 80rpx;
padding-left: 20rpx;
}
.text1{
flex: 2;
}
.weui-input{
flex: 6;
}

View File

@ -20,7 +20,7 @@ Page({
url: baseUrl + "/order/loadData",
method: "POST",
data: {
userId: 1,
openId: wx.getStorageSync('openid'),
status: 1
},
success(res) {

View File

@ -20,7 +20,7 @@ Page({
url: baseUrl + "/order/loadData",
method: "POST",
data: {
userId: 1,
openId: wx.getStorageSync('openid'),
status: 2
},
success(res) {

View File

@ -5,18 +5,13 @@ Page({
data: {
thumb: "", // 用户头像
nickname: "", // 用户昵称
orders: [], // 订单列表
hasAddress: false, // 是否有地址信息
address: {}, // 地址信息
},
onLoad() {
this.checkUserProfile(); // 检查并获取用户信息
/**
* 发起请求获取订单列表信息
*/
this.loadOrders(); // 加载订单列表
this.getUserProfile();
},
/**
@ -27,8 +22,8 @@ Page({
if (userInfo) {
// 如果有用户信息,直接设置数据
this.setData({
thumb: userInfo.userInfo.avatarUrl,
nickname: userInfo.userInfo.nickName,
thumb: userInfo.avatarUrl,
nickname: userInfo.nickName,
});
} else {
// 没有用户信息,提示用户登录授权
@ -38,7 +33,9 @@ Page({
showCancel: false,
success: (res) => {
if (res.confirm) {
this.getUserProfile();
wx.switchTab({
url: '/page/component/login/login',
});
}
}
});
@ -49,46 +46,61 @@ Page({
* 获取用户信息
*/
getUserProfile() {
wx.getUserProfile({
desc: "用于完善会员资料", // 这里必须声明用途
success: (userInfo) => {
// 存储用户信息到本地缓存中
wx.setStorageSync('userInfo', userInfo);
// 在页面中展示用户信息
this.setData({
thumb: userInfo.userInfo.avatarUrl,
nickname: userInfo.userInfo.nickName,
});
console.log('用户信息', userInfo);
},
fail: (err) => {
wx.showToast({
title: "用户拒绝了授权",
icon: "none",
});
console.log(err);
}
});
},
/**
* 微信登录
*/
wxLogin() {
//通过wx.login()获取登录凭证code
wx.login({
success: (res) => {
if (res.code) {
console.log('微信登录成功code:', res.code);
// 可以将code发送到服务器进行登录验证
} else {
console.log('登录失败!' + res.errMsg);
console.log("code:" + res.code)
const wxConfig = {
appid: "wx865aefa5a7115ae0",
secret: "3f9849429894435abc935eea88178dfd",
code: res.code
}
}
});
wx.request({
url: "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxConfig.appid + "&secret=" + wxConfig.secret + "&code=" + wxConfig.code + "&js_code=" + wxConfig.code + '&grant_type=authorization_code',
success: (res) => {
wx.setStorageSync('openid', res.data.openid);
console.log(res);
},
fail: (err) => {
console.error('wx.login 失败', err);
}
})
},
})
},
// getUserInfo() {
// wx.getUserProfile({
// desc: "用于完善会员资料", // 这里必须声明用途
// success: (userInfo) => {
// // 存储用户信息到本地缓存中
// wx.setStorageSync('userInfo', userInfo);
// // 在页面中展示用户信息
// this.setData({
// thumb: userInfo.userInfo.avatarUrl,
// nickname: userInfo.userInfo.nickName,
// });
// console.log('用户信息', userInfo);
// wx.request({
// url: baseUrl + "/user/addOrUpdate",
// method: "POST",
// data: {
// openId: wx.getStorageSync('openid'),
// userName: userInfo.userInfo.nickName
// },
// success(res) {
// console.log(res)
// },
// })
// },
// fail: (err) => {
// wx.showToast({
// title: "用户拒绝了授权",
// icon: "none",
// });
// console.log(err);
// }
// });
// },
/**
* 登录到服务器可选基于需求
*/
@ -118,23 +130,7 @@ Page({
/**
* 加载订单列表
*/
loadOrders() {
wx.request({
url: `${baseUrl}/order/loadData`,
method: "POST",
data: {
userId: 1, // 示例用户ID
},
success: (res) => {
this.setData({
orders: res.data,
});
},
fail: (err) => {
console.log('获取订单失败', err);
}
});
},
onShow() {
/**
@ -151,26 +147,4 @@ Page({
});
},
/**
* 发起支付请求
*/
payOrders() {
wx.requestPayment({
timeStamp: "String1",
nonceStr: "String2",
package: "String3",
signType: "MD5",
paySign: "String4",
success: (res) => {
console.log(res);
},
fail: (res) => {
wx.showModal({
title: "支付提示",
content: "<text>",
showCancel: false,
});
},
});
},
});

View File

@ -1,11 +1,13 @@
<view class="main">
<view class="header" bindtap="getUserProfile">
<view class="header" >
<view wx:if="{{nickname}}">
<image src="{{thumb}}" class="thumb"></image>
<text class="nickname">{{nickname}}</text>
<text class="about">关于我们</text>
</view>
<view wx:else="">请登录</view>
<view wx:else="">
<navigator url="/page/component/login/login">请登录</navigator>
</view>
</view>
<view class="address-box">
<view class="address-manage">
@ -25,18 +27,5 @@
<view class="address-manage2">
<navigator url="/page/component/myOrders2/myOrder2">待收货订单</navigator>
</view>
<!-- <view class="orders-list" wx:for="{{orders}}" wx:key="index">
<view class="orders-number">订单编号:{{item.number}}</view>
<view class="orders-detail">
<image src="{{item.thumb}}"></image>
<view class="">{{item.name}}</view>
<view class="">{{item.count}}</view>
<text class="orders-status">{{item.status}}</text>
</view>
<view class="orders-footer">
<text>实付:¥{{item.money}}</text>
<button size="mini" class="orders-btn" bindtap="payOrders">付款</button>
</view>
</view> -->
</view>
</view>

View File

@ -25,7 +25,7 @@
},
"compileType": "miniprogram",
"libVersion": "3.5.5",
"appid": "wx8159f868020f7f0a",
"appid": "wx865aefa5a7115ae0",
"projectname": "wxapp-mall",
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},