| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?php
- /**
- * 实名认证
- */
- namespace app\api\controller\v1;
- use AlibabaCloud\SDK\Cloudauth\V20190307\Models\CompareFacesResponseBody\data;
- use AlibabaCloud\SDK\OSS\OSS\GetBucketRequestPaymentResponse\requestPaymentConfiguration;
- use app\api\services\DefaultServices;
- use app\common\model\MachineListModel;
- use app\common\model\MachineModel;
- use app\model\ShopOrderBonusModel as OrderBonus;
- use app\model\SocialTerm;
- use app\Request;
- use jobs\ShopBonusJob;
- use jobs\UserJobs;
- use think\facade\Db;
- use think\facade\Log;
- use utils\InitFaceVerify;
- use utils\Queue;
- use utils\Sample;
- class UserAuth
- {
- protected $RealNameUrl = 'https://cloudauth.aliyuncs.com';
- protected $TokenApi = 'DescribeVerifyToken';
- protected $VerifyApi = 'VerifyMaterial';
- protected $AccessKey;
- protected $Secret;
- protected $Scene = 'zysmrz';
- protected $scene_id = 1000003608; // 认证场景ID 2022-3-3 更换为金融级实名认证
- protected $time = '';
- protected $error = '';
- protected static $user;
- public function __construct ()
- {
- $this->AccessKey = env('aliyun.ACCESS_KEY_FACE');
- $this->Secret = env('aliyun.SECRET_KEY_FACE');
- }
- /**
- * 支付状态
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function authPayStatus (Request $request)
- {
- $uid = $request->uid;
- return api_succ_return('已支付');
- $result = self::_before($uid);
- if (!$result['flag'])
- return api_error_return(['msg'=>$result['msg'], 'code'=>$result['code']]);
- return api_succ_return('已支付');
- }
- /**
- * 获取支付宝token
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function aliToken (Request $request)
- {
- $uid = $request->uid;
- $result = self::_before($uid);
- if (!$result['flag'])
- return api_error_return(['msg'=>$result['msg'], 'code'=>$result['code']]);
- $data = array( // 请求参数
- 'BizId' => md5($uid),
- 'BizType' => $this->Scene,
- 'AccessKey' => $this->AccessKey,
- 'Secret' => $this->Secret
- );
- try {
- $initface = new InitFaceVerify();
- $initface::get_token($data);
- $aliyun = new Sample();
- $rpm = $aliyun::get_token($data);
- } catch (\Exception $e) {
- return api_error_return($e->getMessage());
- }
- }
- // 获取支付宝token金融级别
- public function aliFinanceToken (Request $request)
- {
- if (!$request->post('meta_info')){
- return api_error_return('参数错误');
- }
- $uid = $request->uid;
- $result = self::_before($uid);
- if (!$result['flag'])
- return api_error_return(['msg'=>$result['msg'], 'code'=>$result['code']]);
- $rz_nonce_str = md5(nonce_str() . '_' . $uid); // 认证随机字符串
- $data = array( // 请求参数
- 'outerOrderNo' => $rz_nonce_str,
- 'sceneId' => $this->scene_id,
- 'AccessKey' => $this->AccessKey,
- 'Secret' => $this->Secret,
- 'metaInfo' => html_entity_decode($request->data['meta_info']),
- 'CertName'=>$request->post('cert_name'),
- 'CertNo'=>$request->post('cert_no')
- );
- $cur_user = Db::name('user_data')->where('idcard', $request->post('cert_no'))->find();
- // return $cur_user;
- if ($cur_user) {
- // 被其他暂用 没认证 清除
- $user = Db::name('user')->where('id', $cur_user['uid'])->find();
- if ($user){
- if (!($user['is_auth'] == 1)){
- Db::name('user_data')->where('idcard', $request->post('cert_no'))->save(['idcard'=>'', 'idname'=>'']);
- Db::name('user_data')->where('uid', $uid)->update(['idcard' => $request->post('cert_no'), 'idname' => $request->post('cert_name'), 'bank_no' => '']);
- }else{
- return api_error_return('该身份证已被其他账号认证了');
- }
- }else{
- Db::name('user_data')->where('idcard', $request->post('cert_no'))->save(['idcard'=>'', 'idname'=>'']);
- }
- }else{
- Db::name('user_data')->where('uid', $uid)->update(['idcard' => $request->post('cert_no'), 'idname' => $request->post('cert_name'), 'bank_no' => '']);
- }
- try {
- /** @var Sample $aliyun */
- $aliyun = app()->make(Sample::class);
- $rpm = $aliyun::get_finance_token($data);
- if (!empty($rpm['body']['resultObject']['certifyId'])) {
- Db::name('user_data')->where('uid', $uid)->save(['rz_nonce_str' => $rz_nonce_str, 'ali_certify_id' => $rpm['body']['resultObject']['certifyId']]); // 更新随机字符串
- return api_succ_return(['msg'=>'success', 'data'=>['token' => $rpm['body']['resultObject']['certifyId']]]);
- } else {
- // sr_log('实名认证获取token失败【'.$request->uid.'】'.json_encode($rpm));
- Db::name('getali_token')->insertGetId([
- 'uid'=>$request->uid,
- 'content'=>json_encode($rpm),
- 'create_time'=>sr_getcurtime(time()),
- 'ip'=>$request->ip()
- ]);
- if (intval($rpm['body']['code']) == 401){
- return api_error_return('请检查填写的真实姓名跟身份证号码,(不能输入空格和其他特殊字符,英文字母大写),如还有问题跟客服沟通');
- }
- if (intval($rpm['body']['code']) == 418){
- return api_error_return('当天刷脸认证次数过多,请明天再试。');
- }
- return api_error_return('获取失败,请提供手机号联系客服');
- }
- } catch (\Exception $e) {
- return api_error_return($e->getMessage());
- }
- }
- // 获取实名认证结果
- public function authFinanceResult (Request $request)
- {
- $uid = $request->uid;
- $result = self::_before($uid);
- if (!$result['flag'])
- return api_error_return(['msg'=>$result['msg'], 'code'=>$result['code']]);
- $data = array( // 请求参数
- 'CertifyId' => Db::name('user_data')->where('uid', $uid)->value('ali_certify_id'),
- 'SceneId' => $this->scene_id,
- 'AccessKey' => $this->AccessKey,
- 'Secret' => $this->Secret
- );
- try {
- /** @var Sample $aliyun */
- $aliyun = app()->make(Sample::class);
- $rpm = $aliyun::get_finance_result($data);
- } catch (\Exception $e) {
- return api_error_return($e->getMessage());
- }
- if ($rpm['body']['code'] == '200') {
- $rpm = $rpm['body']['resultObject'];
- sr_log('shim'. $uid.'---');
- $res = self::_afterFinance($uid, $rpm, $result['order_id']);
- if (!$res['flag']) {
- return api_error_return($res['msg']);
- } else {
- return api_succ_return('认证成功');
- }
- }else{
- sr_log('获取实名结果失败【uid:'.$request->uid.'】'.json_encode($rpm));
- }
- return api_error_return('实名认证失败');
- }
- private static function _afterFinance (int $uid, array $rpm, $pay_order='111', $type = 1): array
- {
- if (!in_array($rpm['subCode'], [200, 210])) { // 认证未通过
- Db::name('auth_log')->insert(['uid' => $uid, 'auth_time' => time(), 'type' => $type, 'status' => 2, 'order_id' => $pay_order, 'remarks' => '身份信息错误,认证失败', 'desc' => json_encode($rpm)]);
- return ['flag' => false, 'msg' => '身份信息错误,认证失败,请检查实名人脸跟输入身份信息是否匹配'];
- }
- $materialInfo = json_decode($rpm['materialInfo'], true);
- $data = $materialInfo; // 身份信息
- // sr_log($data);
- $score = $materialInfo['facialPictureFront']['verifyScore']; // 相似度
- $header = $materialInfo['facialPictureFront']['pictureUrl']; // 照片
- // [$real_name, $idcard, $img, $pay_order] = [$data['certName'], $data['certNo'], $header, $pay_order];
- [$img, $pay_order] = [ $header, $pay_order];
- // if (Db::name('user_data')->where('idcard', $idcard)->value('idcard')) {
- // Db::name('auth_log')->insert(['uid' => $uid, 'auth_time' => time(), 'type' => $type, 'status' => 2, 'order_id' => $pay_order, 'remarks' => '该身份证已被其他账号认证了', 'desc' => json_encode($rpm)]);
- // return ['flag' => false, 'msg' => '该身份证已被其他账号认证了'];
- // }
- if ($type == 4) {
- if (Db::name('user_data')->where('bankno', $data['bankno'])->value('bankno')) {
- Db::name('auth_log')->insert(['uid' => $uid, 'auth_time' => time(), 'type' => $type, 'status' => 2, 'order_id' => $pay_order, 'remarks' => '该银行卡已被绑定', 'desc' => json_encode($rpm)]);
- return ['flag' => false, 'msg' => '该银行卡已被绑定'];
- }
- }
- Db::startTrans();
- try {
- Db::name('user')->where('id', $uid)->update(['is_auth' => 1]);
- $userinfo = Db::name('user')->where('id', $uid)->find();
- // 实名赠送一个优先勋章
- edit_user_medal(3,$uid, 1);
- incTeamStatic($uid, 2, $userinfo['path']);
- incTodayTeamStatic($uid, 2, $userinfo['path']);
- Db::name('auth_log')->insert(['uid' => $uid, 'pid' => $userinfo['pid'], 'type' => $type, 'auth_time' => time(), 'status' => 1, 'order_id' => $pay_order, 'remarks' => '实名认证成功', 'desc' => json_encode($rpm), 'img' => '', 'score' => $score]);
- // 新手矿机
- $info = MachineModel::where('mlevel', 999)->find();
- edit_user_active(2, $uid, $info['add_active']);
- Db::commit();
- return ['flag' => true, 'msg' => '认证成功'];
- } catch (\Exception $e) {
- Db::rollback();
- Db::name('auth_log')->insert(['uid' => $uid, 'auth_time' => time(), 'status' => 2, 'order_id' => $pay_order, 'remarks' => '实名认证失败' . $e->getMessage() . 'Line:' . $e->getLine()]);
- return ['flag' => false, 'msg' => $e->getMessage()];
- }
- }
- /**
- * 认证结果
- * @param Request $request
- * @param DefaultServices $services
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function authResult (Request $request, DefaultServices $services)
- {
- $uid = $request->uid;
- $result = self::_before($uid);
- if (!$result['flag'])
- return api_error_return(['msg'=>$result['msg'], 'code'=>$result['code']]);
- $data = array( // 请求参数
- 'BizId' => md5($uid),
- 'BizType' => $this->Scene,
- 'AccessKey' => $this->AccessKey,
- 'Secret' => $this->Secret
- );
- try {
- /** @var Sample $aliyun */
- $aliyun = app()->make(Sample::class);
- $rpm = $aliyun::get_result($data);
- } catch (\Exception $e) {
- return api_error_return('error');
- }
- if ($rpm['body']['verifyStatus'] == 1) {
- $res = self::_after($uid, $rpm, $result['order_id'], $services);
- if (!$res['flag']) {
- return api_error_return($res['msg']);
- } else {
- return api_succ_return('认证成功');
- }
- }
- return api_error_return('实名认证失败');
- }
- /**
- * 前置验证
- * @param int $uid
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- private static function _before (int $uid)
- {
- self::$user = Db::name('user')->field('id,is_auth,pid')->find(['id' => $uid]);
- if (self::$user['is_auth'])
- return ['flag' => false, 'code' => 400, 'msg' => '你已经认证过了'];
- $pay_order = Db::name('payment')->where([
- ['uid', '=', $uid],
- ['order_type', '=', 2],
- ['state', '=', 6],
- ['pay_at', '<>', '']
- ])->order('pay_at', 'desc')->value('id');
- if (!$pay_order)
- // return ['flag' => false, 'code' => 300, 'msg' => '你还未支付实名认证费用'];
- if (Db::name('auth_log')->where('uid', $uid)->where('order_id', $pay_order)->count() >= 3) { // 认证次数
- return ['flag' => false, 'code' => 300, 'msg' => '重复认证,每个身份信息只能提交3次'];
- }
- return ['flag' => true, 'order_id' => $pay_order];
- }
- /**
- * 后置操作
- * @param int $uid
- * @param array $rpm
- * @param int $pay_order
- * @param $services
- * @return array
- */
- private static function _after (int $uid, array $rpm, int $pay_order, $services): array
- {
- $data = $rpm['body']['material'];
- $score = $rpm['body']['authorityComparisionScore']; // 相似度
- [$real_name, $idcard, $img, $pay_order] = [$data['idCardName'], $data['idCardNumber'], $data['faceImageUrl'], $pay_order];
- if (Db::name('user_data')->where('idcard', $idcard)->value('idcard')) {
- Db::name('auth_log')->insert(['uid' => $uid, 'auth_time' => time(), 'status' => 2, 'order_id' => $pay_order, 'remarks' => '该身份证已被其他账号认证了', 'desc' => json_encode($data)]);
- return ['flag' => false, 'msg' => '该身份证已被其他账号认证了'];
- }
- Db::startTrans();
- try {
- Db::name('user_data')->where('uid', $uid)->update(['idcard' => $idcard, 'idname' => $real_name]);
- Db::name('user')->where('id', $uid)->update(['is_auth' => 1]);
- Db::name('auth_log')->insert(['uid' => $uid, 'pid' => self::$user['pid'], 'auth_time' => time(), 'status' => 1, 'order_id' => $pay_order, 'remarks' => '实名认证成功', 'desc' => json_encode($data), 'img' => $img, 'score' => $score]);
- /** @var DefaultServices $services */
- $services->incTeamStatic($uid);
- Db::commit();
- if (self::$user['pid']) // 上级获得邀新奖励
- {
- Queue::instance()->log('邀新执行成功')->job(UserJobs::class)->do('inviteRewards')->push([self::$user['pid'], $uid]);
- Queue::instance()->log('上级订单加速执行成功')->job(ShopBonusJob::class)->do('ShopBonusInviteQuickenJob')->push([self::$user['pid']]);
- }
- return ['flag' => true, 'msg' => '认证成功'];
- } catch (\Exception $e) {
- Db::rollback();
- Db::name('auth_log')->insert(['uid' => $uid, 'auth_time' => time(), 'status' => 2, 'order_id' => $pay_order, 'remarks' => '实名认证失败',]);
- return ['flag' => false, 'msg' => '实名认证失败'];
- }
- }
- }
|