amountInput.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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.substring(0,2)}}
  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. {{confirmText}}
  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: false,
  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. initialMoney(data){
  116. this.money=data.toString()
  117. },
  118. show() {
  119. this.isShow = true;
  120. },
  121. hide() {
  122. this.isShow = false;
  123. },
  124. //处理按键
  125. _handleKeyPress(num) {
  126. uni.vibrateShort();
  127. if (num == -1) return false;
  128. switch (String(num)) {
  129. //小数点
  130. case '.':
  131. this._handleDecimalPoint();
  132. break;
  133. //删除键
  134. case 'delete':
  135. this._handleDeleteKey();
  136. break;
  137. //确认键
  138. case 'confirm':
  139. this._handleConfirmKey();
  140. break;
  141. default:
  142. this._handleNumberKey(num);
  143. break;
  144. }
  145. },
  146. //处理小数点函数
  147. _handleDecimalPoint() {
  148. //如果包含小数点,直接返回
  149. if (this.money.indexOf('.') > -1) return false;
  150. //如果小数点是第一位,补0
  151. if (!this.money.length) {
  152. this.money = '0.'
  153. } else {
  154. this.money = this.money + '.';
  155. }
  156. },
  157. //处理删除键
  158. _handleDeleteKey() {
  159. let S = this.money;
  160. //如果没有输入,直接返回
  161. if (!S.length) return false;
  162. //否则删除最后一个
  163. this.money = S.substring(0, S.length - 1);
  164. },
  165. //处理数字
  166. _handleNumberKey(num) {
  167. if (Number(this.money + num) > this.maxNumber) {
  168. uni.showToast({
  169. title:"不能超过最大交易金额",
  170. icon:"none"
  171. })
  172. return
  173. }
  174. let S = this.money;
  175. //如果有小数点且小数点位数不小于2
  176. if (S.indexOf('.') > -1 && S.substring(S.indexOf('.') + 1).length < 2)
  177. this.money = S + num;
  178. if (S.indexOf('.') > -1 && S.substring(S.indexOf('.') + 1).length >= 2)
  179. return
  180. //没有小数点
  181. if (!(S.indexOf('.') > -1)) {
  182. //如果第一位是0,只能输入小数点
  183. if (num == 0 && S.length == 0)
  184. this.money = '0.';
  185. else {
  186. if (S.length && Number(S.charAt(0)) === 0) return;
  187. this.money = S + num;
  188. }
  189. }
  190. },
  191. //提交
  192. _handleConfirmKey() {
  193. let S = this.money;
  194. //未输入
  195. if (!S.length || S == 0) {
  196. uni.showToast({
  197. title: this.placeholder,
  198. icon: 'none',
  199. duration: 1000
  200. });
  201. return false;
  202. }
  203. //将 8. 这种转换成 8.00
  204. if (S.indexOf('.') > -1 && S.indexOf('.') == (S.length - 1))
  205. S = Number(S.substring(0, S.length - 1)).toFixed(2);
  206. //保留两位
  207. S = Number(S).toFixed(2);
  208. this.$emit('confirm', parseFloat(S)); //提交参数
  209. }
  210. }
  211. }
  212. </script>
  213. <style lang="less" scoped>
  214. .data-content {
  215. font-size: 32rpx;
  216. font-weight: 800;
  217. padding: 50rpx;
  218. }
  219. .keyboard-main {
  220. width: 100%;
  221. display: flex;
  222. justify-content: flex-start;
  223. align-items: flex-end;
  224. padding: 20rpx 0;
  225. box-sizing: initial;
  226. color: #1B1B1B;
  227. text {
  228. font-size: 24px;
  229. margin-right: 10px;
  230. }
  231. }
  232. .keyboard-content {
  233. width: 100%;
  234. height: 30px;
  235. position: relative;
  236. .placeholder {
  237. position: absolute;
  238. top: 0;
  239. left: 0;
  240. width: 100%;
  241. color: #c0c4cc;
  242. display: flex;
  243. justify-content: flex-start;
  244. align-items: flex-end;
  245. }
  246. .input {
  247. position: absolute;
  248. top: 0;
  249. left: 0;
  250. width: 100%;
  251. display: flex;
  252. justify-content: flex-start;
  253. align-items: flex-end;
  254. font-size: 30px;
  255. font-weight: bold;
  256. &.zIndex {
  257. z-index: 999;
  258. }
  259. #text {
  260. display: flex;
  261. justify-content: flex-start;
  262. align-items: flex-end;
  263. }
  264. .line {
  265. display: inline-block;
  266. width: 4rpx;
  267. height: 30px;
  268. border: 2rpx;
  269. background-color: #0063E5;
  270. margin-left: 8rpx;
  271. animation: cursorImg 1s infinite steps(1, start);
  272. @keyframes cursorImg {
  273. 0%,
  274. 100% {
  275. opacity: 0;
  276. }
  277. 50% {
  278. opacity: 1;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. .mask {
  285. position: fixed;
  286. top: 0;
  287. left: 0;
  288. right: 0;
  289. bottom: 0;
  290. z-index: 1;
  291. }
  292. .keyboard {
  293. flex: 1;
  294. position: fixed;
  295. bottom: 0;
  296. left: 0;
  297. width: 100%;
  298. background-color: #f6f6f6;
  299. padding: 20rpx;
  300. transition: all 0.6s;
  301. z-index: 9;
  302. box-sizing: border-box;
  303. &.noPadding {
  304. padding: 0;
  305. bottom: -100%;
  306. }
  307. }
  308. .keyboard .key-row {
  309. display: flex;
  310. justify-content: space-between;
  311. display: -webkit-flex;
  312. position: relative;
  313. box-sizing: border-box;
  314. }
  315. .keyboard .key-cell {
  316. font-size: 18px;
  317. width: 160rpx;
  318. font-weight: 800;
  319. height: 90rpx;
  320. /* margin-right: 20rpx; */
  321. margin-bottom: 20rpx;
  322. background-color: #fff;
  323. border-radius: 4rpx;
  324. display: flex;
  325. justify-content: center;
  326. align-items: center;
  327. &.last-child {
  328. /* margin-right: 0px; */
  329. width: 170rpx;
  330. }
  331. .icon {
  332. width: 50rpx;
  333. height: 40rpx;
  334. }
  335. }
  336. .keyboard .key-confirm {
  337. position: fixed;
  338. text-align: center;
  339. /* min-height: 100rpx; */
  340. width: 170rpx;
  341. padding: 20rpx 30rpx;
  342. border-radius: 6rpx;
  343. box-sizing: border-box;
  344. color: #FFFFFF;
  345. z-index: 10;
  346. right: 20rpx;
  347. top: calc(100vh - 280rpx);
  348. bottom: 200rpx;
  349. display: flex;
  350. justify-content: center;
  351. align-items: center;
  352. font-size: 20px;
  353. letter-spacing: 4px;
  354. font-weight: bold;
  355. transition: all 0.3s;
  356. &.big {
  357. right: 20rpx;
  358. bottom: 20rpx;
  359. padding: 0 30rpx;
  360. bottom: 20rpx;
  361. }
  362. &.frist {
  363. position: absolute;
  364. top: auto;
  365. right: 20rpx;
  366. height: 304rpx;
  367. bottom: 18rpx;
  368. }
  369. }
  370. .key-zero-and-point {
  371. display: flex;
  372. height: 80rpx;
  373. justify-content: space-between;
  374. align-items: center;
  375. font-size: 20px;
  376. font-weight: 800;
  377. .zero {
  378. display: flex;
  379. justify-content: center;
  380. align-items: center;
  381. width: 340rpx;
  382. text-align: center;
  383. background-color: #fff;
  384. border-radius: 4rpx;
  385. height: 100%;
  386. }
  387. .point {
  388. display: flex;
  389. justify-content: center;
  390. align-items: center;
  391. background-color: #fff;
  392. border-radius: 4rpx;
  393. width: 160rpx;
  394. height: 100%;
  395. }
  396. .last-child {
  397. background-color: #fff;
  398. border-radius: 8rpx;
  399. width: 170rpx;
  400. height: 100%;
  401. }
  402. }
  403. .key-cell-num:active {
  404. color: white;
  405. background: black; //黑色
  406. opacity: 0.1; //这里重要,就是通过这个透明度来设置
  407. }
  408. // .a:active,.key-confirm2:active{
  409. // color: white;
  410. // background: black; //黑色
  411. // opacity: 0.1; //这里重要,就是通过这个透明度来设置
  412. // }
  413. #input-text {
  414. width: max-content;
  415. position: fixed;
  416. bottom: -200%;
  417. }
  418. </style>