您当前的位置: 首页 > 解决方案

微信小程序自定义数字键盘|仿支付宝、微信支付数字软键盘

  • 作者: admin
  • 发布于 2018-12-18 09:23:50
  • 来源:  
  • 栏目:解决方案

导语: 前几天有开发过一个html5仿支付宝、微信支付数字键盘,在某些特定场景下自定义数字键盘应用还是蛮广泛的,比如 小程序商

 

01.jpg

前几天有开发过一个html5仿支付宝、微信支付数字键盘,在某些特定场景下自定义数字键盘应用还是蛮广泛的,比如 小程序商城系统 需要零钱支付 ,会员卡支付,心想着微信小程序没有内部数字键盘组件,这样输入密码就需要自己做一个自定义软键盘了。于是就在之前插件的基础上试着开发出了这个小程序wcKeyboard数字键盘插件。02.jpg

03.jpg

可以自定义输入最大值限制,当输入超过最大值时候,会有警告提示:

04.jpg

还可以自定义键盘背景色 style: ' background: xxx; ',最好设置background颜色较浅为佳:

05.jpg

06.jpg

小程序数字键盘插件内置手机号码验证,当type:'tel'时,输入手机号码为11位会自动检测是否合法:

07.jpg

当配置type:'pwd', 则为密码键盘,可设置密码位数 len: 6

08.jpg

可以随意切换微信键盘、支付宝键盘 skin: wechat/alipay 两种皮肤风格:

09.jpg

10.jpg

 
  1. init: function () {
  2. console.log('初始化');
  3.  
  4. var that = this, opt = that.opts;
  5. // 处理传参
  6. __this.setData({
  7. __options: {
  8. isCloseCls: null,
  9. __idx: __idx,
  10. isShowPopup: true,
  11.  
  12. //中间值
  13. kbVal: '', //设置调试默认值
  14. err: false, //键盘错误信息提示
  15.  
  16. debug: opt.debug,
  17.  
  18. id: opt.id,
  19. type: opt.type,
  20. len: opt.len,
  21. complete: opt.complete,
  22. max: opt.max,
  23. style: opt.style,
  24. skin: opt.skin,
  25. ok: opt.ok,
  26. oninput: opt.oninput,
  27.  
  28. shade: opt.shade,
  29. shadeClose: opt.shadeClose,
  30. opacity: opt.opacity,
  31.  
  32. anim: opt.anim
  33. }
  34. });
  35.  
  36. opt.show && opt.show.call(this);
  37. this.__idx = __idx++;
  38. that.callback();
  39. },
  40. callback: function () {
  41. console.log('事件处理');
  42.  
  43. var that = this, opt = that.opts;
  44. // 清除上一个timer
  45. clearTimeout(util.timer[that.__idx - 1]);
  46. delete util.timer[that.__idx - 1];
  47.  
  48. /*
  49. * 键盘处理函数事件 ---------------------------------------
  50. */
  51. // 错误提示
  52. function chkErr(cls, str){
  53. __this.setData({ '__options.err': [cls, str] });
  54. setTimeout(function(){
  55. __this.setData({ '__options.err': false });
  56. }, 2500);
  57. }
  58. // 键盘值检测
  59. function chkVal(text){
  60. if (text.indexOf('.') != -1 && text.substring(text.indexOf('.') + 1, text.length).length == 3) {
  61. return;
  62. }
  63. if (text == '0') {
  64. return;
  65. }
  66. // 输入最大值限制
  67. if (opt.max) {
  68. if (parseInt(text) >= opt.max && text.indexOf('.') == -1) {
  69. chkErr("error", "最大限制值:" + opt.max.toFixed(2));
  70. return;
  71. }
  72. }
  73. // 输入手机号码判断
  74. if (opt.type && opt.type == 'tel') {
  75. var tel = text, _len = parseInt(tel.length), reg = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
  76. if (_len > 11) return;
  77.  
  78. if (_len == 11) {
  79. if (!reg.test(tel)) {
  80. chkErr("error", "手机号码格式有误!");
  81. } else {
  82. chkErr("success", "验证通过!");
  83. }
  84. typeof opt.complete == "function" && opt.complete.call(this, text);
  85. }
  86. }
  87. // 输入密码长度判断
  88. if (opt.type && opt.type == 'pwd') {
  89. var _len = parseInt(text.length);
  90. if (_len > opt.len) return;
  91. if (_len == opt.len) {
  92. typeof opt.complete == "function" && opt.complete.call(this, text);
  93. }
  94. }
  95. return true;
  96. }
  97. // 键盘值输出
  98. function setVal(text){
  99. __this.setData({ '__options.kbVal': text });
  100.  
  101. typeof opt.oninput == "function" && opt.oninput.call(this, text);
  102. }
  103. // 处理数字1-9
  104. __this.tapNum = function(e){
  105. var kbval = this.data.__options.kbVal, text = e.currentTarget.dataset.text;
  106. var val = kbval + text;
  107. if (!chkVal(val)) return;
  108.  
  109. setVal(val);
  110. }
  111.  
  112. // 处理小数点
  113. __this.tapFloat = function(e){
  114. var kbval = this.data.__options.kbVal, text = e.currentTarget.dataset.text;
  115. if(kbval == '' || kbval.indexOf('.') != -1){
  116. return;
  117. }
  118. var val = kbval + text;
  119. setVal(val);
  120. }
  121.  
  122. // 处理数字0
  123. __this.tapZero = function(e){
  124. var kbval = this.data.__options.kbVal, text = e.currentTarget.dataset.text;
  125. var val = kbval + text;
  126. if (!chkVal(val)) return;
  127.  
  128. setVal(val);
  129. }
  130.  
  131. // 处理删除
  132. __this.tapDel = function(e){
  133. var val = this.data.__options.kbVal.substring(0, this.data.__options.kbVal.length - 1);
  134. setVal(val);
  135. }
  136.  
  137. // 处理确定按钮事件
  138. __this.tapSure = function(e){
  139. var kbval = this.data.__options.kbVal;
  140. typeof opt.ok == "function" && opt.ok.call(this, kbval);
  141. }
  142.  
  143.  
  144. /*
  145. ---------------------------------------
  146. */
  147. // 点击遮罩层关闭
  148. __this.shadeTaped = function (e) {
  149. if (!opt.shadeClose) return;
  150. exportAPI.close(that.__idx);
  151. }
  152. // 点击键盘xclose按钮关闭
  153. __this.xcloseTaped = function(e){
  154. exportAPI.close(that.__idx);
  155. }
  156. // 处理销毁函数
  157. opt.end && (util.end[that.__idx] = opt.end);
  158.  
  159. }

 



温馨提示:这篇文章没有解决您的问题?欢迎添加微信:18948083295,有微信小程序专业人员,保证有问必答。转载本站文章请注明转自http://www.okeydown.com/(微信小程序网)。

  • 微信扫描二维码关注官方微信
  • ▲长按图片识别二维码
关注我们

微信小程序官方微信

栏目最新
栏目推荐
返回顶部