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

简介:

微信小程序自定义键盘插件wcKeyboard|仿微信数字软键盘|仿支付宝自定义数字键盘|小程序自定义模拟系统键盘

36e544fbd14022b6fe3bc5ce59a13c1e8e3af05e

前段时间有开发过一个html5仿支付宝、微信支付数字键盘,在某些情况下自定义数字键盘应用还是蛮多的,比如 购物商城系统 零钱付款 ,会员卡支付,恰好微信小程序没有内部数字键盘组件,这样输入密码就需要自己做一个自定义软键盘了。于是就在之前插件的基础上试着开发出了这个小程序wcKeyboard数字键盘插件。

17e2422604cfc57ba78c60e7d63ad40aaea04390

551f59efcb4db226fa8d8d6db107f974e48aecbd 99d92e1e49dde6ded25576672fdde039079c57e3

85a58590eb6b6d0d6832d4f6e60c3ea71d8d1e12 3055a8719f4e18816d1298ef1e95fa8f13bbce5b

d228ca7cf2b905a18effa0f2349aac837440d315 71a6ec0da8161a18f3cfbaa0e5da61d69ba2dd7d

989d89e2c44f0b46867bf57ced8add9b92d9da7f 0668062c226d647dfe26368796c16b494054a2fc


init: function () {
console.log('初始化');

var that = this, opt = that.opts;
// 处理传参
__this.setData({
  __options: {
	isCloseCls: null,
	__idx: __idx,
	isShowPopup: true,

	//中间值
	kbVal: '', //设置调试默认值
	err: false, //键盘错误信息提示

	debug: opt.debug,

	id: opt.id,
	type: opt.type,
	len: opt.len,
	complete: opt.complete,
	max: opt.max,
	style: opt.style,
	skin: opt.skin,
	ok: opt.ok,
	oninput: opt.oninput,

	shade: opt.shade,
	shadeClose: opt.shadeClose,
	opacity: opt.opacity,

	anim: opt.anim
  }
});

opt.show && opt.show.call(this);
this.__idx = __idx++;
that.callback();
},
callback: function () {
console.log('事件处理');

var that = this, opt = that.opts;
// 清除上一个timer
clearTimeout(util.timer[that.__idx - 1]);
delete util.timer[that.__idx - 1];

// 错误提示
function chkErr(cls, str){
  __this.setData({ '__options.err': [cls, str] });
  setTimeout(function(){
	__this.setData({ '__options.err': false });
  }, 2500);
}
// 键盘值检测
function chkVal(text){
  if (text.indexOf('.') != -1 && text.substring(text.indexOf('.') + 1, text.length).length == 3) {
	return;
  }
  if (text == '0') {
	return;
  }
  // 输入最大值限制
  if (opt.max) {
	if (parseInt(text) >= opt.max && text.indexOf('.') == -1) {
	  chkErr("error", "最大限制值:" + opt.max.toFixed(2));
	  return;
	}
  }
  // 输入手机号码判断
  if (opt.type && opt.type == 'tel') {
	var tel = text, _len = parseInt(tel.length), reg = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
	if (_len > 11) return;

	if (_len == 11) {
	  if (!reg.test(tel)) {
		chkErr("error", "手机号码格式有误!");
	  } else {
		chkErr("success", "验证通过!");
	  }
	  typeof opt.complete == "function" && opt.complete.call(this, text);
	}
  }
  // 输入密码长度判断
  if (opt.type && opt.type == 'pwd') {
	var _len = parseInt(text.length);
	if (_len > opt.len) return;
	if (_len == opt.len) {
	  typeof opt.complete == "function" && opt.complete.call(this, text);
	}
  }
  return true;
}
// 键盘值输出
function setVal(text){
  __this.setData({ '__options.kbVal': text });

  typeof opt.oninput == "function" && opt.oninput.call(this, text);
}
// 处理数字1-9
__this.tapNum = function(e){
  var kbval = this.data.__options.kbVal, text = e.currentTarget.dataset.text;
  var val = kbval + text;
  if (!chkVal(val)) return;

  setVal(val);
}

// 处理小数点
__this.tapFloat = function(e){
  var kbval = this.data.__options.kbVal, text = e.currentTarget.dataset.text;
  if(kbval == '' || kbval.indexOf('.') != -1){
	return;
  }
  var val = kbval + text;
  setVal(val);
}

// 处理数字0
__this.tapZero = function(e){
  var kbval = this.data.__options.kbVal, text = e.currentTarget.dataset.text;
  var val = kbval + text;
  if (!chkVal(val)) return;

  setVal(val);
}

// 处理删除
__this.tapDel = function(e){
  var val = this.data.__options.kbVal.substring(0, this.data.__options.kbVal.length - 1);
  setVal(val);
}

// 处理确定按钮事件
__this.tapSure = function(e){
  var kbval = this.data.__options.kbVal;
  typeof opt.ok == "function" && opt.ok.call(this, kbval);
}


/*
  ---------------------------------------
*/
// 点击遮罩层关闭
__this.shadeTaped = function (e) {
  if (!opt.shadeClose) return;
  exportAPI.close(that.__idx);
}
// 点击键盘xclose按钮关闭
__this.xcloseTaped = function(e){
  exportAPI.close(that.__idx);
}
// 处理销毁函数
opt.end && (util.end[that.__idx] = opt.end);

}

