| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 | <?php/** * Identify.php UTF-8 * 玩家实名认证 * * @date    : 2020/6/20 11:53 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : chenbingling <cbl@huosdk.com> * @version : HUOUNION 8.5 */namespace huoIdentify\controller;use huo\controller\common\Base;use huo\controller\common\CommonFunc;use huo\controller\integral\MemIa;use huo\controller\member\MemCache;use huo\model\game\GameModel;use huo\model\member\MemberModel;use huo\model\member\MemGameModel;use huoIdentify\model\IdentifyGameModel;use huoIdentify\model\IdentifyMemModel;use huoIdentify\model\IdentifyOrderModel;use huoIdentify\model\IdentifyPiModel;use huoIdentify\model\IdentifyPlatformLogModel;use huolib\constant\CommonConst;use huolib\constant\DeviceTypeConst;use huolib\constant\GameConst;use huolib\constant\IaConst;use huolib\constant\IdentifyConst;use huolib\constant\MemConst;use huolib\status\CommonStatus;use huolib\status\IdentifyStatus;use huolib\status\MemberStatus;use huolib\tool\RateLimit;use huolib\tool\StrUtils;use huolib\utils\IdentifyUtils;use think\Config;use think\Exception;class Identify_20210908 extends Base {    protected $app_id = 0;    public function __construct() {        $this->__initConfig();    }    private function __initConfig() {        $_app_id = (int)request()->param('game_id/d', 0);        if (empty($_app_id)) {            $_app_id = (int)request()->param('app_id/d', 0);        }        $this->app_id = $_app_id;        if (!empty($_app_id)) {            $_identify_game_model = new IdentifyGameModel();            $_identify_game_info = (array)$_identify_game_model->getInfoByAppId($_app_id);            if (!empty($_identify_game_info['driver_key'])) {                Config::set('identify_conf.default', $_identify_game_info['driver_key']);            }        }    }    /**     * 获取会员认证信息     *     * @param int  $mem_id  玩家id     * @param bool $encrypt 是否加密     * @param int  $app_id  当前游戏id     *     * @return mixed : array     */    public function getIdentifyByMemId($mem_id, $encrypt = false, $app_id = 0) {        $_mem_data = (new IdentifyMemModel())->getInfoByMemId($mem_id);        if (empty($_mem_data) && !empty($app_id)) {            /* 未获取到实名信息,则查看是否有认证中的数据 */            $this->checkMemIdentify($mem_id, $app_id);            $_mem_data = (new IdentifyMemModel())->getInfoByMemId($mem_id);        }        $_data['id_card'] = get_val($_mem_data, 'id_card');        $_data['real_name'] = get_val($_mem_data, 'real_name');        $_data['pi'] = get_val($_mem_data, 'identify_pi');        $_data['birthday'] = IdentifyUtils::getBirthday($_data['id_card']);        $_data['age'] = IdentifyUtils::getAge($_data['birthday']);        $_data['is_auth'] = IdentifyConst::IS_AUTH_NO;        if (!empty($_data['id_card'])) {            $_data['is_auth'] = IdentifyConst::IS_AUTH_YES;        }        if (true == $encrypt) {            if (!empty($_data['real_name'])) {                $_data['real_name'] = StrUtils::encryptName($_data['real_name']);            }            if (!empty($_data['id_card'])) {                $_data['id_card'] = StrUtils::encryptName($_data['id_card']);            }        }        return $_data;    }    /**     * 根据app_id获取游戏实名认证状态     *     * @param int $app_id     *     * @return:int     */    public function getGameAuthByAppId($app_id) {        $_is_auth = (new GameModel())->getIsAuthById($app_id);        return $_is_auth;    }    /**     * 获取用户实名信息状况     *     * @param int    $app_id    游戏id     * @param int    $mem_id    会员id     * @param string $device_id 设备号 用于未实名玩家时长统计     *     * @return array     */    public function getMemIdentifyInfo($app_id, $mem_id, $device_id = '') {        /*定义返回数组*/        $_rdata = [            'auth_info' => [                'id_card'   => '',                'real_name' => '',                'birthday'  => '',                'age'       => '',                'is_auth'   => IdentifyConst::IS_AUTH_NO            ],            'url'       => '',        ];        $_app_ids = CommonFunc::getAppAppId();        $_game_auth = $this->getGameAuthByAppId($app_id);        if (!in_array($app_id, $_app_ids)) {            /* 不需要实名不做任何处理 */            if (GameConst::GAME_IDENTIFY_IS_NO == $_game_auth) {                return $_rdata;            }        }        $_param = [            'app_id'    => $app_id,            'device_id' => $device_id,            'token'     => session_id()        ];        $_identify_url = IdentifyConst::getIdentifyUrl($_param);        $_mem_auth_info = $this->getIdentifyByMemId($mem_id, false, $app_id);        $_mem_auth_info['is_auth'] = $_game_auth;        $_rdata['auth_info'] = $_mem_auth_info;        /* 获取玩家是否已绑定手机 */        $_mem_model = new MemberModel();        $_mobile = $_mem_model->getMobileById($mem_id);        $_status = $_mem_model->getStatus($mem_id);        if (empty($_mobile) && MemConst::STATUS_TRY == $_status) {            $_rdata['url'] = IdentifyConst::getBindMobileUrl($_param);            return $_rdata;        }        $_rdata['url'] = $_identify_url;        /* 玩家未实名返回实名认证地址 */        if (empty($_mem_auth_info['id_card'])) {            return $_rdata;        }        /* 玩家已实名校验未成年限制 */        $_rdata['url'] = (new Online($app_id, $mem_id, $device_id))->checkLimit();        return $_rdata;    }    /**     * 更新实名认证信息     *     * @param     $mem_id     * @param int $type     * @param     $real_name     * @param     $id_card     * @param int $app_id     *     * @return mixed     */    public function updateIdentify($mem_id, $type = 1, $real_name, $id_card, $app_id = 0) {        $id_card = strtoupper($id_card);        $_mem_cache = MemCache::ins();        $_mem_data = $_mem_cache->getInfoById($mem_id);        if (empty($_mem_data)) {            $_code = MemberStatus::LOGIN_IS_OUT;            return $this->huoSuccess($_code, MemberStatus::getMsg(MemberStatus::LOGIN_IS_OUT));        }        $_chk_rs = IdentifyUtils::checkIdentify($type, $real_name, $id_card);        if (IdentifyStatus::NO_ERROR != $_chk_rs) {            return $this->huoError($_chk_rs, IdentifyStatus::getMsg($_chk_rs));        }        $_im_model = new IdentifyMemModel();        $_pi_info = $this->getPiInfoByIdCard($id_card);        $_pi = get_val($_pi_info, 'identify_pi', '');        $_cnt = $_im_model->getCntByIdCard($id_card);        $_id_bind_limit = (new IdentifyConf())->getIdCardBindCnt();        $_is_other_api_check = (new IdentifyConf())->getIsOtherApiCheck();        $_identify_from = Config::get('identify_conf.default');  //实名认证api        /* 需要pi的类型 */        $_need_pi_type = IdentifyConst::getNeedPiTypes();        if ($_cnt >= $_id_bind_limit) {            $_code = IdentifyStatus::IDCARD_BIND_LIMIT;            return $this->huoError($_code, IdentifyStatus::getMsg($_code));        } elseif ((empty($_cnt) && CommonConst::STATUS_YES == $_is_other_api_check)                  || (empty($_pi) && in_array($_identify_from, $_need_pi_type))        ) {            /* 当前证件号没有绑定过玩家并且开启了第三方api校验才需要验证 */            $_app_id = $app_id;            if (empty($_app_id)) {                $_app_id = $this->app_id;            }            if (empty($_app_id)) {                $_app_id = $_mem_data['app_id'];            }            try {                $_identify_conf = \huoIdentify\controller\IdentifyDriverConf::getConfig($_identify_from, $_app_id);                $_identify_class = \huoIdentify\identifyDriver\Identify::ins()->get($_identify_from, $_identify_conf);                $_identify_class->setRealName($real_name);                $_identify_class->setIdCard($id_card);                $_identify_class->setMemId($mem_id);                $_identify_class->setIdentifyFrom($_identify_from);                $_mg_mem_id = (new MemGameModel())->getDefaultMgIdByMemAppId($mem_id, $_app_id);                $_identify_class->setMgMemId($_mg_mem_id);                $_classify = (new \huoIdentify\model\GameModel())->getClassifyById($_app_id);                $_pf = GameConst::GAME_IOS == $_classify ? 2 : 1;  /* 1 安卓 2 ios*/                $_identify_class->setPf($_pf);                $_identify_rs = $_identify_class->identify();                /* 玩家实名认证中可放行 */                if (CommonStatus::MEM_IDENTIFY_IN_PROGRESS == $_identify_rs['code']) {                    return $this->retSucMsg(IdentifyStatus::NO_ERROR);                }                if (CommonStatus::NO_ERROR != $_identify_rs['code']) {                    $_identify_rs['msg'] = '认证失败,请确认身份信息是否正确';                    return $this->huoReturn($_identify_rs);                }                $_identify_pi = isset($_identify_rs['data']['pi']) ? $_identify_rs['data']['pi'] : '';            } catch (Exception $e) {                return $this->huoError($e->getCode(), $e->getMessage());            };        } else {            $_identify_from = get_val($_pi_info, 'identify_from', '');            $_identify_pi = get_val($_pi_info, 'identify_pi', '');        }        $_old_id_data = $_im_model->getInfoByMemId($mem_id);        $_old_id_card = get_val($_old_id_data, 'id_card', '');        $_data = [            'identify_from' => $_identify_from,            'identify_pi'   => $_identify_pi,            'mem_id'        => $mem_id,            'real_name'     => $real_name,            'id_card'       => $id_card,            'identify_type' => $type,        ];        if (empty($_old_id_data)) {            $_rs = $_im_model->addData($_data);        } else {            $_rs = $_im_model->updateData($_data, $_old_id_data['id']);        }        if (false === $_rs) {            return $this->huoSuccess(MemberStatus::INNER_ERROR, MemberStatus::getMsg(MemberStatus::INNER_ERROR));        }        $this->updatePiInfo($id_card, $real_name, $_identify_pi, $_identify_from);        $_mem_data = [            'status' => MemConst::STATUS_NORMAL        ];        $_mem_data['real_name'] = $real_name;        $_mem_data['id_card'] = $id_card;        $_mem_data['identify_type'] = $type;        $_mem_cache->updateMem($mem_id, $_mem_data);        /* 实名认证成功获取积分 */        if (empty($_old_id_card)) {            (new MemIa($mem_id))->doItgAct(IaConst::IA_IDENTIFY);        }        $_data = $_im_model->getInfoByMemId($mem_id);        return $this->retSucMsg(IdentifyStatus::NO_ERROR, $_data);    }    /**     * 获取游戏实名认证信息     *     * @param int    $app_id      游戏id     * @param int    $mem_id      会员id     * @param string $device_type 设备类型     *     * @return array     */    public function getAuthInfo($app_id, $mem_id, $device_type = DeviceTypeConst::DEVICE_TYPE_MP) {        /*定义返回数组*/        $_data = [];        $_is_auth = $this->getGameAuthByAppId($app_id);        if (GameConst::GAME_IDENTIFY_IS_NO == $_is_auth) {            return $_data;        }        $_data['auth_info'] = $this->getIdentifyByMemId($mem_id);        $_data['url'] = '';        if (IdentifyConst::IS_AUTH_NO == $_data['auth_info']['is_auth']) {            $_param = [                'token'       => session_id(),                'app_id'      => $app_id,                'device_type' => $device_type,            ];            $_data['url'] = MemConst::getIdentifyUrl($_param);        }        return $_data;    }    /**     * 上报登录行为     *     * @param $app_id     * @param $mem_id     *     * @return array|bool     */    public function reportLoginBehavior($app_id, $mem_id) {        return $this->reportCommon($app_id, $mem_id, CommonConst::STATUS_YES);    }    /**     * 上报退出登录行为     *     * @param $app_id     * @param $mem_id     *     * @return array|bool     */    public function reportLogoutBehavior($app_id, $mem_id) {        return $this->reportCommon($app_id, $mem_id, CommonConst::STATUS_NO);    }    protected function reportCommon($app_id, $mem_id, $is_login = CommonConst::STATUS_YES) {        $_identify_mem_info = (new IdentifyMemModel())->getInfoByMemId($mem_id);        if (!empty($_identify_mem_info['id_card'])) {            $_identify_from = Config::get('identify_conf.default');            if (!empty($app_id)) {                $_identify_game_model = new IdentifyGameModel();                $_identify_game_info = (array)$_identify_game_model->getInfoByAppId($app_id);                if (!empty($_identify_game_info['driver_key'])) {                    $_identify_from = $_identify_game_info['driver_key'];                    Config::set('identify_conf.default', $_identify_game_info['driver_key']);                }            }            /* 需要pi的类型 */            $_need_pi_type = IdentifyConst::getNeedPiTypes();            try {                $_identify_conf = \huoIdentify\controller\IdentifyDriverConf::getConfig($_identify_from, $app_id);                $_id_card = isset($_identify_mem_info['id_card']) ? $_identify_mem_info['id_card'] : '';                $_real_name = isset($_identify_mem_info['real_name']) ? $_identify_mem_info['real_name'] : '';                $_identify_pi = isset($_identify_mem_info['identify_pi']) ? $_identify_mem_info['identify_pi'] : '';                if (empty($_identify_pi) && !empty($_id_card) && !empty($_real_name)                    && in_array($_identify_from, $_need_pi_type)                ) {                    /* 游戏开启国家防沉迷认证但是玩家没有在国家防沉迷系统认证的需要前往认证 */                    $_rs = $this->updateIdentify($mem_id, 1, $_real_name, $_id_card, $app_id);                    if (IdentifyStatus::NO_ERROR == $_rs['code']) {                        $_identify_pi = get_val($_rs['data'], 'identify_pi', '');                    }                }                $_identify_class = \huoIdentify\identifyDriver\Identify::ins()->get($_identify_from, $_identify_conf);                $_identify_class->setRealName($_real_name);                $_identify_class->setIdCard($_id_card);                $_identify_class->setMemId($mem_id);                $_identify_class->setIdentifyFrom($_identify_from);                $_mg_mem_id = (new MemGameModel())->getDefaultMgIdByMemAppId($mem_id, $app_id);                $_identify_class->setMgMemId($_mg_mem_id);                $_classify = (new \huoIdentify\model\GameModel())->getClassifyById($app_id);                $_pf = GameConst::GAME_IOS == $_classify ? 2 : 1;  /* 1 安卓 2 ios*/                $_identify_class->setPf($_pf);                $_identify_class->setPi($_identify_pi);                if ($is_login == CommonConst::STATUS_YES) {                    $_identify_rs = $_identify_class->loginBehavior($_identify_pi);                } else {                    $_identify_rs = $_identify_class->logoutBehavior($_identify_pi);                }                if (CommonStatus::NO_ERROR != $_identify_rs['code']) {                    $_identify_rs['msg'] = '上报失败';                    return $this->huoReturn($_identify_rs);                }            } catch (exception $e) {                return $this->huoError($e->getCode(), $e->getMessage());            }        }        $_code = IdentifyStatus::NO_ERROR;        return $this->huoSuccess($_code, CommonStatus::getMsg($_code));    }    /**     * 查询是否有认证中数据     *     * @param $mem_id     * @param $app_id     */    protected function checkMemIdentify($mem_id, $app_id) {        /* 限制速率 */        $_rs = (new RateLimit())->rateLimit($mem_id, true, true, 1, 3);        if (false == $_rs) {            return false;        }        $_identify_from = Config::get('identify_conf.default');        switch ($_identify_from) {            case IdentifyConst::DRIVER_KEY_ALIPAY:            case IdentifyConst::DRIVER_KEY_WXPAY:                return false;            case IdentifyConst::DRIVER_KEY_FCMGAME:                try {                    $_identify_conf = \huoIdentify\controller\IdentifyDriverConf::getConfig($_identify_from, $app_id);                } catch (exception $e) {                    return false;                }                $_in_progress_data = $this->getFcmGameInProgressData($mem_id, $_identify_conf['bizId']);                break;            default:                $_in_progress_data = [];                $_class_name = '\\huoIdentify\\identifyDriver\\driver\\'.ucfirst(strtolower($_identify_from));                if (class_exists($_class_name)) {                    $_status_in_progress = $_class_name::STATUS_IN_PROGRESS;  /* 获取各实名驱动类的认证中状态值 */                    $_in_progress_data = $this->getInProgressDataByMemAppFrom(                        $mem_id, $app_id, $_identify_from, $_status_in_progress                    );                }        }        if (empty($_in_progress_data)) {            return false;        }        $_real_name = get_val($_in_progress_data, 'real_name', '');        $_id_card = get_val($_in_progress_data, 'id_card', '');        $_rs = $this->updateIdentify($mem_id, 1, $_real_name, $_id_card, $app_id);        if (IdentifyStatus::NO_ERROR == $_rs['code']) {            return true;        }        return false;    }    /**     * 获取国家防沉迷系统认证中数据     *     * @param $mem_id     * @param $biz_id     *     * @return array     */    public function getFcmGameInProgressData($mem_id, $biz_id) {        $_io_data = (new IdentifyOrderModel())->getLastInfoByBizIdMem($biz_id, $mem_id);        if (empty($_io_data)) {            return [];        }        $_status = get_val($_io_data, 'status', 2);        if (\huoIdentify\identifyDriver\driver\Fcmgame::STATUS_IN_PROGRESS != $_status) {            /* 非认证中不处理 */            return [];        }        return $_io_data;    }    /**     * 获取实名认证中的数据     *     * @param int    $mem_id             玩家id     * @param int    $app_id             游戏id     * @param string $identify_from      认证平台     * @param int    $status_in_progress 认证中状态     *     * @return array     */    private function getInProgressDataByMemAppFrom($mem_id, $app_id, $identify_from, $status_in_progress) {        $_mg_mem_id = (new MemGameModel())->getDefaultMgIdByMemAppId($mem_id, $app_id);        if (empty($_mg_mem_id)) {            return [];        }        $_ipl_data = (new IdentifyPlatformLogModel())->getInfoByMgFrom($_mg_mem_id, $identify_from);        if (empty($_ipl_data)) {            return [];        }        $_status = get_val($_ipl_data, 'status', 0);        if ($status_in_progress != $_status) {            /* 非认证中不处理 */            return [];        }        return $_ipl_data;    }    /**     * 根据身份证号获取pi信息     *     * @param string $id_card 身份证号     *     * @return array     */    public function getPiInfoByIdCard($id_card) {        $_pi_info = (new IdentifyPiModel())->getInfoByIdCard($id_card);//        if (empty($_pi_info)) {//            /* 此处兼容旧数据查找的一种方案可注释,也可将旧数据查找后写入信数据表中后注释 *///            $_pi_info = (new IdentifyMemModel())->getInfoByIdCard($id_card);//        }        return $_pi_info;    }    /**     * 更新PI信息     *     * @param string $id_card       身份证号     * @param string $real_name     真实姓名     * @param string $identify_pi   中宣部PI     * @param string $identify_from 认证来源     *     * @return bool|false|int     */    public function updatePiInfo($id_card, $real_name, $identify_pi, $identify_from) {        if (empty($id_card) || empty($real_name) || empty($identify_pi)) {            return false;        }        $_ip_model = new IdentifyPiModel();        $_pi_info = $_ip_model->getInfoByIdCard($id_card);        $_rs = true;        if (empty($_pi_info)) {            $_data = [                'id_card'       => $id_card,                'real_name'     => $real_name,                'identify_pi'   => $identify_pi,                'identify_from' => $identify_from,            ];            $_rs = $_ip_model->addData($_data);        }        return $_rs;    }}
 |