From 6de50cf8b923fa7a3901a627633f129c19aed13a Mon Sep 17 00:00:00 2001 From: Xubx <1827135378@qq.com> Date: Tue, 3 Sep 2024 00:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.json | 3 +- page/component/address/address.js | 2 +- page/component/details/details.js | 8 -- page/component/login/login.js | 105 ++++++++++++++++++++ page/component/login/login.json | 4 + page/component/login/login.wxml | 12 +++ page/component/login/login.wxss | 32 ++++++ page/component/myOrders/myOrder.js | 2 +- page/component/myOrders2/myOrder2.js | 2 +- page/component/user/user.js | 142 +++++++++++---------------- page/component/user/user.wxml | 19 +--- project.config.json | 2 +- 12 files changed, 221 insertions(+), 112 deletions(-) create mode 100644 page/component/login/login.js create mode 100644 page/component/login/login.json create mode 100644 page/component/login/login.wxml create mode 100644 page/component/login/login.wxss diff --git a/app.json b/app.json index 880372a..de214b4 100644 --- a/app.json +++ b/app.json @@ -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", diff --git a/page/component/address/address.js b/page/component/address/address.js index 0547897..ceb85b6 100644 --- a/page/component/address/address.js +++ b/page/component/address/address.js @@ -43,7 +43,7 @@ Page({ method: "POST", data: { ...value, - openId: "123123dasdas", + openId: wx.getStorageSync('openid'), }, success(res) { console.log(res) diff --git a/page/component/details/details.js b/page/component/details/details.js index f37fd36..153ee81 100644 --- a/page/component/details/details.js +++ b/page/component/details/details.js @@ -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: "枕被套 、床单颜色", diff --git a/page/component/login/login.js b/page/component/login/login.js new file mode 100644 index 0000000..6fdd43d --- /dev/null +++ b/page/component/login/login.js @@ -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 + }) + }) + } +}) diff --git a/page/component/login/login.json b/page/component/login/login.json new file mode 100644 index 0000000..623adf3 --- /dev/null +++ b/page/component/login/login.json @@ -0,0 +1,4 @@ +{ + "navigationBarTitleText": "登录", + "enablePullDownRefresh": false +} \ No newline at end of file diff --git a/page/component/login/login.wxml b/page/component/login/login.wxml new file mode 100644 index 0000000..105be4f --- /dev/null +++ b/page/component/login/login.wxml @@ -0,0 +1,12 @@ + + +
+ + 昵称: + + + +
+
\ No newline at end of file diff --git a/page/component/login/login.wxss b/page/component/login/login.wxss new file mode 100644 index 0000000..b9c7841 --- /dev/null +++ b/page/component/login/login.wxss @@ -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; +} \ No newline at end of file diff --git a/page/component/myOrders/myOrder.js b/page/component/myOrders/myOrder.js index bdf65a9..12b8f2e 100644 --- a/page/component/myOrders/myOrder.js +++ b/page/component/myOrders/myOrder.js @@ -20,7 +20,7 @@ Page({ url: baseUrl + "/order/loadData", method: "POST", data: { - userId: 1, + openId: wx.getStorageSync('openid'), status: 1 }, success(res) { diff --git a/page/component/myOrders2/myOrder2.js b/page/component/myOrders2/myOrder2.js index 48c9b90..a59be80 100644 --- a/page/component/myOrders2/myOrder2.js +++ b/page/component/myOrders2/myOrder2.js @@ -20,7 +20,7 @@ Page({ url: baseUrl + "/order/loadData", method: "POST", data: { - userId: 1, + openId: wx.getStorageSync('openid'), status: 2 }, success(res) { diff --git a/page/component/user/user.js b/page/component/user/user.js index 161a75f..38059aa 100644 --- a/page/component/user/user.js +++ b/page/component/user/user.js @@ -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: "", - showCancel: false, - }); - }, - }); - }, }); diff --git a/page/component/user/user.wxml b/page/component/user/user.wxml index d1f043d..42fa528 100644 --- a/page/component/user/user.wxml +++ b/page/component/user/user.wxml @@ -1,11 +1,13 @@ - + {{nickname}} 关于我们 - 请登录 + + 请登录 + @@ -25,18 +27,5 @@ 待收货订单 - \ No newline at end of file diff --git a/project.config.json b/project.config.json index f566406..8291bcf 100644 --- a/project.config.json +++ b/project.config.json @@ -25,7 +25,7 @@ }, "compileType": "miniprogram", "libVersion": "3.5.5", - "appid": "wx8159f868020f7f0a", + "appid": "wx865aefa5a7115ae0", "projectname": "wxapp-mall", "simulatorType": "wechat", "simulatorPluginLibVersion": {},