From 0813bb091147ae65b4f9343cd5622c7351aae4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E9=9B=8D=E6=95=AC?= <2622604517@qq.com> Date: Tue, 14 Nov 2023 15:38:35 +0800 Subject: [PATCH] 1 --- src/miniprogram/app.js | 29 +++++- src/miniprogram/pages/profile/profile.js | 113 +++++++++++++++++++++-- 2 files changed, 132 insertions(+), 10 deletions(-) diff --git a/src/miniprogram/app.js b/src/miniprogram/app.js index 29a697e..7433439 100644 --- a/src/miniprogram/app.js +++ b/src/miniprogram/app.js @@ -16,6 +16,33 @@ App({ this.globalData = { // OPENID: wx.cloud.getWXContext().OPENID + OPENID: '', + USERID: '', + ISLOGIN: false }; - } + + //尝试读取本地缓存 + try { + wx.clearStorage() + this.globalData.USERID = wx.getStorageSync('user_id') + this.globalData.OPENID = wx.getStorageSync('user_openid') + } catch (e) { + console.error(e); + } + + //检测用户是否首次登录 + if (!this.globalData.OPENID) { + this.globalData.ISLOGIN = false + wx.cloud.callFunction({ + name: "getOpenId", + success: (res) => { + this.globalData.OPENID = res.result.openid; + wx.setStorageSync('user_openid', res.result.openid) + } + }) + } + + //输出全局变量 + console.log("[GLOABLE] User:",this.globalData); + }, }); diff --git a/src/miniprogram/pages/profile/profile.js b/src/miniprogram/pages/profile/profile.js index 417738e..7913cce 100644 --- a/src/miniprogram/pages/profile/profile.js +++ b/src/miniprogram/pages/profile/profile.js @@ -1,12 +1,18 @@ const db = wx.cloud.database().collection("user_Data"); +const _ = wx.cloud.database().command +const app = getApp() const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0' //默认头像地址 Page({ //页面数据 data:{ avatarUrl: defaultAvatarUrl, + nickname: "", + gender: "", birthday: "", - region:"", + region: "", + contact: "", + information: "" }, onLoad(options) { @@ -21,6 +27,44 @@ Page({ } catch (e) { console.log(e); } + + //获取用户信息 + if (!app.globalData.USERID) { + db.add({ + data: { + nickname: '', + gender: '', + birthday: '', + region: '', + contact: '', + information: '' + }, + success: (res) => { + app.globalData.USERID = res._id + wx.setStorageSync('user_id', res._id) + console.log("[PROFILE] AddNewUser"); + } + }) + } else { + db.where({ + _openid: app.globalData.OPENID + }).get({ + success:(res) => { + var data = res.data[0] + this.setData({ + nickname: data.nickname, + gender: data.gender, + birthday: data.birthday, + region: data.region, + contact: data.contact, + information: data.information + }) + app.globalData.USERID = data._id + // app.globalData.ISLOGIN = true + console.log("[PROFILE] UserQuery", data); + } + }) + } }, //选择头像后存储头像地址 @@ -34,22 +78,41 @@ Page({ try { wx.setStorageSync('avatarUrl', avatarUrl) } catch (e) { - console.log(e,"sssss"); + console.log(e); } }, //表单提交方法 submit: function(e){ - wx.setStorageSync('user',e.detail.value) - //todo: 添加输入检测 - - //往数据库里插入的方法 - db.add({ + console.log("[PROFILE] SubmitData", e.detail.value); + + //输入检测,检测输入是否为空 + if(!this.cheakFrom(e.detail.value)) { + return + } + + //往数据库里更新的方法 + db.where({ + _openid: app.globalData.OPENID + }).update({ //传入数据 - data: e.detail.value, + data: { + nickname: this.data.nickname, + gender: this.data.gender, + birthday: this.data.birthday, + region: this.data.region, + contact: this.data.contact, + information: this.data.information + }, + //成功后执行的方法 success:(res) => { - console.log(res,"ssss") + console.log("[PROFILE] SubmitDone",res) + wx.showToast({ + title: '提交成功', + icon: 'success', + duration: 2000 + }) } }) }, @@ -73,6 +136,38 @@ Page({ this.setData({ region: e.detail.value, }); + }, + + //更新页面的性别信息 + genderChange: function(e){ + this.setData({ + gender: e.detail.value, + }); + }, + + cheakFrom: function(e) { + var hint; + if (e.nickname == "") { + hint = "昵称为空!" + } else if (e.gender == "") { + hint = "性别为空!" + } else if (e.birthday == "") { + hint = "年龄为空!" + } else if (e.region == "") { + hint = "地区为空!" + } else if (e.contact == "") { + hint = "联系方式为空!" + } else if (e.information == "") { + hint = "基本情况为空!" + } else{ + return true + } + wx.showToast({ + title: hint, + icon: 'error', + duration: 2000 + }) + return false } }); \ No newline at end of file