2
0

DhammaTermController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Facades\App;
  7. use App\Models\DhammaTerm;
  8. use App\Models\Channel;
  9. use App\Http\Resources\TermResource;
  10. use App\Http\Api\AuthApi;
  11. use App\Http\Api\StudioApi;
  12. use App\Http\Api\ChannelApi;
  13. use App\Http\Api\ShareApi;
  14. use App\Tools\Tools;
  15. use Illuminate\Support\Facades\Cache;
  16. class DhammaTermController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function index(Request $request)
  24. {
  25. $result = false;
  26. $indexCol = [
  27. 'id',
  28. 'guid',
  29. 'word',
  30. 'meaning',
  31. 'other_meaning',
  32. 'note',
  33. 'tag',
  34. 'language',
  35. 'channal',
  36. 'owner',
  37. 'editor_id',
  38. 'created_at',
  39. 'updated_at'
  40. ];
  41. switch ($request->input('view')) {
  42. case 'create-by-channel':
  43. # 新建术语时。根据术语所在channel 给出新建术语所需数据。如语言,备选意思等。
  44. #获取channel信息
  45. $currChannel = Channel::where('uid', $request->input('channel'))->first();
  46. if (!$currChannel) {
  47. return $this->error(__('auth.failed'));
  48. }
  49. #TODO 查询studio信息
  50. #获取同studio的channel列表
  51. $studioChannels = Channel::where('owner_uid', $currChannel->owner_uid)
  52. ->select(['name', 'uid'])
  53. ->get();
  54. #获取全网意思列表
  55. $meanings = DhammaTerm::where('word', $request->input('word'))
  56. ->where('language', $currChannel->lang)
  57. ->select(['meaning', 'other_meaning'])
  58. ->get();
  59. $meaningList = [];
  60. foreach ($meanings as $key => $value) {
  61. # code...
  62. $meaning1 = [$value->meaning];
  63. if (!empty($value->other_meaning)) {
  64. $meaning2 = \explode(',', $value->other_meaning);
  65. $meaning1 = array_merge($meaning1, $meaning2);
  66. }
  67. foreach ($meaning1 as $key => $value) {
  68. # code...
  69. if (isset($meaningList[$value])) {
  70. $meaningList[$value]++;
  71. } else {
  72. $meaningList[$value] = 1;
  73. }
  74. }
  75. }
  76. $meaningCount = [];
  77. foreach ($meaningList as $key => $value) {
  78. # code...
  79. $meaningCount[] = ['meaning' => $key, 'count' => $value];
  80. }
  81. return $this->ok([
  82. "word" => $request->input('word'),
  83. "meaningCount" => $meaningCount,
  84. "studioChannels" => $studioChannels,
  85. "language" => $currChannel->lang,
  86. 'studio' => StudioApi::getById($currChannel->owner_uid),
  87. ]);
  88. break;
  89. case 'studio':
  90. # 获取 studio 内所有 term
  91. $user = AuthApi::current($request);
  92. if (!$user) {
  93. return $this->error(__('auth.failed'), [], 401);
  94. }
  95. //判断当前用户是否有指定的studio的权限
  96. if ($user['user_uid'] !== StudioApi::getIdByName($request->input('name'))) {
  97. return $this->error(__('auth.failed'), [], 403);
  98. }
  99. $table = DhammaTerm::select($indexCol)
  100. ->where('owner', $user["user_uid"]);
  101. break;
  102. case 'channel':
  103. # 获取 studio 内所有 term
  104. $user = AuthApi::current($request);
  105. if (!$user) {
  106. return $this->error(__('auth.failed'));
  107. }
  108. //判断当前用户是否有指定的 channel 的权限
  109. $channel = Channel::find($request->input('id'));
  110. if ($user['user_uid'] !== $channel->owner_uid) {
  111. //看是否为协作
  112. $power = ShareApi::getResPower($user['user_uid'], $request->input('id'));
  113. if ($power === 0) {
  114. return $this->error(__('auth.failed'), [], 403);
  115. }
  116. }
  117. $table = DhammaTerm::select($indexCol)
  118. ->where('channal', $request->input('id'));
  119. break;
  120. case 'show':
  121. return $this->ok(DhammaTerm::find($request->input('id')));
  122. break;
  123. case 'user':
  124. # code...
  125. $user = AuthApi::current($request);
  126. if (!$user) {
  127. return $this->error(__('auth.failed'));
  128. }
  129. $userUid = $user['user_uid'];
  130. $search = $request->input('search');
  131. $table = DhammaTerm::select($indexCol)
  132. ->where('owner', $userUid);
  133. break;
  134. case 'word':
  135. $table = DhammaTerm::select($indexCol)
  136. ->whereIn('word', explode(',', $request->input("word")))
  137. ->orWhereIn('meaning', explode(',', $request->input("word")));
  138. break;
  139. case 'tag':
  140. $table = DhammaTerm::select($indexCol)
  141. ->whereIn('tag', explode(',', $request->input("tag")));
  142. break;
  143. case 'hot-meaning':
  144. $key = 'term/hot_meaning';
  145. $value = Cache::get($key, function () use ($request) {
  146. $hotMeaning = [];
  147. $words = DhammaTerm::select('word')
  148. ->where('language', $request->input("language"))
  149. ->groupby('word')
  150. ->get();
  151. foreach ($words as $key => $word) {
  152. # code...
  153. $result = DhammaTerm::select(DB::raw('count(*) as word_count, meaning'))
  154. ->where('language', $request->input("language"))
  155. ->where('word', $word['word'])
  156. ->groupby('meaning')
  157. ->orderby('word_count', 'desc')
  158. ->first();
  159. if ($result) {
  160. $hotMeaning[] = [
  161. 'word' => $word['word'],
  162. 'meaning' => $result['meaning'],
  163. 'language' => $request->input("language"),
  164. 'owner' => '',
  165. ];
  166. }
  167. }
  168. Cache::put($key, $hotMeaning, 3600);
  169. return $hotMeaning;
  170. }, config('mint.cache.expire'));
  171. return $this->ok(["rows" => $value, "count" => count($value)]);
  172. break;
  173. default:
  174. # code...
  175. break;
  176. }
  177. $search = $request->input('search');
  178. if (!empty($search)) {
  179. $table = $table->where(function ($query) use ($search) {
  180. $query->where('word', 'like', $search . "%")
  181. ->orWhere('word_en', 'like', $search . "%")
  182. ->orWhere('meaning', 'like', "%" . $search . "%");
  183. });
  184. }
  185. $count = $table->count();
  186. $table = $table->orderBy($request->input('order', 'updated_at'), $request->input('dir', 'desc'));
  187. $table = $table->skip($request->input("offset", 0))
  188. ->take($request->input('limit', 1000));
  189. $result = $table->get();
  190. return $this->ok(["rows" => TermResource::collection($result), "count" => $count]);
  191. }
  192. /**
  193. * Store a newly created resource in storage.
  194. *
  195. * @param \Illuminate\Http\Request $request
  196. * @return \Illuminate\Http\Response
  197. */
  198. public function store(Request $request)
  199. {
  200. $user = AuthApi::current($request);
  201. if (!$user) {
  202. return $this->error(__('auth.failed'));
  203. }
  204. $validated = $request->validate([
  205. 'word' => 'required',
  206. 'meaning' => 'required',
  207. ]);
  208. /**
  209. * 查询重复的
  210. * 一个channel下面word+tag+language 唯一
  211. */
  212. $table = DhammaTerm::where('owner', $user["user_uid"])
  213. ->where('word', $request->input("word"))
  214. ->where('tag', $request->input("tag"));
  215. if (!empty($request->input("channel"))) {
  216. $isDoesntExist = $table->where('channal', $request->input("channel"))
  217. ->doesntExist();
  218. } else {
  219. $isDoesntExist = $table->whereNull('channal')->where('language', $request->input("language"))
  220. ->doesntExist();
  221. }
  222. if ($isDoesntExist) {
  223. #没有重复的 插入数据
  224. $term = new DhammaTerm;
  225. $term->id = app('snowflake')->id();
  226. $term->guid = Str::uuid();
  227. $term->word = $request->input("word");
  228. $term->word_en = Tools::getWordEn($request->input("word"));
  229. $term->meaning = $request->input("meaning");
  230. $term->other_meaning = $request->input("other_meaning");
  231. $term->note = $request->input("note");
  232. $term->tag = $request->input("tag");
  233. $term->channal = $request->input("channel");
  234. $term->language = $request->input("language");
  235. if (!empty($request->input("channel"))) {
  236. $channelInfo = ChannelApi::getById($request->input("channel"));
  237. if (!$channelInfo) {
  238. return $this->error("channel id failed");
  239. } else {
  240. //查看有没有channel权限
  241. $power = ShareApi::getResPower($user["user_uid"], $request->input("channel"), 2);
  242. if ($power < 20) {
  243. return $this->error(__('auth.failed'));
  244. }
  245. $term->owner = $channelInfo['studio_id'];
  246. $term->language = $channelInfo['lang'];
  247. }
  248. } else {
  249. if ($request->has("studioId")) {
  250. $studioId = $request->input("studioId");
  251. } else if ($request->has("studioName")) {
  252. $studioId = StudioApi::getIdByName($request->input("studioName"));
  253. }
  254. if (Str::isUuid($studioId)) {
  255. $term->owner = $studioId;
  256. } else {
  257. return $this->error('not valid studioId');
  258. }
  259. }
  260. $term->editor_id = $user["user_id"];
  261. $term->create_time = time() * 1000;
  262. $term->modify_time = time() * 1000;
  263. $term->save();
  264. //删除cache
  265. $this->deleteCache($term);
  266. return $this->ok(new TermResource($term));
  267. } else {
  268. return $this->error("word existed", [], 200);
  269. }
  270. }
  271. private function deleteCache($term)
  272. {
  273. if (empty($term->channal)) {
  274. //通用 查询studio所有channel
  275. $channels = Channel::where('owner_uid', $term->owner)->select('uid')->get();
  276. foreach ($channels as $channel) {
  277. Cache::forget("/term/{$channel}/{$term->word}");
  278. }
  279. } else {
  280. Cache::forget("/term/{$term->channal}/{$term->word}");
  281. }
  282. }
  283. /**
  284. * Display the specified resource.
  285. *
  286. * @param string $id
  287. * @return \Illuminate\Http\Response
  288. */
  289. public function show(Request $request, $id)
  290. {
  291. //
  292. $result = DhammaTerm::where('guid', $id)->first();
  293. if ($result) {
  294. return $this->ok(new TermResource($result));
  295. } else {
  296. return $this->error("没有查询到数据");
  297. }
  298. }
  299. /**
  300. * Update the specified resource in storage.
  301. *
  302. * @param \Illuminate\Http\Request $request
  303. * @param \App\Models\DhammaTerm $dhammaTerm
  304. * @return \Illuminate\Http\Response
  305. */
  306. public function update(Request $request, string $id)
  307. {
  308. //
  309. $user = AuthApi::current($request);
  310. if (!$user) {
  311. return $this->error(__('auth.failed'), [], 401);
  312. }
  313. $dhammaTerm = DhammaTerm::find($id);
  314. if (!$dhammaTerm) {
  315. return $this->error('404');
  316. }
  317. if (empty($dhammaTerm->channal)) {
  318. //查看有没有studio权限
  319. if ($user['user_uid'] !== $dhammaTerm->owner) {
  320. return $this->error(__('auth.failed'), [], 403);
  321. }
  322. } else {
  323. //查看有没有channel权限
  324. $power = ShareApi::getResPower($user["user_uid"], $dhammaTerm->channal, 2);
  325. if ($power < 20) {
  326. return $this->error(__('auth.failed'), [], 403);
  327. }
  328. }
  329. $dhammaTerm->word = $request->input("word");
  330. $dhammaTerm->word_en = Tools::getWordEn($request->input("word"));
  331. $dhammaTerm->meaning = $request->input("meaning");
  332. $dhammaTerm->other_meaning = $request->input("other_meaning");
  333. $dhammaTerm->note = $request->input("note");
  334. $dhammaTerm->tag = $request->input("tag");
  335. $dhammaTerm->language = $request->input("language");
  336. $dhammaTerm->editor_id = $user["user_id"];
  337. $dhammaTerm->create_time = time() * 1000;
  338. $dhammaTerm->modify_time = time() * 1000;
  339. $dhammaTerm->save();
  340. //删除cache
  341. $this->deleteCache($dhammaTerm);
  342. return $this->ok(new TermResource($dhammaTerm));
  343. }
  344. /**
  345. * Remove the specified resource from storage.
  346. *
  347. * @param \App\Models\DhammaTerm $dhammaTerm
  348. * @return \Illuminate\Http\Response
  349. */
  350. public function destroy(DhammaTerm $dhammaTerm, Request $request)
  351. {
  352. /**
  353. * 一次删除多个单词
  354. */
  355. $user = AuthApi::current($request);
  356. if (!$user) {
  357. return $this->error(__('auth.failed'));
  358. }
  359. $count = 0;
  360. if ($request->has("uuid")) {
  361. //查看是否有删除权限
  362. foreach ($request->input("id") as $key => $uuid) {
  363. $term = DhammaTerm::find($uuid);
  364. if ($term->owner !== $user['user_uid']) {
  365. if (!empty($term->channal)) {
  366. //看是否为协作
  367. $power = ShareApi::getResPower($user['user_uid'], $term->channal);
  368. if ($power < 20) {
  369. continue;
  370. }
  371. } else {
  372. continue;
  373. }
  374. }
  375. $count += $term->delete();
  376. //删除cache
  377. $this->deleteCache($term);
  378. }
  379. } else {
  380. $arrId = json_decode($request->input("id"), true);
  381. foreach ($arrId as $key => $id) {
  382. # code...
  383. $term = DhammaTerm::where('id', $id)
  384. ->where('owner', $user['user_uid']);
  385. $result = $term->delete();
  386. $this->deleteCache($term);
  387. if ($result) {
  388. $count++;
  389. }
  390. }
  391. }
  392. return $this->ok($count);
  393. }
  394. }