reset-passwd.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <script setup lang="ts">
  2. import { reactive, computed, ref } from "vue";
  3. import { useUserStoreHook } from "@/store/modules/user";
  4. import * as $userApi from "@/api/sys/user";
  5. import message from "@/utils/message";
  6. const userStore = useUserStoreHook();
  7. const form = ref();
  8. const pageData: any = reactive({
  9. mode: "self",
  10. modalParam: {
  11. visible: false,
  12. closeOnClickModal: true
  13. },
  14. currentData: {},
  15. formParam: {
  16. infoForm: {},
  17. loading: false
  18. }
  19. });
  20. const validatePass = (rule: any, value: any, callback: any) => {
  21. if (value === "") {
  22. callback(new Error("请输入新密码"));
  23. } else if (
  24. value === pageData.formParam.infoForm.oldPassword &&
  25. pageData.mode === "self"
  26. ) {
  27. callback(new Error("输入新密码与旧密码不能相同"));
  28. } else {
  29. callback();
  30. }
  31. };
  32. const validatePass2 = (rule: any, value: any, callback: any) => {
  33. if (value === "") {
  34. callback(new Error("请再次输入新密码"));
  35. } else if (value !== pageData.formParam.infoForm.newPassword) {
  36. callback(new Error("两次输入密码不正确"));
  37. } else {
  38. callback();
  39. }
  40. };
  41. const username = computed(() => {
  42. if (pageData.currentData) {
  43. return pageData.currentData.username;
  44. }
  45. return userStore.username;
  46. });
  47. const formRules = computed(() => {
  48. const rules: any = {
  49. newPassword: [
  50. {
  51. required: true,
  52. message: "请输入新密码",
  53. trigger: "blur"
  54. },
  55. { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  56. { validator: validatePass, trigger: "blur" }
  57. ],
  58. confirmPassword: [
  59. {
  60. required: true,
  61. message: "请输入确认密码",
  62. trigger: "blur"
  63. },
  64. { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
  65. { validator: validatePass2, trigger: "blur" }
  66. ]
  67. };
  68. if (username.value === userStore.username) {
  69. rules.oldPassword = [
  70. {
  71. required: true,
  72. message: "请输入原密码",
  73. trigger: "blur"
  74. }
  75. ];
  76. }
  77. return rules;
  78. });
  79. const formFields = computed(() => {
  80. const fields: any[] = [
  81. {
  82. type: "input",
  83. label: "新密码",
  84. prop: "newPassword",
  85. placeholder: "请输入新密码"
  86. },
  87. {
  88. type: "input",
  89. label: "确认密码",
  90. prop: "confirmPassword",
  91. placeholder: "请输入确认密码"
  92. }
  93. ];
  94. if (username.value === userStore.username) {
  95. fields.splice(1, 0, {
  96. type: "input",
  97. label: "原密码",
  98. prop: "oldPassword",
  99. placeholder: "请输入原密码"
  100. });
  101. }
  102. return fields;
  103. });
  104. const open = (data?: any) => {
  105. pageData.currentData = data;
  106. if (data) {
  107. pageData.mode = "other";
  108. }
  109. pageData.modalParam.visible = true;
  110. };
  111. const initData = () => {
  112. pageData.formParam.infoForm = {};
  113. pageData.currentData = undefined;
  114. pageData.modalParam.visible = false;
  115. };
  116. const handleClose = () => {
  117. initData();
  118. emits("close");
  119. };
  120. const handleConfirm = () => {
  121. form.value!.validate((isValid: boolean) => {
  122. if (isValid) {
  123. if (pageData.mode === "self") {
  124. changePasswd();
  125. } else {
  126. resetPasswd();
  127. }
  128. }
  129. });
  130. };
  131. const changePasswd = () => {
  132. const data = pageData.formParam.infoForm;
  133. pageData.formParam.loading = true;
  134. $userApi
  135. .changePasswd(username.value, data)
  136. .then((res: any) => {
  137. if (res.success) {
  138. message.success("更新密码成功,下次登陆生效");
  139. initData();
  140. emits("confirm");
  141. } else {
  142. message.error(res.message);
  143. }
  144. })
  145. .finally(() => {
  146. pageData.formParam.loading = false;
  147. });
  148. };
  149. const resetPasswd = () => {
  150. const data = pageData.formParam.infoForm;
  151. pageData.formParam.loading = true;
  152. $userApi
  153. .resetPasswd(username.value, data)
  154. .then((res: any) => {
  155. if (res.success) {
  156. message.success("重置密码成功,下次登陆生效");
  157. initData();
  158. emits("confirm");
  159. } else {
  160. message.error(res.message);
  161. }
  162. })
  163. .finally(() => {
  164. pageData.formParam.loading = false;
  165. });
  166. };
  167. const emits = defineEmits(["close", "confirm"]);
  168. defineExpose({
  169. open
  170. });
  171. defineOptions({
  172. name: "UserResetPasswd"
  173. });
  174. </script>
  175. <template>
  176. <div>
  177. <el-dialog
  178. title="重置密码"
  179. v-model="pageData.modalParam.visible"
  180. :close-on-click-modal="pageData.modalParam.closeOnClickModal"
  181. width="30%"
  182. @close="handleClose"
  183. >
  184. <!---->
  185. <div class="flex justify-center items-center m-5">
  186. <div class="font-bold text-lg">{{ username }}</div>
  187. </div>
  188. <el-form
  189. :model="pageData.formParam.infoForm"
  190. :rules="formRules"
  191. ref="form"
  192. label-width="100px"
  193. class="w-full"
  194. v-loading="pageData.formParam.loading"
  195. >
  196. <el-form-item
  197. v-for="(item, index) in formFields"
  198. :key="index"
  199. :prop="item.prop"
  200. :label="item.label"
  201. >
  202. <template v-if="item.type === 'input'">
  203. <el-input
  204. v-model="pageData.formParam.infoForm[item.prop]"
  205. :placeholder="item.placeholder"
  206. type="password"
  207. show-password
  208. clearable
  209. />
  210. </template>
  211. </el-form-item>
  212. </el-form>
  213. <template #footer>
  214. <span class="dialog-footer">
  215. <el-button plain @click="handleClose">取消</el-button>
  216. <el-popconfirm
  217. title="确认提交?"
  218. @confirm="handleConfirm"
  219. confirm-button-text="确定"
  220. cancel-button-text="取消"
  221. >
  222. <template #reference>
  223. <el-button
  224. type="primary"
  225. plain
  226. v-loading="pageData.formParam.loading"
  227. >提 交</el-button
  228. >
  229. </template>
  230. </el-popconfirm>
  231. </span>
  232. </template>
  233. </el-dialog>
  234. </div>
  235. </template>
  236. <style lang="scss" scoped></style>