keyboard.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 ac' 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'>删除</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. if(this.money==''){
  63. clearInterval();
  64. }
  65. this.money = this.money.substring(0,this.money.length - 1);
  66. },200)
  67. },
  68. touchend(){
  69. clearInterval(this.Time);
  70. },
  71. //处理按键
  72. _handleKeyPress(e) {
  73. let num = e.target.dataset.num;
  74. //不同按键处理逻辑
  75. // -1 代表无效按键,直接返回
  76. if (num == -1) return false;
  77. switch (String(num)) {
  78. //小数点
  79. case '.':
  80. this._handleDecimalPoint();
  81. break;
  82. //删除键
  83. case 'D':
  84. this.$parent.payAmount = ''
  85. this._handleDeleteKey();
  86. break;
  87. //清空键
  88. case 'C':
  89. this._handleClearKey();
  90. break;
  91. //确认键
  92. case 'S':
  93. this._handleConfirmKey();
  94. break;
  95. default:
  96. this._handleNumberKey(num);
  97. break;
  98. }
  99. },
  100. //处理小数点函数
  101. _handleDecimalPoint() {
  102. //如果包含小数点,直接返回
  103. if (this.money.indexOf('.') > -1) return false;
  104. //如果小数点是第一位,补0
  105. if (!this.money.length)
  106. this.money = '0.';
  107. //如果不是,添加一个小数点
  108. else
  109. this.money = this.money + '.';
  110. },
  111. //处理删除键
  112. _handleDeleteKey() {
  113. let S = this.money;
  114. //如果没有输入,直接返回
  115. if (!S.length) return false;
  116. //否则删除最后一个
  117. this.money = S.substring(0, S.length - 1);
  118. },
  119. //处理清空键
  120. _handleClearKey() {
  121. this.money = '';
  122. },
  123. //处理数字
  124. _handleNumberKey(num) {
  125. if(this.money.length==10){
  126. return
  127. }
  128. let S = this.money;
  129. //如果有小数点且小数点位数不小于2
  130. if ( S.indexOf('.') > -1 && S.substring(S.indexOf('.') + 1).length < 2)
  131. this.money = S + num;
  132. //没有小数点
  133. if (!(S.indexOf('.') > -1)) {
  134. //如果第一位是0,只能输入小数点
  135. if (num == 0 && S.length == 0)
  136. this.money = '0.';
  137. else {
  138. if (S.length && Number(S.charAt(0)) === 0) return;
  139. this.money = S + num;
  140. }
  141. }
  142. },
  143. //提交
  144. _handleConfirmKey() {
  145. let S = this.money;
  146. //未输入
  147. if (!S.length||S==0){
  148. S = this.$parent.payAmount
  149. // uni.showToast({
  150. // title: '请输入支付金额',
  151. // icon:'none',
  152. // duration: 1000
  153. // });
  154. // return false;
  155. }else{
  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. }
  162. this.$emit('confirmEvent',S); //提交参数
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="less" scoped>
  168. .cell_b{
  169. border-right: 1px solid #d5d5d6;
  170. border-bottom: 1px solid #d5d5d6;
  171. }
  172. .key-container {
  173. width: 100%;
  174. display: flex;
  175. flex-direction: column;
  176. }
  177. .keyboard {
  178. flex: 1;
  179. position: fixed;
  180. bottom: 0;
  181. left: 0;
  182. height: 600rpx;
  183. width: 100%;
  184. background: #FFFFFF;
  185. border-top: 1px solid #d5d5d6;
  186. }
  187. .keyboard .key-row {
  188. display: flex;
  189. display: -webkit-flex;
  190. position: relative;
  191. height: 150rpx;
  192. line-height: 150rpx;
  193. }
  194. .keyboard .key-cell {
  195. flex: 1;
  196. -webkit-box-flex: 1;
  197. font-size: 60upx;
  198. display: flex;
  199. justify-content: center;
  200. align-items: center;
  201. &.ac{
  202. border-bottom:none;
  203. }
  204. }
  205. .keyboard .key-confirm {
  206. position: absolute;
  207. text-align: center;
  208. height: 300rpx;
  209. width: 25%;
  210. line-height: 300rpx;
  211. color: #FFFFFF;
  212. z-index: 5;
  213. right: 0;
  214. bottom: 0;
  215. display:flex;
  216. justify-content: center;
  217. align-items: center;
  218. }
  219. .keyboard .key-confirm2 {
  220. position: absolute;
  221. height: 300rpx;
  222. width: 25%;
  223. line-height: 300rpx;
  224. z-index: 9999;
  225. right: 0;
  226. top: 0;
  227. display: flex;
  228. justify-content: center;
  229. align-items: center;
  230. background: #EBEBEB;
  231. }
  232. .key-zero-and-point{
  233. display: flex;height: 150rpx;justify-content: center;align-items: center;width:75%;font-size: 60upx;
  234. .zero{
  235. display: flex;justify-content: center;align-items: center;width: 67.66%;font-size: 60upx;text-align: center;height: 100%;
  236. }
  237. .point{
  238. display: flex;justify-content: center;align-items: center;width: 33.33%;font-size: 60upx;text-align: center;height: 100%;
  239. }
  240. }
  241. .key-cell:active{
  242. color: white;
  243. background: black; //黑色
  244. opacity: 0.1; //这里重要,就是通过这个透明度来设置
  245. }
  246. .a:active,.key-confirm2:active{
  247. color: white;
  248. background: black; //黑色
  249. opacity: 0.1; //这里重要,就是通过这个透明度来设置
  250. }
  251. </style>