TermExportController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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->get("view")) {
  30. case 'channel':
  31. # code...
  32. $rows = DhammaTerm::where('channal',$request->get("id"))->cursor();
  33. break;
  34. case 'studio':
  35. # code...
  36. $studioId = StudioApi::getIdByName($request->get("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. $user = AuthApi::current($request);
  101. if(!$user){
  102. return $this->error(__('auth.failed'),401,401);
  103. }
  104. /**
  105. * 判断是否有权限
  106. */
  107. switch ($request->get('view')) {
  108. case 'channel':
  109. # 向channel里面导入,忽略源数据的channel id 和 owner 都设置为这个channel 的
  110. $channel = ChannelApi::getById($request->get('id'));
  111. $owner_id = $channel['studio_id'];
  112. if($owner_id !== $user["user_uid"]){
  113. //判断是否为协作
  114. $power = ShareApi::getResPower($user["user_uid"],$request->get('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->get('name'));
  124. if(!$owner_id){
  125. return $this->error('no studio name',403,403);
  126. }
  127. break;
  128. }
  129. $message = "";
  130. $filename = $request->get('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->get('view')) {
  160. case 'channel':
  161. # 向channel里面导入,忽略源数据的channel id 和 owner 都设置为这个channel 的
  162. $query['channal'] = $request->get('id');
  163. $channelId = $request->get('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. * @param \Illuminate\Http\Request $request
  268. * @param \App\Models\DhammaTerm $dhammaTerm
  269. * @return \Illuminate\Http\Response
  270. */
  271. public function update(Request $request, DhammaTerm $dhammaTerm)
  272. {
  273. //
  274. }
  275. /**
  276. * Remove the specified resource from storage.
  277. *
  278. * @param \App\Models\DhammaTerm $dhammaTerm
  279. * @return \Illuminate\Http\Response
  280. */
  281. public function destroy(DhammaTerm $dhammaTerm)
  282. {
  283. //
  284. }
  285. }