forget.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <script setup>
  2. import { ref, reactive, watch } from "vue";
  3. import { post, get } from "@/utils/request";
  4. import { useRequest } from "@/composables/useRequest";
  5. const SendCodeByPhone = (params) => post("/Common/SendCodeByPhone", params);
  6. const ResetPwd = (params) => post("/Login/ResetPwd", params);
  7. // 响应式表单数据
  8. const form = ref({
  9. mobile: "",
  10. code: "",
  11. password: "",
  12. });
  13. // 校验规则
  14. const rules = {
  15. mobile: [
  16. {
  17. required: true,
  18. message: "请输入用户名或手机号",
  19. trigger: ["change", "blur"],
  20. },
  21. {
  22. // 自定义验证函数,见上说明
  23. validator: (rule, value, callback) => {
  24. // 上面有说,返回true表示校验通过,返回false表示不通过
  25. // uni.$u.test.mobile()就是返回true或者false的
  26. return uni.$u.test.mobile(value);
  27. },
  28. message: "手机号码不正确",
  29. // 触发器可以同时用blur和change
  30. trigger: ["change", "blur"],
  31. },
  32. ],
  33. code: [
  34. {
  35. required: true,
  36. message: "请输入验证码",
  37. trigger: ["change", "blur"],
  38. },
  39. ],
  40. password: [
  41. {
  42. required: true,
  43. message: "请输入密码",
  44. trigger: ["change", "blur"],
  45. },
  46. ],
  47. };
  48. const tips = ref("");
  49. const value = ref("");
  50. const seconds = ref(60);
  51. const uCodeRef = ref(null);
  52. watch(value, (newValue, oldValue) => {
  53. // console.log('v-model', newValue);
  54. });
  55. const codeChange = (text) => {
  56. tips.value = text;
  57. };
  58. const getCode = async () => {
  59. // console.log('uCodeRef.canGetCode: ', uCodeRef.value);
  60. if (uCodeRef.value.canGetCode) {
  61. // 模拟向后端请求验证码
  62. uni.showLoading({
  63. title: "正在获取验证码",
  64. });
  65. const data = await SendCodeByPhone({ phone: form.value.mobile });
  66. console.log("data: ", data);
  67. if (data) {
  68. setTimeout(() => {
  69. uni.hideLoading();
  70. // 这里此提示会被start()方法中的提示覆盖
  71. uni.$u.toast("验证码已发送");
  72. // 通知验证码组件内部开始倒计时
  73. uCodeRef.value.start();
  74. }, 2000);
  75. } else {
  76. uni.hideLoading();
  77. }
  78. } else {
  79. uni.$u.toast("倒计时结束后再发送");
  80. }
  81. };
  82. const uFormRef = ref(null);
  83. function submit() {
  84. uFormRef.value
  85. .validate()
  86. .then((valid) => {
  87. if (valid) {
  88. // uni.$u.toast('校验通过');
  89. refresh({
  90. phone: form.value.mobile,
  91. code: form.value.code,
  92. newPassword: form.value.password,
  93. });
  94. } else {
  95. // uni.$u.toast('校验失败');
  96. }
  97. })
  98. .catch(() => {
  99. // 处理验证错误
  100. // uni.$u.toast('校验失败');
  101. });
  102. }
  103. // 使用组合函数
  104. const { data, loading, error, refresh } = useRequest(ResetPwd, {
  105. initialParams: {
  106. phone: form.value.mobile,
  107. code: form.value.code,
  108. newPassword: form.value.password,
  109. },
  110. immediate: false, // 默认立即执行
  111. });
  112. const token = ref("");
  113. watch([loading, error], () => {
  114. if (!loading.value) {
  115. // 加载完成
  116. if (error.value) {
  117. console.log("请求失败: ", error.value); // 打印错误信息
  118. } else {
  119. console.log("请求成功: ", data.value); // 成功时获取数据
  120. uni.$u.toast("修改成功");
  121. setTimeout(() => {
  122. uni.navigateBack();
  123. }, 1500);
  124. }
  125. }
  126. });
  127. const customStyle = {
  128. height: "84px",
  129. "border-radius": "8px",
  130. background: "#f7f8fa",
  131. padding: "0 20px !important",
  132. position: "relative",
  133. };
  134. </script>
  135. <template>
  136. <view>
  137. <view class="company u-flex u-flex-y-center">
  138. <image src="/static/images/logo.png" class="company-icon" mode="widthFix"></image>
  139. <view class="company-head">
  140. <view class="name">华都渠道管家</view>
  141. <view class="into">专业的房产销售管理平台</view>
  142. </view>
  143. </view>
  144. <view class="account">
  145. <view>
  146. <up-form :model="form" :rules="rules" ref="uFormRef">
  147. <up-form-item label="" prop="mobile">
  148. <up-input :customStyle="customStyle" v-model="form.mobile" border="none" placeholder="请输入用户名或手机号" />
  149. </up-form-item>
  150. <up-form-item label="" prop="code">
  151. <up-input :customStyle="customStyle" v-model="form.code" border="none" placeholder="请输入验证码">
  152. <template #suffix>
  153. <up-code ref="uCodeRef" @change="codeChange" :seconds="seconds"></up-code>
  154. <up-text :text="tips" @click="getCode" class="code-text"></up-text>
  155. </template>
  156. </up-input>
  157. </up-form-item>
  158. <up-form-item label="" prop="password">
  159. <up-input :customStyle="customStyle" v-model="form.password" type="password" border="none"
  160. placeholder="新密码" />
  161. </up-form-item>
  162. </up-form>
  163. </view>
  164. </view>
  165. <button type="primary" class="regiset-btn" @click="submit">重置密码</button>
  166. <navigator url="/pages/login/regist" class="account-tip">
  167. 还没有账号?
  168. <text>立即注册</text>
  169. </navigator>
  170. </view>
  171. </template>
  172. <style>
  173. page {
  174. padding: px2rpx(10) px2rpx(52);
  175. box-sizing: border-box;
  176. }
  177. </style>
  178. <style lang="scss" scoped>
  179. @import "@/uni.scss";
  180. button {
  181. background-color: #ea002a;
  182. font-size: px2rpx(14);
  183. font-weight: normal;
  184. height: px2rpx(84);
  185. line-height: px2rpx(84);
  186. }
  187. .company {
  188. padding: px2rpx(40) 0;
  189. }
  190. .company-icon {
  191. width: px2rpx(104);
  192. height: px2rpx(104);
  193. margin-right: px2rpx(14);
  194. }
  195. .company-head {
  196. .name {
  197. font-size: px2rpx(20);
  198. font-weight: 500;
  199. color: #ea002a;
  200. margin-bottom: px2rpx(2);
  201. }
  202. .into {
  203. font-size: px2rpx(13);
  204. font-weight: 350;
  205. color: #222222;
  206. }
  207. }
  208. .account {
  209. .input {
  210. height: px2rpx(84);
  211. border-radius: px2rpx(8);
  212. background: #f7f8fa;
  213. padding: 0 px2rpx(20) !important;
  214. }
  215. }
  216. .regiset-btn {
  217. margin: px2rpx(20) 0;
  218. }
  219. .account-tip {
  220. color: #666666;
  221. font-size: px2rpx(13);
  222. text-align: center;
  223. text {
  224. color: #ea002a;
  225. }
  226. }
  227. .code-text {
  228. padding-right: px2rpx(20);
  229. }
  230. :deep(.u-text__value) {
  231. color: #ea002a !important;
  232. font-size: px2rpx(14) !important;
  233. }
  234. </style>