index.uvue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. onBackPress() {
  49. // 覆盖系统默认的返回行为,返回到指定页面
  50. uni.navigateTo({
  51. url: `/pages/mine/index`,
  52. // 修改动画方向为从左到右退回
  53. animationType: 'slide-in-left', // 使用从左到右滑出的动画效果
  54. animationDuration: 300 // 动画持续时间,单位ms
  55. })
  56. // 返回true表示拦截默认返回行为
  57. return true
  58. },
  59. methods: {
  60. inputFocus(tag : string) {
  61. this.$data[tag] = "my-focus"
  62. },
  63. inputBlur(tag : string) {
  64. this.$data[tag] = "my-blur"
  65. },
  66. inputOld(event : UniInputEvent) {
  67. this.user.oldPassword = event.detail.value
  68. },
  69. inputNew(event : UniInputEvent) {
  70. this.user.newPassword = event.detail.value
  71. },
  72. inputConfirm(event : UniInputEvent) {
  73. this.user.confirmPassword = event.detail.value
  74. },
  75. submit() {
  76. if (this.user.oldPassword == null) {
  77. uni.showToast({
  78. title: "旧密码不能为空",
  79. icon: "error"
  80. })
  81. return
  82. }
  83. if (this.user.newPassword == null) {
  84. uni.showToast({
  85. title: "新密码不能为空",
  86. icon: "error"
  87. })
  88. return
  89. } else if (this.user.newPassword != null && (this.user.newPassword.length < 6 || this.user.newPassword.length > 20)) {
  90. uni.showToast({
  91. title: "长度在6到20个字符",
  92. icon: "error"
  93. })
  94. return
  95. }
  96. if (this.user.oldPassword == this.user.newPassword) {
  97. uni.showToast({
  98. title: "新旧密码不能相同",
  99. icon: "error"
  100. })
  101. return
  102. }
  103. if (this.user.confirmPassword == null) {
  104. uni.showToast({
  105. title: "确认密码不能为空",
  106. icon: "error"
  107. })
  108. return
  109. } else if (this.user.confirmPassword != this.user.newPassword) {
  110. uni.showToast({
  111. title: "两次输入的密码不一致",
  112. icon: "error"
  113. })
  114. return
  115. }
  116. let username = '';
  117. let oldPassword = '';
  118. getList('app_user', 'username', getCurrentUserNameSync(), null, null, null).then((res : UTSJSONObject) => {
  119. let userList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  120. console.log(userList);
  121. if (userList != null && userList.length > 0) {
  122. let user = userList[0];
  123. username = user.getString('username') ?? '';
  124. oldPassword = user.getString('password') ?? '';
  125. }
  126. // 添加逻辑判断:验证原密码是否匹配
  127. if (oldPassword == this.user.oldPassword) {
  128. let updatedData = "password='" + this.user.confirmPassword + "' ";
  129. updateData('app_user', updatedData, 'username', username).then((res : UTSJSONObject) => {
  130. let data = res?.['data'] as boolean ?? false;
  131. if (data != null && data == true) {
  132. uni.showToast({
  133. title: "更新成功!",
  134. });
  135. setTimeout(() => {
  136. }, 1500)
  137. uni.reLaunch({
  138. url: '/pages/mine/index'
  139. });
  140. } else {
  141. uni.showToast({
  142. title: "更新失败,请稍后重试!",
  143. icon: "error"
  144. });
  145. }
  146. });
  147. } else {
  148. uni.showToast({
  149. title: "更新失败,原密码不匹配!",
  150. icon: "error"
  151. });
  152. }
  153. });
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss">
  159. .my-page {
  160. background-color: #ffffff;
  161. width: 100%;
  162. height: 100%;
  163. }
  164. .pwd-retrieve-container {
  165. padding-top: 36rpx;
  166. padding: 15px;
  167. }
  168. </style>