2
0

SentenceApi.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Models\Sentence;
  7. use App\Models\SentHistory;
  8. use App\Models\Channel;
  9. use App\Http\Api\ShareApi;
  10. class SentenceApi{
  11. protected $auth = false;
  12. protected $channel = null;
  13. public function auth($channelId,$userId){
  14. $channel = Channel::where('uid',$channelId)->first();
  15. if(!$channel){
  16. return false;
  17. }
  18. if($channel->owner_uid !== $userId){
  19. //判断是否为协作
  20. $power = ShareApi::getResPower($userId,$channel->uid,2);
  21. if($power < 20){
  22. return false;
  23. }
  24. }
  25. $this->channel = $channel;
  26. $this->auth = true;
  27. return true;
  28. }
  29. public function store($sent,$user,$copy=false){
  30. $row = Sentence::firstOrNew([
  31. "book_id"=>$sent['book_id'],
  32. "paragraph"=>$sent['paragraph'],
  33. "word_start"=>$sent['word_start'],
  34. "word_end"=>$sent['word_end'],
  35. "channel_uid"=>$this->channel->uid,
  36. ],[
  37. "id"=>app('snowflake')->id(),
  38. "uid"=>Str::uuid(),
  39. ]);
  40. $row->content = $sent['content'];
  41. $row->strlen = mb_strlen($sent['content'],"UTF-8");
  42. $row->language = $this->channel->lang;
  43. $row->status = $this->channel->status;
  44. if($copy){
  45. //复制句子,保留原作者信息
  46. $row->editor_uid = $sent["editor_uid"];
  47. $row->acceptor_uid = $user["user_uid"];
  48. $row->pr_edit_at = $sent["updated_at"];
  49. }else{
  50. $row->editor_uid = $user["user_uid"];
  51. $row->acceptor_uid = null;
  52. $row->pr_edit_at = null;
  53. }
  54. $row->create_time = time()*1000;
  55. $row->modify_time = time()*1000;
  56. $row->save();
  57. //保存历史记录
  58. if($copy){
  59. $this->saveHistory($row->uid,$sent["editor_uid"],$sent['content']);
  60. }else{
  61. $this->saveHistory($row->uid,$user["user_uid"],$sent['content']);
  62. }
  63. //清除缓存
  64. $sentId = "{$sent['book_id']}-{$sent['paragraph']}-{$sent['word_start']}-{$sent['word_end']}";
  65. $hKey = "/sentence/res-count/{$sentId}/";
  66. Redis::del($hKey);
  67. }
  68. private function saveHistory($uid,$editor,$content){
  69. $newHis = new SentHistory;
  70. $newHis->id = app('snowflake')->id();
  71. $newHis->sent_uid = $uid;
  72. $newHis->user_uid = $editor;
  73. if(empty($content)){
  74. $newHis->content = "";
  75. }else{
  76. $newHis->content = $content;
  77. }
  78. $newHis->create_time = time()*1000;
  79. $newHis->save();
  80. }
  81. }