cl-keyboard-password.uvue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <template>
  2. <cl-popup
  3. :title="title"
  4. :swipe-close-threshold="100"
  5. :pt="{
  6. inner: {
  7. className: parseClass([
  8. [isDark, '!bg-surface-700', '!bg-surface-100'],
  9. pt.popup?.className
  10. ])
  11. },
  12. mask: {
  13. className: '!bg-transparent'
  14. }
  15. }"
  16. v-model="visible"
  17. >
  18. <view class="cl-keyboard-password" :class="[pt.className]">
  19. <slot name="value" :value="value">
  20. <view
  21. v-if="showValue"
  22. class="cl-keyboard-password__value"
  23. :class="[pt.value?.className]"
  24. >
  25. <cl-text
  26. v-if="value != ''"
  27. :pt="{
  28. className: '!text-2xl'
  29. }"
  30. >{{ valueText }}</cl-text
  31. >
  32. <cl-text
  33. v-else
  34. :pt="{
  35. className: '!text-md !text-surface-400'
  36. }"
  37. >{{ placeholder }}</cl-text
  38. >
  39. </view>
  40. </slot>
  41. <view class="cl-keyboard-password__list">
  42. <view
  43. class="cl-keyboard-password__rows"
  44. v-for="(row, rowIndex) in list"
  45. :key="rowIndex"
  46. :class="[`is-mode-${mode}`]"
  47. >
  48. <view
  49. v-for="(item, index) in row"
  50. :key="item"
  51. class="cl-keyboard-password__item"
  52. :class="[
  53. `is-keycode-${item}`,
  54. {
  55. 'is-empty': item == '',
  56. 'is-dark': isDark
  57. },
  58. pt.item?.className
  59. ]"
  60. :style="{
  61. marginRight: index == row.length - 1 ? '0' : '10rpx'
  62. }"
  63. hover-class="opacity-50"
  64. :hover-stay-time="250"
  65. @touchstart.stop="onCommand(item)"
  66. >
  67. <slot name="item" :item="item">
  68. <cl-icon
  69. v-if="item == 'delete'"
  70. name="delete-back-2-line"
  71. :size="36"
  72. ></cl-icon>
  73. <cl-text
  74. v-else-if="item == 'confirm'"
  75. color="white"
  76. :pt="{
  77. className: '!text-lg'
  78. }"
  79. >{{ confirmText }}</cl-text
  80. >
  81. <cl-text
  82. v-else-if="item == 'letter'"
  83. :pt="{
  84. className: '!text-lg'
  85. }"
  86. >ABC</cl-text
  87. >
  88. <cl-text
  89. v-else-if="item == 'number'"
  90. :pt="{
  91. className: '!text-lg'
  92. }"
  93. >123</cl-text
  94. >
  95. <template v-else-if="item == 'caps'">
  96. <cl-icon name="upload-line" :size="36"></cl-icon>
  97. <cl-badge
  98. dot
  99. position
  100. type="info"
  101. :pt="{
  102. className: '!right-1 !top-1'
  103. }"
  104. v-if="mode == 'letterUpper'"
  105. ></cl-badge>
  106. </template>
  107. <cl-text
  108. v-else
  109. :pt="{
  110. className: '!text-lg'
  111. }"
  112. >{{ item }}</cl-text
  113. >
  114. </slot>
  115. </view>
  116. </view>
  117. </view>
  118. </view>
  119. </cl-popup>
  120. </template>
  121. <script setup lang="ts">
  122. import { useUi } from "../../hooks";
  123. import type { PassThroughProps } from "../../types";
  124. import type { ClPopupProps } from "../cl-popup/props";
  125. import { ref, computed, watch } from "vue";
  126. import { $t, t } from "@/locale";
  127. import { isDark, parseClass, parsePt } from "@/cool";
  128. import { vibrate } from "@/uni_modules/cool-vibrate";
  129. defineOptions({
  130. name: "cl-keyboard-password"
  131. });
  132. defineSlots<{
  133. value(props: { value: string }): any;
  134. item(props: { item: string }): any;
  135. }>();
  136. const props = defineProps({
  137. // 透传样式配置
  138. pt: {
  139. type: Object,
  140. default: () => ({})
  141. },
  142. // v-model绑定的值
  143. modelValue: {
  144. type: String,
  145. default: ""
  146. },
  147. // 弹窗标题
  148. title: {
  149. type: String,
  150. default: () => t("密码键盘")
  151. },
  152. // 输入框占位符
  153. placeholder: {
  154. type: String,
  155. default: () => t("安全键盘,请放心输入")
  156. },
  157. // 最小输入长度
  158. minlength: {
  159. type: Number,
  160. default: 6
  161. },
  162. // 最大输入长度
  163. maxlength: {
  164. type: Number,
  165. default: 20
  166. },
  167. // 确认按钮文本
  168. confirmText: {
  169. type: String,
  170. default: () => t("确定")
  171. },
  172. // 是否显示输入值
  173. showValue: {
  174. type: Boolean,
  175. default: true
  176. },
  177. // 是否输入即绑定
  178. inputImmediate: {
  179. type: Boolean,
  180. default: false
  181. },
  182. // 是否加密
  183. encrypt: {
  184. type: Boolean,
  185. default: false
  186. }
  187. });
  188. // 定义事件发射器,支持v-model和change事件
  189. const emit = defineEmits(["update:modelValue", "change"]);
  190. // 样式穿透类型
  191. type PassThrough = {
  192. className?: string;
  193. item?: PassThroughProps;
  194. value?: PassThroughProps;
  195. popup?: ClPopupProps;
  196. };
  197. // 样式穿透计算
  198. const pt = computed(() => parsePt<PassThrough>(props.pt));
  199. // 获取UI相关的工具方法
  200. const ui = useUi();
  201. // 控制弹窗显示/隐藏
  202. const visible = ref(false);
  203. // 输入框当前值,双向绑定
  204. const value = ref(props.modelValue);
  205. // 键盘模式,letter: 字母区,number: 数字区
  206. const mode = ref<"letter" | "letterUpper" | "number">("letter");
  207. // 输入框显示值
  208. const valueText = computed(() => {
  209. if (props.encrypt) {
  210. return "*".repeat(value.value.length);
  211. }
  212. return value.value;
  213. });
  214. // 数字键盘的按键列表,包含数字、删除、00和小数点
  215. const list = computed(() => {
  216. // 字母键盘的字母区
  217. const letter: string[][] = [
  218. ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"],
  219. ["a", "s", "d", "f", "g", "h", "j", "k", "l", "m"],
  220. ["caps", "z", "x", "c", "v", "b", "n", "m", "delete"],
  221. ["number", "space", "confirm"]
  222. ];
  223. // 大写字母键盘的字母区
  224. const letterUpper: string[][] = [
  225. ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
  226. ["A", "S", "D", "F", "G", "H", "J", "K", "L", "M"],
  227. ["caps", "Z", "X", "C", "V", "B", "N", "M", "delete"],
  228. ["number", "space", "confirm"]
  229. ];
  230. // 数字键盘的数字区
  231. const number: string[][] = [
  232. ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
  233. ["#", "/", ":", ";", "(", ")", "^", "*", "+"],
  234. ["-", "=", "|", "~", "$", "&", ".", ",", "delete"],
  235. ["letter", "%", "?", "!", "{", "}", "confirm"]
  236. ];
  237. switch (mode.value) {
  238. case "letter":
  239. return letter;
  240. case "letterUpper":
  241. return letterUpper;
  242. case "number":
  243. return number;
  244. default:
  245. return letter;
  246. }
  247. });
  248. // 打开键盘弹窗
  249. function open() {
  250. visible.value = true;
  251. }
  252. // 关闭键盘弹窗
  253. function close() {
  254. visible.value = false;
  255. }
  256. // 处理键盘按键点击事件
  257. function onCommand(key: string) {
  258. // 震动
  259. vibrate(1);
  260. // 大写字母键盘
  261. if (key == "caps") {
  262. mode.value = mode.value == "letter" ? "letterUpper" : "letter";
  263. return;
  264. }
  265. // 字母键盘
  266. if (key == "letter") {
  267. mode.value = "letter";
  268. return;
  269. }
  270. // 数字键盘
  271. if (key == "number") {
  272. mode.value = "number";
  273. return;
  274. }
  275. // 确认按钮逻辑
  276. if (key == "confirm") {
  277. if (value.value == "") {
  278. ui.showToast({
  279. message: t("请输入内容")
  280. });
  281. return;
  282. }
  283. // 校验密码长度
  284. if (value.value.length < props.minlength || value.value.length > props.maxlength) {
  285. ui.showToast({
  286. message: $t("请输入{minlength}到{maxlength}位密码", {
  287. minlength: props.minlength,
  288. maxlength: props.maxlength
  289. })
  290. });
  291. return;
  292. }
  293. // 触发v-model和change事件
  294. emit("update:modelValue", value.value);
  295. emit("change", value.value);
  296. // 关闭弹窗
  297. close();
  298. return;
  299. }
  300. // 删除键,去掉最后一位
  301. if (key == "delete") {
  302. value.value = value.value.slice(0, -1);
  303. return;
  304. }
  305. // 超过最大输入长度,提示并返回
  306. if (value.value.length >= props.maxlength) {
  307. ui.showToast({
  308. message: $t("最多输入{maxlength}位", {
  309. maxlength: props.maxlength
  310. })
  311. });
  312. return;
  313. }
  314. // 其他按键直接拼接到value
  315. value.value += key;
  316. }
  317. watch(value, (val: string) => {
  318. // 如果输入即绑定,则立即更新绑定值
  319. if (props.inputImmediate) {
  320. emit("update:modelValue", val);
  321. emit("change", val);
  322. }
  323. });
  324. // 监听外部v-model的变化,保持内部value同步
  325. watch(
  326. computed(() => props.modelValue),
  327. (val: string) => {
  328. value.value = val;
  329. }
  330. );
  331. defineExpose({
  332. open,
  333. close
  334. });
  335. </script>
  336. <style lang="scss" scoped>
  337. .cl-keyboard-password {
  338. padding: 0 20rpx 20rpx 20rpx;
  339. &__value {
  340. @apply flex flex-row items-center justify-center;
  341. height: 80rpx;
  342. margin-bottom: 20rpx;
  343. }
  344. &__list {
  345. @apply relative;
  346. }
  347. &__rows {
  348. @apply flex flex-row items-center;
  349. &.is-mode-province {
  350. @apply justify-center;
  351. }
  352. &.is-mode-plate {
  353. @apply justify-between;
  354. }
  355. }
  356. &__item {
  357. @apply flex items-center justify-center rounded-lg relative bg-white;
  358. height: 80rpx;
  359. width: 62rpx;
  360. margin-top: 10rpx;
  361. flex: 1;
  362. &.is-dark {
  363. @apply bg-surface-800;
  364. }
  365. &.is-keycode-number,
  366. &.is-keycode-letter {
  367. width: 150rpx;
  368. flex: none;
  369. }
  370. &.is-keycode-caps,
  371. &.is-keycode-delete {
  372. width: 80rpx;
  373. flex: none;
  374. }
  375. &.is-keycode-letter,
  376. &.is-keycode-number,
  377. &.is-keycode-caps,
  378. &.is-keycode-delete {
  379. @apply bg-surface-200;
  380. &.is-dark {
  381. @apply bg-surface-600;
  382. }
  383. }
  384. &.is-keycode-confirm {
  385. @apply bg-primary-500;
  386. width: 150rpx !important;
  387. flex: none;
  388. }
  389. &.is-empty {
  390. background-color: transparent !important;
  391. }
  392. }
  393. }
  394. </style>