bind-apple.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. initApple
  13. } = require('../../lib/third-party/index')
  14. /**
  15. * 绑定苹果账号
  16. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-apple
  17. * @param {Object} params
  18. * @param {String} params.identityToken 苹果登录返回identityToken
  19. * @returns
  20. */
  21. module.exports = async function (params = {}) {
  22. const schema = {
  23. identityToken: 'string'
  24. }
  25. this.middleware.validate(params, schema)
  26. const uid = this.authInfo.uid
  27. const {
  28. identityToken
  29. } = params
  30. const appleApi = initApple.call(this)
  31. let verifyResult
  32. try {
  33. verifyResult = await appleApi.verifyIdentityToken(identityToken)
  34. } catch (error) {
  35. await this.middleware.uniIdLog({
  36. success: false,
  37. type: LOG_TYPE.BIND_APPLE
  38. })
  39. throw {
  40. errCode: ERROR.GET_THIRD_PARTY_ACCOUNT_FAILED
  41. }
  42. }
  43. const {
  44. openid
  45. } = verifyResult
  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. }