UserOperation.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Models\UserOperationLog;
  7. use App\Models\UserOperationFrame;
  8. use App\Models\UserOperationDaily;
  9. use App\Http\Api\AuthApi;
  10. define("MAX_INTERVAL", 600000);
  11. define("MIN_INTERVAL", 60000);
  12. /**
  13. * $active_type[10] = "channel_update";
  14. * $active_type[11] = "channel_create";
  15. * $active_type[20] = "article_update";
  16. * $active_type[21] = "article_create";
  17. * $active_type[30] = "dict_lookup";
  18. * $active_type[40] = "term_update";
  19. * $active_type[42] = "term_create";
  20. * $active_type[41] = "term_lookup";
  21. * $active_type[60] = "wbw_update";
  22. * $active_type[61] = "wbw_create";
  23. * $active_type[70] = "sent_update";
  24. * $active_type[71] = "sent_create";
  25. * $active_type[80] = "collection_update";
  26. * $active_type[81] = "collection_create";
  27. * $active_type[90] = "nissaya_open";
  28. */
  29. class UserOperation
  30. {
  31. /**
  32. * Handle an incoming request.
  33. *
  34. * @param \Illuminate\Http\Request $request
  35. * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
  36. * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
  37. */
  38. public function handle(Request $request, Closure $next)
  39. {
  40. //Log::debug('path', ['path' => $request->path()]);
  41. $response = $next($request);
  42. $user = AuthApi::current($request);
  43. if (!$user) {
  44. return $response;
  45. }
  46. $api = explode('/', $request->path());
  47. if (count($api) < 3) {
  48. return $response;
  49. }
  50. if ($api[0] !== 'api' || $api[1] !== 'v2') {
  51. return $response;
  52. }
  53. $method = $request->method();
  54. switch ($api[2]) {
  55. case 'channel':
  56. switch ($method) {
  57. case 'POST':
  58. $newLog = [
  59. "op_type_id" => 11,
  60. "op_type" => "channel_create",
  61. "content" => $request->get('studio') . '/' . $request->get('name'),
  62. ];
  63. break;
  64. case 'PUT':
  65. $newLog = [
  66. "op_type_id" => 10,
  67. "op_type" => "channel_update",
  68. "content" => $request->get('name'),
  69. ];
  70. break;
  71. }
  72. break;
  73. case 'article':
  74. switch ($method) {
  75. case 'POST':
  76. $newLog = [
  77. "op_type_id" => 21,
  78. "op_type" => "article_create",
  79. "content" => $request->get('studio') . '/' . $request->get('title'),
  80. ];
  81. break;
  82. case 'PUT':
  83. $newLog = [
  84. "op_type_id" => 20,
  85. "op_type" => "article_update",
  86. "content" => $request->get('title'),
  87. ];
  88. break;
  89. }
  90. break;
  91. case 'dict':
  92. $newLog = [
  93. "op_type_id" => 30,
  94. "op_type" => "dict_lookup",
  95. "content" => $request->get("word")
  96. ];
  97. break;
  98. case 'terms':
  99. switch ($method) {
  100. case 'POST':
  101. $newLog = [
  102. "op_type_id" => 42,
  103. "op_type" => "term_create",
  104. "content" => $request->get('word'),
  105. ];
  106. break;
  107. case 'PUT':
  108. $newLog = [
  109. "op_type_id" => 40,
  110. "op_type" => "term_update",
  111. "content" => $request->get('word'),
  112. ];
  113. break;
  114. }
  115. break;
  116. case 'sentence':
  117. switch ($method) {
  118. case 'POST':
  119. $newLog = [
  120. "op_type_id" => 71,
  121. "op_type" => "sent_create",
  122. "content" => $request->get('channel'),
  123. ];
  124. break;
  125. case 'PUT':
  126. $newLog = [
  127. "op_type_id" => 70,
  128. "op_type" => "sent_update",
  129. "content" => $request->get('channel'),
  130. ];
  131. break;
  132. /*
  133. case 'GET':
  134. if($request->get('view')==='sent-can-read' && $request->has('type')){
  135. $newLog = [
  136. "op_type"=>"res_read",
  137. "content"=>$request->get('sentence'),
  138. ];
  139. switch ($request->get('type')) {
  140. case 'translation':
  141. $newLog["op_type_id"] = 101;
  142. break;
  143. case 'nissaya':
  144. $newLog["op_type_id"] = 102;
  145. break;
  146. case 'original':
  147. $newLog["op_type_id"] = 103;
  148. break;
  149. case 'wbw':
  150. $newLog["op_type_id"] = 104;
  151. break;
  152. case 'commentary':
  153. $newLog["op_type_id"] = 105;
  154. break;
  155. default:
  156. unset($newLog);
  157. break;
  158. }
  159. }
  160. break;
  161. */
  162. }
  163. break;
  164. case 'anthology':
  165. switch ($method) {
  166. case 'POST':
  167. $newLog = [
  168. "op_type_id" => 81,
  169. "op_type" => "collection_create",
  170. "content" => $request->get('title'),
  171. ];
  172. break;
  173. case 'PUT':
  174. $newLog = [
  175. "op_type_id" => 80,
  176. "op_type" => "collection_update",
  177. "content" => $request->get('title'),
  178. ];
  179. break;
  180. }
  181. break;
  182. /*
  183. case 'article-map':
  184. switch ($method) {
  185. case 'POST':
  186. $newLog = [
  187. "op_type_id"=>111,
  188. "op_type"=>"article_map_create",
  189. "content"=>$request->get('anthology_id'),
  190. ];
  191. break;
  192. case 'PUT':
  193. $newLog = [
  194. "op_type_id"=>110,
  195. "op_type"=>"article_map_update",
  196. ];
  197. if(isset($api[3])){
  198. $newLog['content'] = $api[3];
  199. }
  200. break;
  201. }
  202. break;
  203. */
  204. case 'wbw':
  205. switch ($method) {
  206. case 'POST':
  207. $newLog = [
  208. "op_type_id" => 60,
  209. "op_type" => "wbw_update",
  210. "content" => $request->get('book') . "_" . $request->get('para') . "_" . $request->get('channel_id'),
  211. ];
  212. break;
  213. }
  214. break;
  215. }
  216. if (isset($newLog)) {
  217. $currTime = round((microtime(true)) * 1000, 0);
  218. #获取客户端时区偏移 beijing = +8
  219. if (isset($_COOKIE["timezone"])) {
  220. $client_timezone = (0 - (int) $_COOKIE["timezone"]) * 60 * 1000;
  221. } else {
  222. $client_timezone = 0;
  223. }
  224. $log = new UserOperationLog();
  225. $log->id = app('snowflake')->id();
  226. $log->user_id = $user['user_id'];
  227. $log->op_type_id = $newLog["op_type_id"];
  228. $log->op_type = $newLog["op_type"];
  229. $log->content = $newLog["content"];
  230. $log->timezone = $client_timezone;
  231. $log->create_time = $currTime;
  232. $log->save();
  233. //frame
  234. // 查询上次编辑活跃结束时间
  235. $last = UserOperationFrame::where("user_id", $user['user_id'])->orderBy("updated_at", "desc")->first();
  236. if ($last) {
  237. //找到,判断是否超时,超时新建,未超时修改
  238. $id = (int) $last["id"];
  239. $start_time = (int) $last["op_start"];
  240. $endtime = (int) $last["op_end"];
  241. $hit = (int) $last["hit"];
  242. if ($currTime - $endtime > MAX_INTERVAL) {
  243. //超时新建
  244. $new_record = true;
  245. } else {
  246. //未超时修改
  247. $new_record = false;
  248. }
  249. } else {
  250. //没找到,新建
  251. $new_record = true;
  252. }
  253. $this_active_time = 0; //时间增量
  254. if ($new_record) {
  255. #新建
  256. $newFrame = new UserOperationFrame();
  257. #最小思考时间 MIN_INTERVAL
  258. $newFrame->id = app('snowflake')->id();
  259. $newFrame->user_id = $user['user_id'];
  260. $newFrame->op_start = $currTime - MIN_INTERVAL;
  261. $newFrame->op_end = $currTime;
  262. $newFrame->duration = MIN_INTERVAL;
  263. $newFrame->hit = 1;
  264. $newFrame->timezone = $client_timezone;
  265. $newFrame->save();
  266. $this_active_time = MIN_INTERVAL;
  267. } else {
  268. $this_active_time = $currTime - $last->op_end;
  269. #修改
  270. $last->op_end = $currTime;
  271. $last->duration = $currTime - $start_time;
  272. $last->hit = $last->hit + 1;
  273. $last->save();
  274. }
  275. #更新经验总量表
  276. #计算客户端日期 unix时间戳 以毫秒计
  277. $client_currtime = $currTime + $client_timezone;
  278. $client_date = strtotime(gmdate("Y-m-d", $client_currtime / 1000)) * 1000;
  279. #查询是否存在
  280. $daily = UserOperationDaily::where("user_id", $user['user_id'])->where("date_int", $client_date)->first();
  281. if ($daily) {
  282. #更新
  283. $daily->duration = $daily->duration + $this_active_time;
  284. $daily->hit = $daily->hit + 1;
  285. $daily->save();
  286. } else {
  287. #新建
  288. $daily = new UserOperationDaily();
  289. $daily->id = app('snowflake')->id();
  290. $daily->user_id = $user['user_id'];
  291. $daily->date_int = $client_date;
  292. $daily->duration = MIN_INTERVAL;
  293. $daily->hit = 1;
  294. $daily->save();
  295. }
  296. #更新经验总量表结束
  297. }
  298. return $response;
  299. }
  300. }