LiveGrad.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /**
  3. * 直播部门抓取数据整理
  4. */
  5. namespace app\guildapi\controller;
  6. use app\api\validate\Pay;
  7. use app\common\logic\SubPackage as SubChannel;
  8. use think\Db;
  9. use think\Env;
  10. use think\Exception;
  11. use think\Controller;
  12. class LiveGrad extends Guild
  13. {
  14. protected $_channelId; // 渠道ID
  15. protected $_level; // 层级:0(B+账号),1(B账号),2(B-账号),3(推广员)
  16. protected function _initialize()
  17. {
  18. parent::_initialize();
  19. $guildInfo = session('guild_info');
  20. // dump($guildInfo);
  21. $this->_channelId = $guildInfo['channel_id'];
  22. // $this->_level = $guildInfo['level'];
  23. log_message("input: ".json_encode(input()), 'log', LOG_PATH . 'live_grad_log/');
  24. }
  25. /**
  26. * 添加推广员
  27. *
  28. * @param $channel_id C级的渠道ID(用于判断是否有有上级,没有则新建)
  29. *
  30. * @return \think\response\Json|void
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public function userCreate()
  36. {
  37. $channel_id = $this->request->post('channel');
  38. $level = $this->request->post('level');
  39. $this->_level = $level;
  40. if ($channel_id != ""){
  41. $this->_channelId = $channel_id;
  42. }
  43. try {
  44. // 判断B级账户,没有则创建
  45. // if ($channel_id) {
  46. // $c_info = Db::name('nw_channel')->where(['id' => $channel_id, 'level' => 3])->find();
  47. // if (!$c_info){
  48. // throw new Exception("渠道号错误");
  49. // }
  50. // $c_info = Db::name('nw_channel')->where(['id' => $c_info['parent_id'], 'level' => 2])->find();
  51. // $parent_id = $c_info['id'];
  52. // $parent_name = $c_info['name'];
  53. // }else{
  54. // $b_res = $this->_judgeUserCreateB();
  55. // $parent_id = $b_res['data']['id'];
  56. // $parent_name = $b_res['data']['name'];
  57. // }
  58. if ($level != 3){
  59. $b_res = $this->_judgeUserCreateB();
  60. }else{
  61. $c_info = Db::name('nw_channel')->where(['id' => $channel_id])->find();
  62. $b_res = $this->_judgeUserCreateC($channel_id,$c_info['name']);
  63. }
  64. // 查询B级账户信息
  65. // $b_info = Db::name('nw_channel')->where(['id' => $parent_id, 'level' => 2])->find();
  66. // dump("## b_info: ", $b_info);
  67. // if($c_info['code'] != 20000){
  68. // return $this->jsonResult(['data' => '', 'code' => $c_info['code'], 'msg' => $c_info['msg']]);
  69. // }
  70. // 创建C级账户
  71. // $c_info = $this->_judgeUserCreateC($parent_id,$parent_name);
  72. // dump("## c_info: ", $c_info);
  73. if($b_res['code'] != 20000){
  74. // return $this->jsonResult(['data' => '', 'code' => $c_info['code'], 'msg' => $c_info['msg']]);
  75. throw new Exception($b_res['msg'], $b_res['code']);
  76. }
  77. } catch (\Exception $e) {
  78. // return ['data' => '', 'code' => 10015, 'msg' => "添加失败: " . $e->getMessage()];
  79. $this->jsonResult('', 10015, "添加失败: " . $e->getMessage()." - ".$e->getFile().":".$e->getLine().", errcode:".$e->getCode());
  80. }
  81. return $this->jsonResult($b_res['data'], 20000, 'success');
  82. }
  83. private function _judgeUserCreateB(){
  84. $channel = Db::name('nw_channel')->where(['id' => $this->_channelId])->find();
  85. if ($this->_level == 1){
  86. $id_path = ','.$this->_channelId.',';
  87. }else{
  88. $id_path = ',' . $channel['parent_id'] . ','.$this->_channelId.',';
  89. }
  90. // $parent_id = Db::name('nw_channel')->where(['id' => $this->_channelId])->value('parent_id');
  91. // 获取最新用户名及其标识
  92. $user_pwd = random(8);
  93. if ($this->_level == 1){
  94. $where = ['level' => $this->_level, 'name' => ['like', 'qmsy%']];
  95. // if(!empty($this->_channelId)){
  96. // $where['parent_id'] = $this->_channelId;
  97. // // $where['id_path'] = $id_path;
  98. // }
  99. // dump($where);
  100. $newChannelName = Db::name('nw_channel')->where($where)->order('name', 'desc')->value('name');
  101. $user_name = 'qmsy00001';
  102. // dump($newChannelName);
  103. if (strpos($newChannelName, 'qmsy') !== false) {
  104. $startChannel = explode('qmsy', $newChannelName);
  105. if (is_numeric($startChannel[1]) === true) {
  106. $channelNum = $startChannel[1] + 1;
  107. $user_name = 'qmsy' . str_pad($channelNum, 5, "0", STR_PAD_LEFT);
  108. }
  109. }
  110. }else{
  111. $user_name = $channel['name'].'t1';
  112. $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');
  113. if (strpos($newChannelName, $channel['name'].'t') !== false) {
  114. $startChannel = explode($channel['name'].'t', $newChannelName);
  115. if (is_numeric($startChannel[1]) === true) {
  116. $channelNum = $startChannel[1] + 1;
  117. $user_name = $channel['name'].'t'.$channelNum;
  118. }
  119. }
  120. }
  121. // dump($user_name);
  122. $channelModel = model('channel');
  123. $channelInfo = $channelModel->where(['name' => $user_name])->find();
  124. if ($channelInfo) {
  125. return ['data' => '', 'code' => 10021, 'msg' => '当前创建渠道已存在!'];
  126. }
  127. $userInfo = Db::name('nw_channel_admin')->field('id')->where(['username' => $user_name])->find();
  128. if ($userInfo) {
  129. return ['data' => '', 'code' => 10022, 'msg' => '当前渠道管理员已存在!'];
  130. }
  131. Db::startTrans();
  132. try {
  133. // 添加渠道信息
  134. $channelData = [
  135. 'name' => $user_name,
  136. // 'replacement_status' => $data['replacement_status'],//代充
  137. // 'share_show' => $data['share_show'],//分成比例
  138. 'parent_id' => $this->_channelId,
  139. 'id_path' => $id_path,
  140. 'create_time' => NOW_TIMESTAMP,
  141. 'level' => $this->_level,
  142. 'flag' => 3,
  143. 'status' => 1,
  144. 'gh_status' => 1,
  145. ];
  146. $channelId = $channelModel->insertGetId($channelData);
  147. if ( !$channelId ) {
  148. throw new Exception("添加渠道账号失败");
  149. }
  150. //添加渠道信息
  151. // $id = $channelModel->insertGetId([
  152. // 'name' => $user_name,
  153. // 'parent_id' => $this->_channelId,
  154. // 'id_path' => $id_path,
  155. // 'create_time' => NOW_TIMESTAMP,
  156. // 'level' => 1,
  157. // 'flag' => 3,
  158. // 'status' => 1
  159. // ]);
  160. // if (!$id) {
  161. // throw new Exception("添加渠道账号失败");
  162. // }
  163. $cont['username'] = $user_name;
  164. $cont['password'] = mg_password($user_pwd);
  165. $cont['status'] = 1;
  166. $cont['channel_id'] = $channelId;
  167. $cont['create_time'] = NOW_TIMESTAMP;
  168. $cont['password_update_time'] = NOW_TIMESTAMP;
  169. $b_id = Db::name('nw_channel_admin')->insertGetId($cont);
  170. if ($b_id === false) {
  171. throw new Exception("添加渠道账号失败");
  172. }
  173. Db::commit();
  174. } catch (\Exception $e) {
  175. Db::rollback();
  176. return ['data' => '', 'code' => 10020, 'msg' => $e->getMessage().', '.$e->getFile().':'.$e->getLine()];
  177. }
  178. return [
  179. 'data' => [
  180. 'channel' => $channelId,
  181. 'name' => $user_name,
  182. 'password'=>$user_pwd
  183. ],
  184. 'code' => 20000,
  185. 'msg' => "",
  186. ];
  187. }
  188. /**
  189. * 返回封装后的 API json数据到客户端
  190. * @access protected
  191. * @param mixed $data 要返回的数据
  192. * @param int $code 返回的 code
  193. * @param mixed $msg 提示信息
  194. * @param $isExit boolean 是否强制退出,默认true
  195. * @return void
  196. *
  197. */
  198. public function jsonResult($data, $code = 0, $msg = '', $isExit = true)
  199. {
  200. $result = [
  201. 'code' => $code,
  202. 'msg' => $msg,
  203. 'time' => request()->server('REQUEST_TIME'),
  204. 'data' => $data,
  205. ];
  206. log_message("result: ".json_encode($result), 'log', LOG_PATH . 'live_grad_log/');
  207. echo json_encode($result);
  208. //为了使用fastcgi_finish_request等函数时,后续的执行能够继续生效
  209. if ($isExit) exit;
  210. }
  211. /**
  212. * 创建C级账户
  213. *
  214. * @param $parent_id B级关联ID
  215. * @param $parent_name B级用户名
  216. *
  217. * @return array
  218. */
  219. private function _judgeUserCreateC($parent_id, $parent_name){
  220. $id_path = Db::name('nw_channel')->where(['id'=>$parent_id])->value('id_path').$parent_id.',';
  221. // 获取最新用户名及其标识
  222. $user_name = $parent_name.'-1';
  223. $user_pwd = random(8);
  224. $newChannelName = Db::name('nw_channel')->where(['parent_id' => $parent_id, 'level' => 3, 'name' => ['like', 'qmsy%']])->order('create_time', 'desc')->value('name');
  225. if (strpos($newChannelName, 'qmsy') !== false) {
  226. $startChannel = explode('-', $newChannelName);
  227. if (is_numeric($startChannel[1]) === true) {
  228. $channelNum = $startChannel[1] + 1;
  229. $user_name = $parent_name .'-'. $channelNum;
  230. }
  231. }
  232. //1. 添加渠道信息
  233. $channelModel = model('channel');
  234. $channelInfo = $channelModel->where(['name' => $user_name])->find();
  235. if ( $channelInfo ) {
  236. return ['data'=>'','code'=>10031,'msg'=> "该渠道信息已存在"];
  237. }
  238. $userInfo = Db::name('nw_channel_admin')->field('id')->where(['username' => $user_name])->find();
  239. if ($userInfo) {
  240. return ['data'=>'','code'=>10032,'msg'=> "用户名已存在"];
  241. }
  242. Db::startTrans();
  243. try {
  244. //添加渠道信息
  245. $id = $channelModel->insertGetId([
  246. 'name' => $user_name,
  247. 'parent_id' => $parent_id,
  248. 'id_path' => $id_path,
  249. 'create_time' => NOW_TIMESTAMP,
  250. 'level' => 3,
  251. 'flag' => 3,
  252. 'status' => 1,
  253. 'gh_status' => 1,
  254. ]);
  255. if ( !$id) {
  256. throw new Exception("添加渠道账号失败");
  257. }
  258. $cont['username'] = $user_name;
  259. $cont['password'] = mg_password($user_pwd);
  260. $cont['status'] = 1;
  261. $cont['channel_id'] = $id;
  262. $cont['create_time'] = NOW_TIMESTAMP;
  263. $cont['password_update_time'] = NOW_TIMESTAMP;
  264. $c_id = Db::name('nw_channel_admin')->insertGetId($cont);
  265. if ($c_id === false) {
  266. throw new Exception("添加渠道账号失败");
  267. }
  268. Db::commit();
  269. } catch (\Exception $e) {
  270. Db::rollback();
  271. return ['data'=>'','code'=>10030,'msg'=> $e->getMessage().', '.$e->getFile().':'.$e->getLine()];
  272. }
  273. return [
  274. 'data' => [
  275. 'channel' => $id,
  276. 'name' => $user_name,
  277. 'password' => $user_pwd,
  278. ],
  279. 'code' => 20000,
  280. 'msg' =>"",
  281. ];
  282. }
  283. // 分包
  284. public function gamePack()
  285. {
  286. $game_id = $this->request->post('yxid');
  287. $channel_id = $this->request->post('channel');
  288. // $status = $this->request->post('status');
  289. if (!$game_id) {
  290. return $this->jsonResult('', 10015, '非法操作:缺少游戏ID!');
  291. }
  292. if (!$channel_id) {
  293. return $this->jsonResult('', 10015, '非法操作:缺少渠道ID!');
  294. }
  295. $gameInfo = model('Game')->field(['id', 'channel_version', 'type', 'cooperation_status', 'channel_version', 'pinyin'])->where(['id' => $game_id])->find();
  296. $sdkInfo = model('SdkGameList')->field(['id', 'upload_status', 'channel_version', 'filename'])->where(['gameid' => $game_id, 'channel_id' => $channel_id])->find();
  297. // upload_status=文件状态:0=待处理、1=已处理、2=待审核、3=申请拒绝、4=分包中、5=打包失败、9=已禁用
  298. // 游戏不存在
  299. if (!$gameInfo) {
  300. return $this->jsonResult('', 10015, '当前游戏不存在!');
  301. }
  302. // if (!$sdkInfo) {
  303. // $res = $this->package($game_id, $channel_id, $gameInfo['type']);
  304. // if ($res['code'] == 0) {
  305. // return $this->jsonResult('', 20001, '分包中....');
  306. // }
  307. // return $this->jsonResult('', 10015, "打包失败:" . $res['msg']);
  308. // // return $this->jsonResult('', 10015, '当前游戏渠道不存在!');
  309. // }
  310. // 游戏关闭
  311. if ($gameInfo['cooperation_status'] != 1) {
  312. return $this->jsonResult('', 10015, '当前游戏未上线!');
  313. }
  314. // 渠道禁用
  315. if (!empty($sdkInfo) && $sdkInfo['upload_status'] == 9) {
  316. return $this->jsonResult('', 10017, '当前渠道已禁用!');
  317. }
  318. $pack_status = false;
  319. $msg = '打包失败';
  320. if ($sdkInfo['upload_status'] == 4 || $sdkInfo['upload_status'] == 1) {
  321. $pack_status = true;
  322. $msg = '打包成功';
  323. }
  324. $filename = $sdkInfo['filename'];
  325. if (empty($sdkInfo) || $gameInfo['channel_version'] != $sdkInfo['channel_version'] || $sdkInfo['upload_status'] == 5) {
  326. $result = $this->package($game_id, $channel_id, $gameInfo['type'], 1);
  327. if ($result['code'] == 1) {
  328. $filename = $result['data']['filename'];
  329. $pack_status = true;
  330. } else {
  331. $msg .= ': ' . $result['msg'];
  332. }
  333. }
  334. if ($pack_status) {
  335. $short_link = model('promotion_short_link')->where(['game_id' => $game_id, 'channel_id' => $channel_id, 'type' => 1])->value('short_link');
  336. if (!empty($short_link)) {
  337. $share_url = T_DOMAIN . '/' . $short_link;
  338. } else {
  339. $share_url = T_DOMAIN . '/game_promotion/index/game_id/' . $game_id . '/channel_id/' . $channel_id . '/package_type/' . $sdkInfo['package_type'];
  340. }
  341. $app_link = '';
  342. if ($gameInfo['type'] == 2) {
  343. $app_link = APK_DOWN_DOMAIN_NEW . '/sygame/' . $gameInfo['pinyin'] . '/' . $filename;
  344. }
  345. return $this->jsonResult([
  346. 'tgy_link' => $share_url,
  347. 'app_link' => $app_link,
  348. ], 20002, 'success');
  349. }
  350. return $this->jsonResult('', 10015, $msg);
  351. }
  352. /**
  353. * 分包
  354. *
  355. * @param $game_id 游戏ID
  356. * @param $channel_id 渠道ID
  357. * @param $platform 游戏平台: 0=安卓, 1=IOS
  358. * @param $platform 分包方式: 0=异步(队列), 1=同步
  359. *
  360. * @return bool
  361. */
  362. private function package($game_id, $channel_id, $platform, $pack_type = 1)
  363. {
  364. $subChannel = new SubChannel;
  365. // $res = $subChannel->index($channel_id, $game_id, 'ydd_fb', $platform, 0);
  366. return $subChannel->index($channel_id, $game_id, 'ydd_fb', $platform, 0, $pack_type);
  367. }
  368. }