187 lines
4.9 KiB
PHP
187 lines
4.9 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace app\common\model\cardocr;
|
|||
|
|
|||
|
use app\common\model\BaseModel;
|
|||
|
use think\Config;
|
|||
|
use think\Model;
|
|||
|
|
|||
|
class Card extends BaseModel
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
// 表名
|
|||
|
protected $name = 'cardocr';
|
|||
|
|
|||
|
// 自动写入时间戳字段
|
|||
|
protected $autoWriteTimestamp = 'int';
|
|||
|
|
|||
|
// 定义时间戳字段名
|
|||
|
protected $createTime = 'createtime';
|
|||
|
protected $updateTime = 'updatetime';
|
|||
|
protected $deleteTime = false;
|
|||
|
|
|||
|
// 追加属性
|
|||
|
protected $append = [
|
|||
|
'sex_text',
|
|||
|
'publishtime_text'
|
|||
|
];
|
|||
|
|
|||
|
|
|||
|
public function getSexList()
|
|||
|
{
|
|||
|
return ['男' => __('男'), '女' => __('女'), '未知' => __('未知')];
|
|||
|
}
|
|||
|
|
|||
|
public function getStatusList()
|
|||
|
{
|
|||
|
return ['0' => __('Down'), '1' => __('Pass'), '2' => __('Unaudited')];
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public function getSexTextAttr($value, $data)
|
|||
|
{
|
|||
|
$value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
|
|||
|
$list = $this->getSexList();
|
|||
|
return isset($list[$value]) ? $list[$value] : '';
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public function getPublishtimeTextAttr($value, $data)
|
|||
|
{
|
|||
|
$value = $value ? $value : (isset($data['publishtime']) ? $data['publishtime'] : '');
|
|||
|
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
|
|||
|
}
|
|||
|
|
|||
|
protected function setPublishtimeAttr($value)
|
|||
|
{
|
|||
|
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public function user()
|
|||
|
{
|
|||
|
return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/** 新增实名认证
|
|||
|
* @param $params
|
|||
|
* @param $trans
|
|||
|
* @return $this
|
|||
|
* @throws \Exception
|
|||
|
*/
|
|||
|
public function add($params,$trans=false){
|
|||
|
|
|||
|
if (empty($params)) {
|
|||
|
throw new \Exception(__('Parameter %s can not be empty', ''));
|
|||
|
}
|
|||
|
|
|||
|
$rule = [
|
|||
|
'user_id'=>'require',
|
|||
|
'name'=>'require',
|
|||
|
'idnum'=>'require',
|
|||
|
'positive_img' => 'require',
|
|||
|
'back_img'=>'require',
|
|||
|
];
|
|||
|
|
|||
|
$rule_msg = [
|
|||
|
"user_id.require"=>'认证用户必填',
|
|||
|
"name.require"=>'姓名必填',
|
|||
|
"idnum.require"=>'身份证号必填',
|
|||
|
'positive_img.require' => '身份证正面必填',
|
|||
|
'back_img.require' => '身份证反面必填',
|
|||
|
];
|
|||
|
|
|||
|
self::check($params,$rule,$rule_msg);
|
|||
|
|
|||
|
|
|||
|
$user_id = $params["user_id"];
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//开启api验证
|
|||
|
if (config("site.realname_isthroughapi")) {
|
|||
|
$url = Config::get('upload.cdnurl');
|
|||
|
$ImageUrl = cdnurl($params['positive_img'],true);
|
|||
|
$ImagebackUrl = cdnurl($params['back_img'],true);
|
|||
|
|
|||
|
//orc获取正面信息
|
|||
|
$front_data = $this->checkfront($ImageUrl);
|
|||
|
$front_data = array_change_key_case($front_data, CASE_LOWER);
|
|||
|
$params = $front_data + $params;
|
|||
|
|
|||
|
//orc获取反面信息
|
|||
|
$back_data = $this->checkback($ImagebackUrl);
|
|||
|
$back_data = array_change_key_case($back_data['backdata'], CASE_LOWER);
|
|||
|
|
|||
|
//修改审核通过
|
|||
|
$params['status'] = 1;
|
|||
|
$params["publishtime"] = time();
|
|||
|
$params = array_filter($back_data) + $params;
|
|||
|
|
|||
|
//调用api身份证实名验证
|
|||
|
$params_data = \addons\cardocr\library\Card::checkidcard($params['idnum'], $params['name']);
|
|||
|
|
|||
|
|
|||
|
if ($params_data['Result'] != 0) {
|
|||
|
throw new \Exception(__('姓名与身份号码不一致', ''));
|
|||
|
}
|
|||
|
}
|
|||
|
if(config("site.real_name_automatic_approval")){
|
|||
|
$params['status'] = 1;
|
|||
|
$params["publishtime"] = time();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//判断逻辑
|
|||
|
if($trans){
|
|||
|
self::beginTrans();
|
|||
|
}
|
|||
|
$res = true;
|
|||
|
try{
|
|||
|
//查询是否有认证信息,如果有则修改,如果没有则添加
|
|||
|
$card = self::where('user_id',$user_id)->find();
|
|||
|
if($card){
|
|||
|
$result = $card->allowField(true)->save($params);
|
|||
|
$self = $card;
|
|||
|
}else{
|
|||
|
$result = $this->allowField(true)->save($params);
|
|||
|
$self = $this;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
if($trans){
|
|||
|
self::commitTrans();
|
|||
|
}
|
|||
|
}catch (\Exception $e){
|
|||
|
if($trans){
|
|||
|
self::rollbackTrans();
|
|||
|
}
|
|||
|
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
|
|||
|
}
|
|||
|
return $self;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** 检测实名认证状态
|
|||
|
* @param $user_id
|
|||
|
*/
|
|||
|
public function checkRealname($user_id=0){
|
|||
|
//默认状态为-1未认证,0认证被拒绝,1认证通过,2=等待审核
|
|||
|
$status = -1;
|
|||
|
$card_info = self::where('user_id',$user_id)->find(); //认证信息
|
|||
|
if($card_info){
|
|||
|
$status = $card_info['status'];
|
|||
|
}
|
|||
|
return compact('status' , 'card_info');
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|