From f3793a09b8cd783f627cde6fff0d78cb30960c73 Mon Sep 17 00:00:00 2001 From: linju-json Date: Thu, 22 Apr 2021 22:46:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=82=B9=E5=87=BB=E5=A4=B4?= =?UTF-8?q?=E5=83=8F=E7=9B=B4=E6=8E=A5=E4=BF=AE=E6=94=B9=E5=A4=B4=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baseapp.config.js | 3 +- pages/ucenter/ucenter.vue | 54 +++++++++++++- .../uni-id-users/pages/uni-id-users/edit.vue | 8 +- .../cloudfunctions/user-center/index.js | 74 +++++++------------ 4 files changed, 80 insertions(+), 59 deletions(-) diff --git a/baseapp.config.js b/baseapp.config.js index 16d4af0b..467b0d6e 100644 --- a/baseapp.config.js +++ b/baseapp.config.js @@ -15,7 +15,8 @@ module.exports = { "router":{ "needLogin":[ //配置需要路由拦截的页面地址,在打开这些页面之前会自动检查(不联网)uni_id_token的值是否存在/过期等 "/uni_modules/uni-id-users/pages/uni-id-users/edit", - "/uni_modules/uni-news-favorite/pages/uni-news-favorite/list" + "/uni_modules/uni-news-favorite/pages/uni-news-favorite/list", + "/pages/ucenter/edit/uploadCutImageToUnicloud" ], "login":["univerify","smsCode","username","weixin","apple"] //默认就是短信验证码登陆 }, diff --git a/pages/ucenter/ucenter.vue b/pages/ucenter/ucenter.vue index 02a4bb5f..ee658856 100644 --- a/pages/ucenter/ucenter.vue +++ b/pages/ucenter/ucenter.vue @@ -111,7 +111,10 @@ } // #endif }, - methods: { + methods: { + ...mapMutations({ + setUserInfo: 'user/login' + }), toSettings() { uni.navigateTo({ url: "/pages/ucenter/settings/settings" @@ -131,9 +134,52 @@ }, toEdit() { console.log('点击编辑信息'); - uni.navigateTo({ - url: '/uni_modules/uni-id-users/pages/uni-id-users/edit' - }) + // uni.navigateTo({ + // url: '/uni_modules/uni-id-users/pages/uni-id-users/edit' + // }) + const token = uni.getStorageSync('uni_id_token') + if(token){ + uni.chooseImage({ + count:1, + success:(res)=> { + // 头像剪裁尺寸 + let options = { + width:600, + height:600 + } + // 剪裁并上传头像 + uni.navigateTo({ + url:'/pages/ucenter/edit/uploadCutImageToUnicloud?path=' + res.tempFilePaths[0] + `&options=${JSON.stringify(options)}`, + animationType:"fade-in", + events:{ + uploadAvatarAfter:({url})=>{ + console.log(url); + // 使用 clientDB 提交数据 + db.collection('uni-id-users').where('_id==$env.uid').update({avatar:url}).then((res) => { + console.log(res); + uni.showToast({ + icon: 'none', + title: '修改成功' + }) + this.setUserInfo({avatar:url}); + }).catch((err) => { + uni.showModal({ + content: err.message || '请求服务失败', + showCancel: false + }) + }).finally(() => { + uni.hideLoading() + }) + } + } + }); + } + }) + }else{ + uni.navigateTo({ + url:'/uni_modules/uni-login-page/pages/index/index' + }) + } }, tapGrid(index) { uni.showToast({ diff --git a/uni_modules/uni-id-users/pages/uni-id-users/edit.vue b/uni_modules/uni-id-users/pages/uni-id-users/edit.vue index ac48d9a1..1493cc5c 100644 --- a/uni_modules/uni-id-users/pages/uni-id-users/edit.vue +++ b/uni_modules/uni-id-users/pages/uni-id-users/edit.vue @@ -50,7 +50,7 @@ return { formData: { "avatar": "", - "gender": 0, + "gender": '', "mobile": "", "nickname": "" }, @@ -100,12 +100,6 @@ width:600, height:600 } - uni.navigateTo({ - url: '/pages/ucenter/edit/uploadCutImageToUnicloud', - success: res => {}, - fail: () => {}, - complete: () => {} - }); // 剪裁并上传头像 uni.navigateTo({ url:'/pages/ucenter/edit/uploadCutImageToUnicloud?path=' + res.tempFilePaths[0] + `&options=${JSON.stringify(options)}`, diff --git a/uni_modules/uni-login-page/uniCloud/cloudfunctions/user-center/index.js b/uni_modules/uni-login-page/uniCloud/cloudfunctions/user-center/index.js index 4f765eae..0918d7de 100644 --- a/uni_modules/uni-login-page/uniCloud/cloudfunctions/user-center/index.js +++ b/uni_modules/uni-login-page/uniCloud/cloudfunctions/user-center/index.js @@ -32,18 +32,6 @@ exports.main = async (event, context) => { return recentRecord.data.filter(item => item.state === 0).length === recordSize; } - //注册成功后为用户执行相关操作,如创建该用户的积分表等 - function registerSuccess(uid) { - await db.collection('uni-id-scores').add({ - user_id: uid, - score: 1, - type: 1, - balance: 1, - comment: "", - create_date: Date.now() - }) - } - //设置某些模块不需要token(也就是登陆成功后)才能操作,如果需要token就获取当前操作账户的uid let noCheckAction = [ 'register', 'checkToken', 'login', 'logout', 'sendSmsCode', @@ -66,7 +54,17 @@ exports.main = async (event, context) => { params.uid = payload.uid } - + //注册成功后为用户执行相关操作,如创建该用户的积分表等 + async function registerSuccess(uid) { + await db.collection('uni-id-scores').add({ + user_id: uid, + score: 1, + type: 1, + balance: 1, + comment: "", + create_date: Date.now() + }) + } //记录成功登陆的日志 const loginLog = async (res = {}, type = 'login') => { const now = Date.now() @@ -86,8 +84,10 @@ exports.main = async (event, context) => { } : { state: 0 }) - - return uniIdLogCollection.add(logData) + if(res.type == 'register'){ + await registerSuccess(res.uid) + } + return await uniIdLogCollection.add(logData) } @@ -118,7 +118,7 @@ exports.main = async (event, context) => { password }); if (res.code === 0) { - registerSuccess(res.uid) + await registerSuccess(res.uid) } break; case 'login': @@ -136,10 +136,6 @@ exports.main = async (event, context) => { queryField: ['username', 'email', 'mobile'] }); await loginLog(res); - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } - needCaptcha = await getNeedCaptcha(); } @@ -150,25 +146,17 @@ exports.main = async (event, context) => { await uniID.updateUser({ uid: res.uid, username: "微信用户" - }); - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } + }); res.userInfo.username = "微信用户" - loginLog(res) + await loginLog(res) break; case 'login_by_univerify': - res = await uniID.loginByUniverify(params) - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } + res = await uniID.loginByUniverify(params) + await loginLog(res) break; case 'login_by_apple': - res = await uniID.loginByApple(params) - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } - loginLog(res) + res = await uniID.loginByApple(params) + await loginLog(res) break; case 'checkToken': res = await uniID.checkToken(event.uniIdToken); @@ -202,10 +190,8 @@ exports.main = async (event, context) => { code, type: params.type, templateId - }) - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } + }) + await loginLog(res) break; case 'loginBySms': if (!params.code) { @@ -220,11 +206,8 @@ exports.main = async (event, context) => { msg: '手机号码填写错误' } } - res = await uniID.loginBySms(params) - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } - loginLog(res) + res = await uniID.loginBySms(params) + await loginLog(res) break; case 'inviteLogin': if (!params.code) { @@ -236,10 +219,7 @@ exports.main = async (event, context) => { res = await uniID.loginBySms({ ...params, type: 'register' - }) - if (res.code == 0 && res.type == 'register') { - registerSuccess(res.uid) - } + }) break; case 'resetPwdBySmsCode': if (!params.code) { -- GitLab