2
0

TermExportController.php 11 KB

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