bind-alipay.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const {
  2. preBind,
  3. postBind
  4. } = require('../../lib/utils/relate')
  5. const {
  6. LOG_TYPE
  7. } = require('../../common/constants')
  8. const {
  9. ERROR
  10. } = require('../../common/error')
  11. const {
  12. initAlipay
  13. } = require('../../lib/third-party/index')
  14. /**
  15. * 绑定支付宝账号
  16. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-alipay
  17. * @param {Object} params
  18. * @param {String} params.code 支付宝小程序登录返回的code参数
  19. * @returns
  20. */
  21. module.exports = async function (params = {}) {
  22. const schema = {
  23. code: 'string'
  24. }
  25. this.middleware.validate(params, schema)
  26. const uid = this.authInfo.uid
  27. const {
  28. code
  29. } = params
  30. const alipayApi = initAlipay.call(this)
  31. let getAlipayAccountResult
  32. try {
  33. getAlipayAccountResult = await alipayApi().code2Session(code)
  34. } catch (error) {
  35. await this.middleware.uniIdLog({
  36. success: false,
  37. type: LOG_TYPE.BIND_ALIPAY
  38. })
  39. throw {
  40. errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
  41. }
  42. }
  43. const {
  44. openid
  45. } = getAlipayAccountResult
  46. const bindAccount = {
  47. apple_openid: openid
  48. }
  49. await preBind.call(this, {
  50. uid,
  51. bindAccount,
  52. logType: LOG_TYPE.BIND_APPLE
  53. })
  54. return postBind.call(this, {
  55. uid,
  56. bindAccount,
  57. extraData: {},
  58. logType: LOG_TYPE.BIND_APPLE
  59. })
  60. }