bind-mobile-by-univerify.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const {
  2. getPhoneNumber
  3. } = require('../../lib/utils/univerify')
  4. const {
  5. LOG_TYPE
  6. } = require('../../common/constants')
  7. const {
  8. preBind,
  9. postBind
  10. } = require('../../lib/utils/relate')
  11. /**
  12. * 通过一键登录绑定手机号
  13. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#bind-mobile-by-univerify
  14. * @param {Object} params
  15. * @param {String} params.openid APP端一键登录返回的openid
  16. * @param {String} params.access_token APP端一键登录返回的access_token
  17. * @returns
  18. */
  19. module.exports = async function (params = {}) {
  20. const schema = {
  21. openid: 'string',
  22. access_token: 'string'
  23. }
  24. const {
  25. openid,
  26. // eslint-disable-next-line camelcase
  27. access_token
  28. } = params
  29. this.middleware.validate(params, schema)
  30. const uid = this.authInfo.uid
  31. let mobile
  32. try {
  33. const phoneInfo = await getPhoneNumber.call(this, {
  34. // eslint-disable-next-line camelcase
  35. access_token,
  36. openid
  37. })
  38. mobile = phoneInfo.phoneNumber
  39. } catch (error) {
  40. await this.middleware.uniIdLog({
  41. success: false,
  42. data: {
  43. user_id: uid
  44. },
  45. type: LOG_TYPE.BIND_MOBILE
  46. })
  47. throw error
  48. }
  49. const bindAccount = {
  50. mobile
  51. }
  52. await preBind.call(this, {
  53. uid,
  54. bindAccount,
  55. logType: LOG_TYPE.BIND_MOBILE
  56. })
  57. await postBind.call(this, {
  58. uid,
  59. bindAccount,
  60. extraData: {
  61. mobile_confirmed: 1
  62. },
  63. logType: LOG_TYPE.BIND_MOBILE
  64. })
  65. return {
  66. errCode: 0
  67. }
  68. }