Comments (7)
我现在只能在小程序端不使用qcloud.loginWithCode()尝试避免这个错误,但没有找到“罪魁祸首”,也不知道是否靠谱。
from wafer2-node-sdk.
怀疑是多个小程序使用wafer-node-sdk共用一个mysql服务器产生的问题,由于同一个用户在每个小程序中的openid都不一样,这样不同用户的openID可能相同,公用时可能会产生此类问题,这只是初步推测
from wafer2-node-sdk.
// 查重并决定是插入还是更新数据
return mysql('cSessionInfo').count('open_id as hasUser').where({
open_id
})
.then(res => {
// 如果存在用户则更新
if (res[0].hasUser) {
return mysql('cSessionInfo').update({
skey, last_visit_time, session_key, user_info
}).where({
open_id
})
} else {
return mysql('cSessionInfo').insert({
uuid, skey, create_time, last_visit_time, open_id, session_key, user_info
})
}
})
应该是这里有问题了,如果A用户在小程序1的openID与B用户在小程序2的openID相同,那么在共用同一个数据库的情况下,B用户登陆检测到数据库已经存在此openID,那么就会用自己的userInfo更新,而A用户随后使用就会发现用自己openID返回了B的用户信息
from wafer2-node-sdk.
在不修改数据库表的情况下,临时解决办法,为saveUserInfo添加一个appID参数:
function saveUserInfo (appID,userInfo, skey, session_key) {
const uuid = uuidGenerator()
const create_time = moment().format('YYYY-MM-DD HH:mm:ss')
const last_visit_time = create_time
const open_id = userInfo.openId+'-'+appID
const user_info = JSON.stringify(userInfo)
// 查重并决定是插入还是更新数据
return mysql('cSessionInfo').count('open_id as hasUser').where({
open_id
})
.then(res => {
// 如果存在用户则更新
if (res[0].hasUser) {
return mysql('cSessionInfo').update({
skey, last_visit_time, session_key, user_info
}).where({
open_id
})
} else {
return mysql('cSessionInfo').insert({
uuid, skey, create_time, last_visit_time, open_id, session_key, user_info
})
}
})
.then(() => ({
userinfo: userInfo,
skey: skey
}))
.catch(e => {
debug('%s: %O', ERRORS.DBERR.ERR_WHEN_INSERT_TO_DB, e)
throw new Error(`${ERRORS.DBERR.ERR_WHEN_INSERT_TO_DB}\n${e}`)
})
}
注意这句:const open_id = userInfo.openId+'-'+appID,由于添加了appID作为识别ID,这样就能确保不会产生openID冲突,当然更理想的情况下是更改数据库表中open_id为user_id,这样理解起来就不会与真正的openID产生混淆了
修改完后,再调用此函数的两个地方添加appID参数即可。
期望官方能针对此情况作出更好的设计。
from wafer2-node-sdk.
忘了说了,getUserInfoByOpenId也要做相应修改:
function getUserInfoByOpenId (appID,openId) {
var userID = openId+'-'+appID
if (!openId) throw new Error(ERRORS.DBERR.ERR_NO_OPENID_ON_CALL_GETUSERINFOFUNCTION)
return mysql('cSessionInfo').select('*').where({ open_id: userID }).first()
}
from wafer2-node-sdk.
这里的解决方案,是否是不同的小程序使用不同的 db 好点呢?
from wafer2-node-sdk.
@jas0ncn 这个要看具体情况吧,不是所有小程序都是爆款,很可能多个小程序都没有什么流量,每一个都配独立数据库服务器太不经济,特别是对个人开发者来说,前景未明,在开发测试阶段,用一个数据库更划算。
from wafer2-node-sdk.
Related Issues (20)
- 新版wafer2-node-sdk刷新用户信息的问题 HOT 3
- wafer2-node-sdk拿到的昵称有可能是乱码 HOT 3
- 如何支持mysql 5.6版本 HOT 1
- 考虑支持mongodb吗? HOT 1
- 更新 1.4.0 之后 qcloud.request 参数 login: false 时 skey 会报错
- 新用户首次登录,在authorization方法中出现异常 HOT 1
- 上传文件的接口感觉有问题每次生成的imagekey都是扩展名麻烦尽快修正 HOT 1
- 信道接口是不是存在安全漏洞? HOT 1
- qcloud.uploader 如何支持上传视频? HOT 1
- 登录后去拿login信息时失败
- 求助大神:本地部署,登陆时报错Cannot read property 'user_info' of undefined HOT 2
- 生产环境上传图片拿到imageKey是扩展名 HOT 4
- Jest testEnvironment node throw error
- 关于多URL-信道的BUG HOT 1
- authorization (req) 似乎有个逻辑bug HOT 2
- 官方在DEMO中可否提供COS的DELETE接口?
- demo中的登录接口报 响应错误,{"code":-1,"error":"ERR_REQUEST_PARAM"} HOT 1
- 上传文件有bug,
- 无法从userInfo中获取openid
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wafer2-node-sdk.