首次完整推送,

V:1.20240808.006
This commit is contained in:
fm453
2024-08-13 18:32:37 +08:00
parent 15be3e9373
commit c62d15b288
939 changed files with 111777 additions and 0 deletions

View File

@ -0,0 +1,63 @@
const {
preBind,
postBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE
} = require('../../common/constants')
const {
ERROR
} = require('../../common/error')
const {
initAlipay
} = require('../../lib/third-party/index')
/**
* 绑定支付宝账号
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-alipay
* @param {Object} params
* @param {String} params.code 支付宝小程序登录返回的code参数
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
code: 'string'
}
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
const {
code
} = params
const alipayApi = initAlipay.call(this)
let getAlipayAccountResult
try {
getAlipayAccountResult = await alipayApi().code2Session(code)
} catch (error) {
await this.middleware.uniIdLog({
success: false,
type: LOG_TYPE.BIND_ALIPAY
})
throw {
errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
}
}
const {
openid
} = getAlipayAccountResult
const bindAccount = {
ali_openid: openid
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_APPLE
})
return postBind.call(this, {
uid,
bindAccount,
extraData: {},
logType: LOG_TYPE.BIND_APPLE
})
}

View File

@ -0,0 +1,62 @@
const {
preBind,
postBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE
} = require('../../common/constants')
const {
ERROR
} = require('../../common/error')
const {
initApple
} = require('../../lib/third-party/index')
/**
* 绑定苹果账号
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-apple
* @param {Object} params
* @param {String} params.identityToken 苹果登录返回identityToken
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
identityToken: 'string'
}
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
const {
identityToken
} = params
const appleApi = initApple.call(this)
let verifyResult
try {
verifyResult = await appleApi.verifyIdentityToken(identityToken)
} catch (error) {
await this.middleware.uniIdLog({
success: false,
type: LOG_TYPE.BIND_APPLE
})
throw {
errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
}
}
const {
openid
} = verifyResult
const bindAccount = {
apple_openid: openid
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_APPLE
})
return postBind.call(this, {
uid,
bindAccount,
extraData: {},
logType: LOG_TYPE.BIND_APPLE
})
}

View File

@ -0,0 +1,104 @@
const {
preBind,
postBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE
} = require('../../common/constants')
const {
decryptWeixinData,
getWeixinCache, getWeixinAccessToken
} = require('../../lib/utils/weixin')
const { initWeixin } = require('../../lib/third-party')
const { ERROR } = require('../../common/error')
/**
* 通过微信绑定手机号
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-mp-weixin
* @param {Object} params
* @param {String} params.encryptedData 微信获取手机号返回的加密信息
* @param {String} params.iv 微信获取手机号返回的初始向量
* @param {String} params.code 微信获取手机号返回的code
* @returns
*/
module.exports = async function (params = {}) {
/**
* 微信小程序的规则是客户端应先使用checkSession接口检测上次获取的sessionKey是否仍有效
* 如果有效则直接使用上次存储的sessionKey即可
* 如果无效应重新调用login接口再次刷新sessionKey
* 因此此接口不应直接使用客户端login获取的code只能使用缓存的sessionKey
*/
const schema = {
encryptedData: {
required: false,
type: 'string'
},
iv: {
required: false,
type: 'string'
},
code: {
required: false,
type: 'string'
}
}
const {
encryptedData,
iv,
code
} = params
this.middleware.validate(params, schema)
if ((!encryptedData && !iv) && !code) {
return {
errCode: ERROR.INVALID_PARAM
}
}
const uid = this.authInfo.uid
let mobile
if (code) {
// 区分客户端类型 小程序还是App
const accessToken = await getWeixinAccessToken.call(this)
const weixinApi = initWeixin.call(this)
const res = await weixinApi.getPhoneNumber(accessToken, code)
mobile = res.purePhoneNumber
} else {
const sessionKey = await getWeixinCache.call(this, {
uid,
key: 'session_key'
})
if (!sessionKey) {
throw new Error('Session key not found')
}
const res = decryptWeixinData.call(this, {
encryptedData,
sessionKey,
iv
})
mobile = res.purePhoneNumber
}
const bindAccount = {
mobile
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_MOBILE
})
await postBind.call(this, {
uid,
bindAccount,
extraData: {
mobile_confirmed: 1
},
logType: LOG_TYPE.BIND_MOBILE
})
return {
errCode: 0
}
}

View File

