index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view class="change-password-page">
  3. <nav-bar title="修改密码" bgColor="#fff" color="#000"></nav-bar>
  4. <view class="form-container">
  5. <uni-forms ref="formRef" :model="formData" :rules="rules">
  6. <view class="form-group">
  7. <uni-forms-item label="旧密码" name="oldPassword" required label-width="160rpx">
  8. <uni-easyinput type="password" v-model="formData.oldPassword" placeholder="请输入当前密码"
  9. :inputBorder="false" />
  10. </uni-forms-item>
  11. <view class="line"></view>
  12. <uni-forms-item label="新密码" name="newPassword" required label-width="160rpx">
  13. <uni-easyinput type="password" v-model="formData.newPassword" placeholder="6-20位新密码"
  14. :inputBorder="false" />
  15. </uni-forms-item>
  16. <view class="line"></view>
  17. <uni-forms-item label="确认密码" name="confirmPassword" required label-width="160rpx">
  18. <uni-easyinput type="password" v-model="formData.confirmPassword" placeholder="请再次输入新密码"
  19. :inputBorder="false" />
  20. </uni-forms-item>
  21. </view>
  22. </uni-forms>
  23. <view class="btn-group">
  24. <button class="submit-btn" @click="submit">提交修改</button>
  25. </view>
  26. <view class="tip-text">
  27. <uni-icons type="info" size="14" color="#999"></uni-icons>
  28. <text>密码修改成功后,系统将自动安全退出,需重新登录。</text>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script setup>
  34. import { ref, reactive } from 'vue'
  35. import navBar from '@/components/nav-bar/index.vue'
  36. import { updateUserPwd } from '@/api/system/user'
  37. const formRef = ref(null)
  38. const formData = reactive({
  39. oldPassword: '',
  40. newPassword: '',
  41. confirmPassword: ''
  42. })
  43. const rules = {
  44. oldPassword: {
  45. rules: [{ required: true, errorMessage: '请输入旧密码' }]
  46. },
  47. newPassword: {
  48. rules: [
  49. { required: true, errorMessage: '请输入新密码' },
  50. { minLength: 6, maxLength: 20, errorMessage: '密码长度在 6-20 位之间' }
  51. ]
  52. },
  53. confirmPassword: {
  54. rules: [
  55. { required: true, errorMessage: '请确认新密码' },
  56. {
  57. validateFunction: function (rule, value, data, callback) {
  58. if (value !== data.newPassword) {
  59. callback('两次输入的密码不一致')
  60. }
  61. return true
  62. }
  63. }
  64. ]
  65. }
  66. }
  67. const submit = async () => {
  68. try {
  69. await formRef.value.validate()
  70. uni.showLoading({ title: '修改中...' })
  71. await updateUserPwd(formData.oldPassword, formData.newPassword)
  72. uni.hideLoading()
  73. uni.showToast({ title: '密码修改成功,请重新登录', icon: 'success' })
  74. // 清除令牌并跳转回登录页
  75. setTimeout(() => {
  76. uni.removeStorageSync('token')
  77. uni.reLaunch({ url: '/pages/login/index' })
  78. }, 1500)
  79. } catch (err) {
  80. uni.hideLoading()
  81. console.log('表单校验失败或请求失败', err)
  82. }
  83. }
  84. </script>
  85. <style lang="scss" scoped>
  86. .change-password-page {
  87. min-height: 100vh;
  88. background-color: #f7f8fa;
  89. padding-top: 20rpx;
  90. }
  91. .form-group {
  92. background: #fff;
  93. margin: 0 24rpx;
  94. border-radius: 20rpx;
  95. padding: 0 32rpx;
  96. }
  97. .line {
  98. height: 1rpx;
  99. background: #f5f5f5;
  100. margin: 0;
  101. }
  102. .btn-group {
  103. margin: 60rpx 32rpx 32rpx;
  104. }
  105. .submit-btn {
  106. height: 88rpx;
  107. line-height: 88rpx;
  108. background: linear-gradient(90deg, #ffd53f, #ff9500);
  109. color: #333;
  110. border: none;
  111. border-radius: 44rpx;
  112. font-size: 30rpx;
  113. font-weight: bold;
  114. }
  115. .tip-text {
  116. padding: 0 40rpx;
  117. display: flex;
  118. align-items: center;
  119. gap: 12rpx;
  120. font-size: 24rpx;
  121. color: #999;
  122. }
  123. :deep(.uni-forms-item) {
  124. border: none !important;
  125. padding: 10rpx 0;
  126. }
  127. </style>