amountInput.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <template>
  2. <view class="keyboard-main" :style="{height:height+'px'}">
  3. <view class="mask" @click.stop="hide" v-if="isShow"></view>
  4. <view class='keyboard' :style="'margin-bottom:'+safeAreaBottom+'rpx'" :class="{noPadding:!isShow}">
  5. <view class='key-row' v-if="isShow">
  6. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(1)'>1</view>
  7. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(2)'>2</view>
  8. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(3)'>3</view>
  9. <view class='key-cell last-child' @click.stop="_handleKeyPress('delete')">
  10. <image class="icon" src="../../static/icon/del.png" mode="aspectFill"></image>
  11. </view>
  12. </view>
  13. <view class='key-row' v-if="isShow">
  14. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(4)'>4</view>
  15. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(5)'>5</view>
  16. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(6)'>6</view>
  17. <view class='key-cell last-child'></view>
  18. </view>
  19. <view class='key-row' v-if="isShow">
  20. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(7)'>7</view>
  21. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(8)'>8</view>
  22. <view class='key-cell key-cell-num' @click.stop='_handleKeyPress(9)'>9</view>
  23. <view class='key-cell last-child '></view>
  24. </view>
  25. <view class="key-zero-and-point" v-if="isShow">
  26. <view class="a zero key-cell-num" @click.stop='_handleKeyPress(0)'>0</view>
  27. <view class="a point key-cell-num" @click.stop="_handleKeyPress('.')">.</view>
  28. <view class='a last-child'>
  29. </view>
  30. </view>
  31. <view class='key-confirm key-cell-num' :class="{big:isShow,frist:fristShow}" :style="{'background':btnColor}"
  32. @click.stop="_handleKeyPress('confirm')">
  33. {{confirmText}}
  34. </view>
  35. <view v-if="!isShow">
  36. <view class="" style="height: 90rpx;"></view>
  37. <view class="footer-fixed center" style="bottom: 14%;">
  38. <view @click="_handleKeyPress('confirm')" class="cu-btn df radius " :style="{'background':btnColor}" style="color: #FFFFFF;width: 90%;height: 90rpx;">
  39. 付款
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. name: "keyBoard",
  49. props: {
  50. confirmText: {
  51. default: '充值',
  52. type: String
  53. },
  54. btnColor: {
  55. default: '#2B76EF',
  56. type: String
  57. },
  58. placeholder: {
  59. default: '请输入充值金额',
  60. type: String
  61. },
  62. currency: {
  63. default: '¥',
  64. type: String
  65. },
  66. maxNumber: {
  67. default: 10000000,
  68. type: Number
  69. },
  70. fontSize: {
  71. type: [String, Number],
  72. default: 30
  73. },
  74. isBold: {
  75. type: Boolean,
  76. default: true
  77. },
  78. isFilter: {
  79. type: Boolean,
  80. default: true
  81. }
  82. },
  83. data() {
  84. return {
  85. fristShow: true,
  86. isShow: true,
  87. size: 0,
  88. height: 0,
  89. allWidth: 0,
  90. money: ''
  91. }
  92. },
  93. created() {
  94. },
  95. computed: {
  96. //ios底部安全区域
  97. safeAreaBottom() {
  98. let info = uni.getSystemInfoSync()
  99. let safe = 20
  100. if (
  101. info && ['devtools', 'ios'].includes(info.platform) &&
  102. info.statusBarHeight > safe
  103. ) {
  104. return info.statusBarHeight - safe
  105. }
  106. return 0
  107. }
  108. },
  109. watch: {
  110. money(val) {
  111. this.$emit('change', val);
  112. }
  113. },
  114. methods: {
  115. show() {
  116. this.isShow = true;
  117. },
  118. hide() {
  119. this.isShow = false;
  120. },
  121. //处理按键
  122. _handleKeyPress(num) {
  123. uni.vibrateShort();
  124. if (num == -1) return false;
  125. switch (String(num)) {
  126. //小数点
  127. case '.':
  128. this._handleDecimalPoint();
  129. break;
  130. //删除键
  131. case 'delete':
  132. this._handleDeleteKey();
  133. break;
  134. //确认键
  135. case 'confirm':
  136. this._handleConfirmKey();
  137. break;
  138. default:
  139. this._handleNumberKey(num);
  140. break;
  141. }
  142. },
  143. //处理小数点函数
  144. _handleDecimalPoint() {
  145. //如果包含小数点,直接返回
  146. if (this.money.indexOf('.') > -1) return false;
  147. //如果小数点是第一位,补0
  148. if (!this.money.length) {
  149. this.money = '0.'
  150. } else {
  151. this.money = this.money + '.';
  152. }
  153. },
  154. //处理删除键
  155. _handleDeleteKey() {
  156. let S = this.money;
  157. //如果没有输入,直接返回
  158. if (!S.length) return false;
  159. //否则删除最后一个
  160. this.money = S.substring(0, S.length - 1);
  161. },
  162. //处理数字
  163. _handleNumberKey(num) {
  164. if (Number(this.money + num) > this.maxNumber) {
  165. return
  166. }
  167. let S = this.money;
  168. //如果有小数点且小数点位数不小于2
  169. if (S.indexOf('.') > -1 && S.substring(S.indexOf('.') + 1).length < 2)
  170. this.money = S + num;
  171. if (S.indexOf('.') > -1 && S.substring(S.indexOf('.') + 1).length >= 2)
  172. return
  173. //没有小数点
  174. if (!(S.indexOf('.') > -1)) {
  175. //如果第一位是0,只能输入小数点
  176. if (num == 0 && S.length == 0)
  177. this.money = '0.';
  178. else {
  179. if (S.length && Number(S.charAt(0)) === 0) return;
  180. this.money = S + num;
  181. }
  182. }
  183. },
  184. //提交
  185. _handleConfirmKey() {
  186. let S = this.money;
  187. //未输入
  188. // if (!S.length || S == 0) {
  189. // uni.showToast({
  190. // title: this.placeholder,
  191. // icon: 'none',
  192. // duration: 1000
  193. // });
  194. // return false;
  195. // }
  196. //将 8. 这种转换成 8.00
  197. if (S.indexOf('.') > -1 && S.indexOf('.') == (S.length - 1))
  198. S = Number(S.substring(0, S.length - 1)).toFixed(2);
  199. //保留两位
  200. S = Number(S).toFixed(2);
  201. this.$emit('confirm', parseFloat(S)); //提交参数
  202. }
  203. }
  204. }
  205. </script>
  206. <style lang="less" scoped>
  207. .data-content {
  208. font-size: 32rpx;
  209. font-weight: 800;
  210. padding: 50rpx;
  211. }
  212. .keyboard-main {
  213. width: 100%;
  214. display: flex;
  215. justify-content: flex-start;
  216. align-items: flex-end;
  217. padding: 20rpx 0;
  218. box-sizing: initial;
  219. color: #1B1B1B;
  220. text {
  221. font-size: 24px;
  222. margin-right: 10px;
  223. }
  224. }
  225. .keyboard-content {
  226. width: 100%;
  227. height: 30px;
  228. position: relative;
  229. .placeholder {
  230. position: absolute;
  231. top: 0;
  232. left: 0;
  233. width: 100%;
  234. color: #c0c4cc;
  235. display: flex;
  236. justify-content: flex-start;
  237. align-items: flex-end;
  238. }
  239. .input {
  240. position: absolute;
  241. top: 0;
  242. left: 0;
  243. width: 100%;
  244. display: flex;
  245. justify-content: flex-start;
  246. align-items: flex-end;
  247. font-size: 30px;
  248. font-weight: bold;
  249. &.zIndex {
  250. z-index: 999;
  251. }
  252. #text {
  253. display: flex;
  254. justify-content: flex-start;
  255. align-items: flex-end;
  256. }
  257. .line {
  258. display: inline-block;
  259. width: 4rpx;
  260. height: 30px;
  261. border: 2rpx;
  262. background-color: #0063E5;
  263. margin-left: 8rpx;
  264. animation: cursorImg 1s infinite steps(1, start);
  265. @keyframes cursorImg {
  266. 0%,
  267. 100% {
  268. opacity: 0;
  269. }
  270. 50% {
  271. opacity: 1;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. .mask {
  278. position: fixed;
  279. top: 0;
  280. left: 0;
  281. right: 0;
  282. bottom: 0;
  283. z-index: 1;
  284. }
  285. .keyboard {
  286. flex: 1;
  287. position: fixed;
  288. bottom: 0;
  289. left: 0;
  290. width: 100%;
  291. background-color: #f6f6f6;
  292. padding: 20rpx;
  293. transition: all 0.6s;
  294. z-index: 9;
  295. box-sizing: border-box;
  296. &.noPadding {
  297. padding: 0;
  298. bottom: -100%;
  299. }
  300. }
  301. .keyboard .key-row {
  302. display: flex;
  303. justify-content: space-between;
  304. display: -webkit-flex;
  305. position: relative;
  306. box-sizing: border-box;
  307. }
  308. .keyboard .key-cell {
  309. font-size: 18px;
  310. width: 160rpx;
  311. font-weight: 800;
  312. height: 90rpx;
  313. /* margin-right: 20rpx; */
  314. margin-bottom: 20rpx;
  315. background-color: #fff;
  316. border-radius: 4rpx;
  317. display: flex;
  318. justify-content: center;
  319. align-items: center;
  320. &.last-child {
  321. /* margin-right: 0px; */
  322. width: 170rpx;
  323. }
  324. .icon {
  325. width: 50rpx;
  326. height: 40rpx;
  327. }
  328. }
  329. .keyboard .key-confirm {
  330. position: fixed;
  331. text-align: center;
  332. /* min-height: 100rpx; */
  333. width: 170rpx;
  334. padding: 20rpx 30rpx;
  335. border-radius: 6rpx;
  336. box-sizing: border-box;
  337. color: #FFFFFF;
  338. z-index: 10;
  339. right: 20rpx;
  340. top: calc(100vh - 280rpx);
  341. bottom: 200rpx;
  342. display: flex;
  343. justify-content: center;
  344. align-items: center;
  345. font-size: 20px;
  346. letter-spacing: 4px;
  347. font-weight: bold;
  348. transition: all 0.3s;
  349. &.big {
  350. right: 20rpx;
  351. bottom: 20rpx;
  352. padding: 0 30rpx;
  353. bottom: 20rpx;
  354. }
  355. &.frist {
  356. position: absolute;
  357. top: auto;
  358. right: 20rpx;
  359. height: 304rpx;
  360. bottom: 18rpx;
  361. }
  362. }
  363. .key-zero-and-point {
  364. display: flex;
  365. height: 80rpx;
  366. justify-content: space-between;
  367. align-items: center;
  368. font-size: 20px;
  369. font-weight: 800;
  370. .zero {
  371. display: flex;
  372. justify-content: center;
  373. align-items: center;
  374. width: 340rpx;
  375. text-align: center;
  376. background-color: #fff;
  377. border-radius: 4rpx;
  378. height: 100%;
  379. }
  380. .point {
  381. display: flex;
  382. justify-content: center;
  383. align-items: center;
  384. background-color: #fff;
  385. border-radius: 4rpx;
  386. width: 160rpx;
  387. height: 100%;
  388. }
  389. .last-child {
  390. background-color: #fff;
  391. border-radius: 8rpx;
  392. width: 170rpx;
  393. height: 100%;
  394. }
  395. }
  396. .key-cell-num:active {
  397. color: white;
  398. background: black; //黑色
  399. opacity: 0.1; //这里重要,就是通过这个透明度来设置
  400. }
  401. // .a:active,.key-confirm2:active{
  402. // color: white;
  403. // background: black; //黑色
  404. // opacity: 0.1; //这里重要,就是通过这个透明度来设置
  405. // }
  406. #input-text {
  407. width: max-content;
  408. position: fixed;
  409. bottom: -200%;
  410. }
  411. </style>