From 0896f37dc7ff36431b20d97d958d59f74c9ef97b Mon Sep 17 00:00:00 2001 From: linju-json Date: Tue, 10 Aug 2021 15:26:31 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E5=A4=8D=E7=99=BB=E9=99=86?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=90=8E=E5=93=8D=E5=BA=94=E4=BD=93=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E7=9A=84=E9=97=AE=E9=A2=98=20-=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=E8=A1=A8=E7=9A=84schema=E4=B8=AD=E5=AD=97=E6=AE=B5use?= =?UTF-8?q?rname=E7=9A=84=E7=BC=96=E8=BE=91=E6=9D=83=E9=99=90=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E7=94=A8=E6=88=B7=E9=80=9A=E8=BF=87clientDB?= =?UTF-8?q?=E7=BB=95=E8=BF=87=E7=94=A8=E6=88=B7=E5=90=8D=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E7=9A=84=E8=A7=84=E5=88=99=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog.md | 3 + manifest.json | 9 +- package.json | 2 +- .../database/uni-id-users.schema.json | 10 +- uni_modules/uni-id-cf/changelog.md | 6 + uni_modules/uni-id-cf/package.json | 6 +- uni_modules/uni-id-cf/readme.md | 9 +- .../cloudfunctions/uni-id-cf/index.js | 320 +++++++++++------- uni_modules_tools/copy/manifest.json | 11 +- 9 files changed, 252 insertions(+), 124 deletions(-) diff --git a/changelog.md b/changelog.md index eac0e91a..de0dc8e1 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,6 @@ +## 1.0.48(2021-08-10) +- 修复登陆成功后响应体包含`userInfo.password`的问题 +- 修改了`uni-id-users`表的schema中字段username的编辑权限,防止用户通过clientDB绕过用户名不能重复的规则更新用户名的问题 ## 1.0.47(2021-08-09) - 更新文档快速体验部署流程 - 修复一键登陆优先时报变量找不到的问题 diff --git a/manifest.json b/manifest.json index 9236c4a4..8b6484ed 100644 --- a/manifest.json +++ b/manifest.json @@ -181,7 +181,14 @@ "enable": false }, "h5": { - "template": "" + "template": "", + "sdkConfigs": { + "maps": { + "qqmap": { + "key": "" + } + } + } }, "_spaceID": "", "vueVersion": "2" diff --git a/package.json b/package.json index 310c36ab..38537319 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "id": "uni-starter", "displayName": "uni-starter", - "version": "1.0.47", + "version": "1.0.48", "description": "云端一体应用快速开发基本项目模版", "keywords": [ "login", diff --git a/uniCloud-aliyun/database/uni-id-users.schema.json b/uniCloud-aliyun/database/uni-id-users.schema.json index 7809b890..b1e9939f 100644 --- a/uniCloud-aliyun/database/uni-id-users.schema.json +++ b/uniCloud-aliyun/database/uni-id-users.schema.json @@ -12,13 +12,19 @@ "bsonType": "string", "title": "用户名", "description": "用户名,不允许重复", - "trim": "both" + "trim": "both", + "permission":{ + "write":false + } }, "password": { "bsonType": "password", "title": "密码", "description": "密码,加密存储", - "trim": "both" + "trim": "both", + "permission":{ + "write":false + } }, "password_secret_version": { "bsonType": "int", diff --git a/uni_modules/uni-id-cf/changelog.md b/uni_modules/uni-id-cf/changelog.md index b9bfcf78..003b54b0 100644 --- a/uni_modules/uni-id-cf/changelog.md +++ b/uni_modules/uni-id-cf/changelog.md @@ -1,3 +1,9 @@ +## 1.0.5(2021-08-10) +- 修复登陆成功后响应体包含userInfo.password的问题 +- 新增微信登陆成功后,自动获取用户的微信昵称和头像完善用户个人资料 +## 1.0.4(2021-07-31) +- 修复 登录日志在登录失败时不写入记录的 bug +- 修复 写入记录登录是未传递 type 参数的 bug ## 1.0.3(2021-07-02) - 框架设定非 admin 不能创建用户, 用户可自定义 ## 1.0.2(2021-07-01) diff --git a/uni_modules/uni-id-cf/package.json b/uni_modules/uni-id-cf/package.json index 4833b63f..3ffcb281 100644 --- a/uni_modules/uni-id-cf/package.json +++ b/uni_modules/uni-id-cf/package.json @@ -1,7 +1,7 @@ { "id": "uni-id-cf", "displayName": "uni-id-cf", - "version": "1.0.3", + "version": "1.0.5", "description": "uni-id-cf", "keywords": [ "uni-id-cf", @@ -70,6 +70,10 @@ "快应用": { "华为": "u", "联盟": "u" + }, + "Vue": { + "vue2": "y", + "vue3": "u" } } } diff --git a/uni_modules/uni-id-cf/readme.md b/uni_modules/uni-id-cf/readme.md index abd4402b..75be0bbf 100644 --- a/uni_modules/uni-id-cf/readme.md +++ b/uni_modules/uni-id-cf/readme.md @@ -1,4 +1,7 @@ -uni-id-cf是uni-id-uniCloudFunction的缩写。 -直接调用他内置的云函数,即可直接使用uni-id的各类api。 +#### uni-id-cf是uni-id-uniCloudFunction的缩写。 + +#### 直接调用他内置的云函数,即可直接使用uni-id的各类api。 + 含:登录注册(含用户名密码登录、手机号验证码登录、app一键登录、微信登录、Apple登录、微信小程序登录)、修改密码、忘记密码、退出登录等 -详细的使用方式见[uni-starter](https://ext.dcloud.net.cn/plugin?id=5057) \ No newline at end of file + +> 详细的使用方式见[uni-starter](https://ext.dcloud.net.cn/plugin?id=5057) \ No newline at end of file diff --git a/uni_modules/uni-id-cf/uniCloud/cloudfunctions/uni-id-cf/index.js b/uni_modules/uni-id-cf/uniCloud/cloudfunctions/uni-id-cf/index.js index 06bd957d..96a3cd8b 100644 --- a/uni_modules/uni-id-cf/uniCloud/cloudfunctions/uni-id-cf/index.js +++ b/uni_modules/uni-id-cf/uniCloud/cloudfunctions/uni-id-cf/index.js @@ -63,7 +63,7 @@ exports.main = async (event, context) => { } //禁止前台用户传递角色 - if (action.slice(0,7) == "loginBy") { + if (action.slice(0, 7) == "loginBy") { if (params.role) { return { code: 403, @@ -75,8 +75,11 @@ exports.main = async (event, context) => { //3.注册成功后创建新用户的积分表方法 async function registerSuccess(uid) { //用户接受邀请 - if(inviteCode){ - await uniID.acceptInvite({inviteCode,uid}); + if (inviteCode) { + await uniID.acceptInvite({ + inviteCode, + uid + }); } //添加当前用户设备信息 await db.collection('uni-id-device').add({ @@ -93,10 +96,7 @@ exports.main = async (event, context) => { }) } //4.记录成功登录的日志方法 - const loginLog = async (res = {}) => { - if(res.code != 0){ - return false - } + const loginLog = async (res = {}) => { const now = Date.now() const uniIdLogCollection = db.collection('uni-id-log') let logData = { @@ -105,25 +105,30 @@ exports.main = async (event, context) => { type: res.type, ua: context.CLIENTUA, create_date: now - }; + }; - Object.assign(logData, - res.code === 0 ? { - user_id: res.uid, - state: 1 - } : { - state: 0 - }) - if (res.type == 'register') { - await registerSuccess(res.uid) - } else { - if (Object.keys(deviceInfo).length) { - console.log(979797,{deviceInfo,user_id: res}); - //更新当前用户设备信息 - await db.collection('uni-id-device').where({ - user_id: res.uid - }).update(deviceInfo) - } + if(res.code === 0){ + logData.user_id = res.uid + logData.state = 1 + if(res.userInfo&&res.userInfo.password){ + delete res.userInfo.password + } + if (res.type == 'register') { + await registerSuccess(res.uid) + } else { + if (Object.keys(deviceInfo).length) { + // console.log(979797, { + // deviceInfo, + // user_id: res + // }); + //更新当前用户设备信息 + await db.collection('uni-id-device').where({ + user_id: res.uid + }).update(deviceInfo) + } + } + }else{ + logData.state = 0 } return await uniIdLogCollection.add(logData) } @@ -151,6 +156,11 @@ exports.main = async (event, context) => { } break; case 'bindMobileBySms': + // console.log({ + // uid: params.uid, + // mobile: params.mobile, + // code: params.code + // }); res = await uniID.bindMobile({ uid: params.uid, mobile: params.mobile, @@ -159,7 +169,9 @@ exports.main = async (event, context) => { // console.log(res); break; case 'register': - var {username, password, nickname} = params + var { + username, password, nickname + } = params if (/^1\d{10}$/.test(username)) { return { code: 401, @@ -172,7 +184,12 @@ exports.main = async (event, context) => { msg: '用户名不能是邮箱' } } - res = await uniID.register({username, password, nickname,inviteCode}); + res = await uniID.register({ + username, + password, + nickname, + inviteCode + }); if (res.code === 0) { await registerSuccess(res.uid) } @@ -212,59 +229,60 @@ exports.main = async (event, context) => { ...params, queryField: ['username', 'email', 'mobile'] }); + res.type = 'login' await loginLog(res); needCaptcha = await getNeedCaptcha(); } res.needCaptcha = needCaptcha; break; - case 'loginByWeixin': - let loginRes = await uniID.loginByWeixin(params); - if(loginRes.code===0){ - //用户完善资料(昵称、头像) - if(context.PLATFORM == "app-plus" && !loginRes.userInfo.nickname){ - let {accessToken:access_token,openid} = loginRes, - {appid,appsecret:secret} = uniIdConfig['app-plus'].oauth.weixin; - let wxRes = await uniCloud.httpclient.request( - `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&scope=snsapi_userinfo&appid=${appid}&secret=${secret}`, { - method: 'POST', - contentType: 'json', // 指定以application/json发送data内的数据 - dataType: 'json' // 指定返回值为json格式,自动进行parse - }) - if(wxRes.status == 200){ - let {nickname,headimgurl} = wxRes.data; - let headimgurlFile = {},cloudPath = loginRes.uid+'/'+Date.now()+"headimgurl.jpg"; - let getImgBuffer = await uniCloud.httpclient.request(headimgurl) - if(getImgBuffer.status == 200){ - let {fileID} = await uniCloud.uploadFile({ - cloudPath, - fileContent: getImgBuffer.data - }); - headimgurlFile = { - name:cloudPath, - extname:"jpg", - url:fileID - } - }else{ - return getImgBuffer - } - await uniID.updateUser({ - uid: loginRes.uid, - nickname, - avatar_file:headimgurlFile - }) - loginRes.userInfo.nickname = nickname; - loginRes.userInfo.avatar_file = headimgurlFile; - }else{ - return wxRes - } - } - delete loginRes.accessToken - delete loginRes.refreshToken - return loginRes - }else{ - return loginRes - } + case 'loginByWeixin': + let loginRes = await uniID.loginByWeixin(params); + if(loginRes.code===0){ + //用户完善资料(昵称、头像) + if(context.PLATFORM == "app-plus" && !loginRes.userInfo.nickname){ + let {accessToken:access_token,openid} = loginRes, + {appid,appsecret:secret} = uniIdConfig['app-plus'].oauth.weixin; + let wxRes = await uniCloud.httpclient.request( + `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&scope=snsapi_userinfo&appid=${appid}&secret=${secret}`, { + method: 'POST', + contentType: 'json', // 指定以application/json发送data内的数据 + dataType: 'json' // 指定返回值为json格式,自动进行parse + }) + if(wxRes.status == 200){ + let {nickname,headimgurl} = wxRes.data; + let headimgurlFile = {},cloudPath = loginRes.uid+'/'+Date.now()+"headimgurl.jpg"; + let getImgBuffer = await uniCloud.httpclient.request(headimgurl) + if(getImgBuffer.status == 200){ + let {fileID} = await uniCloud.uploadFile({ + cloudPath, + fileContent: getImgBuffer.data + }); + headimgurlFile = { + name:cloudPath, + extname:"jpg", + url:fileID + } + }else{ + return getImgBuffer + } + await uniID.updateUser({ + uid: loginRes.uid, + nickname, + avatar_file:headimgurlFile + }) + loginRes.userInfo.nickname = nickname; + loginRes.userInfo.avatar_file = headimgurlFile; + }else{ + return wxRes + } + } + delete loginRes.accessToken + delete loginRes.refreshToken + return loginRes + }else{ + return loginRes + } await loginLog(res) break; case 'loginByUniverify': @@ -281,15 +299,15 @@ exports.main = async (event, context) => { case 'logout': res = await uniID.logout(uniIdToken) break; - case 'sendSmsCode': + case 'sendSmsCode': /* -开始- 测试期间,为节约资源。统一虚拟短信验证码为: 123456;开启以下代码块即可 */ - return uniID.setVerifyCode({ - mobile: params.mobile, - code: '123456', - type: params.type - }) - /* -结束- */ - + // return uniID.setVerifyCode({ + // mobile: params.mobile, + // code: '123456', + // type: params.type + // }) + /* -结束- */ + // 简单限制一下客户端调用频率 const ipLimit = await db.collection('opendb-verify-codes').where({ ip: context.CLIENTIP, @@ -392,10 +410,11 @@ exports.main = async (event, context) => { } break; - // ----------- admin api ----------- - case 'registerAdmin': + // =========================== admin api start ========================= + case 'registerAdmin': { var { - username, password + username, + password } = params let { total @@ -408,46 +427,119 @@ exports.main = async (event, context) => { message: '超级管理员已存在,请登录...' } } - return uniID.register({ + const appid = params.appid + const appName = params.appName + delete params.appid + delete params.appName + res = await uniID.register({ username, password, role: ["admin"] }) - break; - case 'registerUser': - const { - userInfo - } = await uniID.getUserInfo({ - uid: params.uid - }) - if (userInfo.role.indexOf('admin') === -1) { - res = { - code: 403, - message: '非法访问, 无权限注册超级管理员', - } - } else { - res = await uniID.register({ - ...params - }) - if (res.code === 0) { - delete res.token - delete res.tokenExpired + if (res.code === 0) { + const app = await db.collection('opendb-app-list').where({ + appid + }).count() + if (!app.total) { + await db.collection('opendb-app-list').add({ + appid, + name: appName, + description: "admin 管理后台", + create_date: Date.now() + }) } + } - break; - case 'getCurrentUserInfo': - res = uniID.getUserInfo({ - uid: params.uid, + } + break; + case 'registerUser': + const { + userInfo + } = await uniID.getUserInfo({ + uid: params.uid + }) + if (userInfo.role.indexOf('admin') === -1) { + res = { + code: 403, + message: '非法访问, 无权限注册超级管理员', + } + } else { + // 过滤 dcloud_appid,注册用户成功后再提交 + const dcloudAppidList = params.dcloud_appid + delete params.dcloud_appid + res = await uniID.register({ + autoSetDcloudAppid: false, ...params }) - break; - default: + if (res.code === 0) { + delete res.token + delete res.tokenExpired + await uniID.setAuthorizedAppLogin({ + uid: res.uid, + dcloudAppidList + }) + } + } + break; + case 'updateUser': { + const { + userInfo + } = await uniID.getUserInfo({ + uid: params.uid + }) + if (userInfo.role.indexOf('admin') === -1) { res = { code: 403, - msg: '非法访问' + message: '非法访问, 无权限注册超级管理员', } - break; + } else { + // 过滤 dcloud_appid,注册用户成功后再提交 + const dcloudAppidList = params.dcloud_appid + delete params.dcloud_appid + + // 过滤 password,注册用户成功后再提交 + const password = params.password + delete params.password + + // 过滤 uid、id + const id = params.id + delete params.id + delete params.uid + + + res = await uniID.updateUser({ + uid: id, + ...params + }) + if (res.code === 0) { + if (password) { + await uniID.resetPwd({ + uid: id, + password + }) + } + await uniID.setAuthorizedAppLogin({ + uid: id, + dcloudAppidList + }) + } + } + break; + } + case 'getCurrentUserInfo': + res = await uniID.getUserInfo({ + uid: params.uid, + ...params + }) + break; + // =========================== admin api end ========================= + default: + res = { + code: 403, + msg: '非法访问' + } + break; } //返回数据给客户端 return res -} +} diff --git a/uni_modules_tools/copy/manifest.json b/uni_modules_tools/copy/manifest.json index ca120295..72bb790d 100644 --- a/uni_modules_tools/copy/manifest.json +++ b/uni_modules_tools/copy/manifest.json @@ -160,8 +160,15 @@ "enable" : false }, "h5" : { - "template" : "" + "template" : "", + "sdkConfigs" : { + "maps" : { + "qqmap" : { + "key" : "" + } + } + } }, "_spaceID" : "76ce2c5e-31c7-4d81-8fcf-ed1541ecbc6e", - "vueVersion" : "2" + "vueVersion" : "3" } -- GitLab