AuthAvatar.jsx 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 { onCancel = (x => x) } = props;
  9. const [loading, setLoading] = useState(false)
  10. const { updateUserInfo } = useAuth()
  11. const handleUserProfile = (detail) => {
  12. const { errMsg, ...data } = detail || {}
  13. if (errMsg === 'getUserProfile:ok') {
  14. setLoading(true)
  15. updateUserInfo(data).then(() => {
  16. setLoading(false)
  17. }).catch((err) => {
  18. console.error(err)
  19. setLoading(false)
  20. })
  21. } else {
  22. console.error(errMsg);
  23. Taro.showToast({
  24. title: '授权头像失败',
  25. icon: 'none',
  26. duration: 2000
  27. })
  28. }
  29. }
  30. return (
  31. <AuthBox
  32. title='授权头像'
  33. openType='getUserInfo'
  34. loading={loading}
  35. onGetUserInfo={handleUserProfile}
  36. onCancel={onCancel}
  37. >
  38. <View className='auth-avatar'>
  39. <OpenData type='userAvatarUrl' />
  40. </View>
  41. <View className='auth-nickname'>
  42. <OpenData type='userNickName' />
  43. </View>
  44. </AuthBox>
  45. )
  46. }