77 lines
1.9 KiB
PHP
77 lines
1.9 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace app\controller\api;
|
||
|
|
||
|
use app\model\AccountFootmarks;
|
||
|
use app\model\Event;
|
||
|
use Exception;
|
||
|
use think\db\exception\DataNotFoundException;
|
||
|
use think\db\exception\DbException;
|
||
|
use think\db\exception\ModelNotFoundException;
|
||
|
use think\response\Json;
|
||
|
|
||
|
class Statistics extends Base
|
||
|
{
|
||
|
protected $noNeedLogin = ['event'];
|
||
|
/**
|
||
|
* 事件列表
|
||
|
*
|
||
|
* @return Json
|
||
|
* @throws DataNotFoundException
|
||
|
* @throws DbException
|
||
|
* @throws ModelNotFoundException
|
||
|
*/
|
||
|
public function event(): Json
|
||
|
{
|
||
|
$data = Event::field('id, name')->select()->toArray();
|
||
|
|
||
|
return $this->json(0, 'success', $data);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 上报
|
||
|
*
|
||
|
* @return Json
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public function report(): Json
|
||
|
{
|
||
|
$data = input();
|
||
|
$accountId = $this->request->user['user_id'] ?? 0;
|
||
|
|
||
|
if (empty($data)) {
|
||
|
return $this->json();
|
||
|
}
|
||
|
|
||
|
if (!is_array($data)) {
|
||
|
$data = json_decode($data, true);
|
||
|
}
|
||
|
|
||
|
$insert = [];
|
||
|
if (count($data) > 1000) {
|
||
|
//TODO 太大就分片处理
|
||
|
|
||
|
} else {
|
||
|
foreach ($data as $d) {
|
||
|
if (!isset($d['e']) || !isset($d['t'])) {
|
||
|
return $this->json(4001, '参数错误');
|
||
|
}
|
||
|
$arr = [];
|
||
|
$arr['event_id'] = (int) ($d['e'] ?? 0);
|
||
|
$arr['account_id'] = $accountId;
|
||
|
$arr['content_id'] = (int) ($d['c'] ?? 0);
|
||
|
$t = (strlen($d['t']) == 13) ? $d['t'] / 1000 : $d['t'];
|
||
|
$arr['created_at'] = date('Y-m-d H:i:s', $t);
|
||
|
|
||
|
$insert[] = $arr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 若量大 时间长 可丢入队列操作
|
||
|
if (count($insert) > 0) {
|
||
|
(new AccountFootmarks())->saveAll($insert);
|
||
|
}
|
||
|
|
||
|
return $this->json();
|
||
|
}
|
||
|
}
|