ProfileSetting.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Buttons from "@/components/basic/Buttons";
  2. import { clear, logout, update } from "@/services/user";
  3. import { View, Text, Image, Button } from "@tarojs/components";
  4. import { useDispatch, useSelector } from "react-redux";
  5. import './ProfileSetting.scss'
  6. import Box from "@/components/layout/Box";
  7. import Taro from "@tarojs/taro";
  8. import { useEffect } from "react";
  9. import { useTranslation } from "react-i18next";
  10. import { baseUrl } from "@/services/http/api";
  11. import { updateSuccess } from "@/store/user";
  12. import TableCell from "@/components/layout/TableCell";
  13. export default function Page() {
  14. const dispatch = useDispatch();
  15. const user = useSelector((state: any) => state.user);
  16. const { t } = useTranslation()
  17. useEffect(() => {
  18. Taro.setNavigationBarTitle({
  19. title: t('page.user_profile.title')
  20. })
  21. }, [])
  22. function editNickname() {
  23. Taro.navigateTo({
  24. url: '/pages/account/EditPage'
  25. })
  26. }
  27. function chooseAvatar(e) {
  28. const { avatarUrl } = e.detail
  29. var dot = avatarUrl.lastIndexOf('.')
  30. var fileExt = dot > 0 ? avatarUrl.substring(dot) : ''
  31. // console.log(avatarUrl)
  32. Taro.request({
  33. method: 'GET',
  34. url: `${baseUrl}/api/thirdparty/aliyun/oss-form-upload`,
  35. header: {
  36. 'Authorization': 'bearer ' + global.token
  37. },
  38. data: {
  39. type: 'AVATAR',
  40. file_ext: fileExt
  41. },
  42. success: (rsp) => {
  43. debugger
  44. Taro.uploadFile({
  45. url: rsp.data.upload_url,
  46. filePath: avatarUrl,
  47. name: 'file',
  48. formData: rsp.data.fields,
  49. success: rlt => {
  50. console.log(rlt)
  51. uploadAvatar(rsp.data.view_url)
  52. // _this.changeAvatar(rsp.data.view_url);
  53. },
  54. fail: rlt => {
  55. Taro.showModal({
  56. content: rlt.errMsg
  57. })
  58. }
  59. })
  60. }
  61. })
  62. }
  63. function uploadAvatar(url) {
  64. update({ avatar: url }).then(res => {
  65. dispatch(updateSuccess({ avatar: url }));
  66. Taro.showToast({
  67. title: '更新成功',
  68. icon: 'none'
  69. })
  70. })
  71. }
  72. return <View className="container">
  73. {/* <View className="row">
  74. <Text>头像</Text>
  75. <Image src={user.avatar} className="avatar" />
  76. <Button className="btn" openType="chooseAvatar" onChooseAvatar={chooseAvatar}></Button>
  77. </View> */}
  78. <View className="avatar_row">
  79. <View className="avatar_bg">
  80. <Image src={user.avatar} className="avatar" />
  81. <View className="camera_bg">
  82. <Image className="camera" src={require('@assets/images/camera.png')} />
  83. </View>
  84. <Button className="btn" openType="chooseAvatar" onChooseAvatar={chooseAvatar}></Button>
  85. </View>
  86. </View>
  87. <View onClick={editNickname}>
  88. <TableCell title="昵称" showArrow={true} showMarginBottom={true}>
  89. <Text style={{ opacity: 0.8 }}>{user.nickname}</Text>
  90. </TableCell>
  91. </View>
  92. {/* <View className="row" onClick={editNickname}>
  93. <Text>昵称</Text>
  94. <Text>{user.nickname}</Text>
  95. </View> */}
  96. {/* <View className="row">
  97. <Text>用户名</Text>
  98. <Text>{user.username}</Text>
  99. </View> */}
  100. {/* <Buttons title='退出登录' onClick={logoutF} style={{ width: 289, marginBottom: 30 }} />
  101. <Buttons title='注销账号' onClick={clearF} style={{ width: 289, marginBottom: 30 }} /> */}
  102. </View>
  103. }