index.uvue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="container">
  3. <view class="download-card">
  4. <form>
  5. <view class="uni-form-item uni-row">
  6. <view class="title">旧密码</view>
  7. <input :class="'uni-input '+oldPassword" name="oldPassword" :password="true" :value="user.oldPassword"
  8. placeholder="请输入旧密码" @focus="inputFocus('oldPassword')" @blur="inputBlur('oldPassword')"
  9. @input="inputOld" />
  10. </view>
  11. <view class="uni-form-item uni-row">
  12. <view class="title">新密码</view>
  13. <input :class="'uni-input '+newPassword" name="newPassword" :password="true" :value="user.newPassword"
  14. placeholder="请输入新密码" @focus="inputFocus('newPassword')" @blur="inputBlur('newPassword')"
  15. @input="inputNew" />
  16. </view>
  17. <view class="uni-form-item uni-row">
  18. <view class="title">确认密码</view>
  19. <input :class="'uni-input '+confirmPassword" name="confirmPassword" :password="true"
  20. :value="user.confirmPassword" placeholder="请确认新密码" @focus="inputFocus('confirmPassword')"
  21. @blur="inputBlur('confirmPassword')" @input="inputConfirm" />
  22. </view>
  23. <button type="primary" @click="submit">提交</button>
  24. </form>
  25. </view>
  26. </view>
  27. </template>
  28. <script lang="uts">
  29. import { updateUserPwd } from "@/api/system/user"
  30. import { updateData, getList } from '@/api/work';
  31. import { getCurrentUserSync, getCurrentUserNameSync } from '@/utils/auth.uts'
  32. type User = {
  33. oldPassword : string | null,
  34. newPassword : string | null,
  35. confirmPassword : string | null
  36. }
  37. export default {
  38. data() {
  39. return {
  40. oldPassword: '',
  41. newPassword: '',
  42. confirmPassword: '',
  43. user: {
  44. oldPassword: null,
  45. newPassword: null,
  46. confirmPassword: null
  47. } as User,
  48. }
  49. },
  50. onBackPress() {
  51. // 覆盖系统默认的返回行为,返回到指定页面
  52. uni.navigateTo({
  53. url: `/pages/mine/index`,
  54. // 修改动画方向为从左到右退回
  55. animationType: 'slide-in-left', // 使用从左到右滑出的动画效果
  56. animationDuration: 300 // 动画持续时间,单位ms
  57. })
  58. // 返回true表示拦截默认返回行为
  59. return true
  60. },
  61. methods: {
  62. inputFocus(tag : string) {
  63. this.$data[tag] = "my-focus"
  64. },
  65. inputBlur(tag : string) {
  66. this.$data[tag] = "my-blur"
  67. },
  68. inputOld(event : UniInputEvent) {
  69. this.user.oldPassword = event.detail.value
  70. },
  71. inputNew(event : UniInputEvent) {
  72. this.user.newPassword = event.detail.value
  73. },
  74. inputConfirm(event : UniInputEvent) {
  75. this.user.confirmPassword = event.detail.value
  76. },
  77. submit() {
  78. if (this.user.oldPassword == null) {
  79. uni.showToast({
  80. title: "旧密码不能为空",
  81. icon: "error"
  82. })
  83. return
  84. }
  85. if (this.user.newPassword == null) {
  86. uni.showToast({
  87. title: "新密码不能为空",
  88. icon: "error"
  89. })
  90. return
  91. } else if (this.user.newPassword != null && (this.user.newPassword.length < 6 || this.user.newPassword.length > 20)) {
  92. uni.showToast({
  93. title: "长度在6到20个字符",
  94. icon: "error"
  95. })
  96. return
  97. }
  98. if (this.user.oldPassword == this.user.newPassword) {
  99. uni.showToast({
  100. title: "新旧密码不能相同",
  101. icon: "error"
  102. })
  103. return
  104. }
  105. if (this.user.confirmPassword == null) {
  106. uni.showToast({
  107. title: "确认密码不能为空",
  108. icon: "error"
  109. })
  110. return
  111. } else if (this.user.confirmPassword != this.user.newPassword) {
  112. uni.showToast({
  113. title: "两次输入的密码不一致",
  114. icon: "error"
  115. })
  116. return
  117. }
  118. let username = '';
  119. let oldPassword = '';
  120. getList('app_user', 'username', getCurrentUserNameSync(), null, null, null).then((res : UTSJSONObject) => {
  121. let userList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  122. console.log(userList);
  123. if (userList != null && userList.length > 0) {
  124. let user = userList[0];
  125. username = user.getString('username') ?? '';
  126. oldPassword = user.getString('password') ?? '';
  127. }
  128. // 添加逻辑判断:验证原密码是否匹配
  129. if (oldPassword == this.user.oldPassword) {
  130. let updatedData = "password='" + this.user.confirmPassword + "' ";
  131. updateData('app_user', updatedData, 'username', username).then((res : UTSJSONObject) => {
  132. let data = res?.['data'] as boolean ?? false;
  133. if (data != null && data == true) {
  134. uni.showToast({
  135. title: "更新成功!",
  136. });
  137. setTimeout(() => {
  138. }, 1500)
  139. uni.reLaunch({
  140. url: '/pages/mine/index'
  141. });
  142. } else {
  143. uni.showToast({
  144. title: "更新失败,请稍后重试!",
  145. icon: "error"
  146. });
  147. }
  148. });
  149. } else {
  150. uni.showToast({
  151. title: "更新失败,原密码不匹配!",
  152. icon: "error"
  153. });
  154. }
  155. });
  156. }
  157. }
  158. }
  159. </script>
  160. <style lang="scss" scope>
  161. .container {
  162. padding: 10rpx;
  163. background-color: #f0f4f8;
  164. flex: 1;
  165. display: flex;
  166. flex-direction: column;
  167. font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
  168. /* #ifndef APP-ANDROID */
  169. min-height: 100vh;
  170. /* #endif */
  171. height: 120rpx;
  172. }
  173. .download-card {
  174. background: #ffffff;
  175. border-radius: 20rpx;
  176. padding: 15rpx 15rpx;
  177. box-shadow: 0 8rpx 15rpx rgba(0, 43, 92, 0.1);
  178. display: flex;
  179. flex-direction: column;
  180. }
  181. .title {
  182. width: 100rpx;
  183. }
  184. </style>