BaseLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\basic;
  8. use plugin\saiadmin\exception\ApiException;
  9. use think\facade\Db;
  10. /**
  11. * 逻辑层基础类
  12. * @package app\service
  13. * @method static where($data) think-orm的where方法
  14. * @method static find($id) think-orm的find方法
  15. * @method static findOrEmpty($id) think-orm的findOrEmpty方法
  16. * @method static hidden($data) think-orm的hidden方法
  17. * @method static order($data) think-orm的order方法
  18. * @method static save($data) think-orm的save方法
  19. * @method static create($data) think-orm的create方法
  20. * @method static saveAll($data) think-orm的saveAll方法
  21. * @method static update($data, $where, $allow = []) think-orm的update方法
  22. * @method static select() think-orm的select方法
  23. * @method static count($data) think-orm的count方法
  24. * @method static max($data) think-orm的max方法
  25. * @method static min($data) think-orm的min方法
  26. * @method static sum($data) think-orm的sum方法
  27. * @method static avg($data) think-orm的avg方法
  28. */
  29. class BaseLogic
  30. {
  31. /**
  32. * @var object 模型注入
  33. */
  34. protected $model;
  35. /**
  36. * @var object 管理员信息
  37. */
  38. protected $adminInfo;
  39. /**
  40. * 排序字段
  41. * @var string
  42. */
  43. protected string $orderField = '';
  44. /**
  45. * 排序方式
  46. * @var string
  47. */
  48. protected string $orderType = 'ASC';
  49. /**
  50. * 初始化
  51. * @param $user
  52. * @return void
  53. */
  54. public function init($user): void
  55. {
  56. $this->adminInfo = $user;
  57. }
  58. /**
  59. * 设置排序字段
  60. * @param $field
  61. * @return void
  62. */
  63. public function setOrderField($field): void
  64. {
  65. $this->orderField = $field;
  66. }
  67. /**
  68. * 设置排序方式
  69. * @param $type
  70. * @return void
  71. */
  72. public function setOrderType($type): void
  73. {
  74. $this->orderType = $type;
  75. }
  76. /**
  77. * 数据库事务操作
  78. * @param callable $closure
  79. * @param bool $isTran
  80. * @return mixed
  81. */
  82. public function transaction(callable $closure, bool $isTran = true): mixed
  83. {
  84. return $isTran ? Db::transaction($closure) : $closure();
  85. }
  86. /**
  87. * 添加数据
  88. * @param $data
  89. * @return mixed
  90. */
  91. public function add($data): mixed
  92. {
  93. $this->model->save($data);
  94. return $this->model->getKey();
  95. }
  96. /**
  97. * 修改数据
  98. * @param $id
  99. * @param $data
  100. * @return mixed
  101. */
  102. public function edit($id, $data): mixed
  103. {
  104. $model = $this->model->findOrEmpty($id);
  105. if ($model->isEmpty()) {
  106. throw new ApiException('数据不存在');
  107. }
  108. return $model->save($data);
  109. }
  110. /**
  111. * 读取数据
  112. * @param $id
  113. * @return mixed
  114. */
  115. public function read($id): mixed
  116. {
  117. $model = $this->model->findOrEmpty($id);
  118. if ($model->isEmpty()) {
  119. throw new ApiException('数据不存在');
  120. }
  121. return $model;
  122. }
  123. /**
  124. * 删除数据
  125. * @param $ids
  126. */
  127. public function destroy($ids)
  128. {
  129. $this->model->destroy($ids);
  130. }
  131. /**
  132. * 搜索器搜索
  133. * @param array $searchWhere
  134. * @return mixed
  135. */
  136. public function search(array $searchWhere = []): mixed
  137. {
  138. $withSearch = array_keys($searchWhere);
  139. $data = $searchWhere;
  140. foreach ($withSearch as $k => $v) {
  141. if ($data[$v] === '') {
  142. unset($data[$v]);
  143. unset($withSearch[$k]);
  144. }
  145. }
  146. return $this->model->withSearch($withSearch, $data);
  147. }
  148. /**
  149. * 根据权限搜索器
  150. * @param array $searchWhere
  151. * @param array $withSearch 搜索器
  152. * @return mixed
  153. */
  154. public function searchByAuth(array $searchWhere = []): mixed
  155. {
  156. $withSearch = array_keys($searchWhere);
  157. $data = $searchWhere;
  158. // 获取游戏权限
  159. $auth_game_list = request()->header('auth_game_list');
  160. $auth_normal_game_list = request()->header('auth_normal_game_list');
  161. $auth_ad_permission = request()->header('auth_ad_permission');
  162. foreach ($withSearch as $k => $v) {
  163. if ($data[$v] === '') {
  164. unset($data[$v]);
  165. unset($withSearch[$k]);
  166. }
  167. }
  168. // 游戏权限
  169. if(!empty($auth_game_list)){
  170. // 如果传入的game_id存在,则取交集
  171. if(!empty($data['game_id'])){
  172. $data['game_id'] = array_values(array_intersect(explode(',', $data['game_id']), explode(',', $auth_game_list)));
  173. }else{
  174. // 如果传入的game_id不存在,则取权限中的game_id
  175. $data['game_id'] = $auth_game_list?explode(',', $auth_game_list) : '';
  176. }
  177. }
  178. // // 自然量游戏权限
  179. // if(!empty($auth_normal_game_list)){
  180. // // 如果传入的game_id存在,则取交集
  181. // if(!empty($data['nomal_game_id'])){
  182. // $data['nomal_game_id'] = array_values(array_intersect(explode(',', $data['nomal_game_id']), explode(',', $auth_normal_game_list)));
  183. // }else{
  184. // // 如果传入的game_id不存在,则取权限中的game_id
  185. // $data['nomal_game_id'] = $auth_normal_game_list?explode(',', $auth_normal_game_list) : '';
  186. // }
  187. // }
  188. // 广告数据权限
  189. // 全部
  190. if($auth_ad_permission==1){
  191. $data['auth_id'] = [];
  192. }
  193. // 自己以及下面组员
  194. if($auth_ad_permission==2){
  195. $current_user_id = $this->adminInfo['id'];
  196. $result = Db::connect('db_system')->table('sa_system_user')->field('dept_id')->where('id', $current_user_id)->select()->toArray();
  197. $dept_ids = array_column($result, 'dept_id');
  198. $under_user_ids = Db::connect('db_system')->table('sa_system_user')->field('id')->where('dept_id', 'in', $dept_ids)->select()->toArray();
  199. $under_user_ids = array_column($under_user_ids, 'id');
  200. $data['auth_id'] = $under_user_ids;
  201. }
  202. return $data;
  203. }
  204. /**
  205. * 分页查询数据
  206. * @param $query
  207. * @return mixed
  208. */
  209. public function getList($query): mixed
  210. {
  211. $saiType = request()->input('saiType', 'list');
  212. $page = request()->input('page', 1);
  213. $limit = request()->input('limit', 10);
  214. $orderBy = request()->input('orderBy', '');
  215. $orderType = request()->input('orderType', $this->orderType);
  216. if(empty($orderBy)) {
  217. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  218. }
  219. $query->order($orderBy, $orderType);
  220. if ($saiType === 'all') {
  221. return $query->select()->toArray();
  222. }
  223. return $query->paginate($limit, false, ['page' => $page])->toArray();
  224. }
  225. /**
  226. * 获取全部数据
  227. * @param $query
  228. * @return mixed
  229. */
  230. public function getAll($query): mixed
  231. {
  232. $orderBy = request()->input('orderBy', '');
  233. $orderType = request()->input('orderType', $this->orderType);
  234. if(empty($orderBy)) {
  235. $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk();
  236. }
  237. $query->order($orderBy, $orderType);
  238. return $query->select()->toArray();
  239. }
  240. /**
  241. * 获取IP地理位置
  242. */
  243. public function getIpLocation($ip): string
  244. {
  245. $ip2region = new \Ip2Region();
  246. try {
  247. $region = $ip2region->memorySearch($ip);
  248. } catch (\Exception $e) {
  249. return '未知';
  250. }
  251. list($country, $number, $province, $city, $network) = explode('|', $region['region']);
  252. if ($network === '内网IP') {
  253. return $ip.' '.$network;
  254. }
  255. if ($country == '中国') {
  256. return $ip.' '.$province.'-'.$city.':'.$network;
  257. } else if ($country == '0') {
  258. return $ip.' 未知';
  259. } else {
  260. return $ip.' '.$country;
  261. }
  262. }
  263. /**
  264. * 转换列表列
  265. * @param $data
  266. * @param $fields
  267. * @return mixed
  268. */
  269. public function trandformListColumn($data, $fields=['site', 'agent', 'game', 'auth', 'media', 'pay_channel', 'game_pay_channel', 'ip']){
  270. if(in_array('site', $fields)){
  271. $agentSiteList = Db::connect('db_advert')->table('agent_site')->field('id,name')->select()->toArray();
  272. $agentSiteList = array_column($agentSiteList, 'name', 'id');
  273. }
  274. if(in_array('agent', $fields)){
  275. $agentList = Db::connect('db_advert')->table('agent_list')->field('id,name')->select()->toArray();
  276. $agentList = array_column($agentList, 'name', 'id');
  277. }
  278. if(in_array('game', $fields)){
  279. $gameList = Db::connect('db_center')->table('pf_game')->field('id,name,os,ios_appid')->select()->toArray();
  280. $gameList = array_column($gameList, null, 'id');
  281. }
  282. if(in_array('auth', $fields)){
  283. $authList = Db::connect('db_system')->table('sa_system_user')->field('id,username')->select()->toArray();
  284. $authList = array_column($authList, 'username', 'id');
  285. }
  286. if(in_array('media', $fields)){
  287. $mediaList = Db::connect('db_advert')->table('media_list')->field('id,name')->select()->toArray();
  288. $mediaList = array_column($mediaList, 'name', 'id');
  289. }
  290. // if(in_array('package', $fields)){
  291. // $packageList = Db::connect('db_game_log')->table('game_package')->field('id,name')->select()->toArray();
  292. // $packageList = array_column($packageList, 'name', 'id');
  293. // }
  294. if(in_array('pay_channel', $fields)){
  295. $payChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status',1)->select()->toArray();
  296. $payChannelList = array_column($payChannelList, 'name', 'id');
  297. }
  298. if(in_array('game_pay_channel', $fields)){
  299. $gamePayChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status',1)->select()->toArray();
  300. $gamePayChannelList = array_column($gamePayChannelList, 'name', 'id');
  301. }
  302. foreach ($data as $key => $value) {
  303. $data[$key]['site_name'] = !empty($agentSiteList) ? $agentSiteList[$value['site_id']] ?? '':'';
  304. $data[$key]['agent_name'] = !empty($agentList) ? $agentList[$value['agent_id']] ?? '':'';
  305. $data[$key]['game_name'] = !empty($gameList) ? $gameList[$value['game_id']]['name'] ?? '':'';
  306. $data[$key]['game_os'] = !empty($gameList) ? $gameList[$value['game_id']]['os'] ?? '':'';
  307. $data[$key]['ios_appid'] = !empty($gameList) ? $gameList[$value['game_id']]['ios_appid'] ?? '':'';
  308. $data[$key]['auth_name'] = !empty($authList) ? $authList[$value['auth_id']] ?? '':'';
  309. $data[$key]['media_name'] = !empty($mediaList) ? $mediaList[$value['media_id']] ?? '':'';
  310. // $data[$key]['package_name'] = !empty($packageList) ? $packageList[$value['package_id']] ?? '':'';
  311. $data[$key]['pay_channel_name'] = !empty($payChannelList) ? $payChannelList[$value['pay_channel_id']] ?? '':'';
  312. $data[$key]['alipay_wap_name'] = !empty($gamePayChannelList) ? $gamePayChannelList[$value['alipay_wap']] ?? '':'';
  313. $data[$key]['inapp_name'] = !empty($gamePayChannelList) ? $gamePayChannelList[$value['inapp']] ?? '':'';
  314. $data[$key]['wechat_wap_name'] = !empty($gamePayChannelList) ? $gamePayChannelList[$value['wechat_wap']] ?? '':'';
  315. $data[$key]['wechat_scan_name'] = !empty($gamePayChannelList) ? $gamePayChannelList[$value['wechat_scan']] ?? '':'';
  316. $data[$key]['wechat_jsapi_name'] = !empty($gamePayChannelList) ? $gamePayChannelList[$value['wechat_jsapi']] ?? '':'';
  317. $data[$key]['ip'] = in_array('ip', $fields) ? $this->getIpLocation($value['ip']) : '';
  318. }
  319. return $data;
  320. }
  321. /**
  322. * 获取上传的导入文件
  323. * @param $file
  324. * @return string
  325. */
  326. public function getImport($file): string
  327. {
  328. $full_dir = runtime_path() . '/resource/';
  329. if (!is_dir($full_dir)) {
  330. mkdir($full_dir, 0777, true);
  331. }
  332. $ext = $file->getUploadExtension() ?: null;
  333. $full_path = $full_dir. md5(time()). '.'. $ext;
  334. $file->move($full_path);
  335. return $full_path;
  336. }
  337. /**
  338. * 方法调用
  339. * @param $name
  340. * @param $arguments
  341. * @return mixed
  342. */
  343. public function __call($name, $arguments)
  344. {
  345. // TODO: Implement __call() method.
  346. return call_user_func_array([$this->model, $name], $arguments);
  347. }
  348. }