Work.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * User raingad
  4. * Date 2021/8/2 15:31
  5. */
  6. namespace app\job;
  7. use app\common\controller\Upload;
  8. use app\enterprise\model\{Group,GroupUser,User,Message,File};
  9. use Exception;
  10. use think\queue\Job;
  11. use think\facade\Filesystem;
  12. class Work
  13. {
  14. // 允许的队列
  15. protected $actions=[
  16. 'createAvatar', //创建、更新头像
  17. 'clearFiles', //清理文件
  18. 'clearVoice', //清理音频
  19. 'setIsRead', //设置消息为已读
  20. 'forwardMessage' //转发消息
  21. ];
  22. // 执行队列
  23. public function fire(Job $job, $data)
  24. {
  25. $action = $data['action'] ?? '';
  26. //如果重试超过3次,或者没有action或者没有type,直接删除队列
  27. if ($job->attempts() > 3 || !in_array($action,$this->actions)) {
  28. $job->delete();
  29. return;
  30. }
  31. try {
  32. // 根据不同的方法执行处理数据
  33. $res=$this->$action($data);
  34. if($res){
  35. print("<info>".$action." is success</info> \n");
  36. }else{
  37. print("<info>".$action." is error</info> \n");
  38. }
  39. } catch (\Exception $e) {
  40. print("<info>".$action." error: ".$e->getMessage()."</info> \n");
  41. }
  42. $job->delete();
  43. }
  44. // 创建头像
  45. public function createAvatar($data)
  46. {
  47. $group_id=$data['group_id'] ?? 0;
  48. if(!$group_id){ return false;}
  49. $userList = GroupUser::where(['group_id' => $group_id,'status'=>1])->limit(9)->column('user_id');
  50. $userList = User::where('user_id', 'in', $userList)->select()->toArray();
  51. $imgList = [];
  52. $dirPath = app()->getRootPath() . 'public/temp';
  53. foreach ($userList as $k => $v) {
  54. if ($v['avatar']) {
  55. $imgList[] = avatarUrl($v['avatar'], $v['realname'], $v['user_id']);
  56. } else {
  57. $imgList[] = circleAvatar($v['realname'], 80, $v['user_id'], 1, $dirPath);
  58. }
  59. }
  60. $groupId = 'group_' . $group_id;
  61. $path = $dirPath . '/' . $groupId . '.jpg';
  62. $a = getGroupAvatar($imgList, 1, $path);
  63. $url = '';
  64. if ($a) {
  65. $upload = new Upload();
  66. $newPath = $upload->uploadLocalAvatar($path, [], $groupId);
  67. if ($newPath) {
  68. Group::where('group_id', $group_id)->update(['avatar' => $newPath]);
  69. $url = avatarUrl($newPath);
  70. }
  71. }
  72. $files = glob($dirPath . '/*'); // 获取目录下所有文件路径
  73. foreach ($files as $file) {
  74. if (is_file($file)) { // 如果是文件则删除
  75. unlink($file);
  76. }
  77. }
  78. wsSendMsg($group_id,"setManager",['group_id'=>'group-'.$group_id,'avatar'=>$url],1);
  79. return true;
  80. }
  81. // 清理除了音频相关的文件
  82. public function clearFiles($data){
  83. $fileIds = $data['fileIds'];
  84. $count=0;
  85. foreach($fileIds as $fileId){
  86. $message=Message::where(['file_id'=>$fileId])->count();
  87. // 如果还有消息,就不删除文件
  88. if($message>0){
  89. continue;
  90. }
  91. $file=File::find($fileId);
  92. if($file){
  93. $MD5=$file->md5;
  94. $src=$file->src;
  95. $file->delete();
  96. // 查询相同文件
  97. $sameFile=File::where(['md5'=>$MD5])->count();
  98. // 如果有相同的文件,则不删除原件
  99. if($sameFile){
  100. continue;
  101. }
  102. $count++;
  103. // 删除源文件
  104. $disk=env('filesystem.driver','local');
  105. Filesystem::disk($disk)->delete($src);
  106. }
  107. print("<info>成功删除".$count."个文件!</info> \n");
  108. return true;
  109. }
  110. }
  111. // 清理除了音频相关的文件
  112. public function clearVoice($data){
  113. $list = $data['list'];
  114. foreach($list as $content){
  115. $src = str_encipher($content,false);
  116. // 解密文件路径,删除源文件
  117. $disk=env('filesystem.driver','local');
  118. Filesystem::disk($disk)->delete($src);
  119. }
  120. print("<info>成功删除".count($list)."个音频文件!</info> \n");
  121. return true;
  122. }
  123. // 设置已读
  124. public function setIsRead($data){
  125. $is_group=$data['is_group'];
  126. $to_user= $data['to_user'];
  127. $messages=$data['messages'];
  128. $user_id=$data['user_id'];
  129. if ($is_group==1) {
  130. $toContactId = explode('-', $to_user)[1];
  131. // 将@消息放到定时任务中逐步清理
  132. if($messages){
  133. Message::setAtRead($messages,$user_id);
  134. }
  135. // 更新群里面我的所有未读消息为0
  136. GroupUser::editGroupUser(['user_id' => $user_id, 'group_id' => $toContactId], ['unread' => 0]);
  137. } else if($is_group==0) {
  138. $chat_identify = chat_identify($user_id, $to_user);
  139. // 更新我的未读消息为0
  140. Message::update(['is_read' => 1], [['chat_identify', '=', $chat_identify], ['to_user', '=', $user_id]]);
  141. // 告诉对方我阅读了消息
  142. wsSendMsg($to_user, 'readAll', ['toContactId' => $user_id]);
  143. }
  144. return true;
  145. }
  146. // 转发消息
  147. public function forwardMessage($data){
  148. try{
  149. $is_group=0;
  150. $error=0;
  151. $simpleChat=$data['config']['chatInfo']['simpleChat'] ?? 1;
  152. $userInfo=$data['userInfo'];
  153. $userIds=$data['user_ids'];
  154. foreach($userIds as $k=>$v){
  155. $msgInfo=$data['message'];
  156. if(strpos($v,'group')!==false){
  157. $is_group=1;
  158. }else{
  159. $is_group=0;
  160. }
  161. if($is_group==0 && $simpleChat==0){
  162. $error++;
  163. continue;
  164. }
  165. $msgInfo['id']=\utils\Str::getUuid();
  166. $msgInfo['status']='successd';
  167. $msgInfo['user_id']=$userInfo['user_id'];
  168. $msgInfo['sendTime']=time()*1000;
  169. $msgInfo['toContactId']=$v;
  170. $msgInfo['content']=str_encipher($msgInfo['content'],false);
  171. $msgInfo['fromUser']=[
  172. 'id'=>$userInfo['user_id'],
  173. 'avatar'=>avatarUrl($userInfo['avatar'],$userInfo['realname'],$userInfo['user_id'],120),
  174. 'displayName'=>$userInfo['realname']
  175. ];
  176. $msgInfo['is_group']=$is_group;
  177. $message=new Message();
  178. $msgInfo['is_forward']=1;
  179. $isSend=$message->sendMessage($msgInfo,$data['config']);
  180. if(!$isSend){
  181. $error++;
  182. }
  183. }
  184. $count=count($userIds);
  185. print("<info>成功转发".($count - $error)."条消息</info> \n");
  186. return true;
  187. }catch(\Exception $e){
  188. print("<info>转发失败!</info> \n".$e->getMessage().$e->getLine());
  189. }
  190. }
  191. }