index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { useState } from "react";
  2. import { Image, Label, View } from "@tarojs/components";
  3. import Taro, { FC } from "@tarojs/taro";
  4. import { AtButton, AtForm, AtInput, AtTextarea } from "taro-ui";
  5. import BgImg from "../../images/feedback@2x-min.png";
  6. import { handleValidate } from "../../../utils";
  7. import { getBaseURL } from "@dage/service";
  8. import { Rules } from "async-validator";
  9. import { feedbackApi } from "../../../api";
  10. import { FooterProtocol } from "../../../components/FooterProtocol";
  11. import "./index.scss";
  12. const DEFAULT_PARAMS = {
  13. name: "",
  14. phone: "",
  15. content: "",
  16. randCode: "",
  17. };
  18. const formRules: Rules = {
  19. name: [{ required: true, message: "请输入称呼" }],
  20. phone: [{ required: true, message: "请输入联系方式" }],
  21. content: [{ required: true, message: "请输入反馈内容" }],
  22. randCode: [
  23. { required: true, message: "请输入验证码" },
  24. {
  25. validator: (_, value) => {
  26. return value.length === 5;
  27. },
  28. message: "验证码输入有误",
  29. },
  30. ],
  31. };
  32. const FeedBackPage: FC = () => {
  33. const [checked, setChecked] = useState(false);
  34. const [params, setParams] = useState({ ...DEFAULT_PARAMS });
  35. const [timestamp, setTimestamp] = useState(new Date().getTime());
  36. const [loading, setLoading] = useState(false);
  37. const handleSubmit = async (event, readProtocol = false) => {
  38. if (!(await handleValidate(params, formRules))) return;
  39. if (!checked && !readProtocol) {
  40. Taro.showModal({
  41. title: "提示",
  42. content: "是否已阅读并同意《用户服务协议》及《个人信息保护政策》",
  43. confirmText: "同意",
  44. success: (res) => {
  45. if (res.confirm) {
  46. setChecked(true);
  47. handleSubmit(undefined, true);
  48. }
  49. },
  50. });
  51. return;
  52. }
  53. try {
  54. setLoading(true);
  55. await feedbackApi(params);
  56. setParams({ ...DEFAULT_PARAMS });
  57. setTimestamp(new Date().getTime());
  58. Taro.showToast({
  59. title: "提交成功",
  60. icon: "success",
  61. });
  62. } finally {
  63. setLoading(false);
  64. }
  65. };
  66. return (
  67. <View className="feedback-page">
  68. <View className="feedback">
  69. <AtForm className="feedback-main" onSubmit={handleSubmit}>
  70. <View className="feedback-main__form">
  71. <Label className="feedback__label">您的称呼</Label>
  72. <AtInput
  73. name="name"
  74. className="feedback__input"
  75. maxLength={20}
  76. placeholder="请输入内容,20字以内"
  77. value={params.name}
  78. onChange={(val) =>
  79. setParams({
  80. ...params,
  81. name: val as string,
  82. })
  83. }
  84. />
  85. <Label className="feedback__label">联系方式</Label>
  86. <AtInput
  87. name="phone"
  88. type="phone"
  89. className="feedback__input"
  90. maxLength={20}
  91. placeholder="请输入内容,20字以内"
  92. value={params.phone}
  93. onChange={(val) =>
  94. setParams({
  95. ...params,
  96. phone: val as string,
  97. })
  98. }
  99. />
  100. <Label className="feedback__label">反馈内容</Label>
  101. <AtTextarea
  102. value={params.content}
  103. className="feedback__input textarea"
  104. maxLength={200}
  105. placeholder="请输入内容,200字以内"
  106. onChange={(val) =>
  107. setParams({
  108. ...params,
  109. content: val,
  110. })
  111. }
  112. />
  113. </View>
  114. <View className="feedback-main__form">
  115. <Label className="feedback__label">验证码</Label>
  116. <View className="code">
  117. <AtInput
  118. name="randCode"
  119. value={params.randCode}
  120. className="feedback__input"
  121. maxLength={5}
  122. placeholder="请输入内容"
  123. onChange={(val) =>
  124. setParams({
  125. ...params,
  126. randCode: val as string,
  127. })
  128. }
  129. />
  130. <Image
  131. className="code__img"
  132. src={`${getBaseURL()}/api/show/getRandCode?t=${timestamp}`}
  133. onClick={() => setTimestamp(new Date().getTime())}
  134. />
  135. </View>
  136. <View className="toolbar">
  137. <AtButton
  138. className="toolbar__btn cancel"
  139. onClick={() => {
  140. Taro.navigateBack();
  141. }}
  142. >
  143. 取消
  144. </AtButton>
  145. <AtButton
  146. className="toolbar__btn submit"
  147. formType="submit"
  148. loading={loading}
  149. >
  150. 提交
  151. </AtButton>
  152. </View>
  153. </View>
  154. <FooterProtocol checked={checked} setChecked={setChecked} />
  155. </AtForm>
  156. <Image src={BgImg} className="feedback__bg" mode="widthFix" />
  157. </View>
  158. </View>
  159. );
  160. };
  161. export default FeedBackPage;