index.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Component } from "react";
  2. import { View, Text, Image,Input, Button } from "@tarojs/components";
  3. import { AtInput } from "taro-ui";
  4. import Taro from "@tarojs/taro";
  5. import { updateUserInfo,getUserInfo } from "../../../service";
  6. import avatar from "../../../images/mine/avatar.png";
  7. import "./index.less";
  8. export default class Index extends Component {
  9. state = {
  10. wxAccount: "",
  11. nickName: "",
  12. phone: "",
  13. avatar: "",
  14. };
  15. componentDidMount() {
  16. this.getUserInfo();
  17. }
  18. // 获取用户信息
  19. async getUserInfo() {
  20. const res = await getUserInfo();
  21. this.setState({
  22. wxAccount: res.wechat_account,
  23. nickName: res.name,
  24. phone: res.phone,
  25. avatar: res.icon,
  26. });
  27. }
  28. // 微信账号
  29. handleAccountChange(value) {
  30. this.setState({
  31. wxAccount: value,
  32. });
  33. return value;
  34. }
  35. // 昵称
  36. handleNickNameChange(value) {
  37. this.setState({
  38. nickName: value,
  39. });
  40. return value;
  41. }
  42. // 手机号
  43. handlePhoneChange(value) {
  44. this.setState({
  45. phone: value,
  46. });
  47. return value;
  48. }
  49. // 选择头像
  50. handleChooseAvatar = (e) => {
  51. this.setState({
  52. avatar: e.detail.avatarUrl,
  53. });
  54. }
  55. // 提交
  56. handleSubmit = async () => {
  57. const phoneRegex = /^1[3-9]\d{9}$/; // 中国大陆手机号正则
  58. if (!this.state.nickName || !this.state.phone) {
  59. Taro.showToast({
  60. title: "请输入完整信息",
  61. icon: "none",
  62. });
  63. return;
  64. } else if (!phoneRegex.test(this.state.phone)) {
  65. Taro.showToast({
  66. title: "请输入正确的手机号",
  67. icon: "none",
  68. });
  69. return;
  70. }
  71. const res = await updateUserInfo({
  72. icon: this.state.avatar,
  73. name: this.state.nickName,
  74. phone: this.state.phone,
  75. wechat_account: this.state.wxAccount,
  76. });
  77. Taro.showToast({
  78. title: '修改成功',
  79. icon: "none",
  80. });
  81. };
  82. render() {
  83. return (
  84. <View className="index">
  85. {/* 头像 */}
  86. <View className="info-edit">
  87. <Image
  88. src={this.state.avatar||avatar}
  89. className="info-edit-img"
  90. />
  91. {/* <Button onChooseAvatar={this.handleChooseAvatar} open-type="chooseAvatar" className="info-edit-title">更换头像</Button> */}
  92. </View>
  93. {/* 内容 */}
  94. <View className="info-edit-content">
  95. <AtInput
  96. name="value"
  97. title="微信账号"
  98. type="text"
  99. placeholder="请输入微信账号"
  100. value={this.state.wxAccount}
  101. onChange={this.handleAccountChange.bind(this)}
  102. />
  103. <AtInput
  104. name="value"
  105. title="鱼市昵称"
  106. type="text"
  107. placeholder="请输入鱼市昵称"
  108. value={this.state.nickName}
  109. onChange={this.handleNickNameChange.bind(this)}
  110. />
  111. <AtInput
  112. border={false}
  113. name="value"
  114. title="手机号"
  115. type="number"
  116. placeholder="请输入手机号"
  117. value={this.state.phone}
  118. onChange={this.handlePhoneChange.bind(this)}
  119. />
  120. </View>
  121. {/* 提交按钮 */}
  122. <View onClick={this.handleSubmit} className="info-edit-submit">
  123. <Text className="info-edit-submit-text">提交</Text>
  124. </View>
  125. </View>
  126. );
  127. }
  128. }