index.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view class=" pwd-retrieve-container my-page">
  3. <form>
  4. <view class="uni-form-item uni-row">
  5. <view class="title">旧密码</view>
  6. <input :class="'uni-input '+oldPassword" name="oldPassword" :password="true" :value="user.oldPassword"
  7. placeholder="请输入旧密码" @focus="inputFocus('oldPassword')" @blur="inputBlur('oldPassword')"
  8. @input="inputOld" />
  9. </view>
  10. <view class="uni-form-item uni-row">
  11. <view class="title">新密码</view>
  12. <input :class="'uni-input '+newPassword" name="newPassword" :password="true" :value="user.newPassword"
  13. placeholder="请输入新密码" @focus="inputFocus('newPassword')" @blur="inputBlur('newPassword')"
  14. @input="inputNew" />
  15. </view>
  16. <view class="uni-form-item uni-row">
  17. <view class="title">确认密码</view>
  18. <input :class="'uni-input '+confirmPassword" name="confirmPassword" :password="true"
  19. :value="user.confirmPassword" placeholder="请确认新密码" @focus="inputFocus('confirmPassword')"
  20. @blur="inputBlur('confirmPassword')" @input="inputConfirm" />
  21. </view>
  22. <button type="primary" @click="submit">提交</button>
  23. </form>
  24. </view>
  25. </template>
  26. <script lang="uts">
  27. import { updateUserPwd } from "@/api/system/user"
  28. import { updateData, getList } from '@/api/work';
  29. import { getCurrentUserSync, getCurrentUserNameSync } from '@/utils/auth.uts'
  30. type User = {
  31. oldPassword : string | null,
  32. newPassword : string | null,
  33. confirmPassword : string | null
  34. }
  35. export default {
  36. data() {
  37. return {
  38. oldPassword: '',
  39. newPassword: '',
  40. confirmPassword: '',
  41. user: {
  42. oldPassword: null,
  43. newPassword: null,
  44. confirmPassword: null
  45. } as User,
  46. }
  47. },
  48. methods: {
  49. inputFocus(tag : string) {
  50. this.$data[tag] = "my-focus"
  51. },
  52. inputBlur(tag : string) {
  53. this.$data[tag] = "my-blur"
  54. },
  55. inputOld(event : UniInputEvent) {
  56. this.user.oldPassword = event.detail.value
  57. },
  58. inputNew(event : UniInputEvent) {
  59. this.user.newPassword = event.detail.value
  60. },
  61. inputConfirm(event : UniInputEvent) {
  62. this.user.confirmPassword = event.detail.value
  63. },
  64. submit() {
  65. if (this.user.oldPassword == null) {
  66. uni.showToast({
  67. title: "旧密码不能为空",
  68. icon: "error"
  69. })
  70. return
  71. }
  72. if (this.user.newPassword == null) {
  73. uni.showToast({
  74. title: "新密码不能为空",
  75. icon: "error"
  76. })
  77. return
  78. } else if (this.user.newPassword != null && (this.user.newPassword.length < 6 || this.user.newPassword.length > 20)) {
  79. uni.showToast({
  80. title: "长度在6到20个字符",
  81. icon: "error"
  82. })
  83. return
  84. }
  85. if (this.user.oldPassword == this.user.newPassword) {
  86. uni.showToast({
  87. title: "新旧密码不能相同",
  88. icon: "error"
  89. })
  90. return
  91. }
  92. if (this.user.confirmPassword == null) {
  93. uni.showToast({
  94. title: "确认密码不能为空",
  95. icon: "error"
  96. })
  97. return
  98. } else if (this.user.confirmPassword != this.user.newPassword) {
  99. uni.showToast({
  100. title: "两次输入的密码不一致",
  101. icon: "error"
  102. })
  103. return
  104. }
  105. let username = '';
  106. let oldPassword = '';
  107. getList('app_user', 'username', getCurrentUserNameSync(), null, null, null).then((res : UTSJSONObject) => {
  108. let userList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  109. console.log(userList);
  110. if (userList != null && userList.length > 0) {
  111. let user = userList[0];
  112. username = user.getString('username') ?? '';
  113. oldPassword = user.getString('password') ?? '';
  114. }
  115. // 添加逻辑判断:验证原密码是否匹配
  116. if (oldPassword == this.user.oldPassword) {
  117. let updatedData = "password='" + this.user.confirmPassword + "' ";
  118. updateData('app_user', updatedData, 'username', username).then((res : UTSJSONObject) => {
  119. let data = res?.['data'] as boolean ?? false;
  120. if (data != null && data == true) {
  121. uni.showToast({
  122. title: "更新成功!",
  123. });
  124. setTimeout(() => {
  125. }, 1500)
  126. uni.reLaunch({
  127. url: '/pages/mine/index'
  128. });
  129. } else {
  130. uni.showToast({
  131. title: "更新失败,请稍后重试!",
  132. icon: "error"
  133. });
  134. }
  135. });
  136. } else {
  137. uni.showToast({
  138. title: "更新失败,原密码不匹配!",
  139. icon: "error"
  140. });
  141. }
  142. });
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss">
  148. .my-page {
  149. background-color: #ffffff;
  150. width: 100%;
  151. height: 100%;
  152. }
  153. .pwd-retrieve-container {
  154. padding-top: 36rpx;
  155. padding: 15px;
  156. }
  157. </style>