MainController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use cmf\controller\AdminBaseController;
  13. use think\facade\Db;
  14. use app\admin\model\Menu;
  15. class MainController extends AdminBaseController{
  16. /**
  17. * 后台欢迎页
  18. */
  19. public function index(){
  20. $config=getConfigPub();
  21. $this->assign("config",$config);
  22. $nowtime=time();
  23. //当天0点
  24. $today=date("Y-m-d",$nowtime);
  25. $today_start=strtotime($today);
  26. //当天 23:59:59
  27. $today_end=strtotime("{$today} + 1 day");
  28. /* 总注册数 */
  29. $users_total=Db::name("user")->where("user_type=2")->count();
  30. $this->assign("users_total",number_format($users_total));
  31. /* 基础数据 */
  32. $congifpri=getConfigPri();
  33. if($congifpri['um_appkey_android']){
  34. $appkey=$congifpri['um_appkey_android'];
  35. $basic_today_android=$this->getDailyData($appkey,$today);
  36. }else{
  37. $basic_today_android=[
  38. 'newUsers'=>0,
  39. 'totalUsers'=>0,
  40. 'activityUsers'=>0,
  41. 'launches'=>0,
  42. ];
  43. }
  44. if($congifpri['um_appkey_ios']){
  45. $appkey=$congifpri['um_appkey_ios'];
  46. $basic_today_ios=$this->getDailyData($appkey,$today);
  47. }else{
  48. $basic_today_ios=[
  49. 'newUsers'=>0,
  50. 'totalUsers'=>0,
  51. 'activityUsers'=>0,
  52. 'launches'=>0,
  53. ];
  54. }
  55. $basic_today['newUsers']=number_format($basic_today_android['newUsers']+$basic_today_ios['newUsers']);
  56. $basic_today['totalUsers']=number_format($basic_today_android['totalUsers']+$basic_today_ios['totalUsers']);
  57. $basic_today['activityUsers']=number_format($basic_today_android['activityUsers']+$basic_today_ios['activityUsers']);
  58. $basic_today['launches']=number_format($basic_today_android['launches']+$basic_today_ios['launches']);
  59. $data_basic=$this->getBasic($today_start,$today_end,1);
  60. $this->assign("basic_today",$basic_today);
  61. $this->assign("data_basicj",json_encode($data_basic));
  62. //设备终端
  63. $android_nums=Db::name("user")
  64. ->where("user_type=2 and source='android'")
  65. ->count();
  66. $ios_nums=Db::name("user")
  67. ->where("user_type=2 and source='ios'")
  68. ->count();
  69. $data_source=[
  70. 'name'=>[],
  71. 'v_n'=>[],
  72. ];
  73. $data_source['v_n'][]=['value'=>$android_nums,'name'=>'安卓','itemStyle'=>['color'=>'#4da2ff']];
  74. $data_source['name'][]='安卓';
  75. $data_source['v_n'][]=['value'=>$ios_nums,'name'=>'IOS','itemStyle'=>['color'=>'#fecc2d']];
  76. $data_source['name'][]='IOS';
  77. $this->assign("data_sourcej",json_encode($data_source));
  78. /* 注册渠道 */
  79. $qq_nums=Db::name("user")
  80. ->where("user_type=2 and login_type='qq'")
  81. ->count();
  82. $wx_nums=Db::name("user")
  83. ->where("user_type=2 and login_type='wx'")
  84. ->count();
  85. $phone_nums=Db::name("user")
  86. ->where("user_type=2 and login_type='phone'")
  87. ->count();
  88. $other_nums=Db::name("user")
  89. ->where("user_type=2 and login_type!='qq' and login_type!='wx' and login_type!='phone'")
  90. ->count();
  91. $login_nums_totoal=$qq_nums+$wx_nums+$phone_nums+$other_nums;
  92. $data_type=[
  93. 'name'=>[],
  94. 'nums'=>[],
  95. 'nums_per'=>[],
  96. 'color'=>[],
  97. ];
  98. $data_type['name'][]='QQ';
  99. $data_type['nums'][]=$qq_nums;
  100. $data_type['color'][]='#ff7a8b';
  101. $data_type['nums_per'][]=$login_nums_totoal!=0? round($qq_nums*100/$login_nums_totoal) : '0';
  102. $data_type['name'][]='微信';
  103. $data_type['nums'][]=$wx_nums;
  104. $data_type['color'][]='#fecc2d';
  105. $data_type['nums_per'][]=$login_nums_totoal!=0? round($wx_nums*100/$login_nums_totoal) : '0';
  106. $data_type['name'][]='手机';
  107. $data_type['nums'][]=$phone_nums;
  108. $data_type['color'][]='#4da2ff';
  109. $data_type['nums_per'][]=$login_nums_totoal!=0? round($phone_nums*100/$login_nums_totoal) : '0';
  110. $data_type['name'][]='其他';
  111. $data_type['nums'][]=$other_nums;
  112. $data_type['color'][]='#83d688';
  113. $data_type['nums_per'][]=$login_nums_totoal!=0? round($other_nums*100/$login_nums_totoal) : '0';
  114. $this->assign("data_typej",json_encode($data_type));
  115. /* 七天数据 */
  116. $time_7=$today_start-60*60*24*7;
  117. $data_week=array(
  118. 'date'=>[],
  119. 'value'=>[],
  120. 'fans'=>[],
  121. 'likes'=>[],
  122. );
  123. for($i=$time_7;$i<$today_start;){
  124. $end=$i+60*60*24;
  125. $videonums=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=0 and addtime > {$i} and addtime <= {$end}")->count();
  126. $likes=Db::name('user_video_like')->where("addtime > {$i} and addtime <= {$end}")->count();
  127. $fans=Db::name('user_attention')->where("addtime > {$i} and addtime <= {$end}")->count();
  128. $data_week['date'][]=date("Y-m-d",$i);
  129. $data_week['value'][]=$videonums;
  130. $data_week['fans'][]=$fans;
  131. $data_week['likes'][]=$likes;
  132. $i=$end;
  133. }
  134. $this->assign("data_weekj",json_encode($data_week));
  135. /* 广告数量 */
  136. $data_ad=array(
  137. 'date'=>[],
  138. 'value'=>[],
  139. 'videoviews'=>[],
  140. );
  141. $data_ad['date'][]='00';
  142. $data_ad['value'][]='0';
  143. $data_ad['videoviews'][]='0';
  144. for($i2=$today_start;$i2<$today_end;){
  145. $end=$i2+60*60;
  146. $videonums=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=1 and addtime > {$i2} and addtime <= {$end}")->count();
  147. $videoviews=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=1 and addtime > {$i2} and addtime <= {$end}")->sum('views');
  148. if(!$videoviews){
  149. $videoviews=0;
  150. }
  151. if($end==$today_end){
  152. $data_ad['date'][]='24';
  153. }else{
  154. $data_ad['date'][]=date("H",$end);
  155. }
  156. $data_ad['value'][]=$videonums;
  157. $data_ad['videoviews'][]=$videoviews;
  158. $i2=$end;
  159. }
  160. $this->assign("data_adj",json_encode($data_ad));
  161. /* 平台数据 */
  162. $data_plat=array(
  163. 'fans'=>0,
  164. 'commnets'=>0,
  165. 'release'=>0,
  166. 'likes'=>0,
  167. 'shares'=>0,
  168. 'attents'=>0,
  169. 'video_total'=>0,
  170. 'video_add'=>0,
  171. 'commnets_30'=>0,
  172. );
  173. /* 30天前的时间 */
  174. $time_30=$today_start-60*60*24*29;
  175. /* 粉丝数 */
  176. $fans=Db::name('user_attention')->count();
  177. $data_plat['fans']=NumberFormat($fans);
  178. /* 评论数 */
  179. $commnets=Db::name('user_video_comments')->count();
  180. $data_plat['commnets']=NumberFormat($commnets);
  181. /* 近30天发布视频 */
  182. $release=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=0 and addtime > {$time_30}")->count();
  183. $data_plat['release']=NumberFormat($release);
  184. /* 近30天点赞数 */
  185. $likes=Db::name('user_video_like')->where("addtime > {$time_30}")->count();
  186. $data_plat['likes']=NumberFormat($likes);
  187. /* 近30天分享数 */
  188. //$likes=Db::name('user_video_like')->where("addtime > {$time_30}")->count();
  189. /* 近30天关注增量 */
  190. $attents=Db::name('user_attention')->where("addtime > {$time_30}")->count();
  191. $data_plat['attents']=NumberFormat($attents);
  192. /* 视频总数 */
  193. $video_total=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=0")->count();
  194. $data_plat['video_total']=NumberFormat($video_total);
  195. /* 近30天留言 */
  196. $commnets_30=Db::name('user_video_comments')->where("addtime > {$time_30}")->count();
  197. $data_plat['commnets_30']=NumberFormat($commnets_30);
  198. $this->assign("data_plat",$data_plat);
  199. $stayinfo=[];
  200. //店铺待审核数量
  201. $stayinfo['shopapply_count'] = Db::name('shop_apply')->where("status=0")->count();
  202. //视频待审核数量
  203. $stayinfo['video_count'] = Db::name('user_video')->where("isdel=0 and status=0")->count();
  204. //视频举报数量
  205. $stayinfo['videorepot_count'] = Db::name('user_video_report')->where("status=0")->count();
  206. //直播间举报数量
  207. $stayinfo['livereport_count'] = Db::name('user_live_report')->where("status=0")->count();
  208. //用户认证待审核数量
  209. $stayinfo['auth_count'] = Db::name('user_auth')->where("status=0")->count();
  210. $this->assign("stayinfo",$stayinfo);
  211. return $this->fetch();
  212. }
  213. public function getdata(){
  214. $data = $this->request->param();
  215. $action=$data['action'];
  216. $start_time=$data['start_time'];
  217. $end_time=$data['end_time'];
  218. $nowtime=time();
  219. //当天0点
  220. $today=date("Y-m-d",$nowtime);
  221. $today_start=strtotime($today);
  222. //当天 23:59:59
  223. $today_end=strtotime("{$today} + 1 day");
  224. $start=$today_start;
  225. $end=$today_end;
  226. if($start_time){
  227. $start=strtotime($start_time);
  228. }
  229. if($end_time){
  230. $end=strtotime($end_time) + 60*60*24;
  231. }
  232. switch($action){
  233. case '1':
  234. $info=$this->getBasic($start,$end);
  235. break;
  236. case '2':
  237. $info=$this->getUsers($start,$end);
  238. break;
  239. case '3':
  240. $info=$this->getAds($start,$end);
  241. break;
  242. }
  243. $this->success($info);
  244. }
  245. /* 基础数据 */
  246. public function getBasic($starttime,$endtime){
  247. $rs=[
  248. 'name'=>[],
  249. ];
  250. $start=date("Y-m-d",$starttime);
  251. $end=date("Y-m-d",($endtime - 60*60*24));
  252. $congifpri=getConfigPri();
  253. $periodType='daily';
  254. for($i=$starttime;$i<$endtime;$i+=60*60*24){
  255. $rs['name'][]=date("Y-m-d",$i);
  256. }
  257. if($congifpri['um_appkey_android']){
  258. $appkey=$congifpri['um_appkey_android'];
  259. $newusers_android=$this->getNewUsers($appkey,$start,$end,$periodType);
  260. $launches_android=$this->getLaunches($appkey,$start,$end,$periodType);
  261. $durations_android=$this->getDurations($appkey,$start,$end,$periodType);
  262. $activeusers_android=$this->getActiveUsers($appkey,$start,$end,$periodType);
  263. //$retentions_android=$this->getRetentions($appkey,$start,$end,$periodType);
  264. }
  265. if($congifpri['um_appkey_ios']){
  266. $appkey=$congifpri['um_appkey_ios'];
  267. $newusers_ios=$this->getNewUsers($appkey,$start,$end,$periodType);
  268. $launches_ios=$this->getLaunches($appkey,$start,$end,$periodType);
  269. $durations_ios=$this->getDurations($appkey,$start,$end,$periodType);
  270. $activeusers_ios=$this->getActiveUsers($appkey,$start,$end,$periodType);
  271. //$retentions_ios=$this->getRetentions($appkey,$start,$end,$periodType);
  272. }
  273. $newusers=0;
  274. $launches=0;
  275. $durations=0;
  276. $activeusers=0;
  277. /* value */
  278. foreach($rs['name'] as $k=>$v){
  279. $newusers_a=isset($newusers_android[$k]['value'])?$newusers_android[$k]['value']:0;
  280. $newusers_i=isset($newusers_ios[$k]['value'])?$newusers_ios[$k]['value']:0;
  281. if($newusers_a||$newusers_i){
  282. $newusers+=$newusers_a + $newusers_i;
  283. }
  284. }
  285. foreach($rs['name'] as $k=>$v){
  286. $launches_a=isset($launches_android[$k]['value'])?$launches_android[$k]['value']:0;
  287. $launches_i=isset($launches_ios[$k]['value'])?$launches_ios[$k]['value']:0;
  288. if($launches_a||$launches_i){
  289. $launches+=$launches_a + $launches_i;
  290. }
  291. }
  292. foreach($rs['name'] as $k=>$v){
  293. $duration_a=isset($durations_android[$k])?$durations_android[$k]:0;
  294. $duration_i=isset($durations_ios[$k])?$durations_ios[$k]:0;
  295. if($duration_a||$duration_i){
  296. $durations+= floor( ($duration_a + $duration_i)/60);
  297. }
  298. }
  299. foreach($rs['name'] as $k=>$v){
  300. $activeusers_a=isset($activeusers_android[$k]['value'])?$activeusers_android[$k]['value']:0;
  301. $activeusers_i=isset($activeusers_ios[$k]['value'])?$activeusers_ios[$k]['value']:0;
  302. if($activeusers_a||$activeusers_i){
  303. $activeusers+=$activeusers_a + $activeusers_i;
  304. }
  305. }
  306. $data=[
  307. 'newusers'=>$newusers,
  308. 'launches'=>$launches,
  309. 'durations'=>$durations,
  310. 'activeusers'=>$activeusers,
  311. ];
  312. return $data;
  313. }
  314. /* 获取某天总数 */
  315. public function getDailyData($appkey,$start){
  316. $res=[
  317. 'newUsers'=>0,
  318. 'totalUsers'=>0,
  319. 'activityUsers'=>0,
  320. 'launches'=>0,
  321. ];
  322. $data=[
  323. 'appkey'=>$appkey,
  324. 'date'=>$start,
  325. ];
  326. $urlPath='param2/1/com.umeng.uapp/umeng.uapp.getDailyData/';
  327. $rs=$this->getUmengData($urlPath,$data);
  328. return isset($rs['dailyData'])?$rs['dailyData']:$res;
  329. }
  330. /* 获取App新增用户数 */
  331. public function getNewUsers($appkey,$start,$end,$periodType){
  332. $data=[
  333. 'appkey'=>$appkey,
  334. 'startDate'=>$start,
  335. 'endDate'=>$end,
  336. 'periodType'=>$periodType,
  337. ];
  338. $urlPath='param2/1/com.umeng.uapp/umeng.uapp.getNewUsers/';
  339. $rs=$this->getUmengData($urlPath,$data);
  340. return isset($rs['newUserInfo'])?$rs['newUserInfo']:[];
  341. }
  342. /* 获取App启动次数 */
  343. public function getLaunches($appkey,$start,$end,$periodType){
  344. $data=[
  345. 'appkey'=>$appkey,
  346. 'startDate'=>$start,
  347. 'endDate'=>$end,
  348. 'periodType'=>$periodType,
  349. ];
  350. $urlPath='param2/1/com.umeng.uapp/umeng.uapp.getLaunches/';
  351. $rs=$this->getUmengData($urlPath,$data);
  352. return isset($rs['launchInfo'])?$rs['launchInfo']:[];
  353. }
  354. /* 获取App使用时长 */
  355. public function getDurations($appkey,$start,$end,$periodType){
  356. $urlPath='param2/1/com.umeng.uapp/umeng.uapp.getDurations/';
  357. $info=[];
  358. $start_time=strtotime($start);
  359. $end_time=strtotime($end);
  360. for($i=$start_time;$i<=$end_time;$i+=60*60*24){
  361. $date=date("Y-m-d",$i);
  362. $data=[
  363. 'appkey'=>$appkey,
  364. 'date'=>$date,
  365. 'statType'=>'daily',
  366. ];
  367. $rs=$this->getUmengData($urlPath,$data);
  368. $info[]=isset($rs['average'])?$rs['average']:[];
  369. }
  370. return $info;
  371. }
  372. /* 活跃用户数 */
  373. public function getActiveUsers($appkey,$start,$end,$periodType){
  374. $data=[
  375. 'appkey'=>$appkey,
  376. 'startDate'=>$start,
  377. 'endDate'=>$end,
  378. 'periodType'=>$periodType,
  379. ];
  380. $urlPath='param2/1/com.umeng.uapp/umeng.uapp.getActiveUsers/';
  381. $rs=$this->getUmengData($urlPath,$data);
  382. return isset($rs['activeUserInfo'])?$rs['activeUserInfo']:[];
  383. }
  384. /* 留存用户数 */
  385. public function getRetentions($appkey,$start,$end,$periodType){
  386. $data=[
  387. 'appkey'=>$appkey,
  388. 'startDate'=>$start,
  389. 'endDate'=>$end,
  390. 'periodType'=>$periodType,
  391. ];
  392. $urlPath='param2/1/com.umeng.uapp/umeng.uapp.getRetentions/';
  393. $rs=$this->getUmengData($urlPath,$data);
  394. return isset($rs['retentionInfo'])?$rs['retentionInfo']:[];
  395. }
  396. public function getUmengData($urlPath,$data){
  397. $congifpri=getConfigPri();
  398. $url='https://gateway.open.umeng.com/openapi/';
  399. $appkey=$congifpri['um_apikey'];
  400. $apiSecurity=$congifpri['um_apisecurity'];
  401. $urlPath.=$appkey;
  402. ksort($data);
  403. $param='';
  404. foreach($data as $k=>$v){
  405. $param.=$k.$v;
  406. }
  407. $s=$urlPath.$param;
  408. $Signature=strtoupper ( bin2hex ( hash_hmac("sha1", $s, $apiSecurity, true) ) );
  409. $url.=$urlPath;
  410. $query=http_build_query($data);
  411. $query.='&_aop_signature='.$Signature;
  412. $rs=$this->Post($query,$url);
  413. return json_decode($rs,true);
  414. }
  415. public function Post($curlPost,$url){
  416. $curl = curl_init();
  417. curl_setopt($curl, CURLOPT_URL, $url);
  418. curl_setopt($curl, CURLOPT_HEADER, false);
  419. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  420. curl_setopt($curl, CURLOPT_NOBODY, true);
  421. curl_setopt($curl, CURLOPT_POST, true);
  422. curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
  423. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  424. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名
  425. $return_str = curl_exec($curl);
  426. curl_close($curl);
  427. return $return_str;
  428. }
  429. /* 广告数据 */
  430. public function getAds($start,$end){
  431. $data_ad=array(
  432. 'date'=>[],
  433. 'value'=>[],
  434. 'videoviews'=>[],
  435. );
  436. $start_time=$start;
  437. $end_time=$end;
  438. if($end_time - $start_time == 60*60*24){
  439. $data_ad['date'][]='00';
  440. $data_ad['value'][]='0';
  441. $data_ad['videoviews'][]='0';
  442. for($i2=$start_time;$i2<$end_time;){
  443. $end=$i2+60*60;
  444. $videonums=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=1 and addtime > {$i2} and addtime <= {$end}")->count();
  445. $videoviews=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=1 and addtime > {$i2} and addtime <= {$end}")->sum('views');
  446. if(!$videoviews){
  447. $videoviews=0;
  448. }
  449. if($end==$end_time){
  450. $data_ad['date'][]='24';
  451. }else{
  452. $data_ad['date'][]=date("H",$end);
  453. }
  454. $data_ad['value'][]=$videonums;
  455. $data_ad['videoviews'][]=$videoviews;
  456. $i2=$end;
  457. }
  458. }else{
  459. for($i2=$start_time;$i2<$end_time;){
  460. $end=$i2+60*60*24;
  461. $videonums=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=1 and addtime > {$i2} and addtime <= {$end}")->count();
  462. $videoviews=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=1 and addtime > {$i2} and addtime <= {$end}")->sum('views');
  463. if(!$videoviews){
  464. $videoviews=0;
  465. }
  466. $data_ad['date'][]=date("Y-m-d",$i2);
  467. $data_ad['value'][]=$videonums;
  468. $data_ad['videoviews'][]=$videoviews;
  469. $i2=$end;
  470. }
  471. }
  472. return $data_ad;
  473. }
  474. /* 导出 */
  475. public function export(){
  476. $data = $this->request->param();
  477. $action=isset($data['action']) ? $data['action']: '';
  478. $start_time=isset($data['start_time']) ? $data['start_time']: '';
  479. $end_time=isset($data['end_time']) ? $data['end_time']: '';
  480. $nowtime=time();
  481. //当天0点
  482. $today=date("Y-m-d",$nowtime);
  483. $today_start=strtotime($today);
  484. //当天 23:59:59
  485. $today_end=strtotime("{$today} + 1 day");
  486. $start=$today_start;
  487. $end=$today_end;
  488. if($start_time){
  489. $start=strtotime($start_time);
  490. }
  491. if($end_time){
  492. $end=strtotime($end_time) + 60*60*24;
  493. }
  494. $xlsData=[];
  495. switch($action){
  496. case '1':
  497. $result=$this->getBasic($start,$end);
  498. $xlsName = "基本指标导出";
  499. $cellName = array('A','B','C','D','E');
  500. $xlsCell = array('newusers','launches','duration','activityUsers','users_total');
  501. $xlsData[]=[
  502. 'newusers'=>'基本指标',
  503. 'launches'=>'',
  504. 'duration'=>'',
  505. 'activityUsers'=>'',
  506. 'users_total'=>'',
  507. 'ismerge'=>'1',
  508. ];
  509. $date=date("Y-m-d",$start).'至'.date("Y-m-d",($end-1));
  510. if($end - $start == 60*60*24){
  511. $date=date("Y-m-d",$start);
  512. }
  513. $xlsData[]=[
  514. 'newusers'=>$date,
  515. 'launches'=>'',
  516. 'duration'=>'',
  517. 'activityUsers'=>'',
  518. 'users_total'=>'',
  519. 'ismerge'=>'1',
  520. ];
  521. $xlsData[]=[
  522. 'newusers'=>'新用户数(位)',
  523. 'launches'=>'启动次数(次)',
  524. 'duration'=>'使用时长( 分钟)',
  525. 'activityUsers'=>'活跃用户(位)',
  526. 'users_total'=>'总注册量',
  527. 'ismerge'=>'0',
  528. ];
  529. $users_total=Db::name("user")->where("user_type=2")->count();
  530. $xlsData[]=[
  531. 'newusers'=>$result['newusers'],
  532. 'launches'=>$result['launches'],
  533. 'duration'=>$result['durations'],
  534. 'activityUsers'=>$result['activeusers'],
  535. 'users_total'=>$users_total,
  536. 'ismerge'=>'0',
  537. ];
  538. break;
  539. case '2':
  540. /* 七天数据 */
  541. $time_7=$today_start-60*60*24*7;
  542. $data_week=array(
  543. 'date'=>[],
  544. 'value'=>[],
  545. 'fans'=>[],
  546. 'likes'=>[],
  547. );
  548. for($i=$time_7;$i<$today_start;){
  549. $end=$i+60*60*24;
  550. $videonums=Db::name('user_video')->where("isdel=0 and status=1 and is_ad=0 and addtime > {$i} and addtime <= {$end}")->count();
  551. $likes=Db::name('user_video_like')->where("addtime > {$i} and addtime <= {$end}")->count();
  552. $fans=Db::name('user_attention')->where("addtime > {$i} and addtime <= {$end}")->count();
  553. $data_week['date'][]=date("Y-m-d",$i);
  554. $data_week['value'][]=$videonums;
  555. $data_week['fans'][]=$fans;
  556. $data_week['likes'][]=$likes;
  557. $i=$end;
  558. }
  559. $xlsName = "七天数据";
  560. $cellName = array('A','B','C','D');
  561. $xlsCell = array('date','value','fans','likes');
  562. $xlsData[]=[
  563. 'date'=>'七天数据',
  564. 'value'=>'',
  565. 'fans'=>'',
  566. 'likes'=>'',
  567. 'ismerge'=>'1',
  568. ];
  569. $date=date("Y-m-d",$time_7).'至'.date("Y-m-d",($today_start-1));
  570. $xlsData[]=[
  571. 'date'=>$date,
  572. 'value'=>'',
  573. 'fans'=>'',
  574. 'likes'=>'',
  575. 'ismerge'=>'1',
  576. ];
  577. $xlsData[]=[
  578. 'date'=>'时间',
  579. 'value'=>'视频数量',
  580. 'fans'=>'粉丝数',
  581. 'likes'=>'点赞数',
  582. 'ismerge'=>'0',
  583. ];
  584. foreach($data_week['date'] as $k=>$v){
  585. $info=[];
  586. $info['date']=$v;
  587. $info['value']=$data_week['value'][$k];
  588. $info['fans']=$data_week['fans'][$k];
  589. $info['likes']=$data_week['likes'][$k];
  590. $info['ismerge']='0';
  591. $xlsData[]=$info;
  592. }
  593. break;
  594. case '3':
  595. $result=$this->getAds($start,$end);
  596. $xlsName = "广告数据";
  597. $cellName = array('A','B','C');
  598. $xlsCell = array('date','value','videoviews');
  599. $xlsData[]=[
  600. 'date'=>'广告数据',
  601. 'value'=>'',
  602. 'videoviews'=>'',
  603. 'ismerge'=>'1',
  604. ];
  605. $date=date("Y-m-d",$start).'至'.date("Y-m-d",($end-1));
  606. if($end - $start == 60*60*24){
  607. $date=date("Y-m-d",$start);
  608. }
  609. $xlsData[]=[
  610. 'date'=>$date,
  611. 'value'=>'',
  612. 'videoviews'=>'',
  613. 'ismerge'=>'1',
  614. ];
  615. $xlsData[]=[
  616. 'date'=>'时间',
  617. 'value'=>'广告数量',
  618. 'videoviews'=>'浏览数量',
  619. 'ismerge'=>'0',
  620. ];
  621. foreach($result['date'] as $k=>$v){
  622. $info=[];
  623. $info['date']=$v;
  624. $info['value']=$result['value'][$k];
  625. $info['videoviews']=$result['videoviews'][$k];
  626. $info['ismerge']='0';
  627. $xlsData[]=$info;
  628. }
  629. break;
  630. }
  631. $this->exportExcel($xlsName,$xlsCell,$xlsData,$cellName);
  632. }
  633. /**导出Excel 表格【暂不使用】
  634. * @param $expTitle 名称
  635. * @param $expCellName 参数
  636. * @param $expTableData 内容
  637. * @throws \PHPExcel_Exception
  638. * @throws \PHPExcel_Reader_Exception
  639. */
  640. public function exportExcelBF($xlsName,$expCellName,$expTableData,$cellName){
  641. $xlsTitle = iconv('utf-8', 'gb2312', $xlsName);//文件名称
  642. $fileName = $xlsTitle.'_'.date('YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
  643. $cellNum = count($expCellName);
  644. $dataNum = count($expTableData);
  645. require_once CMF_ROOT.'sdk/PHPExcel/PHPExcel.php';
  646. $objPHPExcel = new \PHPExcel();
  647. $objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(20);
  648. for($i=0;$i<$dataNum;$i++){
  649. $cellinfo=$expTableData[$i];
  650. if($cellinfo['ismerge']==1){
  651. $objPHPExcel->getActiveSheet()->mergeCells('A'.($i+1).':'.end($cellName).($i+1));//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错)
  652. $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+1), $cellinfo[$expCellName[0]]);
  653. }else{
  654. for($j=0;$j<$cellNum;$j++){
  655. $key=$expCellName[$j];
  656. $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+1), $cellinfo[$key]);
  657. }
  658. }
  659. }
  660. header('pragma:public');
  661. header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
  662. header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
  663. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//Excel5为xls格式,excel2007为xlsx格式
  664. $objWriter->save('php://output');
  665. }
  666. public function dashboardWidget()
  667. {
  668. $dashboardWidgets = [];
  669. $widgets = $this->request->param('widgets/a');
  670. if (!empty($widgets)) {
  671. foreach ($widgets as $widget) {
  672. if ($widget['is_system']) {
  673. array_push($dashboardWidgets, ['name' => $widget['name'], 'is_system' => 1]);
  674. } else {
  675. array_push($dashboardWidgets, ['name' => $widget['name'], 'is_system' => 0]);
  676. }
  677. }
  678. }
  679. cmf_set_option('admin_dashboard_widgets', $dashboardWidgets, true);
  680. $this->success('更新成功!');
  681. }
  682. /**
  683. * 导出Excel
  684. * @param string $fileName 文件名称
  685. * @param array $headArr Excel标题头数组
  686. * @param array $data 数据内容
  687. * @param array $cellName Excel标题字母
  688. * @param string $suffix 文件后缀,xlsx 和 xls
  689. * @return bool
  690. */
  691. protected function exportExcel($fileName = "myData", $headArr = [], $data = [],$cellName = [], $suffix = 'xls'){
  692. @ini_set('memory_limit', '2048M');
  693. @set_time_limit(0);
  694. if (!$headArr || !$data || !is_array($data)) {
  695. return false;
  696. }
  697. require_once( CMF_ROOT."sdk/phpoffice/autoload.php");
  698. $fileName .= "_" . date("YmdHis");// 文件名称连接上相应的时间戳
  699. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  700. $cellNum = count($headArr);
  701. $dataNum = count($data);
  702. for($i=0;$i<$dataNum;$i++){
  703. $cellinfo=$data[$i];
  704. if($cellinfo['ismerge']==1){
  705. $spreadsheet->getActiveSheet()->mergeCells('A'.($i+1).':'.end($cellName).($i+1));//合并单元格(如果要拆分单元格是需要先合并再拆分的,否则程序会报错)
  706. $spreadsheet->getActiveSheet(0)->setCellValue('A'.($i+1), $cellinfo[$headArr[0]]);
  707. }else{
  708. for($j=0;$j<$cellNum;$j++){
  709. $key=$headArr[$j];
  710. $spreadsheet->getActiveSheet(0)->setCellValue($cellName[$j].($i+1), $cellinfo[$key]);
  711. }
  712. }
  713. }
  714. // 重命名表(UTF8编码不需要这一步)
  715. $fileName = iconv("utf-8", "gbk//IGNORE", $fileName);
  716. // 清理缓存
  717. ob_end_clean();
  718. if ($suffix == 'xlsx') {
  719. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  720. $class = "\PhpOffice\PhpSpreadsheet\Writer\Xlsx";
  721. } elseif ($suffix == 'xls') {
  722. header('Content-Type:application/vnd.ms-excel');
  723. $class = "\PhpOffice\PhpSpreadsheet\Writer\Xls";
  724. }
  725. header('Content-Disposition: attachment;filename="' . $fileName . '.' . $suffix . '"');
  726. header('Cache-Control: max-age=0');
  727. $writer = new $class($spreadsheet);
  728. $writer->save('php://output');
  729. // 删除清空 释放内存
  730. $spreadsheet->disconnectWorksheets();
  731. unset($spreadsheet);
  732. }
  733. }