index.jsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { useState, useEffect } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import { useSelector } from 'react-redux'
  4. import withLayout from '@/layout'
  5. import { ScrollView, Input, Image, Block, Textarea } from '@tarojs/components'
  6. import { getImgURL } from '@/utils/image'
  7. import { fetch, uploadFiles } from '@/utils/request'
  8. import { UPDATE_USER_INFO, ROLE_CODE } from '@/constants/user'
  9. import { API_EDIT_AGENT, API_UPDATE_PHOTO } from '@/constants/api'
  10. import store from '@/store'
  11. import '@/assets/css/iconfont.css'
  12. import './index.scss'
  13. export default withLayout(() => {
  14. const { dispatch } = store
  15. const user = useSelector(state => state.user)
  16. const [UserInfo, setUserInfo] = useState({})
  17. const [UserRole, setUserRole] = useState(null) // 1-普通用户 2-经纪人 3-置业顾问 4-驻场管理
  18. const [FormData, setFormData] = useState({
  19. name: '',
  20. phone: '',
  21. description: ''
  22. })
  23. useEffect(() => {
  24. if (user?.userInfo?.person?.personId) {
  25. const person = user.userInfo.person
  26. setUserRole(person.personType === ROLE_CODE.CHANNEL_AGENT ? 2 : person.personType === ROLE_CODE.CONSULTANT ? 3 : person.personType === ROLE_CODE.MARKETING ? 4 : 1)
  27. }
  28. }, [user])
  29. useEffect(() => {
  30. setUserInfo(user?.userInfo?.person)
  31. setFormData({ name: user?.userInfo?.person.name, phone: user?.userInfo?.person.phone, description: user?.userInfo?.person.description })
  32. }, [user])
  33. const Save = () => {
  34. let params = ''
  35. if(UserRole === 3) {
  36. params = `name=${FormData.name}&phone=${FormData.phone}&description=${FormData.description}`
  37. } else {
  38. params = `name=${FormData.name}&phone=${FormData.phone}`
  39. }
  40. fetch({ url: `${API_EDIT_AGENT}?${params}`, method: 'put' }).then(() => {
  41. Taro.showToast({
  42. title: '修改成功',
  43. icon: 'none'
  44. })
  45. dispatch({ type: UPDATE_USER_INFO, payload: { name: FormData.name, phone: FormData.phone, description: FormData.description } })
  46. // login({ path: '', scene: '' })
  47. })
  48. }
  49. const FormChange = (key, e) => {
  50. let Data = { ...FormData }
  51. Data[key] = e.detail.value
  52. setFormData(Data)
  53. }
  54. const ToUploadImg = () => {
  55. if (UserRole - 0 === 3) {
  56. Taro.chooseImage({
  57. count: 1,
  58. success: function (res) {
  59. if (res.errMsg === 'chooseImage:ok') {
  60. Taro.showToast({ title: '正在上传...', icon: 'loading', mask: true, duration: 10000 })
  61. uploadFiles(res.tempFilePaths).then((subRes) => {
  62. fetch({ url: API_UPDATE_PHOTO, payload: { photoUrl: subRes[0] }, method: 'put' }).then(() => {
  63. Taro.hideToast()
  64. Taro.showToast({ title: '修改成功', icon: 'none', duration: 2000 })
  65. dispatch({ type: UPDATE_USER_INFO, payload: { userPhoto: subRes[0] } })
  66. })
  67. })
  68. }
  69. }
  70. })
  71. }
  72. }
  73. return (
  74. <view className='Page UserInfo'>
  75. <ScrollView scroll-y>
  76. <view className='PageContent'>
  77. <text>头像</text>
  78. <view className='FormLine flex-h' onClick={ToUploadImg}>
  79. <view className='flex-item'>
  80. <view className='Icon'>
  81. <Image mode='aspectFill' src={getImgURL(UserInfo.userPhoto || UserInfo.avatarurl)}></Image>
  82. </view>
  83. </view>
  84. {/* <text className='iconfont icon-jiantouright'></text> */}
  85. </view>
  86. <text>用户名</text>
  87. <view className='FormLine flex-h'>
  88. <view className='flex-item'>
  89. <Input placeholder='请输入用户名' value={FormData.name} onInput={FormChange.bind(this, 'name')}></Input>
  90. </view>
  91. </view>
  92. <text>手机号码</text>
  93. <view className='FormLine flex-h'>
  94. <view className='flex-item'>
  95. <Input placeholder='请输入手机号码' value={FormData.phone} onInput={FormChange.bind(this, 'phone')}></Input>
  96. </view>
  97. </view>
  98. {
  99. true &&
  100. <Block>
  101. <text>简介</text>
  102. <view className='FormLine flex-h'>
  103. <view className='flex-item'>
  104. <Textarea auto-height placeholder='请输入简介' value={FormData.description} onInput={FormChange.bind(this, 'description')} />
  105. </view>
  106. </view>
  107. </Block>
  108. }
  109. </view>
  110. <view className='Btn'>
  111. <text onClick={Save}>保存</text>
  112. </view>
  113. </ScrollView>
  114. </view>
  115. )
  116. })