BaseLogic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic;
  8. use plugin\saiadmin\app\cache\UserInfoCache;
  9. use plugin\saiadmin\exception\ApiException;
  10. use think\facade\Db;
  11. /**
  12. * 逻辑层基础类
  13. * @package app\service
  14. * @method static where($data) think-orm的where方法
  15. * @method static find($id) think-orm的find方法
  16. * @method static findOrEmpty($id) think-orm的findOrEmpty方法
  17. * @method static hidden($data) think-orm的hidden方法
  18. * @method static order($data) think-orm的order方法
  19. * @method static save($data) think-orm的save方法
  20. * @method static create($data) think-orm的create方法
  21. * @method static saveAll($data) think-orm的saveAll方法
  22. * @method static update($data, $where, $allow = []) think-orm的update方法
  23. * @method static select() think-orm的select方法
  24. * @method static count($data) think-orm的count方法
  25. * @method static max($data) think-orm的max方法
  26. * @method static min($data) think-orm的min方法
  27. * @method static sum($data) think-orm的sum方法
  28. * @method static avg($data) think-orm的avg方法
  29. */
  30. class BaseLogic
  31. {
  32. /**
  33. * @var object 模型注入
  34. */
  35. protected $model;
  36. /**
  37. * @var object 管理员信息
  38. */
  39. protected $adminInfo;
  40. /**
  41. * 排序字段
  42. * @var string
  43. */
  44. protected string $orderField = '';
  45. /**
  46. * 排序方式
  47. * @var string
  48. */
  49. protected string $orderType = 'ASC';
  50. /**
  51. * 初始化
  52. * @param $user
  53. * @return void
  54. */
  55. public function init($user): void
  56. {
  57. $this->adminInfo = $user;
  58. }
  59. /**
  60. * 设置排序字段
  61. * @param $field
  62. * @return void
  63. */
  64. public function setOrderField($field): void
  65. {
  66. $this->orderField = $field;
  67. }
  68. /**
  69. * 设置排序方式
  70. * @param $type
  71. * @return void
  72. */
  73. public function setOrderType($type): void
  74. {
  75. $this->orderType = $type;
  76. }
  77. /**
  78. * 数据库事务操作
  79. * @param callable $closure
  80. * @param bool $isTran
  81. * @return mixed
  82. */
  83. public function transaction(callable $closure, bool $isTran = true): mixed
  84. {
  85. return $isTran ? Db::transaction($closure) : $closure();
  86. }
  87. /**
  88. * 添加数据
  89. * @param $data
  90. * @return mixed
  91. */
  92. public function add($data): mixed
  93. {
  94. $this->model->save($data);
  95. return $this->model->getKey();
  96. }
  97. /**
  98. * 修改数据
  99. * @param $id
  100. * @param $data
  101. * @return mixed
  102. */
  103. public function edit($id, $data): mixed
  104. {
  105. $model = $this->model->findOrEmpty($id);
  106. if ($model->isEmpty()) {
  107. throw new ApiException('数据不存在');
  108. }
  109. return $model->save($data);
  110. }
  111. /**
  112. * 读取数据
  113. * @param $id
  114. * @return mixed
  115. */
  116. public function read($id): mixed
  117. {
  118. $model = $this->model->findOrEmpty($id);
  119. if ($model->isEmpty()) {
  120. throw new ApiException('数据不存在');
  121. }
  122. return $model;
  123. }
  124. /**
  125. * 删除数据
  126. * @param $ids
  127. */
  128. public function destroy($ids)
  129. {
  130. $this->model->destroy($ids);
  131. }
  132. /**
  133. * 搜索器搜索
  134. * @param array $searchWhere
  135. * @return mixed
  136. */
  137. public function search(array $searchWhere = []): mixed
  138. {
  139. $withSearch = array_keys($searchWhere);
  140. $data = $searchWhere;
  141. foreach ($withSearch as $k => $v) {
  142. if ($data[$v] === '') {
  143. unset($data[$v]);
  144. unset($withSearch[$k]);
  145. }
  146. }
  147. return $this->model->withSearch($withSearch, $data);
  148. }
  149. /**
  150. * 根据权限搜索器
  151. * @param array $searchWhere
  152. * @param array $withSearch 搜索器
  153. * @return mixed
  154. */
  155. public function searchByAuth(array $searchWhere = []): mixed
  156. {
  157. $withSearch = array_keys($searchWhere);
  158. $data = $searchWhere;
  159. // 获取游戏权限
  160. $authGameList = request()->header('auth_game_list');
  161. $authNormalGameList = request()->header('auth_normal_game_list');
  162. $authAdPermission = request()->header('auth_ad_permission');
  163. foreach ($withSearch as $k => $v) {
  164. if ($data[$v] === '') {
  165. unset($data[$v]);
  166. unset($withSearch[$k]);
  167. }
  168. }
  169. // 游戏权限
  170. if(!empty($authGameList)){
  171. if(!empty($data['game_id'])){
  172. // 如果传入了game_id,则取权限交集
  173. $inputGameIds = is_array($data['game_id']) ? $data['game_id'] : explode(',', $data['game_id']);
  174. $authGameIds = explode(',', $authGameList);
  175. $data['game_id'] = array_values(array_intersect($inputGameIds, $authGameIds));
  176. }else{
  177. // 如果没传入game_id,则取权限
  178. $data['game_id'] = $authGameList ? explode(',', $authGameList) : '';
  179. }
  180. }
  181. /**广告数据权限-开始***************************************** */
  182. // 广告数据权限-自己以及下面组员
  183. if($authAdPermission==2){
  184. // 根据用户id获取用户权限
  185. $token = getCurrentInfo();
  186. // 用户权限缓存
  187. $userAuthCache = new UserInfoCache($token['id']);
  188. $user_info = $userAuthCache->getUserInfo();
  189. $current_user_id = $user_info['id'];
  190. $result = Db::connect('db_system')->table('sa_system_user')->field('dept_id')->where('id', $current_user_id)->select()->toArray();
  191. $dept_ids = array_column($result, 'dept_id');
  192. $under_user_ids = Db::connect('db_system')->table('sa_system_user')->field('id')->where('dept_id', 'in', $dept_ids)->select()->toArray();
  193. $under_user_ids = array_column($under_user_ids, 'id');
  194. if(!empty($data['auth_id'])){
  195. // 如果传入了负责人ID,则取交集
  196. $data['auth_id'] = array_values(array_intersect( $data['auth_id'], array_values($under_user_ids)));
  197. }else{
  198. // 如果没有传入负责人ID, 则取当前用户以及下面组员
  199. $data['auth_id'] = array_values($under_user_ids);
  200. }
  201. }
  202. // 广告数据权限-如果auth_id为空,则取当前用户
  203. if($authAdPermission==0){
  204. // 根据用户id获取用户权限
  205. $token = getCurrentInfo();
  206. // 用户权限缓存
  207. $userAuthCache = new UserInfoCache($token['id']);
  208. $user_info = $userAuthCache->getUserInfo();
  209. $current_user_id = $user_info['id'];
  210. if(!empty($data['auth_id'])){
  211. // 如果传入了负责人ID,则取交集
  212. $data['auth_id'] = array_values(array_intersect( $data['auth_id'], array_values($under_user_ids)));
  213. }else{
  214. // 如果没有传入负责人ID, 则取当前用户
  215. $data['auth_id'] = $current_user_id;
  216. }
  217. }
  218. /**广告数据权限-结束***************************************** */
  219. /**自然量权限-开始***************************************** */
  220. // 全部游戏的自然量
  221. if($authNormalGameList=='*'){
  222. if(!empty($data['auth_id'])){
  223. $data['auth_id'][] = 0;
  224. }else{
  225. $data['auth_id'] = [0];
  226. }
  227. }
  228. // 看指定游戏的自然量
  229. $data['nomal_game_id'] = "";
  230. if(!empty($authNormalGameList) && $authNormalGameList!='*' && $authNormalGameList!='-1'){
  231. // 如果传入了game_id,则取auth_normal_game_list交集
  232. if(!empty($data['game_id'])){
  233. $authNormalGameList = explode(',', $authNormalGameList);
  234. $data['nomal_game_id'] = array_values(array_intersect($authNormalGameList, $data['game_id']));
  235. }else{
  236. $data['nomal_game_id'] = $authNormalGameList ? explode(',', $authNormalGameList) : '';
  237. }
  238. }
  239. /**自然量权限-结束***************************************** */
  240. return $data;
  241. }
  242. // Todo 玩家日志的公共 where 子句
  243. protected function getUlogCommonWhere($params): array
  244. {
  245. $eqParams = ["user_name", "media_id", "site_id", "agent_id", "vt", "server_id", "server_name"];
  246. $inParams = ["game_id", "auth_id"];
  247. $betweenParams = ["pay_date", "reg_date"];
  248. $betweenTimeParams = ["reg_time", "login_time"];
  249. $where = [];
  250. foreach ($params as $key => $value){
  251. if (in_array($key, $eqParams)) {
  252. $where[$key] = $value;
  253. } elseif (in_array($key, $inParams)) {
  254. $where[$key] = is_string($value) ? explode(',', $value) : $value;
  255. } elseif (in_array($key, $betweenParams)) {
  256. if (!empty($value)) {
  257. $where[] = [$key, 'between', [$value[0] .' 00:00:00', $value[1] .' 23:59:59']];
  258. }
  259. }
  260. elseif (in_array($key, $betweenTimeParams)) {
  261. if (!empty($value)) {
  262. $where[] = [$key, 'between', [strtotime($value[0] .' 00:00:00'), strtotime($value[1] .' 23:59:59')]];
  263. }
  264. }
  265. }
  266. $whereOr = [];
  267. if(!empty($params['nomal_game_id'])){
  268. foreach ($params['nomal_game_id'] as $gameId){
  269. $whereOr[] = "(game_id = {$gameId} AND auth_id=0)";
  270. }
  271. }
  272. $whereRaw = $whereOr ? implode(' OR ', $whereOr) : "1=1";
  273. return [$where, $whereRaw];
  274. }
  275. /**
  276. * 分页查询数据
  277. * @param $query
  278. * @return mixed
  279. */
  280. public function getList($query): mixed
  281. {
  282. $saiType = request()->input('saiType', 'list');
  283. $page = request()->input('page', 1);
  284. $limit = request()->input('limit', 10);
  285. $orderBy = request()->input('orderBy', '');
  286. $orderType = request()->input('orderType', $this->orderType);
  287. if(empty($orderBy)) {
  288. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  289. }
  290. $query->order($orderBy, $orderType);
  291. if ($saiType === 'all') {
  292. return $query->select()->toArray();
  293. }
  294. return $query->paginate($limit, false, ['page' => $page])->toArray();
  295. }
  296. /**
  297. * 获取全部数据
  298. * @param $query
  299. * @return mixed
  300. */
  301. public function getAll($query): mixed
  302. {
  303. $orderBy = request()->input('orderBy', '');
  304. $orderType = request()->input('orderType', $this->orderType);
  305. if(empty($orderBy)) {
  306. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  307. }
  308. $query->order($orderBy, $orderType);
  309. return $query->select()->toArray();
  310. }
  311. /**
  312. * 获取IP地理位置
  313. */
  314. public function getIpLocation($ip): string
  315. {
  316. $ip2region = new \Ip2Region();
  317. try {
  318. $region = $ip2region->memorySearch($ip);
  319. } catch (\Exception $e) {
  320. return '未知';
  321. }
  322. list($country, $number, $province, $city, $network) = explode('|', $region['region']);
  323. if ($network === '内网IP') {
  324. return $ip.' '.$network;
  325. }
  326. if ($country == '中国') {
  327. return $ip.' '.$province.'-'.$city.':'.$network;
  328. } else if ($country == '0') {
  329. return $ip.' 未知';
  330. } else {
  331. return $ip.' '.$country;
  332. }
  333. }
  334. /**
  335. * 转换列表列
  336. * @param $data
  337. * @param $fields
  338. * @return mixed
  339. */
  340. public function trandformListColumn($data, $fields=['site', 'agent', 'game', 'auth', 'author', 'media', 'pay_channel', 'game_pay_channel', 'ip']){
  341. if(in_array('site', $fields)){
  342. $agentSiteList = Db::connect('db_advert')->table('agent_site')->field('id,name')->select()->toArray();
  343. $agentSiteList = array_column($agentSiteList, 'name', 'id');
  344. }
  345. if(in_array('agent', $fields)){
  346. $agentList = Db::connect('db_advert')->table('agent_list')->field('id,name')->select()->toArray();
  347. $agentList = array_column($agentList, 'name', 'id');
  348. }
  349. if(in_array('game', $fields)){
  350. $gameList = Db::connect('db_center')->table('pf_game')->field('id,name,os,ios_appid')->select()->toArray();
  351. $gameList = array_column($gameList, null, 'id');
  352. }
  353. if(in_array('auth', $fields)){
  354. $authList = Db::connect('db_system')->table('sa_system_user')->field('id,username')->select()->toArray();
  355. $authList = array_column($authList, 'username', 'id');
  356. }
  357. if(in_array('author', $fields)){
  358. $authorList = Db::connect('db_system')->table('sa_system_user')->field('id,username')->select()->toArray();
  359. $authorList = array_column($authorList, 'username', 'id');
  360. }
  361. if(in_array('media', $fields)){
  362. $mediaList = Db::connect('db_advert')->table('media_list')->field('id,name')->select()->toArray();
  363. $mediaList = array_column($mediaList, 'name', 'id');
  364. }
  365. if(in_array('pay_channel', $fields)){
  366. $payChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status',1)->select()->toArray();
  367. $payChannelList = array_column($payChannelList, 'name', 'id');
  368. }
  369. if(in_array('game_pay_channel', $fields)){
  370. $gamePayChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status',1)->select()->toArray();
  371. $gamePayChannelList = array_column($gamePayChannelList, 'name', 'id');
  372. }
  373. foreach ($data as $key => $value) {
  374. if(!empty($agentSiteList) ){
  375. $data[$key]['site_name'] = $agentSiteList[$value['site_id']] ?? '';
  376. }
  377. if(!empty($agentList) ){
  378. $data[$key]['agent_name'] =$agentList[$value['agent_id']] ?? '';
  379. }
  380. if(!empty($gameList) ){
  381. $data[$key]['game_name'] = $gameList[$value['game_id']]['name'] ?? '';
  382. $data[$key]['game_os'] = $gameList[$value['game_id']]['os'] ?? '';
  383. $data[$key]['ios_appid'] = $gameList[$value['game_id']]['ios_appid'] ?? '';
  384. }
  385. if(!empty($authList) ){
  386. $data[$key]['auth_name'] = $authList[$value['auth_id']] ?? '';
  387. }
  388. if(!empty($authorList) ){
  389. $data[$key]['author_name'] = $authorList[$value['author_id']] ?? '';
  390. }
  391. if(!empty($mediaList) ){
  392. $data[$key]['media_name'] = $mediaList[$value['media_id']] ?? '';
  393. }
  394. if(!empty($payChannelList) ){
  395. $data[$key]['pay_channel_name'] = $payChannelList[$value['pay_channel_id']] ?? '';
  396. }
  397. if(!empty($gamePayChannelList)){
  398. $data[$key]['alipay_wap_name'] = $gamePayChannelList[$value['alipay_wap']] ?? '-';
  399. $data[$key]['inapp_name'] = $gamePayChannelList[$value['inapp']] ?? '-';
  400. $data[$key]['wechat_wap_name'] = $gamePayChannelList[$value['wechat_wap']] ?? '-';
  401. $data[$key]['wechat_scan_name'] = $gamePayChannelList[$value['wechat_scan']] ?? '-';
  402. $data[$key]['wechat_jsapi_name'] = $gamePayChannelList[$value['wechat_jsapi']] ?? '-';
  403. }
  404. if(in_array('ip', $fields)){
  405. $data[$key]['ip'] = $this->getIpLocation($value['ip']) ?? '';
  406. }
  407. }
  408. return $data;
  409. }
  410. /**
  411. * 获取上传的导入文件
  412. * @param $file
  413. * @return string
  414. */
  415. public function getImport($file): string
  416. {
  417. $full_dir = runtime_path() . '/resource/';
  418. if (!is_dir($full_dir)) {
  419. mkdir($full_dir, 0777, true);
  420. }
  421. $ext = $file->getUploadExtension() ?: null;
  422. $full_path = $full_dir. md5(time()). '.'. $ext;
  423. $file->move($full_path);
  424. return $full_path;
  425. }
  426. /**
  427. * 方法调用
  428. * @param $name
  429. * @param $arguments
  430. * @return mixed
  431. */
  432. public function __call($name, $arguments)
  433. {
  434. // TODO: Implement __call() method.
  435. return call_user_func_array([$this->model, $name], $arguments);
  436. }
  437. // 渠道分析、运营分析生成的whereSql
  438. public function generateWhereSql($params): string
  439. {
  440. $whereSql = "";
  441. // 游戏id
  442. if (!empty($params['game_id'])) {
  443. if (is_array($params['game_id'])) {
  444. $whereSql .= " AND game_id IN(" . implode(',', $params['game_id']) . ")";
  445. } else {
  446. $whereSql .= " AND game_id = {$params['game_id']}";
  447. }
  448. }
  449. // 媒体id
  450. if (!empty($params['media_id'])) {
  451. $whereSql .= " AND media_id = {$params['media_id']}";
  452. }
  453. // 渠道id
  454. if (!empty($params['agent_id'])) {
  455. $whereSql .= " AND agent_id = {$params['agent_id']}";
  456. }
  457. // 广告位id
  458. if (!empty($params['site_id'])) {
  459. if (is_array($params['site_id'])) {
  460. $whereSql .= " AND site_id IN(" . implode(',', $params['site_id']) . ")";
  461. } else {
  462. $whereSql .= " AND site_id = {$params['site_id']}";
  463. }
  464. }
  465. // 负责人
  466. if (!empty($params['auth_id'])) {
  467. if (is_array($params['auth_id'])) {
  468. $whereSql .= " AND auth_id IN(" . implode(',', $params['auth_id']) . ")";
  469. } else {
  470. $whereSql .= " AND auth_id = {$params['auth_id']}";
  471. }
  472. }
  473. // 注册日期
  474. if (!empty($params['reg_date']) ?? null) {
  475. $whereSql .= " AND tdate BETWEEN '{$params['reg_date'][0]}' AND '{$params['reg_date'][1]}'";
  476. }
  477. // 自然量ID, auth_id=0为自然量
  478. if(!empty($params['nomal_game_id'])){
  479. for($i=0;$i<count($params['nomal_game_id']);$i++){
  480. $whereSql .= " OR (game_id = {$params['nomal_game_id'][$i]} AND auth_id=0)";
  481. }
  482. }
  483. // 用户名
  484. if (!empty($params['user_name'])) {
  485. $whereSql .= " AND user_name = {$params['user_name']}";
  486. }
  487. return $whereSql;
  488. }
  489. }