| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- <?php
- /**
- * 直播部门抓取数据整理
- */
- namespace app\guildapi\controller;
- use app\api\validate\Pay;
- use app\common\logic\SubPackage as SubChannel;
- use think\Db;
- use think\Env;
- use think\Exception;
- use think\Controller;
- class LiveGrad extends Guild
- {
- protected $_channelId; // 渠道ID
- protected $_level; // 层级:0(B+账号),1(B账号),2(B-账号),3(推广员)
- protected function _initialize()
- {
- parent::_initialize();
- $guildInfo = session('guild_info');
- // dump($guildInfo);
- $this->_channelId = $guildInfo['channel_id'];
- // $this->_level = $guildInfo['level'];
- log_message("input: ".json_encode(input()), 'log', LOG_PATH . 'live_grad_log/');
- }
- /**
- * 添加推广员
- *
- * @param $channel_id C级的渠道ID(用于判断是否有有上级,没有则新建)
- *
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function userCreate()
- {
- $channel_id = $this->request->post('channel');
- $level = $this->request->post('level');
- $this->_level = $level;
- if ($channel_id != ""){
- $this->_channelId = $channel_id;
- }
- try {
- // 判断B级账户,没有则创建
- // if ($channel_id) {
- // $c_info = Db::name('nw_channel')->where(['id' => $channel_id, 'level' => 3])->find();
- // if (!$c_info){
- // throw new Exception("渠道号错误");
- // }
- // $c_info = Db::name('nw_channel')->where(['id' => $c_info['parent_id'], 'level' => 2])->find();
- // $parent_id = $c_info['id'];
- // $parent_name = $c_info['name'];
- // }else{
- // $b_res = $this->_judgeUserCreateB();
- // $parent_id = $b_res['data']['id'];
- // $parent_name = $b_res['data']['name'];
- // }
- if ($level != 3){
- $b_res = $this->_judgeUserCreateB();
- }else{
- $c_info = Db::name('nw_channel')->where(['id' => $channel_id])->find();
- $b_res = $this->_judgeUserCreateC($channel_id,$c_info['name']);
- }
- // 查询B级账户信息
- // $b_info = Db::name('nw_channel')->where(['id' => $parent_id, 'level' => 2])->find();
- // dump("## b_info: ", $b_info);
- // if($c_info['code'] != 20000){
- // return $this->jsonResult(['data' => '', 'code' => $c_info['code'], 'msg' => $c_info['msg']]);
- // }
- // 创建C级账户
- // $c_info = $this->_judgeUserCreateC($parent_id,$parent_name);
- // dump("## c_info: ", $c_info);
- if($b_res['code'] != 20000){
- // return $this->jsonResult(['data' => '', 'code' => $c_info['code'], 'msg' => $c_info['msg']]);
- throw new Exception($b_res['msg'], $b_res['code']);
- }
- } catch (\Exception $e) {
- // return ['data' => '', 'code' => 10015, 'msg' => "添加失败: " . $e->getMessage()];
- $this->jsonResult('', 10015, "添加失败: " . $e->getMessage()." - ".$e->getFile().":".$e->getLine().", errcode:".$e->getCode());
- }
- return $this->jsonResult($b_res['data'], 20000, 'success');
- }
- private function _judgeUserCreateB(){
- $channel = Db::name('nw_channel')->where(['id' => $this->_channelId])->find();
- if ($this->_level == 1){
- $id_path = ','.$this->_channelId.',';
- }else{
- $id_path = ',' . $channel['parent_id'] . ','.$this->_channelId.',';
- }
- // $parent_id = Db::name('nw_channel')->where(['id' => $this->_channelId])->value('parent_id');
- // 获取最新用户名及其标识
- $user_pwd = random(8);
- if ($this->_level == 1){
- $where = ['level' => $this->_level, 'name' => ['like', 'qmsy%']];
- // if(!empty($this->_channelId)){
- // $where['parent_id'] = $this->_channelId;
- // // $where['id_path'] = $id_path;
- // }
- // dump($where);
- $newChannelName = Db::name('nw_channel')->where($where)->order('name', 'desc')->value('name');
- $user_name = 'qmsy00001';
- // dump($newChannelName);
- if (strpos($newChannelName, 'qmsy') !== false) {
- $startChannel = explode('qmsy', $newChannelName);
- if (is_numeric($startChannel[1]) === true) {
- $channelNum = $startChannel[1] + 1;
- $user_name = 'qmsy' . str_pad($channelNum, 5, "0", STR_PAD_LEFT);
- }
- }
- }else{
- $user_name = $channel['name'].'t1';
- $newChannelName = Db::name('nw_channel')->where(['parent_id' => $this->_channelId, 'id_path' => $id_path, 'level' => $this->_level, 'name' => ['like', $channel['name'].'%']])->order('create_time', 'desc')->value('name');
- if (strpos($newChannelName, $channel['name'].'t') !== false) {
- $startChannel = explode($channel['name'].'t', $newChannelName);
- if (is_numeric($startChannel[1]) === true) {
- $channelNum = $startChannel[1] + 1;
- $user_name = $channel['name'].'t'.$channelNum;
- }
- }
- }
- // dump($user_name);
- $channelModel = model('channel');
- $channelInfo = $channelModel->where(['name' => $user_name])->find();
- if ($channelInfo) {
- return ['data' => '', 'code' => 10021, 'msg' => '当前创建渠道已存在!'];
- }
- $userInfo = Db::name('nw_channel_admin')->field('id')->where(['username' => $user_name])->find();
- if ($userInfo) {
- return ['data' => '', 'code' => 10022, 'msg' => '当前渠道管理员已存在!'];
- }
- Db::startTrans();
- try {
- // 添加渠道信息
- $channelData = [
- 'name' => $user_name,
- // 'replacement_status' => $data['replacement_status'],//代充
- // 'share_show' => $data['share_show'],//分成比例
- 'parent_id' => $this->_channelId,
- 'id_path' => $id_path,
- 'create_time' => NOW_TIMESTAMP,
- 'level' => $this->_level,
- 'flag' => 3,
- 'status' => 1,
- 'gh_status' => 1,
- ];
- $channelId = $channelModel->insertGetId($channelData);
- if ( !$channelId ) {
- throw new Exception("添加渠道账号失败");
- }
- //添加渠道信息
- // $id = $channelModel->insertGetId([
- // 'name' => $user_name,
- // 'parent_id' => $this->_channelId,
- // 'id_path' => $id_path,
- // 'create_time' => NOW_TIMESTAMP,
- // 'level' => 1,
- // 'flag' => 3,
- // 'status' => 1
- // ]);
- // if (!$id) {
- // throw new Exception("添加渠道账号失败");
- // }
- $cont['username'] = $user_name;
- $cont['password'] = mg_password($user_pwd);
- $cont['status'] = 1;
- $cont['channel_id'] = $channelId;
- $cont['create_time'] = NOW_TIMESTAMP;
- $cont['password_update_time'] = NOW_TIMESTAMP;
- $b_id = Db::name('nw_channel_admin')->insertGetId($cont);
- if ($b_id === false) {
- throw new Exception("添加渠道账号失败");
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return ['data' => '', 'code' => 10020, 'msg' => $e->getMessage().', '.$e->getFile().':'.$e->getLine()];
- }
- return [
- 'data' => [
- 'channel' => $channelId,
- 'name' => $user_name,
- 'password'=>$user_pwd
- ],
- 'code' => 20000,
- 'msg' => "",
- ];
- }
- /**
- * 返回封装后的 API json数据到客户端
- * @access protected
- * @param mixed $data 要返回的数据
- * @param int $code 返回的 code
- * @param mixed $msg 提示信息
- * @param $isExit boolean 是否强制退出,默认true
- * @return void
- *
- */
- public function jsonResult($data, $code = 0, $msg = '', $isExit = true)
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => request()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- log_message("result: ".json_encode($result), 'log', LOG_PATH . 'live_grad_log/');
- echo json_encode($result);
- //为了使用fastcgi_finish_request等函数时,后续的执行能够继续生效
- if ($isExit) exit;
- }
- /**
- * 创建C级账户
- *
- * @param $parent_id B级关联ID
- * @param $parent_name B级用户名
- *
- * @return array
- */
- private function _judgeUserCreateC($parent_id, $parent_name){
- $id_path = Db::name('nw_channel')->where(['id'=>$parent_id])->value('id_path').$parent_id.',';
- // 获取最新用户名及其标识
- $user_name = $parent_name.'-1';
- $user_pwd = random(8);
- $newChannelName = Db::name('nw_channel')->where(['parent_id' => $parent_id, 'level' => 3, 'name' => ['like', 'qmsy%']])->order('create_time', 'desc')->value('name');
- if (strpos($newChannelName, 'qmsy') !== false) {
- $startChannel = explode('-', $newChannelName);
- if (is_numeric($startChannel[1]) === true) {
- $channelNum = $startChannel[1] + 1;
- $user_name = $parent_name .'-'. $channelNum;
- }
- }
- //1. 添加渠道信息
- $channelModel = model('channel');
- $channelInfo = $channelModel->where(['name' => $user_name])->find();
- if ( $channelInfo ) {
- return ['data'=>'','code'=>10031,'msg'=> "该渠道信息已存在"];
- }
- $userInfo = Db::name('nw_channel_admin')->field('id')->where(['username' => $user_name])->find();
- if ($userInfo) {
- return ['data'=>'','code'=>10032,'msg'=> "用户名已存在"];
- }
- Db::startTrans();
- try {
- //添加渠道信息
- $id = $channelModel->insertGetId([
- 'name' => $user_name,
- 'parent_id' => $parent_id,
- 'id_path' => $id_path,
- 'create_time' => NOW_TIMESTAMP,
- 'level' => 3,
- 'flag' => 3,
- 'status' => 1,
- 'gh_status' => 1,
- ]);
- if ( !$id) {
- throw new Exception("添加渠道账号失败");
- }
- $cont['username'] = $user_name;
- $cont['password'] = mg_password($user_pwd);
- $cont['status'] = 1;
- $cont['channel_id'] = $id;
- $cont['create_time'] = NOW_TIMESTAMP;
- $cont['password_update_time'] = NOW_TIMESTAMP;
- $c_id = Db::name('nw_channel_admin')->insertGetId($cont);
- if ($c_id === false) {
- throw new Exception("添加渠道账号失败");
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- return ['data'=>'','code'=>10030,'msg'=> $e->getMessage().', '.$e->getFile().':'.$e->getLine()];
- }
- return [
- 'data' => [
- 'channel' => $id,
- 'name' => $user_name,
- 'password' => $user_pwd,
- ],
- 'code' => 20000,
- 'msg' =>"",
- ];
- }
- // 分包
- public function gamePack()
- {
- $game_id = $this->request->post('yxid');
- $channel_id = $this->request->post('channel');
- // $status = $this->request->post('status');
- if (!$game_id) {
- return $this->jsonResult('', 10015, '非法操作:缺少游戏ID!');
- }
- if (!$channel_id) {
- return $this->jsonResult('', 10015, '非法操作:缺少渠道ID!');
- }
- $gameInfo = model('Game')->field(['id', 'channel_version', 'type', 'cooperation_status', 'channel_version', 'pinyin'])->where(['id' => $game_id])->find();
- $sdkInfo = model('SdkGameList')->field(['id', 'upload_status', 'channel_version', 'filename'])->where(['gameid' => $game_id, 'channel_id' => $channel_id])->find();
- // upload_status=文件状态:0=待处理、1=已处理、2=待审核、3=申请拒绝、4=分包中、5=打包失败、9=已禁用
- // 游戏不存在
- if (!$gameInfo) {
- return $this->jsonResult('', 10015, '当前游戏不存在!');
- }
- // if (!$sdkInfo) {
- // $res = $this->package($game_id, $channel_id, $gameInfo['type']);
- // if ($res['code'] == 0) {
- // return $this->jsonResult('', 20001, '分包中....');
- // }
- // return $this->jsonResult('', 10015, "打包失败:" . $res['msg']);
- // // return $this->jsonResult('', 10015, '当前游戏渠道不存在!');
- // }
- // 游戏关闭
- if ($gameInfo['cooperation_status'] != 1) {
- return $this->jsonResult('', 10015, '当前游戏未上线!');
- }
- // 渠道禁用
- if (!empty($sdkInfo) && $sdkInfo['upload_status'] == 9) {
- return $this->jsonResult('', 10017, '当前渠道已禁用!');
- }
- $pack_status = false;
- $msg = '打包失败';
- if ($sdkInfo['upload_status'] == 4 || $sdkInfo['upload_status'] == 1) {
- $pack_status = true;
- $msg = '打包成功';
- }
- $filename = $sdkInfo['filename'];
- if (empty($sdkInfo) || $gameInfo['channel_version'] != $sdkInfo['channel_version'] || $sdkInfo['upload_status'] == 5) {
- $result = $this->package($game_id, $channel_id, $gameInfo['type'], 1);
- if ($result['code'] == 1) {
- $filename = $result['data']['filename'];
- $pack_status = true;
- } else {
- $msg .= ': ' . $result['msg'];
- }
- }
- if ($pack_status) {
- $short_link = model('promotion_short_link')->where(['game_id' => $game_id, 'channel_id' => $channel_id, 'type' => 1])->value('short_link');
- if (!empty($short_link)) {
- $share_url = T_DOMAIN . '/' . $short_link;
- } else {
- $share_url = T_DOMAIN . '/game_promotion/index/game_id/' . $game_id . '/channel_id/' . $channel_id . '/package_type/' . $sdkInfo['package_type'];
- }
- $app_link = '';
- if ($gameInfo['type'] == 2) {
- $app_link = APK_DOWN_DOMAIN_NEW . '/sygame/' . $gameInfo['pinyin'] . '/' . $filename;
- }
- return $this->jsonResult([
- 'tgy_link' => $share_url,
- 'app_link' => $app_link,
- ], 20002, 'success');
- }
- return $this->jsonResult('', 10015, $msg);
- }
- /**
- * 分包
- *
- * @param $game_id 游戏ID
- * @param $channel_id 渠道ID
- * @param $platform 游戏平台: 0=安卓, 1=IOS
- * @param $platform 分包方式: 0=异步(队列), 1=同步
- *
- * @return bool
- */
- private function package($game_id, $channel_id, $platform, $pack_type = 1)
- {
- $subChannel = new SubChannel;
- // $res = $subChannel->index($channel_id, $game_id, 'ydd_fb', $platform, 0);
- return $subChannel->index($channel_id, $game_id, 'ydd_fb', $platform, 0, $pack_type);
- }
- }
|