AgentState.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * AgentState.php UTF-8
  4. * 玩家渠道State码
  5. *
  6. * @date : 2018/8/17 14:41
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\controller\agent;
  13. use huo\controller\agent\AgentGame;
  14. use huo\controller\member\MemAgent;
  15. use huo\model\agent\AgentGameModel;
  16. use huolib\constant\CacheConst;
  17. use think\Cache;
  18. class AgentState {
  19. private $agent_game_id = 0;
  20. private $start_key = 'hy';
  21. private $channel_id = '1001';
  22. public function __construct() {
  23. $this->start_key = 'hy'; /* 读取配置 */
  24. // $this->channel_id = '';/* 读取配置 */
  25. }
  26. /**
  27. * @param string $string 原串
  28. *
  29. * @return int agent_game_id 默认为0
  30. */
  31. public function getIdByCode($string) {
  32. $_string = $string;
  33. /* 1 判断是否合法 */
  34. if (!preg_match("/^[A-Za-z0-9]+$/", $_string)) {
  35. return 0;
  36. }
  37. /* 1.1 判断前两位是否正确 */
  38. $_check_start_key = substr($_string, 0, 2);
  39. if ($_check_start_key != $this->start_key) {
  40. return 0;
  41. }
  42. /* 1.2 判断渠道编号是否正确 */
  43. $_check_channel_id = substr($_string, 2, 7);
  44. if ($_check_channel_id != $this->channel_id) {
  45. return 0;
  46. }
  47. /* 1.3 判断校验码是否正确 */
  48. $_check_sum = substr($string, -1);
  49. $_sub_str = substr($_string, 0, -1);
  50. $_check_check_num = $this->genCheckNum($_sub_str);
  51. if ($_check_check_num != $_check_sum) {
  52. return 0;
  53. }
  54. /* 2 获取agent_game_id */
  55. $_agent_str = substr($string, 9, 6);
  56. $_agent_game_id = base_convert($_agent_str, 36, 10);
  57. if (!is_numeric($_agent_game_id)) {
  58. return 0;
  59. }
  60. return intval($_agent_game_id);
  61. }
  62. /**
  63. * 生成渠道编码
  64. * 1. 运营平台标识 hy
  65. * 2. 运营平台编号 7位数字 不足7位 前面补0
  66. * 3. 代理编号(字符串) 6位
  67. * 4. 最后一位 校验码 0-9 与身份证验证方法同理
  68. *
  69. * @param int $ag_id
  70. *
  71. * @return string
  72. */
  73. public function genAgentCode($ag_id = 0) {
  74. if (!empty($ag_id)) {
  75. $this->agent_game_id = $ag_id;
  76. }
  77. $_start_key = $this->start_key;
  78. $_channel_id = $this->getChannelStr();
  79. $_agent_str = $this->genAgentStr();
  80. $_string = $_start_key.$_channel_id.$_agent_str;
  81. $_check_num = $this->genCheckNum($_string);
  82. return $_string.$_check_num;
  83. }
  84. /**
  85. * 生成渠道字符串
  86. *
  87. * @return string
  88. */
  89. private function genAgentStr() {
  90. $_str = base_convert($this->agent_game_id, 10, 36);
  91. $_bit = 6;//产生7位数的字符串
  92. $_str_len = strlen($_str);
  93. $_zero = '';
  94. for ($_i = $_str_len; $_i < $_bit; $_i++) {
  95. $_zero .= "0";
  96. }
  97. return $_zero.$_str;
  98. }
  99. private function getChannelStr($string = '') {
  100. $_str = $string;
  101. if (empty($string)) {
  102. $_str = $this->channel_id;
  103. }
  104. $_bit = 7;//产生7位数的字符串
  105. $_str_len = strlen($_str);
  106. $_zero = '';
  107. for ($_i = $_str_len; $_i < $_bit; $_i++) {
  108. $_zero .= "0";
  109. }
  110. return $_zero.$_str;
  111. }
  112. /**
  113. * 生成校验码
  114. *
  115. * @param $string
  116. *
  117. * @return bool|mixed
  118. */
  119. private function genCheckNum($string) {
  120. /* 英文字符 */
  121. if (!preg_match("/^[A-Za-z0-9]+$/", $string)) {
  122. return false;
  123. }
  124. /* 加权因子 */
  125. $_factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8);
  126. /* 校验码对应值 */
  127. $_verify_number_list = array('1', '0', '9', '8', '7', '6', '5', '4', '3', '2');
  128. $_checksum = 0;
  129. $_count_factor = count($_factor);
  130. for ($_i = 0; $_i < strlen($string); $_i++) {
  131. $_temp = substr($string, $_i, 1);
  132. $_temp = ord($_temp) % 10;
  133. $_factor_i = $_i % $_count_factor;
  134. $_checksum += ($_temp * $_factor[$_factor_i]) % 10;
  135. }
  136. $_mod = $_checksum % 10;
  137. $_verify_number = $_verify_number_list[$_mod];
  138. return $_verify_number;
  139. }
  140. /**
  141. * @return int
  142. */
  143. public function getAgentGameId() {
  144. return $this->agent_game_id;
  145. }
  146. /**
  147. * @param int $agent_game_id
  148. */
  149. public function setAgentGameId($agent_game_id) {
  150. $this->agent_game_id = $agent_game_id;
  151. }
  152. /**
  153. * @return string
  154. */
  155. public function getStartKey() {
  156. return $this->start_key;
  157. }
  158. /**
  159. * @param string $start_key
  160. */
  161. public function setStartKey($start_key) {
  162. $this->start_key = $start_key;
  163. }
  164. /**
  165. * @return string
  166. */
  167. public function getChannelId() {
  168. return $this->channel_id;
  169. }
  170. /**
  171. * @param string $channel_id
  172. */
  173. public function setChannelId($channel_id) {
  174. $this->channel_id = $channel_id;
  175. }
  176. /**
  177. * 通过玩家应用获取分享码
  178. *
  179. * @param $mem_id
  180. * @param $app_id
  181. *
  182. * @return mixed|string
  183. */
  184. public function getCodeByMemApp($mem_id, $app_id) {
  185. $_cache_key = CacheConst::CACHE_MEM_APP_SHARE_CODE_PREFIX.$mem_id.$app_id;
  186. $_share_code = Cache::get($_cache_key);
  187. if (!empty($_share_code)) {
  188. return $_share_code;
  189. }
  190. $_agent_id = (new MemAgent())->getMemAgentId($mem_id);
  191. if (empty($_agent_id)) {
  192. return '';
  193. }
  194. $_ag_model = new AgentGameModel();
  195. $_ag_id = $_ag_model->getAgIdByAgentIdAppId($_agent_id, $app_id);
  196. if (empty($_ag_id)) {
  197. (new AgentGame())->addGame($_agent_id, $app_id,false);
  198. $_ag_id = $_ag_model->getAgIdByAgentIdAppId($_agent_id, $app_id);
  199. }
  200. $_share_code = $this->genAgentCode($_ag_id);
  201. if (empty($_share_code)) {
  202. return '';
  203. }
  204. Cache::set($_cache_key, $_share_code);
  205. return $_share_code;
  206. }
  207. }