AgentLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * AgentLogic.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/9 14:33
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\logic\agent;
  13. use huo\controller\agent\Agent;
  14. use huo\model\common\CommonModel;
  15. use huo\model\member\MemberModel;
  16. use huo\model\user\RoleModel;
  17. use huo\model\user\UserModel;
  18. use huolib\constant\AgentConst;
  19. use huolib\constant\CommonConst;
  20. use huolib\tool\Time;
  21. class AgentLogic extends CommonModel {
  22. /**
  23. * 通过玩家ID获取玩家信息
  24. *
  25. * @param string $agent_id
  26. *
  27. * @return array|bool
  28. */
  29. public function getInfoByAgentId($agent_id) {
  30. $_info = (new UserModel())->getUserInfo($agent_id);
  31. return $_info;
  32. }
  33. /**
  34. * @param $username
  35. *
  36. * @return array|bool
  37. */
  38. public function getIdByName($username) {
  39. $_map['user_login'] = $username;
  40. $_agent_id = (new UserModel())->where($_map)->value('id');
  41. if (false === $_agent_id) {
  42. return false;
  43. }
  44. if (empty($_agent_id)) {
  45. return 0;
  46. }
  47. return $_agent_id;
  48. }
  49. /**
  50. * 更新数据
  51. *
  52. * @param array $data
  53. *
  54. * @param int $agent_id
  55. *
  56. * @return bool|mixed
  57. */
  58. public function updateAgent($data = [], $agent_id) {
  59. $_map['id'] = $agent_id;
  60. $_data = $data;
  61. if (isset($_data['user_pass']) && cmf_pwd_is_encrypt($_data['user_pass'])) {
  62. unset($_data['user_pass']);
  63. }
  64. if (isset($_data['pay_pwd']) && cmf_pwd_is_encrypt($_data['pay_pwd'])) {
  65. unset($_data['pay_pwd']);
  66. }
  67. return (new UserModel())->updateData($_data, $agent_id);
  68. }
  69. /**
  70. * 获取渠道ID
  71. *
  72. * @param int $agent_id
  73. * @param bool $inc_me
  74. * @param array $role_type
  75. *
  76. * @return array
  77. */
  78. public function getAgentIds(
  79. $agent_id = 0, $inc_me = false, $role_type = [AgentConst::ROLE_TYPE_GROUP, AgentConst::ROLE_TYPE_AGENT]
  80. ) {
  81. $_role_type = (new Agent())->getRoleType($agent_id);
  82. $_agent_model = new UserModel();
  83. if (AgentConst::ROLE_TYPE_ADMIN == $_role_type || AgentConst::ROLE_TYPE_MANAGER == $_role_type) {
  84. $_role_ids = (new RoleModel())->getIdsByRoleType($role_type);
  85. $_agent_arr = $_agent_model->getIdsByRoleId($_role_ids);
  86. } elseif (AgentConst::ROLE_TYPE_PC == $_role_type) {
  87. if (1 == count($role_type) && in_array(AgentConst::ROLE_TYPE_GROUP, $role_type)) {
  88. $_agent_arr = $_agent_model->getSubAgents($agent_id, $inc_me);
  89. } elseif (1 == count($role_type) && in_array(AgentConst::ROLE_TYPE_AGENT, $role_type)) {
  90. $_group_arr = $_agent_model->getSubAgents($agent_id, $inc_me);
  91. $_agent_arr = $_agent_model->getSubAgents($_group_arr, false);
  92. } else {
  93. $_group_arr = $_agent_model->getSubAgents($agent_id, $inc_me);
  94. $_agent_arr = $_agent_model->getSubAgents($_group_arr, true);
  95. }
  96. } elseif (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
  97. $_role_id = $_agent_model->getRoleIdById($agent_id);
  98. if (AgentConst::AGENT_ROLE_MP_ACCOUNT == $_role_id) {
  99. $_agent_arr = $_agent_model->getSubAgents($agent_id, $inc_me, AgentConst::AGENT_ROLE_MP_AD);
  100. } else {
  101. $_agent_arr = $_agent_model->getSubAgents($agent_id, $inc_me, AgentConst::AGENT_ROLE_MP_AGENT);
  102. }
  103. } else {
  104. $_agent_arr = [$agent_id];
  105. }
  106. return $_agent_arr;
  107. }
  108. /**
  109. * 获取渠道Id对应名称
  110. *
  111. * @param int $agent_id
  112. * @param bool $inc_me
  113. * @param array $role_type
  114. * @param bool $inc_nice
  115. *
  116. * @return array
  117. */
  118. public function getIdNames(
  119. $agent_id, $inc_me = false, $role_type = [AgentConst::ROLE_TYPE_GROUP, AgentConst::ROLE_TYPE_AGENT],
  120. $inc_nice = false
  121. ) {
  122. $_role_type = (new Agent())->getRoleType($agent_id);
  123. $_role_ids = (new RoleModel())->getIdsByRoleType($role_type);
  124. $_map['role_id'] = ['in', $_role_ids];
  125. $_map['user_status'] = AgentConst::USER_STATUS_ACTIVATED;
  126. $_agent_model = new UserModel();
  127. if (AgentConst::ROLE_TYPE_PC == $_role_type) {
  128. if (1 == count($role_type) && in_array(AgentConst::ROLE_TYPE_AGENT, $role_type)) {
  129. $_map['id'] = ['in', $_agent_model->getSubAgents($agent_id, $inc_me)];
  130. } elseif (1 == count($role_type) && in_array(AgentConst::ROLE_TYPE_GROUP, $role_type)) {
  131. $_map['parent_id'] = ['in', $_agent_model->getSubAgents($agent_id, $inc_me)];
  132. } else {
  133. $_group_arr = $_agent_model->getSubAgents($agent_id, $inc_me);
  134. $_map['parent_id'] = ['in', $_group_arr];
  135. }
  136. } elseif (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
  137. $_agent_arr = $_agent_model->getSubAgents($agent_id, $inc_me);
  138. $_map['id'] = ['in', $_agent_arr];
  139. } elseif (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
  140. $_map['id'] = $agent_id;
  141. }
  142. return $_agent_model->getIdNames($_map, $inc_nice);
  143. }
  144. public function getIdNamesByRoleId($role_id = [], $inc_nice = false) {
  145. $_map['role_id'] = ['in', $role_id];
  146. $_agent_model = new UserModel();
  147. return $_agent_model->getIdNames($_map, $inc_nice);
  148. }
  149. /**
  150. * 添加渠道
  151. *
  152. * @param array $data
  153. *
  154. * @return bool|mixed
  155. */
  156. public function addAgent($data) {
  157. $_id = (new UserModel())->addData($data);
  158. return $_id;
  159. }
  160. /**
  161. * 根据域名获取AgentId
  162. *
  163. * @param $site
  164. *
  165. * @return int|mixed
  166. */
  167. public function getAgentIdBySite($site) {
  168. if (strpos($site, DOCDOMAIN)) {
  169. /* 系统分配的域名 */
  170. $site = str_replace("http://", "", $site);
  171. $site = str_replace("https://", "", $site);
  172. $_domain_arr = explode('.', $site);
  173. $_map['user_login'] = $_domain_arr['0'];
  174. if (empty($_map['user_login'])) {
  175. return 0;
  176. }
  177. } else {
  178. /* 独有域名 */
  179. $_map['user_url'] = $site;
  180. }
  181. $_key = 'agent_site_key_'.$site;
  182. $_agent_id = (new UserModel())->cache($_key)->where($_map)->value('id');
  183. if (empty($_agent_id)) {
  184. return 0;
  185. }
  186. return $_agent_id;
  187. }
  188. /**
  189. * 统计总玩家 昨日玩家 今日玩家
  190. *
  191. * @param int|array $agent_ids
  192. *
  193. * @return array
  194. * sum_user_cnt 总玩家数
  195. * today_user_cnt 今日玩家数
  196. * yesterday_user_cnt 昨日玩家数
  197. */
  198. public function getStaticMem($agent_ids) {
  199. $_agent_ids = $agent_ids;
  200. if (is_numeric($agent_ids)) {
  201. $_agent_ids = [$agent_ids];
  202. }
  203. $_map = [];
  204. if (!empty($agent_ids)) {
  205. $_map['agent_id'] = ['in', $_agent_ids];
  206. }
  207. $_mem_model = new MemberModel();
  208. $_rdata['sum_user_cnt'] = $_mem_model->where($_map)->count();
  209. list($today_start, $today_end) = Time::today();
  210. $_map['create_time'] = ['gt', $today_start];
  211. $_rdata['today_user_cnt'] = $_mem_model->where($_map)->count();
  212. $_map['create_time'] = ['between', [$today_start - CommonConst::CONST_DAY_SECONDS, $today_start]];
  213. $_rdata['yesterday_user_cnt'] = $_mem_model->where($_map)->count();
  214. return $_rdata;
  215. }
  216. }