2
0

DhammaTermController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\ShareApi;
  5. use App\Http\Api\StudioApi;
  6. use App\Http\Resources\TermResource;
  7. use App\Models\Channel;
  8. use App\Models\DhammaTerm;
  9. use App\Services\AuthService;
  10. use App\Tools\Tools;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\Response;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Str;
  16. class DhammaTermController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return 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 = AuthService::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 = AuthService::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 = AuthService::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::remember($key, config('mint.cache.expire'), 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. });
  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. * @return Response
  196. */
  197. public function store(Request $request)
  198. {
  199. $user = AuthService::current($request);
  200. if (! $user) {
  201. return $this->error(__('auth.failed'));
  202. }
  203. $validated = $request->validate([
  204. 'word' => 'required',
  205. 'meaning' => 'required',
  206. ]);
  207. /**
  208. * 查询重复的
  209. * 一个channel下面word+tag+language 唯一
  210. */
  211. $table = DhammaTerm::where('owner', $user['user_uid'])
  212. ->where('word', $request->input('word'))
  213. ->where('tag', $request->input('tag'));
  214. if (! empty($request->input('channel'))) {
  215. $isDoesntExist = $table->where('channal', $request->input('channel'))
  216. ->doesntExist();
  217. } else {
  218. $isDoesntExist = $table->whereNull('channal')->where('language', $request->input('language'))
  219. ->doesntExist();
  220. }
  221. if ($isDoesntExist) {
  222. // 没有重复的 插入数据
  223. $term = new DhammaTerm;
  224. $term->id = app('snowflake')->id();
  225. $term->guid = Str::uuid();
  226. $term->word = $request->input('word');
  227. $term->word_en = Tools::getWordEn($request->input('word'));
  228. $term->meaning = $request->input('meaning');
  229. $term->other_meaning = $request->input('other_meaning');
  230. $term->note = $request->input('note');
  231. $term->tag = $request->input('tag');
  232. $term->channal = $request->input('channel');
  233. $term->language = $request->input('language');
  234. if (! empty($request->input('channel'))) {
  235. $channelInfo = ChannelApi::getById($request->input('channel'));
  236. if (! $channelInfo) {
  237. return $this->error('channel id failed');
  238. } else {
  239. // 查看有没有channel权限
  240. $power = ShareApi::getResPower($user['user_uid'], $request->input('channel'), 2);
  241. if ($power < 20) {
  242. return $this->error(__('auth.failed'));
  243. }
  244. $term->owner = $channelInfo['studio_id'];
  245. $term->language = $channelInfo['lang'];
  246. }
  247. } else {
  248. if ($request->has('studioId')) {
  249. $studioId = $request->input('studioId');
  250. } elseif ($request->has('studioName')) {
  251. $studioId = StudioApi::getIdByName($request->input('studioName'));
  252. }
  253. if (Str::isUuid($studioId)) {
  254. $term->owner = $studioId;
  255. } else {
  256. return $this->error('not valid studioId');
  257. }
  258. }
  259. $term->editor_id = $user['user_id'];
  260. $term->create_time = time() * 1000;
  261. $term->modify_time = time() * 1000;
  262. $term->save();
  263. // 删除cache
  264. $this->deleteCache($term);
  265. return $this->ok(new TermResource($term));
  266. } else {
  267. return $this->error('word existed', [], 200);
  268. }
  269. }
  270. private function deleteCache($term)
  271. {
  272. if (empty($term->channal)) {
  273. // 通用 查询studio所有channel
  274. $channels = Channel::where('owner_uid', $term->owner)->select('uid')->get();
  275. foreach ($channels as $channel) {
  276. Cache::forget("/term/{$channel}/{$term->word}");
  277. }
  278. } else {
  279. Cache::forget("/term/{$term->channal}/{$term->word}");
  280. }
  281. }
  282. /**
  283. * Display the specified resource.
  284. *
  285. * @param string $id
  286. * @return Response
  287. */
  288. public function show(Request $request, $id)
  289. {
  290. //
  291. $result = DhammaTerm::where('guid', $id)->first();
  292. if ($result) {
  293. return $this->ok(new TermResource($result));
  294. } else {
  295. return $this->error('没有查询到数据');
  296. }
  297. }
  298. /**
  299. * Update the specified resource in storage.
  300. *
  301. * @param DhammaTerm $dhammaTerm
  302. * @return Response
  303. */
  304. public function update(Request $request, string $id)
  305. {
  306. //
  307. $user = AuthService::current($request);
  308. if (! $user) {
  309. return $this->error(__('auth.failed'), [], 401);
  310. }
  311. $dhammaTerm = DhammaTerm::find($id);
  312. if (! $dhammaTerm) {
  313. return $this->error('404');
  314. }
  315. if (empty($dhammaTerm->channal)) {
  316. // 查看有没有studio权限
  317. if ($user['user_uid'] !== $dhammaTerm->owner) {
  318. return $this->error(__('auth.failed'), [], 403);
  319. }
  320. } else {
  321. // 查看有没有channel权限
  322. $power = ShareApi::getResPower($user['user_uid'], $dhammaTerm->channal, 2);
  323. if ($power < 20) {
  324. return $this->error(__('auth.failed'), [], 403);
  325. }
  326. }
  327. $dhammaTerm->word = $request->input('word');
  328. $dhammaTerm->word_en = Tools::getWordEn($request->input('word'));
  329. $dhammaTerm->meaning = $request->input('meaning');
  330. $dhammaTerm->other_meaning = $request->input('other_meaning');
  331. $dhammaTerm->note = $request->input('note');
  332. $dhammaTerm->tag = $request->input('tag');
  333. $dhammaTerm->language = $request->input('language');
  334. $dhammaTerm->editor_id = $user['user_id'];
  335. $dhammaTerm->create_time = time() * 1000;
  336. $dhammaTerm->modify_time = time() * 1000;
  337. $dhammaTerm->save();
  338. // 删除cache
  339. $this->deleteCache($dhammaTerm);
  340. return $this->ok(new TermResource($dhammaTerm));
  341. }
  342. /**
  343. * Remove the specified resource from storage.
  344. *
  345. * @return Response
  346. */
  347. public function destroy(DhammaTerm $dhammaTerm, Request $request)
  348. {
  349. /**
  350. * 一次删除多个单词
  351. */
  352. $user = AuthService::current($request);
  353. if (! $user) {
  354. return $this->error(__('auth.failed'));
  355. }
  356. $count = 0;
  357. if ($request->has('uuid')) {
  358. // 查看是否有删除权限
  359. foreach ($request->input('id') as $key => $uuid) {
  360. $term = DhammaTerm::find($uuid);
  361. if ($term->owner !== $user['user_uid']) {
  362. if (! empty($term->channal)) {
  363. // 看是否为协作
  364. $power = ShareApi::getResPower($user['user_uid'], $term->channal);
  365. if ($power < 20) {
  366. continue;
  367. }
  368. } else {
  369. continue;
  370. }
  371. }
  372. $count += $term->delete();
  373. // 删除cache
  374. $this->deleteCache($term);
  375. }
  376. } else {
  377. $arrId = json_decode($request->input('id'), true);
  378. foreach ($arrId as $key => $id) {
  379. // code...
  380. $term = DhammaTerm::where('id', $id)
  381. ->where('owner', $user['user_uid']);
  382. $result = $term->delete();
  383. $this->deleteCache($term);
  384. if ($result) {
  385. $count++;
  386. }
  387. }
  388. }
  389. return $this->ok($count);
  390. }
  391. }