index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {
  2. Cell,
  3. Form,
  4. Input,
  5. NavBar,
  6. Popup,
  7. Button,
  8. } from "@nutui/nutui-react-taro";
  9. import { View } from "@tarojs/components";
  10. import { ArrowLeft, ArrowRight } from "@nutui/icons-react-taro";
  11. import { getStorageSync, navigateBack, showToast } from "@tarojs/taro";
  12. import {
  13. changePasswordApi,
  14. sendChangePasswordCodeApi,
  15. } from "../../../api/auth";
  16. import "./index.scss";
  17. import { useEffect, useState } from "react";
  18. export default function Setting() {
  19. const [showBottom, setShowBottom] = useState(false);
  20. const [form] = Form.useForm();
  21. const [formData, setFormData] = useState({
  22. user_name: "",
  23. user_pwd: "",
  24. phone: "",
  25. code: "",
  26. });
  27. const [countdown, setCountdown] = useState(0);
  28. // 获取验证码
  29. const handleGetCode = async () => {
  30. if (countdown > 0) return;
  31. if (!formData.phone) {
  32. showToast({
  33. icon: "none",
  34. title: "请输入手机号码",
  35. duration: 2000,
  36. });
  37. return;
  38. }
  39. // 校验手机号格式
  40. const phoneRegex = /^1[3-9]\d{9}$/;
  41. if (!phoneRegex.test(formData.phone)) {
  42. showToast({
  43. icon: "none",
  44. title: "请输入正确的手机号",
  45. });
  46. return;
  47. }
  48. try {
  49. // 调用发送验证码接口
  50. await sendChangePasswordCodeApi({
  51. phone: formData.phone,
  52. });
  53. // 这里可以调用发送验证码的API
  54. showToast({
  55. title: "验证码已发送",
  56. duration: 2000,
  57. icon: "none",
  58. });
  59. setCountdown(60);
  60. } catch (error: any) {
  61. showToast({
  62. title: error.message,
  63. duration: 2000,
  64. icon: "none",
  65. });
  66. }
  67. };
  68. const handleSubmit = async (data: any) => {
  69. await changePasswordApi({
  70. user_name: data.user_name,
  71. user_pwd: data.user_pwd,
  72. phone: data.phone,
  73. code: data.code.detail.value,
  74. });
  75. showToast({
  76. title: "修改密码成功",
  77. duration: 2000,
  78. icon: "none",
  79. });
  80. setShowBottom(false);
  81. };
  82. useEffect(() => {
  83. let info = getStorageSync("vipInfo");
  84. setFormData({
  85. user_name: info.user_name,
  86. phone: info.phone,
  87. code: "",
  88. user_pwd: "",
  89. });
  90. form.setFieldValue("user_name", info.user_name);
  91. form.setFieldValue("phone", info.phone);
  92. }, []);
  93. return (
  94. <View>
  95. <NavBar
  96. back={
  97. <>
  98. <ArrowLeft />
  99. 返回
  100. </>
  101. }
  102. fixed
  103. onBackClick={(e) => {
  104. e.preventDefault();
  105. navigateBack({ delta: 1 });
  106. }}
  107. >
  108. 设置
  109. </NavBar>
  110. <Cell.Group className="pt-[40px]">
  111. <Cell
  112. className="nutui-cell-clickable"
  113. title="修改密码"
  114. align="center"
  115. extra={<ArrowRight />}
  116. onClick={() => {
  117. setShowBottom(true);
  118. }}
  119. />
  120. </Cell.Group>
  121. <Popup
  122. visible={showBottom}
  123. position="bottom"
  124. onClose={() => {
  125. setShowBottom(false);
  126. }}
  127. >
  128. <View>
  129. <Form form={form} onFinish={(data) => handleSubmit(data)}>
  130. <Form.Item label="账号" name="user_name">
  131. <Input
  132. placeholder="请输入账号"
  133. readOnly={true}
  134. value={formData.user_name}
  135. onChange={(value) =>
  136. setFormData({ ...formData, user_name: value })
  137. }
  138. />
  139. </Form.Item>
  140. <Form.Item label="新密码" name="user_pwd">
  141. <Input
  142. placeholder="请输入新密码"
  143. value={formData.user_pwd}
  144. onChange={(value) =>
  145. setFormData({ ...formData, user_pwd: value })
  146. }
  147. />
  148. </Form.Item>
  149. <Form.Item label="手机号码" name="phone">
  150. <Input
  151. placeholder="请输入手机号码"
  152. readOnly={true}
  153. value={formData.phone}
  154. onChange={(value) => setFormData({ ...formData, phone: value })}
  155. />
  156. </Form.Item>
  157. <Form.Item label="验证码" name="code">
  158. <View className="flex items-center">
  159. <Input
  160. placeholder="请输入验证码"
  161. value={formData.code}
  162. onChange={(value) =>
  163. setFormData({ ...formData, code: value })
  164. }
  165. />
  166. <Button
  167. className="w-[100px] h-[40px] text-[14px] text-[#333]"
  168. disabled={countdown > 0}
  169. onClick={handleGetCode}
  170. size="small"
  171. type="primary"
  172. >
  173. {countdown > 0 ? `${countdown}s` : "获取验证码"}
  174. </Button>
  175. </View>
  176. </Form.Item>
  177. <Form.Item>
  178. <Button
  179. block
  180. type="primary"
  181. size="large"
  182. onClick={() => form.submit}
  183. >
  184. 提交
  185. </Button>
  186. </Form.Item>
  187. </Form>
  188. </View>
  189. </Popup>
  190. </View>
  191. );
  192. }