ChangePasswordCard.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <script setup lang="ts">
  2. import { useThemeStore } from '@/stores/modules/theme'
  3. import { useRouter, useRoute } from 'vue-router'
  4. import { useUserStore } from '@/stores/modules/user'
  5. const userStore = useUserStore()
  6. const router = useRouter()
  7. const themeStore = useThemeStore()
  8. const route = useRoute()
  9. const tips = ref('Please change your password for security.')
  10. if (route.query.firstLogin === 't') {
  11. tips.value = 'First login, please change your password.'
  12. }
  13. const loginForm = ref({
  14. username: '',
  15. oldPassword: '',
  16. newPassword: '',
  17. confirmPassword: ''
  18. })
  19. loginForm.value.username = userStore.userInfo?.uname || ''
  20. // 没有用户名时,跳转到登录页
  21. if (!loginForm.value.username) {
  22. router.push({
  23. name: 'Login'
  24. })
  25. ElMessage.warning('Please login first')
  26. }
  27. const newPwdErrTips = ref('')
  28. const loginError: any = ref({
  29. oldPassword: false,
  30. newPassword: false,
  31. confirmPassword: false
  32. })
  33. const handleChangePwd = () => {
  34. if (loginForm.value.newPassword !== loginForm.value.confirmPassword) {
  35. loginError.value.confirmPassword = true
  36. return
  37. }
  38. if (loginForm.value.newPassword.length < 12 || loginForm.value.newPassword.length > 20) {
  39. loginError.value.newPassword = true
  40. newPwdErrTips.value = 'Password length between 12 - 20'
  41. return
  42. }
  43. if (!/[A-Z]/.test(loginForm.value.newPassword)) {
  44. loginError.value.newPassword = true
  45. newPwdErrTips.value = 'Password must contain uppercase letters'
  46. return
  47. }
  48. if (!/[a-z]/.test(loginForm.value.newPassword)) {
  49. loginError.value.newPassword = true
  50. newPwdErrTips.value = 'Password must contain lowercase letters'
  51. return
  52. }
  53. if (!/[0-9]/.test(loginForm.value.newPassword)) {
  54. loginError.value.newPassword = true
  55. newPwdErrTips.value = 'Password must contain numbers'
  56. return
  57. }
  58. $api
  59. .resetPwd({
  60. uname: loginForm.value.username,
  61. old_password: loginForm.value.oldPassword,
  62. password: loginForm.value.newPassword
  63. })
  64. .then((res: any) => {
  65. if (res.code === 200) {
  66. router.push({
  67. name: 'Login',
  68. query: {
  69. isChange: 'true'
  70. }
  71. })
  72. } else if (res.code === 400) {
  73. if (res.msg === 'Old password is incorrect') {
  74. loginError.value.oldPassword = true
  75. } else {
  76. loginError.value.newPassword = true
  77. newPwdErrTips.value = res.msg
  78. }
  79. }
  80. })
  81. }
  82. const hiddenError = (key: string) => {
  83. loginError.value[key] = false
  84. }
  85. const confirmPwd = () => {
  86. if (loginForm.value.confirmPassword !== loginForm.value.newPassword) {
  87. loginError.value.confirmPassword = true
  88. } else {
  89. loginError.value.confirmPassword = false
  90. }
  91. }
  92. const isUserNameExit = ref(false)
  93. const handleForgot = () => {
  94. router.push({
  95. name: 'Login',
  96. query: {
  97. status: 'reset'
  98. }
  99. })
  100. }
  101. const hasUppercase = ref(false)
  102. const hasLowercase = ref(false)
  103. const hasNumber = ref(false)
  104. const isValidLength = ref(false)
  105. const checkPassword = () => {
  106. const pwd = loginForm.value.newPassword
  107. // 检测是否包含大写字母
  108. hasUppercase.value = /[A-Z]/.test(pwd)
  109. // 检测是否包含小写字母
  110. hasLowercase.value = /[a-z]/.test(pwd)
  111. // 检测是否包含数字
  112. hasNumber.value = /[0-9]/.test(pwd)
  113. // 检测长度是否符合要求
  114. isValidLength.value = pwd.length >= 12 && pwd.length <= 20
  115. }
  116. </script>
  117. <template>
  118. <div class="login" :class="{ 'dark-bg': themeStore.theme === 'dark' }">
  119. <el-card class="login-card">
  120. <div class="title" :class="{ 'is-dark': themeStore.theme === 'dark' }">
  121. <span class="welcome">Change Password</span>
  122. <span class="tips">{{ tips }}</span>
  123. </div>
  124. <div class="login-form">
  125. <div class="label">
  126. <span>User Name</span>
  127. </div>
  128. <el-input :disabled="true" ref="userNameRef" v-model="loginForm.username" class="user-name">
  129. <template #prefix>
  130. <span class="font_family icon-icon_username_b"></span>
  131. </template>
  132. <template #suffix>
  133. <span v-if="isUserNameExit" class="font_family icon-icon_confirm_b confirm-icon"></span>
  134. </template>
  135. </el-input>
  136. <div class="label">
  137. <span>Old Password</span>
  138. <span class="forgot-password" @click="handleForgot">Forgot Password?</span>
  139. </div>
  140. <el-input
  141. ref="passWordRef"
  142. :class="{ 'is-error': loginError.oldPassword }"
  143. v-model="loginForm.oldPassword"
  144. type="password"
  145. placeholder="Please input password"
  146. show-password
  147. @focus="hiddenError('oldPassword')"
  148. >
  149. <template #prefix>
  150. <span class="font_family icon-icon_password_b"></span>
  151. </template>
  152. </el-input>
  153. <div class="error" v-if="loginError.oldPassword">Incorrect password. Please try again.</div>
  154. <div class="label">
  155. <span>New Password</span>
  156. </div>
  157. <el-input
  158. ref="passWordRef"
  159. :class="{ 'is-error': loginError.newPassword }"
  160. v-model="loginForm.newPassword"
  161. type="password"
  162. placeholder="Please input password"
  163. show-password
  164. @focus="hiddenError('newPassword')"
  165. @input="checkPassword"
  166. >
  167. <template #prefix>
  168. <span class="font_family icon-icon_password_b"></span>
  169. </template>
  170. </el-input>
  171. <div class="error" v-if="loginError.newPassword">{{ newPwdErrTips }}</div>
  172. <div class="limit-tips">
  173. <div class="tip-item">
  174. <span class="font_family icon-icon_confirm_b" :class="{ active: hasUppercase }"></span>
  175. <span>Password must contain uppercase letters</span>
  176. </div>
  177. <div class="tip-item">
  178. <span class="font_family icon-icon_confirm_b" :class="{ active: hasLowercase }"></span>
  179. <span>Password must contain lowercase letters</span>
  180. </div>
  181. <div class="tip-item">
  182. <span class="font_family icon-icon_confirm_b" :class="{ active: hasNumber }"></span>
  183. <span>Password must contain numbers</span>
  184. </div>
  185. <div class="tip-item">
  186. <span
  187. style="padding-top: 2px"
  188. class="font_family icon-icon_confirm_b"
  189. :class="{ active: isValidLength }"
  190. ></span>
  191. <span>Password length between12 - 20 </span>
  192. </div>
  193. </div>
  194. <div class="label">
  195. <span>Confirm New Password</span>
  196. </div>
  197. <el-input
  198. ref="passWordRef"
  199. :class="{ 'is-error': loginError.confirmPassword }"
  200. v-model="loginForm.confirmPassword"
  201. type="password"
  202. placeholder="Please input password"
  203. show-password
  204. @focus="hiddenError('confirmPassword')"
  205. @blur="confirmPwd"
  206. >
  207. <template #prefix>
  208. <span class="font_family icon-icon_password_b"></span>
  209. </template>
  210. </el-input>
  211. <div class="error" v-if="loginError.confirmPassword">
  212. The password does not match. Please try again.
  213. </div>
  214. <el-button @click="handleChangePwd" class="el-button--dark login-btn"
  215. >Change Password</el-button
  216. >
  217. </div>
  218. <template #footer>
  219. <div class="license">
  220. <span>© KLN LOGISTICS GROUP LIMITED. ALL RIGHTS RESERVED.</span>
  221. </div>
  222. </template>
  223. </el-card>
  224. </div>
  225. </template>
  226. <style lang="scss" scoped>
  227. .login {
  228. position: relative;
  229. display: flex;
  230. justify-content: center;
  231. align-items: center;
  232. height: 100%;
  233. width: 100%;
  234. background: url(../image/bg.png) no-repeat center center;
  235. background-size: cover;
  236. &.dark-bg {
  237. background: url(../image/bg-dark.png) no-repeat center center;
  238. background-size: cover;
  239. }
  240. }
  241. .login-card {
  242. width: 400px;
  243. border-radius: 12px;
  244. .title {
  245. display: flex;
  246. flex-direction: column;
  247. align-items: center;
  248. margin-bottom: 24px;
  249. padding: 40px 40px 0;
  250. background: url(../image/bg-login-card.png) no-repeat;
  251. background-position: top left; /* 从左上角开始显示 */
  252. background-size: 400px 100px; /* 保持背景图像的原始尺寸 */
  253. &.is-dark {
  254. background: url(../image/bg-login-card-dark.png) no-repeat;
  255. }
  256. .welcome {
  257. margin-bottom: 16px;
  258. font-size: 24px;
  259. font-weight: 700;
  260. }
  261. }
  262. :deep(.el-card__body) {
  263. padding: 0;
  264. padding-bottom: 16px;
  265. }
  266. .login-btn {
  267. width: 100%;
  268. height: 40px;
  269. margin-top: 16px;
  270. }
  271. }
  272. .login-form {
  273. display: flex;
  274. flex-direction: column;
  275. align-items: flex-start;
  276. padding: 0 40px;
  277. .el-input {
  278. height: 40px;
  279. .confirm-icon {
  280. display: inline-flex;
  281. align-items: center;
  282. justify-content: center;
  283. height: 16px;
  284. width: 16px;
  285. margin-right: 4px;
  286. font-size: 14px;
  287. color: #fff;
  288. background-color: var(--color-success);
  289. border-radius: 50%;
  290. }
  291. &.is-error {
  292. :deep(.el-input__wrapper) {
  293. box-shadow: 0 0 0 1px var(--color-danger) inset;
  294. }
  295. }
  296. :deep(.el-input__prefix) {
  297. margin: 0 4px;
  298. background-color: transparent;
  299. }
  300. &.is-disabled {
  301. :deep(.el-input__inner) {
  302. -webkit-text-fill-color: var(--color-neutral-1);
  303. color: var(--color-neutral-1);
  304. font-weight: 700;
  305. }
  306. }
  307. }
  308. .limit-tips {
  309. margin-top: 10px;
  310. .tip-item {
  311. display: flex;
  312. align-items: center;
  313. margin-bottom: 8px;
  314. font-size: 12px;
  315. color: var(--color-neutral-2);
  316. &:nth-last-child(1) {
  317. margin-bottom: 0;
  318. }
  319. .font_family {
  320. display: flex;
  321. justify-content: center;
  322. align-items: center;
  323. height: 12px;
  324. width: 12px;
  325. padding-top: 1px;
  326. background-color: #b5b9bf;
  327. font-size: 10px;
  328. color: white;
  329. border-radius: 50%;
  330. &.active {
  331. background-color: #00a870;
  332. }
  333. }
  334. span {
  335. margin-left: 4px;
  336. }
  337. }
  338. }
  339. .verification-code {
  340. margin-top: 16px;
  341. .verification-code-img {
  342. width: 130px;
  343. height: 38px;
  344. object-fit: cover;
  345. }
  346. :deep(.el-input-group__append) {
  347. padding: 0;
  348. padding-right: 1px;
  349. }
  350. }
  351. .el-input.user-name {
  352. :deep(.el-input__wrapper) {
  353. padding-right: 6px;
  354. box-shadow: 0 0 0 1px var(--color-input-disabled-border) inset;
  355. }
  356. }
  357. .label {
  358. display: flex;
  359. justify-content: space-between;
  360. width: 100%;
  361. margin-top: 16px;
  362. font-size: 12px;
  363. line-height: 18px;
  364. span {
  365. color: var(--color-neutral-2);
  366. }
  367. .forgot-password {
  368. color: var(--color-theme);
  369. cursor: pointer;
  370. }
  371. }
  372. .error {
  373. font-size: 12px;
  374. color: var(--color-danger);
  375. line-height: 14px;
  376. }
  377. }
  378. .license {
  379. display: flex;
  380. flex-direction: column;
  381. align-items: center;
  382. font-size: 12px;
  383. .company {
  384. color: var(--color-theme);
  385. }
  386. span {
  387. color: var(--color-neutral-2);
  388. }
  389. }
  390. </style>