UserDictController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Redis;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Http\Api;
  8. use App\Http\Api\AuthApi;
  9. use App\Http\Api\DictApi;
  10. class UserDictController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. //
  20. $result=false;
  21. $indexCol = ['id','word','type','grammar','mean','parent','note','factors','confidence','updated_at','creator_id'];
  22. switch ($request->get('view')) {
  23. case 'studio':
  24. # 获取studio内所有channel
  25. $user = \App\Http\Api\AuthApi::current($request);
  26. if($user){
  27. //判断当前用户是否有指定的studio的权限
  28. if($user['user_uid'] === \App\Http\Api\StudioApi::getIdByName($request->get('name'))){
  29. $table = UserDict::select($indexCol)
  30. ->where('creator_id', $user["user_id"])
  31. ->where('source', "_USER_WBW_");
  32. }else{
  33. return $this->error(__('auth.failed'));
  34. }
  35. }else{
  36. return $this->error(__('auth.failed'));
  37. }
  38. break;
  39. case 'user':
  40. # code...
  41. $table = UserDict::select($indexCol)
  42. ->where('creator_id', $_COOKIE["user_id"])
  43. ->where('source', '<>', "_SYS_USER_WBW_");
  44. break;
  45. case 'word':
  46. $table = UserDict::select($indexCol)
  47. ->where('word', $_GET["word"]);
  48. break;
  49. case 'compound':
  50. $dict_id = DictApi::getSysDict('robot_compound');
  51. if($dict_id===false){
  52. $this->error('no robot_compound');
  53. }
  54. $table = UserDict::where("dict_id",$dict_id)->where("word",$request->get('word'));
  55. break;
  56. default:
  57. # code...
  58. break;
  59. }
  60. if(isset($_GET["search"])){
  61. $table->where('word', 'like', $_GET["search"]."%");
  62. }
  63. if(isset($_GET["order"]) && isset($_GET["dir"])){
  64. $table->orderBy($_GET["order"],$_GET["dir"]);
  65. }else{
  66. if($request->get('view') === "compound"){
  67. $table->orderBy('confidence','desc');
  68. }else{
  69. $table->orderBy('updated_at','desc');
  70. }
  71. }
  72. $count = $table->count();
  73. if(isset($_GET["limit"])){
  74. $offset = 0;
  75. if(isset($_GET["offset"])){
  76. $offset = $_GET["offset"];
  77. }
  78. $table->skip($offset)->take($_GET["limit"]);
  79. }
  80. $result = $table->get();
  81. if($result){
  82. return $this->ok(["rows"=>$result,"count"=>$count]);
  83. }else{
  84. return $this->error("没有查询到数据");
  85. }
  86. }
  87. /**
  88. * Store a newly created resource in storage.
  89. *
  90. * @param \Illuminate\Http\Request $request
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function store(Request $request)
  94. {
  95. //
  96. if(!isset($_COOKIE["user_id"])){
  97. $this->error("not login");
  98. }
  99. $_data = json_decode($_POST["data"],true);
  100. switch($request->get('view')){
  101. case "wbw":
  102. #查询用户重复的数据
  103. $iOk = 0;
  104. $updateOk=0;
  105. foreach ($_data as $key => $word) {
  106. # code...
  107. $isDoesntExist = UserDict::where('creator_id', $_COOKIE["user_id"])
  108. ->where('word',$word["word"])
  109. ->where('type',$word["type"])
  110. ->where('grammar',$word["grammar"])
  111. ->where('parent',$word["parent"])
  112. ->where('mean',$word["mean"])
  113. ->where('factors',$word["factors"])
  114. ->where('factormean',$word["factormean"])
  115. ->where('source','_USER_WBW_')
  116. ->doesntExist();
  117. if($isDoesntExist){
  118. #不存在插入数据
  119. $word["id"]=app('snowflake')->id();
  120. $word["source"]='_USER_WBW_';
  121. $word["create_time"]=mTime();
  122. $word["creator_id"]=$_COOKIE["user_id"];
  123. $id = UserDict::insert($word);
  124. $updateOk = $this->update_sys_wbw($word);
  125. $this->update_redis($word);
  126. $iOk++;
  127. }
  128. }
  129. $this->ok([$iOk,$updateOk]);
  130. break;
  131. case "dict":
  132. break;
  133. }
  134. }
  135. /**
  136. * Display the specified resource.
  137. *
  138. * @param int $id
  139. * @return \Illuminate\Http\Response
  140. */
  141. public function show($id)
  142. {
  143. //
  144. $result = UserDict::find($id);
  145. if($result){
  146. return $this->ok($result);
  147. }else{
  148. return $this->error("没有查询到数据");
  149. }
  150. }
  151. /**
  152. * Update the specified resource in storage.
  153. *
  154. * @param \Illuminate\Http\Request $request
  155. * @param int $id
  156. * @return \Illuminate\Http\Response
  157. */
  158. public function update(Request $request, $id)
  159. {
  160. //
  161. $newData = $request->all();
  162. Log::info("id={$id}");
  163. Log::info($newData);
  164. $result = UserDict::where('id', $id)
  165. ->update($newData);
  166. if($result){
  167. $updateOk = $this->update_sys_wbw($newData);
  168. $this->update_redis($newData);
  169. return $this->ok([$result,$updateOk]);
  170. }else{
  171. return $this->error("没有查询到数据");
  172. }
  173. }
  174. /**
  175. * Remove the specified resource from storage.
  176. * @param \Illuminate\Http\Request $request
  177. * @param int $id
  178. * @return \Illuminate\Http\Response
  179. */
  180. public function destroy(Request $request,$id)
  181. {
  182. //
  183. Log::info("userDictController->destroy start");
  184. Log::info("userDictController->destroy id= {$id}");
  185. if(isset($_COOKIE["user_id"])){
  186. $user_id = $_COOKIE["user_id"];
  187. }else{
  188. $user = AuthApi::current($request);
  189. if(!$user){
  190. return $this->error(__('auth.failed'));
  191. }
  192. $user_id = $user['user_id'];
  193. }
  194. if($request->has("id")){
  195. $arrId = json_decode($request->get("id"),true) ;
  196. $count = 0;
  197. foreach ($arrId as $key => $id) {
  198. # 找到对应数据
  199. $data = UserDict::find($id);
  200. //查看是否有权限删除
  201. if($data->creator_id == $user_id){
  202. $result = UserDict::where('id', $id)
  203. ->delete();
  204. $count += $result;
  205. $updateOk = $this->update_sys_wbw($data);
  206. $this->update_redis($data);
  207. }
  208. }
  209. return $this->ok([$count,$updateOk]);
  210. }else{
  211. //删除单个单词
  212. $userDict = UserDict::find($id);
  213. //判断当前用户是否有指定的studio的权限
  214. if((int)$user_id !== $userDict->creator_id){
  215. return $this->error(__('auth.failed'));
  216. }
  217. $delete = $userDict->delete();
  218. return $this->ok($delete);
  219. }
  220. }
  221. public function delete(Request $request){
  222. Log::info("userDictController->delete start");
  223. $arrId = json_decode($request->get("id"),true) ;
  224. Log::info("id=".$request->get("id"));
  225. $count = 0;
  226. $updateOk = false;
  227. foreach ($arrId as $key => $id) {
  228. $data = UserDict::where('id',$id)->first();
  229. if($data){
  230. # 找到对应数据
  231. Log::info('creator_id:'.$data->creator_id);
  232. $param = [
  233. "id"=>$id,
  234. 'creator_id'=>$_COOKIE["user_id"]
  235. ];
  236. Log::info($param);
  237. $del = UserDict::where($param)->delete();
  238. $count += $del;
  239. $updateOk = $this->update_sys_wbw($data);
  240. $this->update_redis($data);
  241. }
  242. }
  243. Log::info("delete:".$count);
  244. return $this->ok(['deleted'=>$count]);
  245. }
  246. /*
  247. 更新系统wbw汇总表
  248. */
  249. private function update_sys_wbw($data){
  250. #查询用户重复的数据
  251. $count = UserDict::where('word',$data["word"])
  252. ->where('type',$data["type"])
  253. ->where('grammar',$data["grammar"])
  254. ->where('parent',$data["parent"])
  255. ->where('mean',$data["mean"])
  256. ->where('factors',$data["factors"])
  257. ->where('factormean',$data["factormean"])
  258. ->where('source','_USER_WBW_')
  259. ->count();
  260. if($count==0){
  261. # 没有任何用户有这个数据
  262. #删除数据
  263. $result = UserDict::where('word',$data["word"])
  264. ->where('type',$data["type"])
  265. ->where('grammar',$data["grammar"])
  266. ->where('parent',$data["parent"])
  267. ->where('mean',$data["mean"])
  268. ->where('factors',$data["factors"])
  269. ->where('factormean',$data["factormean"])
  270. ->where('source','_SYS_USER_WBW_')
  271. ->delete();
  272. return($result);
  273. }else{
  274. #更新或新增
  275. #查询最早上传这个数据的用户
  276. $creator_id = UserDict::where('word',$data["word"])
  277. ->where('type',$data["type"])
  278. ->where('grammar',$data["grammar"])
  279. ->where('parent',$data["parent"])
  280. ->where('mean',$data["mean"])
  281. ->where('factors',$data["factors"])
  282. ->where('factormean',$data["factormean"])
  283. ->where('source','_USER_WBW_')
  284. ->orderby("created_at",'asc')
  285. ->value("creator_id");
  286. $count = UserDict::where('word',$data["word"])
  287. ->where('type',$data["type"])
  288. ->where('grammar',$data["grammar"])
  289. ->where('parent',$data["parent"])
  290. ->where('mean',$data["mean"])
  291. ->where('factors',$data["factors"])
  292. ->where('factormean',$data["factormean"])
  293. ->where('source','_SYS_USER_WBW_')
  294. ->count();
  295. if($count==0){
  296. #系统字典没有 新增
  297. $result = UserDict::insert(
  298. [
  299. 'id' =>$snowflake->id(),
  300. 'word'=>$data["word"],
  301. 'type'=>$data["type"],
  302. 'grammar'=>$data["grammar"],
  303. 'parent'=>$data["parent"],
  304. 'mean'=>$data["mean"],
  305. 'factors'=>$data["factors"],
  306. 'factormean'=>$data["factormean"],
  307. 'source'=>"_SYS_USER_WBW_",
  308. 'creator_id' => $creator_id,
  309. 'ref_counter' => 1,
  310. "create_time"=>time()*1000
  311. ]);
  312. }else{
  313. #有,更新
  314. $result = UserDict::where('word',$data["word"])
  315. ->where('type',$data["type"])
  316. ->where('grammar',$data["grammar"])
  317. ->where('parent',$data["parent"])
  318. ->where('mean',$data["mean"])
  319. ->where('factors',$data["factors"])
  320. ->where('factormean',$data["factormean"])
  321. ->where('source','_SYS_USER_WBW_')
  322. ->update(
  323. [
  324. 'creator_id'=>$creator_id,
  325. 'ref_counter'=>$count
  326. ]);
  327. }
  328. return($result);
  329. }
  330. }
  331. private function update_redis($word){
  332. #更新 redis
  333. $Fetch = UserDict::where(['word'=>$word['word'],"source"=>"_USER_WBW_"])->get();
  334. $redisWord=array();
  335. foreach ($Fetch as $one) {
  336. # code...
  337. $redisWord[] = array(
  338. $one["id"],
  339. $one["word"],
  340. $one["type"],
  341. $one["grammar"],
  342. $one["parent"],
  343. $one["mean"],
  344. $one["note"],
  345. $one["factors"],
  346. $one["factormean"],
  347. $one["status"],
  348. $one["confidence"],
  349. $one["creator_id"],
  350. $one["source"],
  351. $one["language"]
  352. );
  353. }
  354. $redisData = json_encode($redisWord,JSON_UNESCAPED_UNICODE);
  355. Log::info("word={$word['word']} redis-data={$redisData}");
  356. Redis::hSet("dict/user",$word['word'],$redisData);
  357. $redisData1 = Redis::hGet("dict/user",$word['word']);
  358. Log::info("word={$word['word']} redis-data1={$redisData1}");
  359. #更新redis结束
  360. }
  361. }