cl-keyboard-password.uvue 8.7 KB

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