password.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view class="password-container">
  3. <u-form
  4. :model="formData"
  5. :rules="rules"
  6. ref="formRef"
  7. labelPosition="top"
  8. :borderBottom="true"
  9. labelWidth="300px"
  10. >
  11. <u-form-item
  12. label="原密码"
  13. prop="oldPassword"
  14. borderBottom
  15. >
  16. <u-input
  17. v-model="formData.oldPassword"
  18. type="password"
  19. placeholder="请输入原密码"
  20. :password="!showPassword.old"
  21. :border="false"
  22. >
  23. <template #suffix>
  24. <u-icon
  25. :name="showPassword.old ? 'eye-fill' : 'eye-off'"
  26. size="20"
  27. color="#909399"
  28. @click="togglePasswordVisible('old')"
  29. ></u-icon>
  30. </template>
  31. </u-input>
  32. </u-form-item>
  33. <u-form-item
  34. label="新密码"
  35. prop="newPassword"
  36. borderBottom
  37. >
  38. <u-input
  39. v-model="formData.newPassword"
  40. type="password"
  41. placeholder="请输入新密码"
  42. :password="!showPassword.new"
  43. :border="false"
  44. >
  45. <template #suffix>
  46. <u-icon
  47. :name="showPassword.new ? 'eye-fill' : 'eye-off'"
  48. size="20"
  49. color="#909399"
  50. @click="togglePasswordVisible('new')"
  51. ></u-icon>
  52. </template>
  53. </u-input>
  54. </u-form-item>
  55. <u-form-item
  56. label="确认新密码"
  57. prop="confirmPassword"
  58. borderBottom
  59. >
  60. <u-input
  61. v-model="formData.confirmPassword"
  62. type="password"
  63. placeholder="请再次输入新密码"
  64. :password="!showPassword.confirm"
  65. :border="false"
  66. >
  67. <template #suffix>
  68. <u-icon
  69. :name="showPassword.confirm ? 'eye-fill' : 'eye-off'"
  70. size="20"
  71. color="#909399"
  72. @click="togglePasswordVisible('confirm')"
  73. ></u-icon>
  74. </template>
  75. </u-input>
  76. </u-form-item>
  77. </u-form>
  78. <view class="submit-btn">
  79. <u-button
  80. type="primary"
  81. text="修改密码"
  82. @click="submitForm"
  83. :loading="loading"
  84. ></u-button>
  85. </view>
  86. </view>
  87. </template>
  88. <script setup>
  89. import { ref, reactive } from 'vue'
  90. // 表单引用
  91. const formRef = ref(null)
  92. // 加载状态
  93. const loading = ref(false)
  94. // 密码显示状态
  95. const showPassword = reactive({
  96. old: false,
  97. new: false,
  98. confirm: false
  99. })
  100. // 表单数据
  101. const formData = reactive({
  102. oldPassword: '',
  103. newPassword: '',
  104. confirmPassword: ''
  105. })
  106. // 表单验证规则
  107. const rules = {
  108. oldPassword: [{
  109. required: true,
  110. message: '请输入原密码',
  111. trigger: ['blur', 'change']
  112. }, {
  113. min: 6,
  114. message: '密码长度不能小于6位',
  115. trigger: ['blur', 'change']
  116. }],
  117. newPassword: [{
  118. required: true,
  119. message: '请输入新密码',
  120. trigger: ['blur', 'change']
  121. }, {
  122. min: 6,
  123. message: '密码长度不能小于6位',
  124. trigger: ['blur', 'change']
  125. }, {
  126. validator: (rule, value, callback) => {
  127. if (value === formData.oldPassword) {
  128. callback(new Error('新密码不能与原密码相同'))
  129. } else {
  130. callback()
  131. }
  132. },
  133. trigger: ['blur', 'change']
  134. }],
  135. confirmPassword: [{
  136. required: true,
  137. message: '请再次输入新密码',
  138. trigger: ['blur', 'change']
  139. }, {
  140. validator: (rule, value, callback) => {
  141. if (value !== formData.newPassword) {
  142. callback(new Error('两次输入的密码不一致'))
  143. } else {
  144. callback()
  145. }
  146. },
  147. trigger: ['blur', 'change']
  148. }]
  149. }
  150. // 切换密码显示状态
  151. function togglePasswordVisible(field) {
  152. showPassword[field] = !showPassword[field]
  153. }
  154. // 提交表单
  155. function submitForm() {
  156. if (!formRef.value) return
  157. formRef.value.validate(valid => {
  158. if (valid) {
  159. loading.value = true
  160. // 这里添加修改密码的API调用
  161. setTimeout(() => {
  162. uni.showToast({
  163. title: '密码修改成功',
  164. icon: 'success'
  165. })
  166. loading.value = false
  167. // 重置表单
  168. resetForm()
  169. // 可以添加成功后的跳转
  170. // uni.navigateBack()
  171. }, 1500)
  172. }
  173. })
  174. }
  175. // 重置表单
  176. function resetForm() {
  177. if (!formRef.value) return
  178. formRef.value.resetFields()
  179. }
  180. </script>
  181. <style>
  182. page{
  183. background-color: #ffffff;
  184. }
  185. </style>
  186. <style lang="scss" scoped>
  187. .password-container {
  188. padding: 30rpx;
  189. box-sizing: border-box;
  190. :deep(.u-form) {
  191. .u-input {
  192. &__content {
  193. background-color: #f5f5f5;
  194. border-radius: 8rpx;
  195. padding: 20rpx;
  196. }
  197. }
  198. }
  199. .submit-btn {
  200. margin-top: 60rpx;
  201. padding: 0 20rpx;
  202. :deep(.u-button) {
  203. height: 88rpx;
  204. border-radius: 44rpx;
  205. }
  206. }
  207. }
  208. </style>