// +---------------------------------------------------------------------- namespace plugin\saiadmin\basic; use plugin\saiadmin\exception\ApiException; use think\facade\Db; /** * 逻辑层基础类 * @package app\service * @method static where($data) think-orm的where方法 * @method static find($id) think-orm的find方法 * @method static findOrEmpty($id) think-orm的findOrEmpty方法 * @method static hidden($data) think-orm的hidden方法 * @method static order($data) think-orm的order方法 * @method static save($data) think-orm的save方法 * @method static create($data) think-orm的create方法 * @method static saveAll($data) think-orm的saveAll方法 * @method static update($data, $where, $allow = []) think-orm的update方法 * @method static select() think-orm的select方法 * @method static count($data) think-orm的count方法 * @method static max($data) think-orm的max方法 * @method static min($data) think-orm的min方法 * @method static sum($data) think-orm的sum方法 * @method static avg($data) think-orm的avg方法 */ class BaseLogic { /** * @var object 模型注入 */ protected $model; /** * @var object 管理员信息 */ protected $adminInfo; /** * 排序字段 * @var string */ protected string $orderField = ''; /** * 排序方式 * @var string */ protected string $orderType = 'ASC'; /** * 初始化 * @param $user * @return void */ public function init($user): void { $this->adminInfo = $user; } /** * 设置排序字段 * @param $field * @return void */ public function setOrderField($field): void { $this->orderField = $field; } /** * 设置排序方式 * @param $type * @return void */ public function setOrderType($type): void { $this->orderType = $type; } /** * 数据库事务操作 * @param callable $closure * @param bool $isTran * @return mixed */ public function transaction(callable $closure, bool $isTran = true): mixed { return $isTran ? Db::transaction($closure) : $closure(); } /** * 添加数据 * @param $data * @return mixed */ public function add($data): mixed { $this->model->save($data); return $this->model->getKey(); } /** * 修改数据 * @param $id * @param $data * @return mixed */ public function edit($id, $data): mixed { $model = $this->model->findOrEmpty($id); if ($model->isEmpty()) { throw new ApiException('数据不存在'); } return $model->save($data); } /** * 读取数据 * @param $id * @return mixed */ public function read($id): mixed { $model = $this->model->findOrEmpty($id); if ($model->isEmpty()) { throw new ApiException('数据不存在'); } return $model; } /** * 删除数据 * @param $ids */ public function destroy($ids) { $this->model->destroy($ids); } /** * 搜索器搜索 * @param array $searchWhere * @return mixed */ public function search(array $searchWhere = []): mixed { $withSearch = array_keys($searchWhere); $data = $searchWhere; foreach ($withSearch as $k => $v) { if ($data[$v] === '') { unset($data[$v]); unset($withSearch[$k]); } } return $this->model->withSearch($withSearch, $data); } /** * 根据权限搜索器 * @param array $searchWhere * @param array $withSearch 搜索器 * @return mixed */ public function searchByAuth(array $searchWhere = []): mixed { $withSearch = array_keys($searchWhere); $data = $searchWhere; // 获取游戏权限 $auth_game_list = request()->header('auth_game_list'); $auth_normal_game_list = request()->header('auth_normal_game_list'); $auth_ad_permission = request()->header('auth_ad_permission'); foreach ($withSearch as $k => $v) { if ($data[$v] === '') { unset($data[$v]); unset($withSearch[$k]); } } // 游戏权限 // if(!empty($auth_game_list)){ // // 如果传入的game_id存在,则取交集 // if(!empty($data['game_id'])){ // $data['game_id'] = array_values(array_intersect(explode(',', $data['game_id']), explode(',', $auth_game_list))); // }else{ // // 如果传入的game_id不存在,则取权限中的game_id // $data['game_id'] = $auth_game_list?explode(',', $auth_game_list) : ''; // } // } if(!empty($auth_game_list)){ echo '进入auth_game_list'; if(!empty($data['game_id'])){ $inputGameIds = is_array($data['game_id']) ? $data['game_id'] : explode(',', $data['game_id']); $authGameIds = explode(',', $auth_game_list); $data['game_id'] = array_values(array_intersect($inputGameIds, $authGameIds)); }else{ $data['game_id'] = $auth_game_list ? explode(',', $auth_game_list) : ''; } } // // 自然量游戏权限 // if(!empty($auth_normal_game_list)){ // // 如果传入的game_id存在,则取交集 // if(!empty($data['nomal_game_id'])){ // $data['nomal_game_id'] = array_values(array_intersect(explode(',', $data['nomal_game_id']), explode(',', $auth_normal_game_list))); // }else{ // // 如果传入的game_id不存在,则取权限中的game_id // $data['nomal_game_id'] = $auth_normal_game_list?explode(',', $auth_normal_game_list) : ''; // } // } // 广告数据权限 // 全部 if($auth_ad_permission==1){ $data['auth_id'] = []; } // 自己以及下面组员 if($auth_ad_permission==2){ $current_user_id = $this->adminInfo['id']; $result = Db::connect('db_system')->table('sa_system_user')->field('dept_id')->where('id', $current_user_id)->select()->toArray(); $dept_ids = array_column($result, 'dept_id'); $under_user_ids = Db::connect('db_system')->table('sa_system_user')->field('id')->where('dept_id', 'in', $dept_ids)->select()->toArray(); $under_user_ids = array_column($under_user_ids, 'id'); $data['auth_id'] = $under_user_ids; } // return $data; // print_r($data); // return $this->model->withSearch($withSearch, $data); return $data; } /** * 分页查询数据 * @param $query * @return mixed */ public function getList($query): mixed { $saiType = request()->input('saiType', 'list'); $page = request()->input('page', 1); $limit = request()->input('limit', 10); $orderBy = request()->input('orderBy', ''); $orderType = request()->input('orderType', $this->orderType); if(empty($orderBy)) { $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk(); } $query->order($orderBy, $orderType); if ($saiType === 'all') { return $query->select()->toArray(); } return $query->paginate($limit, false, ['page' => $page])->toArray(); } /** * 获取全部数据 * @param $query * @return mixed */ public function getAll($query): mixed { $orderBy = request()->input('orderBy', ''); $orderType = request()->input('orderType', $this->orderType); if(empty($orderBy)) { $orderBy = $this->orderField !== '' ? $this->orderField : $this->model->getPk(); } $query->order($orderBy, $orderType); return $query->select()->toArray(); } /** * 获取IP地理位置 */ public function getIpLocation($ip): string { $ip2region = new \Ip2Region(); try { $region = $ip2region->memorySearch($ip); } catch (\Exception $e) { return '未知'; } list($country, $number, $province, $city, $network) = explode('|', $region['region']); if ($network === '内网IP') { return $ip.' '.$network; } if ($country == '中国') { return $ip.' '.$province.'-'.$city.':'.$network; } else if ($country == '0') { return $ip.' 未知'; } else { return $ip.' '.$country; } } /** * 转换列表列 * @param $data * @param $fields * @return mixed */ public function trandformListColumn($data, $fields=['site', 'agent', 'game', 'auth', 'media', 'pay_channel', 'game_pay_channel', 'ip']){ if(in_array('site', $fields)){ $agentSiteList = Db::connect('db_advert')->table('agent_site')->field('id,name')->select()->toArray(); $agentSiteList = array_column($agentSiteList, 'name', 'id'); } if(in_array('agent', $fields)){ $agentList = Db::connect('db_advert')->table('agent_list')->field('id,name')->select()->toArray(); $agentList = array_column($agentList, 'name', 'id'); } if(in_array('game', $fields)){ $gameList = Db::connect('db_center')->table('pf_game')->field('id,name,os,ios_appid')->select()->toArray(); $gameList = array_column($gameList, null, 'id'); } if(in_array('auth', $fields)){ $authList = Db::connect('db_system')->table('sa_system_user')->field('id,username')->select()->toArray(); $authList = array_column($authList, 'username', 'id'); } if(in_array('media', $fields)){ $mediaList = Db::connect('db_advert')->table('media_list')->field('id,name')->select()->toArray(); $mediaList = array_column($mediaList, 'name', 'id'); } if(in_array('pay_channel', $fields)){ $payChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status',1)->select()->toArray(); $payChannelList = array_column($payChannelList, 'name', 'id'); } if(in_array('game_pay_channel', $fields)){ $gamePayChannelList = Db::connect('db_center')->table('pay_channel')->field('id,name')->where('status',1)->select()->toArray(); $gamePayChannelList = array_column($gamePayChannelList, 'name', 'id'); } foreach ($data as $key => $value) { if(!empty($agentSiteList) ){ print_r('$agentSiteList'); $data[$key]['site_name'] = $agentSiteList[$value['site_id']] ?? ''; } if(!empty($agentList) ){ $data[$key]['agent_name'] =$agentList[$value['agent_id']] ?? ''; } if(!empty($gameList) ){ $data[$key]['game_name'] = $gameList[$value['game_id']]['name'] ?? ''; $data[$key]['game_os'] = $gameList[$value['game_id']]['os'] ?? ''; $data[$key]['ios_appid'] = $gameList[$value['game_id']]['ios_appid'] ?? ''; } if(!empty($authList) ){ $data[$key]['auth_name'] = $authList[$value['auth_id']] ?? ''; } if(!empty($mediaList) ){ $data[$key]['media_name'] = $mediaList[$value['media_id']] ?? ''; } if(!empty($payChannelList) ){ $data[$key]['pay_channel_name'] = $payChannelList[$value['pay_channel_id']] ?? ''; } if(!empty($gamePayChannelList)){ $data[$key]['alipay_wap_name'] = $gamePayChannelList[$value['alipay_wap']] ?? '-'; $data[$key]['inapp_name'] = $gamePayChannelList[$value['inapp']] ?? '-'; $data[$key]['wechat_wap_name'] = $gamePayChannelList[$value['wechat_wap']] ?? '-'; $data[$key]['wechat_scan_name'] = $gamePayChannelList[$value['wechat_scan']] ?? '-'; $data[$key]['wechat_jsapi_name'] = $gamePayChannelList[$value['wechat_jsapi']] ?? '-'; } if(in_array('ip', $fields)){ $data[$key]['ip'] = $this->getIpLocation($value['ip']) ?? ''; } } return $data; } /** * 获取上传的导入文件 * @param $file * @return string */ public function getImport($file): string { $full_dir = runtime_path() . '/resource/'; if (!is_dir($full_dir)) { mkdir($full_dir, 0777, true); } $ext = $file->getUploadExtension() ?: null; $full_path = $full_dir. md5(time()). '.'. $ext; $file->move($full_path); return $full_path; } /** * 方法调用 * @param $name * @param $arguments * @return mixed */ public function __call($name, $arguments) { // TODO: Implement __call() method. return call_user_func_array([$this->model, $name], $arguments); } }