支付宝小程序人脸识别认证

 博主最近在做小程序开发, 之前支付宝小程序使用的是芝麻人脸识别认证,因为996,暂时先放代码

1.芝麻认证

submit(e) {
		if (gData.isclick) return;
		gData.isclick = true
		var userName = _self.name
		var idCard = _self.idCard
		_self.$api.showLoading();
		_self.$api.zhimaCreditApply({
			userName,
			idCard,
		}).then(({
			data
		}) => {
			return new Promise(function(resolve, reject) {
				if (data.resultCode == 'Y') {
					const bizNo = data.bizNo;
					// 个人支付宝且存在可用于调用芝麻信用的单号就调用人脸识别
					if (bizNo && _self.userType == "2")
						my.startZMVerify({
							bizNo,
							success(res) {
								if (res.passed) {
									resolve();
								} else {
									reject(res.failed_reason);
								}
							},
							fail(err) {
								reject(err);
							}
						});
					else if (_self.userType == "1")
						resolve();
					else
						reject('认证失败,请联系客服进行反馈')
				} else {
					reject()
				}
			})
		}).then(() => {
			if (_self.userType == "2") {
				return _self.$api.zhimaCreditConfirm({})
			} else if (_self.userType == "1") {
				return Promise.resolve({})
			} else {
				return Promise.reject()
			}
		}).then(({
			data
		}) => {
			if (_self.userType == "1" || data.resultCode == 'Y') {
				console.log(data)
				uni.showToast({
					icon: "none",
					title: "认证成功",
					success: () => {
						_self.doLogin({
							anew: 1,
							success() {
								uni.navigateBack();
							}
						})
					}
				})
			}
			uni.hideLoading();
			gData.isclick = false
		}, function(err) {
			uni.hideLoading();
			gData.isclick = false
			if (err)
				_self.$api.alert(err);
		})
	},

	// 提交芝麻认证授权申请(姓名与身份证号码)
	function zhimaCreditApply(obj) {
		const userName = obj.userName;
		const idCard = obj.idCard;
		qcloud.request({
			url: api + 'user/zhimaCreditApply',
			data: {
				userName,
				idCard
			},
			login: true,
			success(res) {
				obj.res = res;
				success(obj);
			},
			fail(err) {
				obj.err = err;
				fail(obj)
			}
		});
	}

2. 支付宝认证

介绍:支付宝人脸识别认证是收费的功能,之前芝麻认证要下架,替换使用

//原芝麻认证  现支付宝认证
	submit() {
		if (isclick) {
			return isclick = true;
		}
		var that = this;
		var _self = this
		let userName = that.data.name
		let idCard = that.data.idcard
		let tradeNo = that.tradeNo().toString() //订单号
		let bizId = tradeNo;
		let userId = that.data.userId;
		console.log(userId)
		mcdj.zhimaCreditApply({
			userName,
			idCard,
			success(data) {
				if (data.resultCode == 'Y') {
					my.ap.faceVerify({
						bizId: tradeNo, //业务请求的唯一标识,需要保证唯一性
						bizType: '2', //业务场景参数,必须填写‘2’,代表刷脸认证  
						success: (res) => {
							var zimId = res.zimId;
							mcdj.faceCheck({
								'zimId': zimId,
								'bizId': bizId,
								'userId': userId,
								success: (e) => {
									let msg = _self.IdMsg(e.data.code)
									mcdj.showToast(msg)
									console.log(e)
								},
								fail: () => {
									mcdj.showToast('服务器繁忙,请稍后再试')
								}
							})
						},
						fail: () => {
							mcdj.showToast('服务器繁忙,请稍后再试')
						}
					});
				}
			}
		})


	},
	//生成 唯一订单号
	tradeNo() {
		const now = new Date()
		const year = now.getFullYear();
		let month = now.getMonth() + 1;
		let day = now.getDate();
		let hour = now.getHours();
		let minutes = now.getMinutes();
		let seconds = now.getSeconds();
		let milliseconds = now.getMilliseconds(); //毫秒
		String(month).length < 2 ? (month = "0" + month) : month;
		String(day).length < 2 ? (day = "0" + day) : day;
		String(hour).length < 2 ? (hour = "0" + hour) : hour;
		String(minutes).length < 2 ? (minutes = "0" + minutes) : minutes;
		String(seconds).length < 2 ? (seconds = "0" + seconds) : seconds;
		String(milliseconds).length < 2 ? (milliseconds = "0" + milliseconds) : milliseconds;
		const yyyyMMddHHmmss = year + '' + month + '' + day + '' + hour + '' + minutes + '' + seconds + '' + milliseconds;
		var ob = yyyyMMddHHmmss + Math.random().toString(36).substr(2, 9);
		console.log(ob)
		return ob;
	},
	//根据身份验证返回码,提示信息
	IdMsg(e) {
		e = parseInt(e)
		let msg = ''; //返回
		if (e == 10000) {
			msg = '支付宝认证成功'
			mcdj.setStorageSync('face', '1') //1是 通过 0是未完成
			my.navigateBack();
		} else {
			msg = "认证失败,请联系客服进行反馈"
			mcdj.setStorageSync('face', '0')
		}
		return msg;
	},

	// 提交认证授权申请(姓名与身份证号码)
	function zhimaCreditApply(obj) {
		const userName = obj.userName;
		const idCard = obj.idCard;
		qcloud.request({
			url: api + 'user/zhimaCreditApply',
			data: {
				userName,
				idCard
			},
			login: true,
			success(res) {
				obj.res = res;
				success(obj);
			},
			fail(err) {
				obj.err = err;
				fail(obj)
			}
		});
	}

 

你可能感兴趣的:(小程序)