@ -0,0 +1,92 @@
const {
getNeedCaptcha,
verifyCaptcha
} = require('../../lib/utils/captcha')
const {
LOG_TYPE,
SMS_SCENE,
CAPTCHA_SCENE
} = require('../../common/constants')
const {
verifyMobileCode
} = require('../../lib/utils/verify-code')
const {
preBind,
postBind
} = require('../../lib/utils/relate')
/**
* 通过短信验证码绑定手机号
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-sms
* @param {Object} params
* @param {String} params.mobile 手机号
* @param {String} params.code 短信验证码
* @param {String} params.captcha 图形验证码
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
mobile: 'mobile',
code: 'string',
captcha: {
type: 'string',
required: false
}
}
const {
mobile,
code,
captcha
} = params
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
// 判断是否需要验证码
const needCaptcha = await getNeedCaptcha.call(this, {
uid,
type: LOG_TYPE.BIND_MOBILE
})
if (needCaptcha) {
await verifyCaptcha.call(this, {
captcha,
scene: CAPTCHA_SCENE.BIND_MOBILE_BY_SMS
})
}
try {
// 验证手机号验证码,验证不通过时写入失败日志
await verifyMobileCode({
mobile,
code,
scene: SMS_SCENE.BIND_MOBILE_BY_SMS
})
} catch (error) {
await this.middleware.uniIdLog({
data: {
user_id: uid
},
type: LOG_TYPE.BIND_MOBILE,
success: false
})
throw error
}
const bindAccount = {
mobile
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_MOBILE
})
await postBind.call(this, {
uid,
bindAccount,
extraData: {
mobile_confirmed: 1
},
logType: LOG_TYPE.BIND_MOBILE
})
return {
errCode: 0
}
}

View File

@ -0,0 +1,70 @@
const {
getPhoneNumber
} = require('../../lib/utils/univerify')
const {
LOG_TYPE
} = require('../../common/constants')
const {
preBind,
postBind
} = require('../../lib/utils/relate')
/**
* 通过一键登录绑定手机号
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-univerify
* @param {Object} params
* @param {String} params.openid APP端一键登录返回的openid
* @param {String} params.access_token APP端一键登录返回的access_token
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
openid: 'string',
access_token: 'string'
}
const {
openid,
// eslint-disable-next-line camelcase
access_token
} = params
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
let mobile
try {
const phoneInfo = await getPhoneNumber.call(this, {
// eslint-disable-next-line camelcase
access_token,
openid
})
mobile = phoneInfo.phoneNumber
} catch (error) {
await this.middleware.uniIdLog({
success: false,
data: {
user_id: uid
},
type: LOG_TYPE.BIND_MOBILE
})
throw error
}
const bindAccount = {
mobile
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_MOBILE
})
await postBind.call(this, {
uid,
bindAccount,
extraData: {
mobile_confirmed: 1
},
logType: LOG_TYPE.BIND_MOBILE
})
return {
errCode: 0
}
}

View File

@ -0,0 +1,110 @@
const {
preBind,
postBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE
} = require('../../common/constants')
const {
ERROR
} = require('../../common/error')
const {
initQQ
} = require('../../lib/third-party/index')
const {
generateQQCache,
getQQPlatform,
saveQQUserKey
} = require('../../lib/utils/qq')
/**
* 绑定QQ
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-qq
* @param {Object} params
* @param {String} params.code 小程序端QQ登录返回的code
* @param {String} params.accessToken APP端QQ登录返回的accessToken
* @param {String} params.accessTokenExpired accessToken过期时间由App端QQ登录返回的expires_in参数计算而来单位毫秒
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
code: {
type: 'string',
required: false
},
accessToken: {
type: 'string',
required: false
},
accessTokenExpired: {
type: 'number',
required: false
}
}
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
const {
code,
accessToken,
accessTokenExpired
} = params
const qqPlatform = getQQPlatform.call(this)
const appId = this.getUniversalClientInfo().appId
const qqApi = initQQ.call(this)
const clientPlatform = this.clientPlatform
const apiName = clientPlatform === 'mp-qq' ? 'code2Session' : 'getOpenidByToken'
let getQQAccountResult
try {
getQQAccountResult = await qqApi[apiName]({
code,
accessToken
})
} catch (error) {
await this.middleware.uniIdLog({
success: false,
type: LOG_TYPE.BIND_QQ
})
throw {
errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
}
}
const {
openid,
unionid,
// 保存下面四个字段
sessionKey // 微信小程序用户sessionKey
} = getQQAccountResult
const bindAccount = {
qq_openid: {
[qqPlatform]: openid
},
qq_unionid: unionid
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_QQ
})
await saveQQUserKey.call(this, {
openid,
sessionKey,
accessToken,
accessTokenExpired
})
return postBind.call(this, {
uid,
bindAccount,
extraData: {
qq_openid: {
[`${qqPlatform}_${appId}`]: openid
},
...generateQQCache.call(this, {
openid,
sessionKey
})
},
logType: LOG_TYPE.BIND_QQ
})
}

View File

@ -0,0 +1,100 @@
const {
preBind,
postBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE
} = require('../../common/constants')
const {
generateWeixinCache,
saveWeixinUserKey,
getWeixinPlatform
} = require('../../lib/utils/weixin')
const {
initWeixin
} = require('../../lib/third-party/index')
const {
ERROR
} = require('../../common/error')
/**
* 绑定微信
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-weixin
* @param {Object} params
* @param {String} params.code 微信登录返回的code
* @returns
*/
module.exports = async function (params = {}) {
const schema = {
code: 'string'
}
this.middleware.validate(params, schema)
const uid = this.authInfo.uid
const {
code
} = params
const weixinPlatform = getWeixinPlatform.call(this)
const appId = this.getUniversalClientInfo().appId
const weixinApi = initWeixin.call(this)
const clientPlatform = this.clientPlatform
const apiName = clientPlatform === 'mp-weixin' ? 'code2Session' : 'getOauthAccessToken'
let getWeixinAccountResult
try {
getWeixinAccountResult = await weixinApi[apiName](code)
} catch (error) {
await this.middleware.uniIdLog({
success: false,
type: LOG_TYPE.BIND_WEIXIN
})
throw {
errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
}
}
const {
openid,
unionid,
// 保存下面四个字段
sessionKey, // 微信小程序用户sessionKey
accessToken, // App端微信用户accessToken
refreshToken, // App端微信用户refreshToken
expired: accessTokenExpired // App端微信用户accessToken过期时间
} = getWeixinAccountResult
const bindAccount = {
wx_openid: {
[weixinPlatform]: openid
},
wx_unionid: unionid
}
await preBind.call(this, {
uid,
bindAccount,
logType: LOG_TYPE.BIND_WEIXIN
})
await saveWeixinUserKey.call(this, {
openid,
sessionKey,
accessToken,
refreshToken,
accessTokenExpired
})
return postBind.call(this, {
uid,
bindAccount,
extraData: {
wx_openid: {
[`${weixinPlatform}_${appId}`]: openid
},
...generateWeixinCache.call(this, {
openid,
sessionKey, // 微信小程序用户sessionKey
accessToken, // App端微信用户accessToken
refreshToken, // App端微信用户refreshToken
accessTokenExpired // App端微信用户accessToken过期时间
})
},
logType: LOG_TYPE.BIND_WEIXIN
})
}

