keyboard.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class='keyboard' @click.stop='_handleKeyPress'>
  3. <view class='key-row'>
  4. <view class='key-cell cell_b' data-num='7'>7</view>
  5. <view class='key-cell cell_b' data-num='8'>8</view>
  6. <view class='key-cell cell_b' data-num='9'>9</view>
  7. <view class='key-cell cell_b' data-num='-1'></view>
  8. </view>
  9. <view class='key-row'>
  10. <view class='key-cell cell_b' data-num='4'>4</view>
  11. <view class='key-cell cell_b' data-num='5'>5</view>
  12. <view class='key-cell cell_b' data-num='6'>6</view>
  13. <view class='key-cell cell_b' data-num='-1'></view>
  14. </view>
  15. <view class='key-row'>
  16. <view class='key-cell cell_b' data-num='1'>1</view>
  17. <view class='key-cell cell_b' data-num='2'>2</view>
  18. <view class='key-cell cell_b' data-num='3'>3</view>
  19. <view class='key-cell cell_b' data-num='-1'></view>
  20. </view>
  21. <view class="key-zero-and-point">
  22. <view class="a cell_b zero" data-num='0'>0</view>
  23. <view class="a cell_b point" data-num='.'>.</view>
  24. </view>
  25. <view @touchstart="touchstart" @touchend="touchend" data-num='D' class="key-confirm2">
  26. <text data-num='D'>C</text>
  27. </view>
  28. <view class='key-confirm' :style="{'background':btnColor}" data-num='S'>
  29. <view data-num='S' class="">
  30. <view data-num='S' class="title">{{title}}</view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. export default{
  37. name:"keyBoard",
  38. props:{
  39. title:{
  40. default:'确认',
  41. type:String
  42. },
  43. btnColor:{
  44. default:'green',
  45. }
  46. },
  47. data(){
  48. return {
  49. money:'',
  50. Cdel:'',
  51. Time:''
  52. }
  53. },
  54. watch:{
  55. money(val){
  56. this.$emit('update:money',val);
  57. }
  58. },
  59. methods : {
  60. touchstart(){
  61. this.Time=setInterval(()=>{
  62. console.log(this.money);
  63. if(this.money==''){
  64. clearInterval();
  65. }
  66. this.money = this.money.substring(0,this.money.length - 1);
  67. },200)
  68. },
  69. touchend(){
  70. clearInterval(this.Time);
  71. },
  72. //处理按键
  73. _handleKeyPress(e) {
  74. let num = e.target.dataset.num;
  75. //不同按键处理逻辑
  76. // -1 代表无效按键,直接返回
  77. if (num == -1) return false;
  78. switch (String(num)) {
  79. //小数点
  80. case '.':
  81. this._handleDecimalPoint();
  82. break;
  83. //删除键
  84. case 'D':
  85. this.$parent.payAmount = ''
  86. this._handleDeleteKey();
  87. break;
  88. //清空键
  89. case 'C':
  90. this._handleClearKey();
  91. break;
  92. //确认键
  93. case 'S':
  94. this._handleConfirmKey();
  95. break;
  96. default:
  97. this._handleNumberKey(num);
  98. break;
  99. }
  100. },
  101. //处理小数点函数
  102. _handleDecimalPoint() {
  103. //如果包含小数点,直接返回
  104. if (this.money.indexOf('.') > -1) return false;
  105. //如果小数点是第一位,补0
  106. if (!this.money.length)
  107. this.money = '0.';
  108. //如果不是,添加一个小数点
  109. else
  110. this.money = this.money + '.';
  111. },
  112. //处理删除键
  113. _handleDeleteKey() {
  114. let S = this.money;
  115. //如果没有输入,直接返回
  116. if (!S.length) return false;
  117. //否则删除最后一个
  118. this.money = S.substring(0, S.length - 1);
  119. },
  120. //处理清空键
  121. _handleClearKey() {
  122. this.money = '';
  123. },
  124. //处理数字
  125. _handleNumberKey(num) {
  126. if(this.money.length==10){
  127. return
  128. }
  129. let S = this.money;
  130. //如果有小数点且小数点位数不小于2
  131. if ( S.indexOf('.') > -1 && S.substring(S.indexOf('.') + 1).length < 2)
  132. this.money = S + num;
  133. //没有小数点
  134. if (!(S.indexOf('.') > -1)) {
  135. //如果第一位是0,只能输入小数点
  136. if (num == 0 && S.length == 0)
  137. this.money = '0.';
  138. else {
  139. if (S.length && Number(S.charAt(0)) === 0) return;
  140. this.money = S + num;
  141. }
  142. }
  143. },
  144. //提交
  145. _handleConfirmKey() {
  146. let S = this.money;
  147. //未输入
  148. if (!S.length||S==0){
  149. uni.showToast({
  150. title: '请输入支付金额',
  151. icon:'none',
  152. duration: 1000
  153. });
  154. return false;
  155. }
  156. //将 8. 这种转换成 8.00
  157. if (S.indexOf('.') > -1 && S.indexOf('.') == (S.length - 1))
  158. S = Number(S.substring(0, S.length - 1)).toFixed(2);
  159. //保留两位
  160. S = Number(S).toFixed(2);
  161. this.$emit('confirmEvent',S); //提交参数
  162. }
  163. }
  164. }
  165. </script>
  166. <style lang="less" scoped>
  167. .cell_b{
  168. border-right: 1px solid #d5d5d6;
  169. border-bottom: 1px solid #d5d5d6;
  170. }
  171. .key-container {
  172. width: 100%;
  173. display: flex;
  174. flex-direction: column;
  175. }
  176. .keyboard {
  177. flex: 1;
  178. position: fixed;
  179. bottom: 0;
  180. left: 0;
  181. height: 600rpx;
  182. width: 100%;
  183. background: #FFFFFF;
  184. }
  185. .keyboard .key-row {
  186. display: flex;
  187. display: -webkit-flex;
  188. position: relative;
  189. height: 150rpx;
  190. line-height: 150rpx;
  191. }
  192. .keyboard .key-cell {
  193. flex: 1;
  194. -webkit-box-flex: 1;
  195. font-size: 60upx;
  196. display: flex;
  197. justify-content: center;
  198. align-items: center;
  199. }
  200. .keyboard .key-confirm {
  201. position: absolute;
  202. text-align: center;
  203. height: 450rpx;
  204. width: 25%;
  205. line-height: 450rpx;
  206. color: #FFFFFF;
  207. z-index: 5;
  208. right: 0;
  209. bottom: 0;
  210. display:flex;
  211. justify-content: center;
  212. align-items: center;
  213. }
  214. .keyboard .key-confirm2 {
  215. position: absolute;
  216. height: 150rpx;
  217. width: 25%;
  218. line-height: 150rpx;
  219. z-index: 9999;
  220. right: 0;
  221. top: 0;
  222. display: flex;
  223. justify-content: center;
  224. align-items: center;
  225. }
  226. .key-zero-and-point{
  227. display: flex;height: 150rpx;justify-content: center;align-items: center;width:75%;font-size: 60upx;
  228. .zero{
  229. display: flex;justify-content: center;align-items: center;width: 67.66%;font-size: 60upx;text-align: center;height: 100%;
  230. }
  231. .point{
  232. display: flex;justify-content: center;align-items: center;width: 33.33%;font-size: 60upx;text-align: center;height: 100%;
  233. }
  234. }
  235. .key-cell:active{
  236. color: white;
  237. background: black; //黑色
  238. opacity: 0.1; //这里重要,就是通过这个透明度来设置
  239. }
  240. .a:active,.key-confirm2:active{
  241. color: white;
  242. background: black; //黑色
  243. opacity: 0.1; //这里重要,就是通过这个透明度来设置
  244. }
  245. </style>