UserDictController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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(isset($_COOKIE["user_id"])){
  174. $user_id = $_COOKIE["user_id"];
  175. }else{
  176. $user = AuthApi::current($request);
  177. if(!$user){
  178. return $this->error(__('auth.failed'));
  179. }
  180. $user_id = $user['user_id'];
  181. }
  182. if($request->has("id")){
  183. $arrId = json_decode($request->get("id"),true) ;
  184. $count = 0;
  185. foreach ($arrId as $key => $id) {
  186. # 找到对应数据
  187. $data = UserDict::find($id);
  188. //查看是否有权限删除
  189. if($data->creator_id == $user_id){
  190. $result = UserDict::where('id', $id)
  191. ->delete();
  192. $count += $result;
  193. $updateOk = $this->update_sys_wbw($data);
  194. $this->update_redis($data);
  195. }
  196. }
  197. return $this->ok([$count,$updateOk]);
  198. }else{
  199. //删除单个单词
  200. $userDict = UserDict::find($id);
  201. //判断当前用户是否有指定的studio的权限
  202. if((int)$user_id !== $userDict->creator_id){
  203. return $this->error(__('auth.failed'));
  204. }
  205. $delete = $userDict->delete();
  206. return $this->ok($delete);
  207. }
  208. }
  209. public function delete(Request $request){
  210. Log::info("userDictController->delete start");
  211. $arrId = json_decode($request->get("id"),true) ;
  212. Log::info("id=".$request->get("id"));
  213. $count = 0;
  214. $updateOk = false;
  215. foreach ($arrId as $key => $id) {
  216. $data = UserDict::where('id',$id)->first();
  217. if($data){
  218. # 找到对应数据
  219. Log::info('creator_id:'.$data->creator_id);
  220. $param = [
  221. "id"=>$id,
  222. 'creator_id'=>$_COOKIE["user_id"]
  223. ];
  224. Log::info($param);
  225. $del = UserDict::where($param)->delete();
  226. $count += $del;
  227. $updateOk = $this->update_sys_wbw($data);
  228. $this->update_redis($data);
  229. }
  230. }
  231. Log::info("delete:".$count);
  232. return $this->ok(['deleted'=>$count]);
  233. }
  234. /*
  235. 更新系统wbw汇总表
  236. */
  237. private function update_sys_wbw($data){
  238. #查询用户重复的数据
  239. $count = UserDict::where('word',$data["word"])
  240. ->where('type',$data["type"])
  241. ->where('grammar',$data["grammar"])
  242. ->where('parent',$data["parent"])
  243. ->where('mean',$data["mean"])
  244. ->where('factors',$data["factors"])
  245. ->where('factormean',$data["factormean"])
  246. ->where('source','_USER_WBW_')
  247. ->count();
  248. if($count==0){
  249. # 没有任何用户有这个数据
  250. #删除数据
  251. $result = 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','_SYS_USER_WBW_')
  259. ->delete();
  260. return($result);
  261. }else{
  262. #更新或新增
  263. #查询最早上传这个数据的用户
  264. $creator_id = UserDict::where('word',$data["word"])
  265. ->where('type',$data["type"])
  266. ->where('grammar',$data["grammar"])
  267. ->where('parent',$data["parent"])
  268. ->where('mean',$data["mean"])
  269. ->where('factors',$data["factors"])
  270. ->where('factormean',$data["factormean"])
  271. ->where('source','_USER_WBW_')
  272. ->orderby("created_at",'asc')
  273. ->value("creator_id");
  274. $count = UserDict::where('word',$data["word"])
  275. ->where('type',$data["type"])
  276. ->where('grammar',$data["grammar"])
  277. ->where('parent',$data["parent"])
  278. ->where('mean',$data["mean"])
  279. ->where('factors',$data["factors"])
  280. ->where('factormean',$data["factormean"])
  281. ->where('source','_SYS_USER_WBW_')
  282. ->count();
  283. if($count==0){
  284. #系统字典没有 新增
  285. $result = UserDict::insert(
  286. [
  287. 'id' =>$snowflake->id(),
  288. 'word'=>$data["word"],
  289. 'type'=>$data["type"],
  290. 'grammar'=>$data["grammar"],
  291. 'parent'=>$data["parent"],
  292. 'mean'=>$data["mean"],
  293. 'factors'=>$data["factors"],
  294. 'factormean'=>$data["factormean"],
  295. 'source'=>"_SYS_USER_WBW_",
  296. 'creator_id' => $creator_id,
  297. 'ref_counter' => 1,
  298. "create_time"=>time()*1000
  299. ]);
  300. }else{
  301. #有,更新
  302. $result = UserDict::where('word',$data["word"])
  303. ->where('type',$data["type"])
  304. ->where('grammar',$data["grammar"])
  305. ->where('parent',$data["parent"])
  306. ->where('mean',$data["mean"])
  307. ->where('factors',$data["factors"])
  308. ->where('factormean',$data["factormean"])
  309. ->where('source','_SYS_USER_WBW_')
  310. ->update(
  311. [
  312. 'creator_id'=>$creator_id,
  313. 'ref_counter'=>$count
  314. ]);
  315. }
  316. return($result);
  317. }
  318. }
  319. private function update_redis($word){
  320. #更新 redis
  321. $Fetch = UserDict::where(['word'=>$word['word'],"source"=>"_USER_WBW_"])->get();
  322. $redisWord=array();
  323. foreach ($Fetch as $one) {
  324. # code...
  325. $redisWord[] = array(
  326. $one["id"],
  327. $one["word"],
  328. $one["type"],
  329. $one["grammar"],
  330. $one["parent"],
  331. $one["mean"],
  332. $one["note"],
  333. $one["factors"],
  334. $one["factormean"],
  335. $one["status"],
  336. $one["confidence"],
  337. $one["creator_id"],
  338. $one["source"],
  339. $one["language"]
  340. );
  341. }
  342. $redisData = json_encode($redisWord,JSON_UNESCAPED_UNICODE);
  343. Log::info("word={$word['word']} redis-data={$redisData}");
  344. Redis::hSet("dict/user",$word['word'],$redisData);
  345. $redisData1 = Redis::hGet("dict/user",$word['word']);
  346. Log::info("word={$word['word']} redis-data1={$redisData1}");
  347. #更新redis结束
  348. }
  349. }