amountInput.vue 9.3 KB

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