AuthAvatar.jsx 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import React, { useState } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { View, OpenData } from '@tarojs/components'
  4. import AuthBox from './AuthBox'
  5. import useAuth from './useAuth'
  6. import './style.scss'
  7. export default (props) => {
  8. const [loading, setLoading] = useState(false)
  9. const { updateUserInfo } = useAuth()
  10. const handleUserProfile = (detail) => {
  11. const { errMsg, ...data } = detail || {}
  12. if (errMsg === 'getUserProfile:ok') {
  13. setLoading(true)
  14. updateUserInfo(data).then(() => {
  15. setLoading(false)
  16. }).catch((err) => {
  17. console.error(err)
  18. setLoading(false)
  19. })
  20. } else {
  21. console.error(errMsg);
  22. Taro.showToast({
  23. title: '授权头像失败',
  24. icon: 'none',
  25. duration: 2000
  26. })
  27. }
  28. }
  29. return (
  30. <AuthBox title='授权头像' openType='getUserInfo' loading={loading} onGetUserInfo={handleUserProfile}>
  31. <View className='auth-avatar'>
  32. <OpenData type='userAvatarUrl' />
  33. </View>
  34. <View className='auth-nickname'>
  35. <OpenData type='userNickName' />
  36. </View>
  37. </AuthBox>
  38. )
  39. }