forgetPassword.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <view class="setting-page">
  3. <view class="content">
  4. <view class="info-item">
  5. <text class="title">手机号码</text>
  6. <input class="input-field code-input" type="text" :password="!phone" v-model="phone"
  7. placeholder="请输入手机号码" />
  8. </view>
  9. <view class="info-item">
  10. <text class="title">验证码</text>
  11. <input class="input-field code-input" type="number" v-model="verifyCode"
  12. placeholder="请输入验证码" maxlength="6" />
  13. <view class="get-code-btn" @click="getVerifyCode">
  14. {{ codeText }}
  15. </view>
  16. </view>
  17. <view class="info-item">
  18. <text class="title">新密码</text>
  19. <input class="input-field code-input" type="text" :password="!showNewPassword" v-model="newPassword"
  20. placeholder="请输入8-20位字母、数字" />
  21. <view class="img-box" @click="toggleNewPassword">
  22. <image class="icon"
  23. :src="showNewPassword?'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png':'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png'"
  24. mode="aspectFill"></image>
  25. </view>
  26. </view>
  27. <view class="info-item">
  28. <text class="title">确认密码</text>
  29. <input class="input-field code-input" type="text" :password="!showConfirmPassword"
  30. v-model="confirmPassword" placeholder="请再次输入新密码" />
  31. <view class="img-box" @click="toggleConfirmPassword">
  32. <image class="icon"
  33. :src="showConfirmPassword?'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_visible.png':'https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/icon_invisible.png'"
  34. mode="aspectFill"></image>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="btn-box">
  39. <view class="confirm" @click="confirm">确认</view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import { forgetPassword } from '@/api/user.js'
  45. import {
  46. sendSmsCode,//发送短信验证码
  47. } from '@/api/user'
  48. export default {
  49. data() {
  50. return {
  51. phone: '',
  52. verifyCode: '', // 验证码
  53. codeText: '获取验证码', // 验证码按钮文字
  54. newPassword: '',
  55. confirmPassword: '',
  56. // 为每个密码框单独设置显示状态
  57. showNewPassword: false,
  58. showConfirmPassword: false,
  59. // 倒计时相关
  60. countdown: 0,
  61. countdownTimer: null
  62. }
  63. },
  64. onLoad(options) {
  65. // this.phone = options.phone || '';
  66. },
  67. methods: {
  68. // 修改密码的API调用
  69. async changePassword() {
  70. try {
  71. uni.showLoading({
  72. title: '修改中...'
  73. });
  74. // 构建请求参数
  75. const params = {
  76. phone: this.phone,
  77. code: this.codeText,
  78. newPassword: this.newPassword,
  79. confirmPassword: this.confirmPassword
  80. };
  81. // 调用修改密码接口
  82. const res = await forgetPassword(params);
  83. uni.hideLoading();
  84. if (res.code === 200) {
  85. uni.showToast({
  86. title: '密码修改成功',
  87. icon: 'success'
  88. });
  89. // 清空表单
  90. this.newPassword = '';
  91. this.confirmPassword = '';
  92. // 跳转到其他页面或返回
  93. setTimeout(() => {
  94. uni.navigateBack();
  95. }, 1500);
  96. } else {
  97. uni.showToast({
  98. title: res.message || '修改失败',
  99. icon: 'none'
  100. });
  101. }
  102. } catch (error) {
  103. uni.hideLoading();
  104. uni.showToast({
  105. title: error.message || '修改失败',
  106. icon: 'none'
  107. });
  108. }
  109. },
  110. getVerifyCode() {
  111. if (this.countdown > 0) {
  112. return;
  113. }
  114. if (!this.phone) {
  115. uni.showToast({
  116. icon: 'none',
  117. title: "请输入手机号",
  118. });
  119. return;
  120. }
  121. if (!/^1[3-9]\d{9}$/.test(this.phone)) {
  122. uni.showToast({
  123. icon: 'none',
  124. title: "请输入正确的手机号",
  125. });
  126. return;
  127. }
  128. // 调用发送验证码API
  129. uni.showLoading({
  130. title: "发送中..."
  131. });
  132. let params = {
  133. phone: this.phone,
  134. type: '1',
  135. }
  136. sendSmsCode(params)
  137. .then(res => {
  138. uni.hideLoading();
  139. if (res.code == 200) {
  140. uni.showToast({
  141. icon: 'success',
  142. title: "验证码已发送",
  143. });
  144. // 开始倒计时
  145. this.countdown = 60;
  146. this.countdownTimer = setInterval(() => {
  147. this.countdown--;
  148. this.codeText = this.countdown + '秒重新获取';
  149. if (this.countdown <= 0) {
  150. clearInterval(this.countdownTimer);
  151. this.countdownTimer = null;
  152. this.codeText = '获取验证码';
  153. }
  154. }, 1000);
  155. } else {
  156. uni.showToast({
  157. icon: 'none',
  158. title: res.message || "发送验证码失败",
  159. });
  160. }
  161. })
  162. .catch(err => {
  163. uni.hideLoading();
  164. uni.showToast({
  165. icon: 'none',
  166. title: "发送验证码接口调用失败",
  167. });
  168. });
  169. },
  170. toggleNewPassword() {
  171. this.showNewPassword = !this.showNewPassword;
  172. },
  173. toggleConfirmPassword() {
  174. this.showConfirmPassword = !this.showConfirmPassword;
  175. },
  176. confirm() {
  177. if (!this.newPassword) {
  178. uni.showToast({
  179. title: '请输入新密码',
  180. icon: 'none'
  181. });
  182. return;
  183. }
  184. // 密码格式验证
  185. const passwordRegex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]{8,20}$/;
  186. if (!passwordRegex.test(this.newPassword)) {
  187. uni.showToast({
  188. title: '密码需8-20位字母和数字组合',
  189. icon: 'none'
  190. });
  191. return;
  192. }
  193. if (this.newPassword !== this.confirmPassword) {
  194. uni.showToast({
  195. title: '两次输入的新密码不一致',
  196. icon: 'none'
  197. });
  198. return;
  199. }
  200. // 调用修改密码的API
  201. this.changePassword();
  202. },
  203. // 修改密码的API调用
  204. async changePassword() {
  205. try {
  206. uni.showLoading({
  207. title: '修改中...'
  208. });
  209. // 构建请求参数
  210. const params = {
  211. phone: this.phone,
  212. code: this.verifyCode,
  213. newPassword: this.newPassword,
  214. confirmPassword: this.confirmPassword
  215. };
  216. // 调用修改密码接口
  217. const res = await forgetPassword(params);
  218. uni.hideLoading();
  219. if (res.code === 200) {
  220. uni.showToast({
  221. title: '密码修改成功',
  222. icon: 'success'
  223. });
  224. // 清空表单
  225. this.newPassword = '';
  226. this.confirmPassword = '';
  227. // 跳转到其他页面或返回
  228. setTimeout(() => {
  229. uni.navigateBack();
  230. }, 1500);
  231. } else {
  232. uni.showToast({
  233. title: res.message || '修改失败',
  234. icon: 'none'
  235. });
  236. }
  237. } catch (error) {
  238. uni.hideLoading();
  239. uni.showToast({
  240. title: error.message || '修改失败',
  241. icon: 'none'
  242. });
  243. }
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .setting-page {
  250. background: #ffffff;
  251. min-height: 100vh;
  252. .content {
  253. background: #ffffff;
  254. }
  255. .info-item {
  256. height: 104upx;
  257. display: flex;
  258. align-items: center;
  259. justify-content: space-between;
  260. border-bottom: 1px solid #F5F6FA;
  261. .title {
  262. width: 160rpx;
  263. font-size: 28rpx;
  264. color: #666666;
  265. margin-left: 32rpx;
  266. }
  267. .input-field {
  268. width: 100%;
  269. font-size: 30rpx;
  270. font-family: PingFang SC;
  271. color: #333333;
  272. }
  273. .code-input {
  274. flex: 1;
  275. }
  276. .img-box {
  277. padding: 32rpx;
  278. .icon {
  279. width: 32rpx;
  280. height: 32rpx;
  281. }
  282. }
  283. .get-code-btn {
  284. margin-right: 32rpx;
  285. font-size: 28rpx;
  286. font-weight: 500;
  287. color: #157CF8;
  288. padding-left: 20rpx;
  289. white-space: nowrap;
  290. }
  291. }
  292. .btn-box {
  293. padding: 64upx 32upx;
  294. .confirm {
  295. width: 100%;
  296. height: 88upx;
  297. line-height: 88upx;
  298. text-align: center;
  299. font-family: PingFang SC;
  300. font-size: 32upx;
  301. color: #FFFFFF;
  302. background: #388BFF;
  303. border-radius: 44upx;
  304. }
  305. }
  306. }
  307. </style>