index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="change-page">
  3. <text class="hint">修改成功后请使用新密码登录。</text>
  4. <view class="form">
  5. <input class="input" v-model="oldPassword" placeholder="当前密码" password />
  6. <input class="input" v-model="newPassword" placeholder="新密码(须含字母与数字)" password />
  7. <input class="input" v-model="confirmPassword" placeholder="确认新密码" password />
  8. <view class="submit" :class="{ disabled: loading }" @click="onSubmit">
  9. <text>{{ loading ? '提交中...' : '确认修改' }}</text>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { getToken, setToken, changePasswordLoggedIn } from '../../utils/api'
  16. function passwordMeetsRule(pwd) {
  17. const s = String(pwd || '')
  18. return /[A-Za-z]/.test(s) && /\d/.test(s)
  19. }
  20. export default {
  21. data() {
  22. return {
  23. oldPassword: '',
  24. newPassword: '',
  25. confirmPassword: '',
  26. loading: false
  27. }
  28. },
  29. onShow() {
  30. if (!getToken()) {
  31. uni.showToast({ title: '请先登录', icon: 'none' })
  32. setTimeout(() => {
  33. uni.reLaunch({ url: '/pages/login/index' })
  34. }, 80)
  35. }
  36. },
  37. methods: {
  38. async onSubmit() {
  39. if (this.loading) return
  40. const oldPwd = String(this.oldPassword || '')
  41. const newPwd = String(this.newPassword || '')
  42. const confirm = String(this.confirmPassword || '')
  43. if (!oldPwd) {
  44. uni.showToast({ title: '请输入当前密码', icon: 'none' })
  45. return
  46. }
  47. if (!newPwd) {
  48. uni.showToast({ title: '请输入新密码', icon: 'none' })
  49. return
  50. }
  51. if (!passwordMeetsRule(newPwd)) {
  52. uni.showToast({ title: '新密码须同时包含字母与数字', icon: 'none' })
  53. return
  54. }
  55. if (newPwd !== confirm) {
  56. uni.showToast({ title: '两次新密码不一致', icon: 'none' })
  57. return
  58. }
  59. if (oldPwd === newPwd) {
  60. uni.showToast({ title: '新密码不能与当前密码相同', icon: 'none' })
  61. return
  62. }
  63. this.loading = true
  64. try {
  65. await changePasswordLoggedIn(oldPwd, newPwd)
  66. uni.showToast({ title: '修改成功,请重新登录', icon: 'success' })
  67. setToken('')
  68. try {
  69. uni.removeStorageSync('current_user')
  70. } catch (e) {}
  71. setTimeout(() => {
  72. uni.reLaunch({ url: '/pages/login/index' })
  73. }, 500)
  74. } catch (e) {
  75. uni.showToast({ title: e.message || '修改失败', icon: 'none' })
  76. } finally {
  77. this.loading = false
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. <style scoped>
  84. .change-page {
  85. min-height: 100vh;
  86. background: #fff;
  87. padding: 32rpx 48rpx 64rpx;
  88. box-sizing: border-box;
  89. }
  90. .hint {
  91. display: block;
  92. font-size: 26rpx;
  93. color: #888;
  94. line-height: 1.5;
  95. margin-bottom: 32rpx;
  96. }
  97. .form {
  98. margin-top: 8rpx;
  99. }
  100. .input {
  101. height: 88rpx;
  102. padding: 0 24rpx;
  103. background: #f7f7f7;
  104. border-radius: 16rpx;
  105. font-size: 28rpx;
  106. margin-bottom: 20rpx;
  107. }
  108. .submit {
  109. margin-top: 32rpx;
  110. height: 92rpx;
  111. border-radius: 18rpx;
  112. background: #259653;
  113. display: flex;
  114. align-items: center;
  115. justify-content: center;
  116. color: #fff;
  117. font-size: 30rpx;
  118. }
  119. .submit.disabled {
  120. opacity: 0.7;
  121. }
  122. </style>