confirmModal1.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <Teleport to="body">
  3. <Transition name="fade">
  4. <div v-if="visible" class="password-dialog-overlay" @click.self="handleOverlayClick">
  5. <div class="password-dialog">
  6. <!-- 标题栏 -->
  7. <div class="dialog-header">
  8. <h3>密码校验</h3>
  9. </div>
  10. <div class="close-btn" @click="handleCancel">
  11. <img src="@/assets/images/home-container/configurable/close.svg" alt="" />
  12. </div>
  13. <!-- 内容区域 -->
  14. <div class="dialog-content">
  15. <!-- 密码输入框 -->
  16. <div class="input-wrapper" :class="{ error: showError }">
  17. <input
  18. ref="passwordInput"
  19. v-model="password"
  20. :type="showPassword ? 'text' : 'password'"
  21. :placeholder="placeholder"
  22. maxlength="20"
  23. @keyup.enter="handleConfirm"
  24. @input="clearError"
  25. />
  26. <!-- 显示/隐藏密码切换 -->
  27. <button type="button" class="toggle-password" @click="togglePassword" tabindex="-1">
  28. <span v-if="showPassword"> <img src="@/assets/images/home-container/configurable/showPassword.svg" alt="" /></span>
  29. <span v-else> <img src="@/assets/images/home-container/configurable/hidePassword.svg" alt="" /></span>
  30. </button>
  31. </div>
  32. <!-- 错误提示 -->
  33. <Transition name="slide">
  34. <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
  35. </Transition>
  36. </div>
  37. <!-- 按钮区域 -->
  38. <div class="dialog-footer">
  39. <button class="btn cancel" @click="handleCancel"><span>取消</span></button>
  40. <button class="btn confirm" @click="handleConfirm"><span>确认</span></button>
  41. </div>
  42. </div>
  43. </div>
  44. </Transition>
  45. </Teleport>
  46. </template>
  47. <script setup>
  48. import { ref, nextTick, watch } from 'vue';
  49. // Props
  50. const props = defineProps({
  51. // 弹窗显示状态
  52. visible: {
  53. type: Boolean,
  54. default: false,
  55. },
  56. // 提示消息
  57. message: {
  58. type: String,
  59. default: '',
  60. },
  61. // 输入框占位符
  62. placeholder: {
  63. type: String,
  64. default: '请输入密码(必填)',
  65. },
  66. // 点击遮罩层是否关闭
  67. closeOnClickOverlay: {
  68. type: Boolean,
  69. default: true,
  70. },
  71. });
  72. // Emits
  73. const emit = defineEmits(['update:visible', 'confirm', 'cancel', 'forgot-password', 'error']);
  74. // 状态
  75. const password = ref('');
  76. const showPassword = ref(false);
  77. const errorMessage = ref('');
  78. const showError = ref(false);
  79. const passwordInput = ref(null);
  80. // 监听弹窗显示,自动聚焦
  81. watch(
  82. () => props.visible,
  83. async (newVal) => {
  84. if (newVal) {
  85. await nextTick();
  86. passwordInput.value?.focus();
  87. // 重置状态
  88. password.value = '';
  89. errorMessage.value = '';
  90. showError.value = false;
  91. }
  92. }
  93. );
  94. // 清除错误
  95. const clearError = () => {
  96. showError.value = false;
  97. errorMessage.value = '';
  98. };
  99. // 切换密码显示
  100. const togglePassword = () => {
  101. showPassword.value = !showPassword.value;
  102. };
  103. // 处理取消
  104. const handleCancel = () => {
  105. emit('update:visible', false);
  106. emit('handleCancel');
  107. resetState();
  108. };
  109. // 处理确认
  110. const handleConfirm = async () => {
  111. if (!password.value) {
  112. showError.value = true;
  113. errorMessage.value = '请输入密码';
  114. return;
  115. }
  116. showError.value = false;
  117. try {
  118. // 触发确认事件,返回密码
  119. const result = await emit('handleConfirm', password.value);
  120. // 如果确认成功(组件外部处理),关闭弹窗
  121. if (result !== false) {
  122. handleCancel();
  123. }
  124. } catch (error) {
  125. // 密码错误
  126. showError.value = true;
  127. errorMessage.value = '密码错误';
  128. // 清空密码输入
  129. password.value = '';
  130. // 重新聚焦
  131. await nextTick();
  132. passwordInput.value?.focus();
  133. } finally {
  134. }
  135. };
  136. // 点击遮罩层
  137. const handleOverlayClick = () => {
  138. if (props.closeOnClickOverlay) {
  139. handleCancel();
  140. }
  141. };
  142. // 重置状态
  143. const resetState = () => {
  144. password.value = '';
  145. showPassword.value = false;
  146. errorMessage.value = '';
  147. showError.value = false;
  148. };
  149. // 暴露方法给父组件
  150. defineExpose({
  151. resetState,
  152. focus: () => passwordInput.value?.focus(),
  153. });
  154. </script>
  155. <style scoped>
  156. @font-face {
  157. font-family: 'douyuFont';
  158. src: url('../../../../assets/font/douyuFont.otf');
  159. }
  160. .password-dialog-overlay {
  161. position: fixed;
  162. top: 0;
  163. left: 0;
  164. right: 0;
  165. bottom: 0;
  166. background-color: rgba(0, 0, 0, 0.5);
  167. display: flex;
  168. align-items: center;
  169. justify-content: center;
  170. z-index: 1000;
  171. }
  172. .password-dialog {
  173. position: fixed;
  174. top: 50%;
  175. left: 50%;
  176. transform: translate(-50%, -50%);
  177. border-radius: 12px;
  178. width: 500px; /* 固定宽度 */
  179. height: 270px; /* 固定高度 */
  180. background: url('/@/assets/images/home-container/configurable/confirmBj.png') no-repeat;
  181. background-size: 100% 100%;
  182. }
  183. /* 调整 close-btn 的位置 */
  184. .close-btn {
  185. width: 30px;
  186. position: absolute;
  187. top: -6px;
  188. right: -23px;
  189. cursor: pointer;
  190. }
  191. /* 调整标题位置 */
  192. .dialog-header {
  193. padding: 20px 24px;
  194. text-align: center;
  195. color: #fff;
  196. background: url('/src/assets/images/home-container/configurable/confirmTitle.png') no-repeat;
  197. background-size: 100% 40%;
  198. width: 70%;
  199. margin: 30px auto 0; /* 改为 margin: 30px auto 0 */
  200. }
  201. .dialog-header h3 {
  202. margin: 5px;
  203. font-size: 18px;
  204. color: #fff;
  205. margin-top: -35px;
  206. font-family: 'douyuFont';
  207. }
  208. .close-btn :hover {
  209. cursor: pointer;
  210. }
  211. /* 内容区域 */
  212. .dialog-content {
  213. padding: 24px;
  214. }
  215. .input-wrapper {
  216. position: relative;
  217. margin-bottom: 8px;
  218. }
  219. .input-wrapper input {
  220. width: 100%;
  221. padding: 12px 40px 12px 12px;
  222. background: url('/@/assets/images/home-container/configurable/input-success.png') no-repeat;
  223. background-size: 100% 100%;
  224. border-radius: 8px;
  225. border: none;
  226. font-size: 16px;
  227. transition: all 0.3s;
  228. box-sizing: border-box;
  229. color: #fff;
  230. height: 53px;
  231. }
  232. .input-wrapper input:focus {
  233. outline: none;
  234. }
  235. .input-wrapper.error input {
  236. background: url('/@/assets/images/home-container/configurable/input-error.png') no-repeat;
  237. background-size: 100% 100%;
  238. }
  239. .toggle-password {
  240. position: absolute;
  241. right: 8px;
  242. top: 50%;
  243. transform: translateY(-50%);
  244. background: none;
  245. border: none;
  246. cursor: pointer;
  247. padding: 4px;
  248. color: #999 !important;
  249. font-size: 18px;
  250. display: flex;
  251. align-items: center;
  252. justify-content: center;
  253. }
  254. .toggle-password:hover {
  255. color: #666;
  256. }
  257. .error-message {
  258. color: #e90203;
  259. font-size: 16px;
  260. margin: 4px 0 0 0;
  261. min-height: 18px;
  262. text-align: center;
  263. }
  264. .forgot-password {
  265. display: inline-block;
  266. margin-top: 12px;
  267. color: #409eff;
  268. font-size: 14px;
  269. text-decoration: none;
  270. }
  271. .forgot-password:hover {
  272. text-decoration: underline;
  273. }
  274. /* 底部按钮 */
  275. /* 调整底部按钮位置 */
  276. .dialog-footer {
  277. position: absolute;
  278. bottom: 30px;
  279. left: 50%;
  280. transform: translateX(-50%);
  281. width: auto;
  282. display: flex;
  283. gap: 10px;
  284. }
  285. .btn {
  286. flex: 1;
  287. cursor: pointer;
  288. transition: all 0.3s;
  289. border-radius: 5px;
  290. height: 40px;
  291. }
  292. .btn.cancel {
  293. width: 100px;
  294. background: none;
  295. color: #fff;
  296. border: 2px solid #1b6e96;
  297. }
  298. .btn.confirm {
  299. width: 100px;
  300. margin-left: 10px;
  301. background-color: #1da2a9;
  302. border: 2px solid #2effee;
  303. color: #fff;
  304. }
  305. .btn.confirm:disabled {
  306. background-color: #a0cfff;
  307. cursor: not-allowed;
  308. }
  309. </style>