Plantask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use app\common\library\Tlssigapiv2;
  5. use think\Db;
  6. class Plantask extends Controller
  7. {
  8. //关于本文件里的计划任务
  9. //只有 public 方法,auto_开头的才是计划任务,其他private 方法都是工具方法
  10. ////////////////////////////////////////下面都是计划任务方法///////////////////////////////////////////////////////////////
  11. //主动拉取im群组内 聊天记录
  12. public function auto_imgroup(){
  13. $im_config = config('tencent_im');
  14. $sdkappid = $im_config['sdkappid'];
  15. $sdkappkey = $im_config['key'];
  16. $identifier= $im_config['identifier'];
  17. $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
  18. $random = rand(10000000,99999999);
  19. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  20. $data = [
  21. 'ChatType' => 'Group',
  22. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  23. ];
  24. $tasklog = [
  25. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  26. 'datehour' => $data['MsgTime'],
  27. 'createtime' => time(),
  28. 'status' => 0,
  29. ];
  30. dump($data);
  31. $jsonStr = json_encode($data);
  32. $header = array(
  33. 'Content-Type: application/json; charset=utf-8',
  34. 'Content-Length: ' . strlen($jsonStr)
  35. );
  36. $rs = curl_post($url,$jsonStr,$header);
  37. $rs = json_decode($rs,true);
  38. dump($rs);
  39. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  40. $tasklog['status'] = 1;
  41. //正常的,可以下载了
  42. if(isset($rs['File']) && !empty($rs['File'])){
  43. foreach($rs['File'] as $key => $val){
  44. echo $val['URL'];
  45. //下载
  46. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  47. dump($gz_path);
  48. //解压
  49. $json_path = $this->jieyagz($gz_path);
  50. dump($json_path);
  51. //分析
  52. $content = $this->readjson_group($json_path);
  53. dump(count($content));
  54. //入库
  55. if(!empty($content)){
  56. Db::name('imlog_group')->insertAll($content);
  57. }
  58. }
  59. }
  60. }
  61. Db::name('imlog_tasklog')->insertGetId($tasklog);
  62. echo '结束';
  63. exit;
  64. }
  65. //主动拉取im用户私聊 聊天记录
  66. public function auto_imc2c(){
  67. $im_config = config('tencent_im');
  68. $sdkappid = $im_config['sdkappid'];
  69. $sdkappkey = $im_config['key'];
  70. $identifier= $im_config['identifier'];
  71. $usersig = $this->usersig($sdkappid,$sdkappkey,$identifier);
  72. $random = rand(10000000,99999999);
  73. $url = 'https://console.tim.qq.com/v4/open_msg_svc/get_history?sdkappid='.$sdkappid.'&identifier=administrator&usersig='.$usersig.'&random='.$random.'&contenttype=json';
  74. $data = [
  75. 'ChatType' => 'C2C',
  76. 'MsgTime' => date('YmdH',strtotime('-3 Hours')),
  77. ];
  78. $tasklog = [
  79. 'type' => $data['ChatType'] == 'C2C' ? 1 : 2,
  80. 'datehour' => $data['MsgTime'],
  81. 'createtime' => time(),
  82. 'status' => 0,
  83. ];
  84. dump($data);
  85. $jsonStr = json_encode($data);
  86. $header = array(
  87. 'Content-Type: application/json; charset=utf-8',
  88. 'Content-Length: ' . strlen($jsonStr)
  89. );
  90. $rs = curl_post($url,$jsonStr,$header);
  91. $rs = json_decode($rs,true);
  92. dump($rs);
  93. if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
  94. $tasklog['status'] = 1;
  95. //正常的,可以下载了
  96. if(isset($rs['File']) && !empty($rs['File'])){
  97. foreach($rs['File'] as $key => $val){
  98. //下载
  99. $gz_path = $this->downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
  100. dump($gz_path);
  101. //解压
  102. $json_path = $this->jieyagz($gz_path);
  103. dump($json_path);
  104. //分析
  105. $content = $this->readjson($json_path);
  106. dump(count($content));
  107. //入库
  108. if(!empty($content)){
  109. Db::name('imlog_c2c')->insertAll($content);
  110. }
  111. }
  112. }
  113. }
  114. Db::name('imlog_tasklog')->insertGetId($tasklog);
  115. echo '结束';
  116. exit;
  117. }
  118. //送礼物(api/party/giveGiftToYou)拆分出来的异步用户升级方法,
  119. public function auto_user_level_up(){
  120. Db::startTrans();
  121. $tasklist = Db::name('gift_user_party')->where('task_status',0)->limit(20)->lock(true)->select();
  122. if(empty($tasklist)){
  123. Db::rollback();
  124. echo 'empty';
  125. exit;
  126. }
  127. try {
  128. $money_to_jewel = config('site.money_to_jewel') ?: 10; //余额兑换钻石
  129. $getempirical_conf = config("site.getempirical");
  130. foreach($tasklist as $key => $giftuserparty){
  131. echo $giftuserparty['id'].'<br>';
  132. $giftValue = $giftuserparty['value'];
  133. $hotValue = $giftValue;
  134. //用户信息
  135. $authuser_info = Db::name('user')->field('nickname')->where('id',$giftuserparty['user_id'])->find();
  136. $touserInfo = Db::name('user')->field('nickname')->where('id',$giftuserparty['user_to_id'])->find();
  137. $party_id = $giftuserparty['party_id'];
  138. //添加获赠用户余额
  139. $user_id = $giftuserparty['user_to_id'];
  140. if($giftuserparty['getvalue'] > 0){
  141. $getMoney = bcdiv($giftuserparty['getvalue'],$money_to_jewel,2);
  142. if($getMoney > 0){
  143. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$authuser_info['nickname']}送你{$giftuserparty['gift_name']}x{$giftuserparty['number']}",101,'money');
  144. if($rs_wallet['status'] == false){
  145. $this->error($rs_wallet['msg']);
  146. Db::rollback();
  147. }
  148. }
  149. }
  150. $partyInfo = \app\common\model\Party::field("id,room_type,party_name,party_hot,user_id,platRate,guilderRate")->where(["id"=>$giftuserparty['party_id']])->find();
  151. //增加房主抽成
  152. if ($partyInfo && $giftuserparty['guildervalue'] > 0) {
  153. $guilderMoney = bcdiv($giftuserparty['guildervalue'],$money_to_jewel,2);
  154. if($guilderMoney > 0){
  155. $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$authuser_info['nickname']}送礼物{$giftuserparty['gift_name']}x{$giftuserparty['number']}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
  156. if($rs_wallet['status'] == false){
  157. $this->error($rs_wallet['msg']);
  158. Db::rollback();
  159. }
  160. }
  161. }
  162. // 如果是主播,则添加魅力值记录做榜单统计
  163. if($partyInfo['room_type'] == 2) {
  164. $data = [];
  165. $data["user_id"] = $giftuserparty['user_to_id'];
  166. $data["party_id"] = $party_id;
  167. $data["charm"] = $hotValue;
  168. $data["createtime"] = time();
  169. \app\common\model\UserCharmRank::insert($data);
  170. }
  171. //用户经验升级
  172. $getempirical = $getempirical_conf * $hotValue;
  173. // 获取用户贵族信息
  174. $noble = \app\common\model\User::getUserNoble($giftuserparty['user_id']);
  175. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  176. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  177. }
  178. // 增加用户经验值
  179. $res = \app\common\model\User::addEmpirical($giftuserparty['user_id'],$getempirical);
  180. /*if ($res){
  181. $this->auth->level = $res->level;
  182. }*/
  183. //增加被送礼物用户的魅力等级
  184. $res_charm = \app\common\model\User::add_charm_level($giftuserparty['user_to_id'],$giftValue);
  185. // +exp
  186. \app\common\model\TaskLog::tofinish($giftuserparty['user_id'],"OBHqCX4g",$giftuserparty['number']);
  187. // +message
  188. \app\common\model\Message::addMessage($giftuserparty['user_to_id'],"礼物通知","收到 ".$authuser_info['nickname']." 赠送的".$giftuserparty['gift_name']." x".$giftuserparty['number']." 价值 ".$giftValue ." 钻石");
  189. //增加送礼用户的财富等级
  190. $res_wealth = \app\common\model\User::add_wealth_level($giftuserparty['user_id'],$giftValue);
  191. $rs_up = Db::name('gift_user_party')->where('id',$giftuserparty['id'])->update(['task_status'=>1]);
  192. if($rs_up === false){
  193. Db::rollback();
  194. echo '更新错误'.$giftuserparty['id'];
  195. exit;
  196. }
  197. }
  198. Db::commit();
  199. } catch (ValidateException $e) {
  200. Db::rollback();
  201. $this->error($e->getMessage());
  202. } catch (PDOException $e) {
  203. Db::rollback();
  204. $this->error($e->getMessage());
  205. } catch (Exception $e) {
  206. Db::rollback();
  207. $this->error($e->getMessage());
  208. }
  209. }
  210. /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
  211. //下载远程文件 到指定目录
  212. private function downloadfile($file_url, $path = '', $save_file_name = '')
  213. {
  214. $basepath = '/uploaded/';
  215. if ($path) {
  216. $basepath = $basepath . $path . '/';
  217. }
  218. $basepath = $basepath . date('Ymd');
  219. $dir_path = ROOT_PATH . '/public' . $basepath;
  220. if (!is_dir($dir_path)) {
  221. mkdir($dir_path, 0777, true);
  222. }
  223. $file = file_get_contents($file_url);
  224. //传入保存文件的名称
  225. $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
  226. $resource = fopen($dir_path. '/'. $filename, 'w');
  227. fwrite($resource, $file);
  228. fclose($resource);
  229. return $dir_path . '/' . $filename;
  230. }
  231. //解压
  232. private function jieyagz($gz_path = ''){
  233. $json_path = substr($gz_path,0,-3);
  234. if ($zp = gzopen($gz_path, 'r')) { // 打开压缩文件
  235. if ($fp = fopen($json_path, 'w')) { // 打开目标文件
  236. while (!gzeof($zp)) {
  237. fwrite($fp, gzread($zp, 1024 * 512)); // 逐块读取和解压缩后写入
  238. }
  239. fclose($fp);
  240. }
  241. gzclose($zp);
  242. }
  243. return $json_path;
  244. }
  245. //读取json并分析,c2c
  246. private function readjson($json_path = ''){
  247. $newMsgList = [];
  248. $json_content = file_get_contents($json_path);
  249. $json_content = json_decode($json_content,true);
  250. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  251. $MsgList = $json_content['MsgList'];
  252. if(!empty($MsgList)){
  253. foreach($MsgList as $key => $val)
  254. {
  255. $newone = [
  256. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  257. 'CloudCustomData' => isset($val['CloudCustomData']) ? $val['CloudCustomData'] : '',
  258. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  259. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  260. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  261. 'MsgRandom' => isset($val['MsgRandom']) ? $val['MsgRandom'] : '',
  262. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  263. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  264. 'To_Account' => isset($val['To_Account']) ? intval($val['To_Account']) : 0,
  265. ];
  266. //解析数据类型
  267. if(isset($val['MsgBody'][0]['MsgType'])){
  268. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  269. //文本
  270. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  271. $newone['MsgInfo'] = '';
  272. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  273. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Text'];
  274. }
  275. }
  276. //图片
  277. if($val['MsgBody'][0]['MsgType'] == 'TIMImageElem'){
  278. $newone['MsgInfo'] = '';
  279. if(isset($val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'])){
  280. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['ImageInfoArray'][0]['URL'];
  281. }
  282. }
  283. //声音
  284. if($val['MsgBody'][0]['MsgType'] == 'TIMSoundElem'){
  285. $newone['MsgInfo'] = '';
  286. if(isset($val['MsgBody'][0]['MsgContent']['Url'])){
  287. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['Url'];
  288. }
  289. }
  290. //视频
  291. if($val['MsgBody'][0]['MsgType'] == 'TIMVideoFileElem'){
  292. $newone['MsgInfo'] = '';
  293. if(isset($val['MsgBody'][0]['MsgContent']['VideoUrl'])){
  294. $newone['MsgInfo'] = $val['MsgBody'][0]['MsgContent']['VideoUrl'];
  295. }
  296. }
  297. //其他
  298. continue;
  299. }else{
  300. continue;
  301. }
  302. $newMsgList[] = $newone;
  303. }
  304. }
  305. }
  306. return $newMsgList;
  307. }
  308. //读取json并分析,group
  309. private function readjson_group($json_path = ''){
  310. $newMsgList = [];
  311. $json_content = file_get_contents($json_path);
  312. $json_content = json_decode($json_content,true);
  313. if(!empty($json_content) && is_array($json_content) && isset($json_content['MsgList'])){
  314. $MsgList = $json_content['MsgList'];
  315. if(!empty($MsgList)){
  316. foreach($MsgList as $key => $val)
  317. {
  318. $newone = [
  319. //'key' => $key,辅助查找
  320. 'ClientIP' => isset($val['ClientIP']) ? $val['ClientIP'] : '',
  321. 'From_Account' => isset($val['From_Account']) ? intval($val['From_Account']) : 0,
  322. 'GroupId' => isset($val['GroupId']) ? intval($val['GroupId']) : 0,
  323. //'MsgBody' => isset($val['MsgBody']) ? json_encode($val['MsgBody']) : '',
  324. 'MsgFromPlatform' => isset($val['MsgFromPlatform']) ? $val['MsgFromPlatform'] : '',
  325. 'MsgSeq' => isset($val['MsgSeq']) ? $val['MsgSeq'] : '',
  326. 'MsgTimestamp' => isset($val['MsgTimestamp']) ? $val['MsgTimestamp'] : '',
  327. ];
  328. //解析数据类型
  329. if(isset($val['MsgBody'][0]['MsgType'])){
  330. $newone['MsgType'] = $val['MsgBody'][0]['MsgType'];
  331. //文本
  332. if($val['MsgBody'][0]['MsgType'] == 'TIMTextElem'){
  333. $newone['MsgInfo'] = '';
  334. if(isset($val['MsgBody'][0]['MsgContent']['Text'])){
  335. //继续解析
  336. $text = json_decode($val['MsgBody'][0]['MsgContent']['Text'],true);
  337. if(isset($text['type']) && $text['type'] == 1){
  338. //TYPE_NORMAL =1;//普通消息
  339. $newone['MsgInfo'] = isset($text['content']) ? $text['content'] : '';
  340. $newMsgList[] = $newone;
  341. }
  342. //其他$text['type']的值都是房间内的,礼物的,表情等,不需要记录
  343. }
  344. }
  345. //房间内的不需要显示的内容
  346. if($val['MsgBody'][0]['MsgType'] == 'TIMCustomElem'){
  347. }
  348. }
  349. //大循环结束
  350. }
  351. }
  352. }
  353. return $newMsgList;
  354. }
  355. //请求im的签名
  356. private function usersig($sdkappid,$key,$identifier){
  357. $api = new TLSSigAPIv2($sdkappid,$key );
  358. $sig = $api->genUserSig($identifier);
  359. return $sig;
  360. }
  361. }