View File

@ -0,0 +1,13 @@
module.exports = {
bindMobileBySms: require('./bind-mobile-by-sms'),
bindMobileByUniverify: require('./bind-mobile-by-univerify'),
bindMobileByMpWeixin: require('./bind-mobile-by-mp-weixin'),
bindAlipay: require('./bind-alipay'),
bindApple: require('./bind-apple'),
bindQQ: require('./bind-qq'),
bindWeixin: require('./bind-weixin'),
unbindWeixin: require('./unbind-weixin'),
unbindAlipay: require('./unbind-alipay'),
unbindQQ: require('./unbind-qq'),
unbindApple: require('./unbind-apple')
}

View File

@ -0,0 +1,32 @@
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
/**
* 解绑支付宝
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-alipay
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
await preUnBind.call(this, {
uid,
unBindAccount: {
ali_openid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_ALIPAY
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
ali_openid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_ALIPAY
})
}

View File

@ -0,0 +1,32 @@
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
/**
* 解绑apple
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-apple
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
await preUnBind.call(this, {
uid,
unBindAccount: {
apple_openid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_APPLE
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
apple_openid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_APPLE
})
}

View File

@ -0,0 +1,33 @@
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
/**
* 解绑QQ
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-qq
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
await preUnBind.call(this, {
uid,
unBindAccount: {
qq_openid: dbCmd.exists(true),
qq_unionid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_QQ
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
qq_openid: dbCmd.remove(),
qq_unionid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_QQ
})
}

View File

@ -0,0 +1,38 @@
const {
preUnBind,
postUnBind
} = require('../../lib/utils/relate')
const {
LOG_TYPE, dbCmd
} = require('../../common/constants')
const {
getWeixinPlatform
} = require('../../lib/utils/weixin')
/**
* 解绑微信
* @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#unbind-weixin
* @returns
*/
module.exports = async function () {
const { uid } = this.authInfo
// const weixinPlatform = getWeixinPlatform.call(this)
await preUnBind.call(this, {
uid,
unBindAccount: {
wx_openid: dbCmd.exists(true),
wx_unionid: dbCmd.exists(true)
},
logType: LOG_TYPE.UNBIND_WEIXIN
})
return await postUnBind.call(this, {
uid,
unBindAccount: {
wx_openid: dbCmd.remove(),
wx_unionid: dbCmd.remove()
},
logType: LOG_TYPE.UNBIND_WEIXIN
})
}