get-account-info.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const {
  2. userCollection
  3. } = require('../../common/constants')
  4. const {
  5. ERROR
  6. } = require('../../common/error')
  7. function isUsernameSet (userRecord) {
  8. return !!userRecord.username
  9. }
  10. function isNicknameSet (userRecord) {
  11. return !!userRecord.nickname
  12. }
  13. function isPasswordSet (userRecord) {
  14. return !!userRecord.password
  15. }
  16. function isMobileBound (userRecord) {
  17. return !!(userRecord.mobile && userRecord.mobile_confirmed)
  18. }
  19. function isEmailBound (userRecord) {
  20. return !!(userRecord.email && userRecord.email_confirmed)
  21. }
  22. function isWeixinBound (userRecord) {
  23. return !!(
  24. userRecord.wx_unionid ||
  25. Object.keys(userRecord.wx_openid || {}).length
  26. )
  27. }
  28. function isQQBound (userRecord) {
  29. return !!(
  30. userRecord.qq_unionid ||
  31. Object.keys(userRecord.qq_openid || {}).length
  32. )
  33. }
  34. function isAlipayBound (userRecord) {
  35. return !!userRecord.ali_openid
  36. }
  37. function isAppleBound (userRecord) {
  38. return !!userRecord.apple_openid
  39. }
  40. /**
  41. * 获取账户账户简略信息
  42. * @tutorial https://uniapp.dcloud.net.cn/uniCloud/uni-id-pages.html#get-account-info
  43. */
  44. module.exports = async function () {
  45. const {
  46. uid
  47. } = this.authInfo
  48. const getUserRes = await userCollection.doc(uid).get()
  49. const userRecord = getUserRes && getUserRes.data && getUserRes.data[0]
  50. if (!userRecord) {
  51. throw {
  52. errCode: ERROR.ACCOUNT_NOT_EXISTS
  53. }
  54. }
  55. return {
  56. errCode: 0,
  57. isUsernameSet: isUsernameSet(userRecord),
  58. isNicknameSet: isNicknameSet(userRecord),
  59. isPasswordSet: isPasswordSet(userRecord),
  60. isMobileBound: isMobileBound(userRecord),
  61. isEmailBound: isEmailBound(userRecord),
  62. isWeixinBound: isWeixinBound(userRecord),
  63. isQQBound: isQQBound(userRecord),
  64. isAlipayBound: isAlipayBound(userRecord),
  65. isAppleBound: isAppleBound(userRecord)
  66. }
  67. }