2
0

TermExportController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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\Models\DhammaTerm;
  7. use App\Services\AuthService;
  8. use App\Tools\Tools;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Support\Facades\Redis;
  12. use Illuminate\Support\Facades\Storage;
  13. use Illuminate\Support\Str;
  14. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  15. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  16. class TermExportController extends Controller
  17. {
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Response
  22. */
  23. public function index(Request $request)
  24. {
  25. $user = AuthService::current($request);
  26. if (! $user) {
  27. return $this->error(__('auth.failed'));
  28. }
  29. // TODO 判断是否有导出权限
  30. switch ($request->input('view')) {
  31. case 'channel':
  32. // code...
  33. $rows = DhammaTerm::where('channal', $request->input('id'))->cursor();
  34. break;
  35. case 'studio':
  36. // code...
  37. $studioId = StudioApi::getIdByName($request->input('name'));
  38. $rows = DhammaTerm::where('owner', $studioId)->cursor();
  39. break;
  40. default:
  41. $this->error('no view');
  42. break;
  43. }
  44. $spreadsheet = new Spreadsheet;
  45. $activeWorksheet = $spreadsheet->getActiveSheet();
  46. $activeWorksheet->setCellValue('A1', 'id');
  47. $activeWorksheet->setCellValue('B1', 'word');
  48. $activeWorksheet->setCellValue('C1', 'meaning');
  49. $activeWorksheet->setCellValue('D1', 'other_meaning');
  50. $activeWorksheet->setCellValue('E1', 'note');
  51. $activeWorksheet->setCellValue('F1', 'tag');
  52. $activeWorksheet->setCellValue('G1', 'language');
  53. $activeWorksheet->setCellValue('H1', 'channel_id');
  54. $currLine = 2;
  55. foreach ($rows as $key => $row) {
  56. // code...
  57. $activeWorksheet->setCellValue("A{$currLine}", $row->guid);
  58. $activeWorksheet->setCellValue("B{$currLine}", $row->word);
  59. $activeWorksheet->setCellValue("C{$currLine}", $row->meaning);
  60. $activeWorksheet->setCellValue("D{$currLine}", $row->other_meaning);
  61. $activeWorksheet->setCellValue("E{$currLine}", $row->note);
  62. $activeWorksheet->setCellValue("F{$currLine}", $row->tag);
  63. $activeWorksheet->setCellValue("G{$currLine}", $row->language);
  64. $activeWorksheet->setCellValue("H{$currLine}", $row->channal);
  65. $currLine++;
  66. }
  67. $writer = new Xlsx($spreadsheet);
  68. $fId = Str::uuid();
  69. $filename = storage_path("app/tmp/{$fId}");
  70. $writer->save($filename);
  71. $key = "download/tmp/{$fId}";
  72. Redis::set($key, file_get_contents($filename));
  73. Redis::expire($key, 300);
  74. unlink($filename);
  75. return $this->ok(['uuid' => $fId, 'filename' => 'term.xlsx', 'type' => 'application/vnd.ms-excel']);
  76. }
  77. /**
  78. * Store a newly created resource in storage.
  79. *
  80. * @return Response
  81. */
  82. public function store(Request $request)
  83. {
  84. //
  85. }
  86. /**
  87. * Display the specified resource.
  88. *
  89. * @return Response
  90. */
  91. public function show(string $downloadId)
  92. {
  93. header('Content-Type: application/vnd.ms-excel');
  94. header('Content-Disposition: attachment; filename="term.xlsx"');
  95. $content = Redis::get("download/tmp/{$downloadId}");
  96. file_put_contents('php://output', $content);
  97. }
  98. public function import(Request $request)
  99. {
  100. $user = AuthService::current($request);
  101. if (! $user) {
  102. return $this->error(__('auth.failed'), 401, 401);
  103. }
  104. /**
  105. * 判断是否有权限
  106. */
  107. switch ($request->input('view')) {
  108. case 'channel':
  109. // 向channel里面导入,忽略源数据的channel id 和 owner 都设置为这个channel 的
  110. $channel = ChannelApi::getById($request->input('id'));
  111. $owner_id = $channel['studio_id'];
  112. if ($owner_id !== $user['user_uid']) {
  113. // 判断是否为协作
  114. $power = ShareApi::getResPower($user['user_uid'], $request->input('id'));
  115. if ($power < 20) {
  116. return $this->error(__('auth.failed'), 403, 403);
  117. }
  118. }
  119. $language = $channel['lang'];
  120. break;
  121. case 'studio':
  122. // 向 studio 里面导入,忽略源数据的 owner 但是要检测 channel id 是否有权限
  123. $owner_id = StudioApi::getIdByName($request->input('name'));
  124. if (! $owner_id) {
  125. return $this->error('no studio name', 403, 403);
  126. }
  127. break;
  128. }
  129. $message = '';
  130. $filename = $request->input('filename');
  131. if (Storage::missing($filename)) {
  132. return $this->error('no file '.$filename);
  133. }
  134. $contents = Storage::get($filename);
  135. $fId = Str::uuid();
  136. $tmpFile = storage_path("app/tmp/{$fId}.xlsx");
  137. $ok = file_put_contents($tmpFile, $contents);
  138. if ($ok === false) {
  139. return $this->error('create tmp file fail '.$tmpFile, 500, 500);
  140. }
  141. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  142. $reader->setReadDataOnly(true);
  143. $spreadsheet = $reader->load($tmpFile);
  144. $activeWorksheet = $spreadsheet->getActiveSheet();
  145. $currLine = 2;
  146. $countFail = 0;
  147. do {
  148. // code...
  149. $id = $activeWorksheet->getCell("A{$currLine}")->getValue();
  150. $word = $activeWorksheet->getCell("B{$currLine}")->getValue();
  151. $meaning = $activeWorksheet->getCell("C{$currLine}")->getValue();
  152. $other_meaning = $activeWorksheet->getCell("D{$currLine}")->getValue();
  153. $note = $activeWorksheet->getCell("E{$currLine}")->getValue();
  154. $tag = $activeWorksheet->getCell("F{$currLine}")->getValue();
  155. $language = $activeWorksheet->getCell("G{$currLine}")->getValue();
  156. $channel_id = $activeWorksheet->getCell("H{$currLine}")->getValue();
  157. $query = ['word' => $word, 'tag' => $tag];
  158. $channelId = null;
  159. switch ($request->input('view')) {
  160. case 'channel':
  161. // 向channel里面导入,忽略源数据的channel id 和 owner 都设置为这个channel 的
  162. $query['channal'] = $request->input('id');
  163. $channelId = $request->input('id');
  164. break;
  165. case 'studio':
  166. // 向 studio 里面导入,忽略源数据的owner 但是要检测 channel id 是否有权限
  167. $query['owner'] = $owner_id;
  168. if (! empty($channel_id)) {
  169. // 有channel 数据,查看是否在studio中
  170. $channel = ChannelApi::getById($channel_id);
  171. if ($channel === false) {
  172. $message .= "没有查到版本信息:{$channel_id} - {$word}\n";
  173. $currLine++;
  174. $countFail++;
  175. continue 2;
  176. }
  177. if ($owner_id != $channel['studio_id']) {
  178. $message .= "版本不在studio中:{$channel_id} - {$word}\n";
  179. $currLine++;
  180. $countFail++;
  181. continue 2;
  182. }
  183. $query['channal'] = $channel_id;
  184. $channelId = $channel_id;
  185. }
  186. // code...
  187. break;
  188. }
  189. if (empty($id) && empty($word)) {
  190. break;
  191. }
  192. // 查询此id是否有旧数据
  193. if (! empty($id)) {
  194. $oldRow = DhammaTerm::find($id);
  195. // TODO 有 id 无 word 删除数据
  196. if (empty($word)) {
  197. // 查看权限
  198. if ($oldRow->owner !== $user['user_uid']) {
  199. if (! empty($oldRow->channal)) {
  200. // 看是否为协作
  201. $power = ShareApi::getResPower($user['user_uid'], $oldRow->channal);
  202. if ($power < 20) {
  203. $message .= "无删除权限:{$id} - {$word}\n";
  204. $currLine++;
  205. $countFail++;
  206. continue;
  207. }
  208. } else {
  209. $message .= "无删除权限:{$id} - {$word}\n";
  210. $currLine++;
  211. $countFail++;
  212. continue;
  213. }
  214. }
  215. // 删除
  216. $oldRow->delete();
  217. $currLine++;
  218. continue;
  219. }
  220. } else {
  221. $oldRow = null;
  222. }
  223. // 查询是否跟已有数据重复
  224. $row = DhammaTerm::where($query)->first();
  225. if (! $row) {
  226. // 不重复
  227. if (isset($oldRow) && $oldRow) {
  228. // 找到旧的记录-修改旧数据
  229. $row = $oldRow;
  230. } else {
  231. // 没找到旧的记录-新建
  232. $row = new DhammaTerm;
  233. $row->id = app('snowflake')->id();
  234. $row->guid = Str::uuid();
  235. $row->word = $word;
  236. $row->create_time = time() * 1000;
  237. }
  238. } else {
  239. // 重复-如果与旧的id不同,报错
  240. if (isset($oldRow) && $oldRow && $row->guid !== $id) {
  241. $message .= "重复的数据:{$id} - {$word}\n";
  242. $currLine++;
  243. $countFail++;
  244. continue;
  245. }
  246. }
  247. $row->word = $word;
  248. $row->word_en = Tools::getWordEn($word);
  249. $row->meaning = $meaning;
  250. $row->other_meaning = $other_meaning;
  251. $row->note = $note;
  252. $row->tag = $tag;
  253. $row->language = $language;
  254. $row->channal = $channelId;
  255. $row->editor_id = $user['user_id'];
  256. $row->owner = $owner_id;
  257. $row->modify_time = time() * 1000;
  258. $row->save();
  259. $currLine++;
  260. } while (true);
  261. unlink($tmpFile);
  262. return $this->ok(['success' => $currLine - 2 - $countFail, 'fail' => ($countFail)], $message);
  263. }
  264. /**
  265. * Update the specified resource in storage.
  266. *
  267. * @return Response
  268. */
  269. public function update(Request $request, DhammaTerm $dhammaTerm)
  270. {
  271. //
  272. }
  273. /**
  274. * Remove the specified resource from storage.
  275. *
  276. * @return Response
  277. */
  278. public function destroy(DhammaTerm $dhammaTerm)
  279. {
  280. //
  281. }
  282. }