参考 : https://blog.csdn.net/qq_36710146/article/details/105110741
实际项目中:以下为封装的 小程序 支付 人脸识别 的方法js文件 在具体的页面中进行引入使用
const request = require('request.js') var app = getApp(); let payUtil = { /** * 获取签名 */ getSign: function (requestParam) { let params = { url: '/wechat/pay/sign', body: requestParam.data, method: 'POST' } request.doRequest( params, function (data) { wx.requestPayment({ 'timeStamp': data.timeStamp + '', 'nonceStr': data.nonceStr, 'package': data.package, 'signType': data.signType, 'paySign': data.paySign, 'success': function (res) { // 支付成功回调 requestParam.successFun(data.wechatpayid); }, 'fail': function (res) { // 支付成功回调 requestParam.failFun(data.wechatpayid); }, 'complete': function (res) { } }) }, function (data) { wx.showToast({ title: '请求错误', icon: 'none' }) } ) }, /** * 支付 * params: * total_fee: 总金额 * paytype: 交易类型 * desc: 交易描述 * vipid: 交易用户 */ pay: function(params) { var that = this; wx.getStorage({ key: 'openId', success: function(res) { params.data['openid'] = res.data; that.getSign(params); }, fail: function(res) { that.getOpenid(function() { that.pay(params); }) } }) }, /** * 调用登录接口获取code * successFun: 成功回调函数 */ getOpenid: function(successFun) { //调用登录接口 wx.login({ success: function (res) { let params = { url: '/wechat/session', body: { js_code: res.code }, method: 'POST' } request.doRequest( params, function (data) { wx.setStorageSync('openId', data.openid); //存在小程序缓存中 wx.setStorageSync('sessionKey', data.session_key); if (successFun && successFun instanceof Function) { successFun(); } }, function (data) { wx.showToast({ title: '请求错误', icon: 'none' }) } ) } }) }, /** * 校验session * @param {*} callBack */ checkSession: function(callBack) { var that = this; // 获取sessionKey wx.checkSession({ success() { //session_key 未过期,并且在本生命周期一直有效 if (callBack && callBack instanceof Function) { callBack(); } }, fail() { // session_key 已经失效,需要重新执行登录流程 that.getOpenid(function() { if (callBack && callBack instanceof Function) { callBack(); } }) } }) }, /** * 获取用户信息 */ getUserInfo: function(callBack) { var that = this; // 查看是否授权 wx.getSetting({ success(res) { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: function (resp) { that.getOpenid(function() { resp.userInfo['openid'] = wx.getStorageSync('openId'); wx.setStorage({ key: 'userInfo', data: resp.userInfo, }) callBack(); }) } }) } } }) }, /** * 获取用户信息 * @param {*} callBack */ getUser: function(callBack) { var that = this; wx.getUserInfo({ success: function (resp) { that.getOpenid(function() { wx.getStorage({ key: 'openId', success: function(res) { resp.userInfo['openid'] = res.data; wx.setStorage({ key: 'userInfo', data: resp.userInfo, }) callBack(); } }) }) } }) }, /** * 进行人脸识别 */ gotoVerify: function (successFun) { this.getBizToken(function(BizToken) { // 调用实名核身功能 wx.startVerify({ data: { token: BizToken // BizToken }, success: (res) => { // 验证成功后触发 // res 包含验证成功的token, 这里需要加500ms延时,防止iOS下不执行后面的逻辑 setTimeout(() => { let params = { url: '/wechat/detectInfo', body: { "bizToken": BizToken }, } request.doRequest( params, function (data) { wx.showModal({ title: "认证成功", showCancel: false }) // if (successFun instanceof Function) { // successFun(data); // } successFun(data); }, ) }, 500); }, fail: (err) => { // 验证失败时触发 // err 包含错误码,错误信息,弹窗提示错误 setTimeout(() => { wx.showModal({ title: "提示", content: err.ErrorMsg, showCancel: false }) }, 500); } }); }); }, /** * 获取bizToken之后回调人脸识别 * @param {*} callBack */ getBizToken: function(callBack) { let params = { url: '/wechat/bizToken', body: {}, } request.doRequest( params, function (data) { callBack(data.bizToken); }, function () { wx.showModal({ title: "提示", content: '网络异常', showCancel: false }) } ) } } module.exports = { payUtil: payUtil }