UserDictController.php 11 KB

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