123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- /**
- * AgentState.php UTF-8
- * 玩家渠道State码
- *
- * @date : 2018/8/17 14:41
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @version : HuoMp 1.0
- */
- namespace huomp\controller\agent;
- use huo\controller\agent\AgentGame;
- use huo\controller\member\MemAgent;
- use huo\model\agent\AgentGameModel;
- use huolib\constant\CacheConst;
- use think\Cache;
- class AgentState {
- private $agent_game_id = 0;
- private $start_key = 'hy';
- private $channel_id = '1001';
- public function __construct() {
- $this->start_key = 'hy'; /* 读取配置 */
- // $this->channel_id = '';/* 读取配置 */
- }
- /**
- * @param string $string 原串
- *
- * @return int agent_game_id 默认为0
- */
- public function getIdByCode($string) {
- $_string = $string;
- /* 1 判断是否合法 */
- if (!preg_match("/^[A-Za-z0-9]+$/", $_string)) {
- return 0;
- }
- /* 1.1 判断前两位是否正确 */
- $_check_start_key = substr($_string, 0, 2);
- if ($_check_start_key != $this->start_key) {
- return 0;
- }
- /* 1.2 判断渠道编号是否正确 */
- $_check_channel_id = substr($_string, 2, 7);
- if ($_check_channel_id != $this->channel_id) {
- return 0;
- }
- /* 1.3 判断校验码是否正确 */
- $_check_sum = substr($string, -1);
- $_sub_str = substr($_string, 0, -1);
- $_check_check_num = $this->genCheckNum($_sub_str);
- if ($_check_check_num != $_check_sum) {
- return 0;
- }
- /* 2 获取agent_game_id */
- $_agent_str = substr($string, 9, 6);
- $_agent_game_id = base_convert($_agent_str, 36, 10);
- if (!is_numeric($_agent_game_id)) {
- return 0;
- }
- return intval($_agent_game_id);
- }
- /**
- * 生成渠道编码
- * 1. 运营平台标识 hy
- * 2. 运营平台编号 7位数字 不足7位 前面补0
- * 3. 代理编号(字符串) 6位
- * 4. 最后一位 校验码 0-9 与身份证验证方法同理
- *
- * @param int $ag_id
- *
- * @return string
- */
- public function genAgentCode($ag_id = 0) {
- if (!empty($ag_id)) {
- $this->agent_game_id = $ag_id;
- }
- $_start_key = $this->start_key;
- $_channel_id = $this->getChannelStr();
- $_agent_str = $this->genAgentStr();
- $_string = $_start_key.$_channel_id.$_agent_str;
- $_check_num = $this->genCheckNum($_string);
- return $_string.$_check_num;
- }
- /**
- * 生成渠道字符串
- *
- * @return string
- */
- private function genAgentStr() {
- $_str = base_convert($this->agent_game_id, 10, 36);
- $_bit = 6;//产生7位数的字符串
- $_str_len = strlen($_str);
- $_zero = '';
- for ($_i = $_str_len; $_i < $_bit; $_i++) {
- $_zero .= "0";
- }
- return $_zero.$_str;
- }
- private function getChannelStr($string = '') {
- $_str = $string;
- if (empty($string)) {
- $_str = $this->channel_id;
- }
- $_bit = 7;//产生7位数的字符串
- $_str_len = strlen($_str);
- $_zero = '';
- for ($_i = $_str_len; $_i < $_bit; $_i++) {
- $_zero .= "0";
- }
- return $_zero.$_str;
- }
- /**
- * 生成校验码
- *
- * @param $string
- *
- * @return bool|mixed
- */
- private function genCheckNum($string) {
- /* 英文字符 */
- if (!preg_match("/^[A-Za-z0-9]+$/", $string)) {
- return false;
- }
- /* 加权因子 */
- $_factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8);
- /* 校验码对应值 */
- $_verify_number_list = array('1', '0', '9', '8', '7', '6', '5', '4', '3', '2');
- $_checksum = 0;
- $_count_factor = count($_factor);
- for ($_i = 0; $_i < strlen($string); $_i++) {
- $_temp = substr($string, $_i, 1);
- $_temp = ord($_temp) % 10;
- $_factor_i = $_i % $_count_factor;
- $_checksum += ($_temp * $_factor[$_factor_i]) % 10;
- }
- $_mod = $_checksum % 10;
- $_verify_number = $_verify_number_list[$_mod];
- return $_verify_number;
- }
- /**
- * @return int
- */
- public function getAgentGameId() {
- return $this->agent_game_id;
- }
- /**
- * @param int $agent_game_id
- */
- public function setAgentGameId($agent_game_id) {
- $this->agent_game_id = $agent_game_id;
- }
- /**
- * @return string
- */
- public function getStartKey() {
- return $this->start_key;
- }
- /**
- * @param string $start_key
- */
- public function setStartKey($start_key) {
- $this->start_key = $start_key;
- }
- /**
- * @return string
- */
- public function getChannelId() {
- return $this->channel_id;
- }
- /**
- * @param string $channel_id
- */
- public function setChannelId($channel_id) {
- $this->channel_id = $channel_id;
- }
- /**
- * 通过玩家应用获取分享码
- *
- * @param $mem_id
- * @param $app_id
- *
- * @return mixed|string
- */
- public function getCodeByMemApp($mem_id, $app_id) {
- $_cache_key = CacheConst::CACHE_MEM_APP_SHARE_CODE_PREFIX.$mem_id.$app_id;
- $_share_code = Cache::get($_cache_key);
- if (!empty($_share_code)) {
- return $_share_code;
- }
- $_agent_id = (new MemAgent())->getMemAgentId($mem_id);
- if (empty($_agent_id)) {
- return '';
- }
- $_ag_model = new AgentGameModel();
- $_ag_id = $_ag_model->getAgIdByAgentIdAppId($_agent_id, $app_id);
- if (empty($_ag_id)) {
- (new AgentGame())->addGame($_agent_id, $app_id,false);
- $_ag_id = $_ag_model->getAgIdByAgentIdAppId($_agent_id, $app_id);
- }
- $_share_code = $this->genAgentCode($_ag_id);
- if (empty($_share_code)) {
- return '';
- }
- Cache::set($_cache_key, $_share_code);
- return $_share_code;
- }
- }
|