<!-- //支付宝键盘布局 -->
<view class="ul">
	<view class="li kb-limit nbor {{err[0]}}">{{err[1]}}</view>
	<view class="li kb-pwd nbor" wx:if="{{type&&type=='pwd'}}"><view class="keyboard__panel-pwd"><label class="kb-pwdlbl" wx:for="{{len}}" wx:key="index"><input type="password" maxlength="1" value="{{kbVal[index]}}" disabled /></label></view></view>
	<view class="li kb-result nbor" style="display:{{debug ? 'block' : 'none'}};">{{kbVal}}</view>

	<view class="kb-flexbox flexbox">
	  <view class="kb-one flex1">
		<view class="li kb-number nbor" data-text="1" bind:tap="tapNum">1</view>
		<view class="li kb-number" data-text="2" bind:tap="tapNum">2</view>
		<view class="li kb-number" data-text="3" bind:tap="tapNum">3</view>
		<view class="li kb-number nbor" data-text="4" bind:tap="tapNum">4</view>
		<view class="li kb-number" data-text="5" bind:tap="tapNum">5</view>
		<view class="li kb-number" data-text="6" bind:tap="tapNum">6</view>
		<view class="li kb-number nbor" data-text="7" bind:tap="tapNum">7</view>
		<view class="li kb-number" data-text="8" bind:tap="tapNum">8</view>
		<view class="li kb-number" data-text="9" bind:tap="tapNum">9</view>
		<view class="li kb-float nbor {{type=='tel' || type=='pwd' ? 'disabled' : ''}}" data-text="." bind:tap="tapFloat">.</view>
		<view class="li kb-zero" data-text="0" bind:tap="tapZero">0</view>
		<view class="li kb-xclose" bind:touchstart="xcloseTaped"><image src="/utils/component/wcKeyboard/img/icon__kb-xclose2.png" mode="aspectFit"></image></view>
	  </view>
	  <view class="kb-two">
		<view class="li kb-del" bind:tap="tapDel"><image src="/utils/component/wcKeyboard/img/icon__kb-del.png" mode="aspectFit"></image></view>
		<view class="li kb-sure" bind:tap="tapSure">确定</view>
	  </view>
	</view>
</view>



目录
相关文章
|
13天前
|
移动开发 小程序 JavaScript
uni-app开发微信小程序
本文详细介绍如何使用 uni-app 开发微信小程序,涵盖需求分析、架构思路及实施方案。主要功能包括用户登录、商品列表展示、商品详情、购物车及订单管理。技术栈采用 uni-app、uView UI 和 RESTful API。文章通过具体示例代码展示了从初始化项目、配置全局样式到实现各页面组件及 API 接口的全过程,并提供了完整的文件结构和配置文件示例。此外,还介绍了微信授权登录及后端接口模拟方法,确保项目的稳定性和安全性。通过本教程,读者可快速掌握使用 uni-app 开发微信小程序的方法。
37 3
|
21天前
|
小程序 开发者
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
本文介绍了如何在Taro项目中实现自定义tabBar。首先,在`app.config.ts`中设置`custom: true`并配置`tabBar`。
Taro@3.x+Vue@3.x+TS开发微信小程序,使用自定义tabBar
|
24天前
|
移动开发 小程序 前端开发
|
1月前
|
JSON 小程序 JavaScript
超详细微信小程序开发学习笔记,看完你也可以动手做微信小程序项目
这篇文章是一份全面的微信小程序开发学习笔记,涵盖了从小程序介绍、环境搭建、项目创建、开发者工具使用、文件结构、配置文件、模板语法、事件绑定、样式规范、组件使用、自定义组件开发到小程序生命周期管理等多个方面的详细教程和指南。
|
30天前
|
小程序 前端开发
微信小程序商城,微信小程序微店 【毕业设计参考项目】
文章推荐了一个微信小程序商城项目作为毕业设计参考,该项目在Github上获得18.2k星,提供了详细的使用教程和前端页面实现,适合学习微信小程序开发和作为毕业设计项目。
微信小程序商城,微信小程序微店 【毕业设计参考项目】
|
1月前
|
小程序 开发者
第一个微信小程序的初始化过程、小程序微信开发平台的下载、如何注册一个微信小程序的账号
这篇文章介绍了微信小程序的初始化过程,包括如何注册微信小程序账号、下载微信小程序开发者平台,并指导了新建小程序的详细步骤。
第一个微信小程序的初始化过程、小程序微信开发平台的下载、如何注册一个微信小程序的账号
|
1月前
|
小程序 前端开发 JavaScript
微信小程序实现微信支付(代码和注释很详细)
微信小程序实现微信支付(代码和注释很详细)
|
2月前
|
小程序
尝试使用阿里云服务器搭建微信小程序
华北电力大学核工程大一学生,出于对编程的热爱与大创项目需求,涉足微信小程序搭建。初期在实验指导下克服不熟悉编程的困难,但后期发现教程引导不足,尤其是对于代码定位缺乏清晰指引。建议加强网页图像指导,以适应不同编程水平用户,尤其是新手。
尝试使用阿里云服务器搭建微信小程序
|
1月前
|
小程序 前端开发 Java
|
1月前
|
小程序
uni-app——微信小程序设置全局分享
uni-app——微信小程序设置全局分享
48 0