love_php/app/Http/Controllers/DriftingBottlesController.php

444 lines
18 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\BlessingUsers;
use App\Models\DriftingBottle;
use App\Models\BottleChatRecord;
use App\Models\BottleReceivingRecord;
use App\Http\Controllers\Controller;
use App\Models\BannedHistory;
use EasyWeChat\Factory;
use App\Models\Channels;
use App\Models\ComplaintHistory;
use Illuminate\Pagination\LengthAwarePaginator;
class DriftingBottlesController extends Controller
{
//用户列表
public function userList(Request $request)
{
$keyword = trim($request->keyword);
if($request->has('channel_id')){
$result = BlessingUsers::where('channel_id',$request->channel_id);
if($keyword)
$result = $result->where('nickname','like','%'.$keyword.'%');
$result = $result->orderby('id','desc')->paginate();
}else{
if($keyword){
$result = BlessingUsers::where('nickname','like','%'.$keyword.'%')->orderby('id','desc')->paginate();
}else{
$result = BlessingUsers::orderby('id','desc')->paginate();
}
}
foreach ($result as $key => $value) {
$value->receive_num = BottleReceivingRecord::where('receiving_user_id',$value->id)->count();
$value->send_num =DriftingBottle::where('b_id',$value->id)->count();
}
return $this->success('ok',$result);
}
// 用户发送的漂流瓶
public function myBottle(Request $request)
{
$user_id = $request->user_id;
$result = DriftingBottle::where('b_id',$user_id)->paginate();
return $this->success('ok',$result);
}
// 用户获取的漂流瓶
public function getBottle(Request $request)
{
$user_id = $request->user_id;
$result = BottleReceivingRecord::where('receiving_user_id',$user_id)->paginate();
foreach ($result as $key => $value) {
$DriftingBottle = DriftingBottle::where('id',$value->bottle_id)->first();
$value->content = $DriftingBottle->content;
$value->type = $DriftingBottle->type;
$value->send_user = $DriftingBottle->user;
}
return $this->success('ok',$result);
}
// 聊天记录
public function BottleRecord(Request $request)
{
$user_id = $request->user_id;
$sql = BottleChatRecord::orderBy('id','DESC')->limit(1000);
$user = BlessingUsers::where('id',$user_id)->first();
if(!$user){
return $this->failure('用户ID错误');
}
$result= BottleChatRecord::where(function($query) use($user){
$query->where('b_id',$user->id);
$query->orwhere('receiver',$user->id);
})->from(\DB::raw('('.$sql->toSql().') as a'))->groupBy('receiving_id')->get();
$data = [];
foreach ($result as $key => $value) {
$count = BottleChatRecord::where('receiving_id',$value->receiving_id)->count();
if($count<2&&$value->b_id==$user->id)
continue;
$user = BlessingUsers::where('id',$value->b_id)->first();
$value->nickname = $user->nickname??'';
$value->pic = $user->pic??'';
array_push($data,$value);
}
$page = $request->page ?? 1;
$perPage = 15;
$offset = ($page * $perPage) - $perPage;
$result = new LengthAwarePaginator(
array_slice($data, $offset, $perPage),
count($data),
$perPage,
$page,
['path' => $request->url(), 'query' => $request->query()]
);
return $this->success('ok',$result);
}
// 聊天详情
public function RecordDetail(Request $request,$id)
{
$user_id = $request->user_id;
$user = BlessingUsers::where('id',$user_id)->first();
BottleChatRecord::where('receiving_id',$id)->where('receiver',$user->id)->update(['status'=>1]);
$result = BottleChatRecord::where('receiving_id',$id)->orderBy('id','desc')->get();
foreach ($result as $key => $value) {
$user = BlessingUsers::where('id',$value->b_id)->first();
$value->user_name = $user->nickname;
$value->user_pic = $user->pic;
}
return $this->success('ok',$result);
}
//漂流瓶--举报列表
public function blessComplaintHistories(Request $request){
try {
$keyword = $request->keyword;
$status = $request->status ?? 0;//0未跟进 1完成跟进 2跟进中
$histories = ComplaintHistory::with('complaintUser','complaintedUser')->where('type','bless');
if($keyword){
$keyword = trim($keyword);
$user_ids = BlessingUsers::where('nickname','like','%'.$keyword.'%')->pluck('id');
$histories = $histories->whereIn('complaint_id',$user_ids);
}
$histories = $histories->where('status',$status)->orderBy('id','desc')->paginate();
$time = date('Y-m-d H:i:s');
foreach ($histories as $key => $history) {
$ban_send = 0;
$ban_chat = 0;
$ban_risk = 0;
$history->label = json_decode($history->label);
if($history->remark && $history->remark != 'null'){
$history->remark = json_decode($history->remark,true);
}
//判断用户是否被封禁
$send_history = BannedHistory::where('user_id',$history->complaint_id)->where('type','bless_send')->get();
foreach ($send_history as $key => $value) {
if($value->start_time < $time && $value->end_time > $time) $ban_send = 1;
}
$chat_history = BannedHistory::where('user_id',$history->complaint_id)->where('type','bless_chat')->get();
foreach ($chat_history as $key => $value) {
if($value->start_time < $time && $value->end_time > $time) $ban_chat = 1;
}
$risk_history = BannedHistory::where('user_id',$history->complaint_id)->where('type','risk_warning')->get();
foreach ($risk_history as $key => $value) {
if($value->start_time < $time && $value->end_time > $time) $ban_risk = 1;
}
$history->ban_send = $ban_send;
$history->ban_chat = $ban_chat;
$history->ban_risk = $ban_risk;
}
return $this->success('ok',$histories);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器开小差,请稍后再试');
}
}
//漂流瓶--祝福列表
public function driftBottles(Request $request){
try {
$keyword = $request->keyword;
$type = $request->type ?? 'all';
$bottles = DriftingBottle::with('user')->select('id','content','b_id','type','duration','created_at','updated_at');
if($keyword){
$keyword = trim($keyword);
$bottles = $bottles->whereHas('user',function($sql) use($keyword){
$sql->where('nickname','like','%'.$keyword.'%');
});
}
if($type != 'all'){
$bottles = $bottles->where('type',$type);
}
$bottles = $bottles->orderBy('id','desc')->paginate();
return $this->success('ok',$bottles);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器开小差,请稍后再试');
}
}
//漂流瓶--祝福详情
public function blessBottle(Request $request,$bottle_id){
try {
$bottle = DriftingBottle::with('user')->where('id',$bottle_id)->first();
if(!$bottle) return $this->failure('该祝福不存在或被删除');
//接收该漂流瓶的人
$recives = BottleReceivingRecord::with('reciver')->where('bottle_id',$bottle_id)->orderBy('id',"desc")->paginate();
return $this->success('ok',compact('bottle','recives'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器开小差,请稍后再试');
}
}
//漂流瓶--祝福聊天记录
public function blessChatRecord(Request $request){
try {
$bottle_id = $request->bottle_id;
$receiving_id = $request->receiving_id;
//发送祝福人id
$b_id = DriftingBottle::where('id',$bottle_id)->value('b_id');
$user = BlessingUsers::where('id',$b_id)->first();
if(empty($user)) return $this->failure('发送该祝福人不存在或已被删除');
$records = BottleChatRecord::where(['bottle_id'=>$bottle_id,'receiving_id'=>$receiving_id])->orderBy('id','asc')->paginate();
foreach ($records as $key => $value) {
$b_user = BlessingUsers::where('id',$value->b_id)->first();
$value->user_name = $b_user->nickname;
$value->user_pic = $b_user->pic;
$value->identity = 'user';
$value->duration = 0;
if($value->type!='chat')
$value->duration = DriftingBottle::where('id',$value->bottle_id)->value('duration')?:0;
if($value->b_id!=$user->id)
$value->identity = 'other_user';
}
return $this->success('ok',$records);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息,请稍后再试');
}
}
//禁止用户发送祝福 聊天
public function banBlessUser(Request $request,$b_user_id){
try {
$user = BlessingUsers::where('id',$b_user_id)->first();
$start_time = $request->start_time;
$end_time = $request->end_time;
if(!$start_time || !$end_time) return $this->failure('请选择封禁时间');
$content = $request->input('content');
$pics = $request->pics ??[];
$ban_type = $request->ban_type??'bless_send';//bless_send 禁止发送祝福 bless_chat禁止聊天 risk_warning风险警告
if(!$user) return $this->failure('该用户不存在');
if(!$content) return $this->failure('请输入封禁理由');
BannedHistory::create([
'user_id'=>$b_user_id,
'admin_user_id'=>auth()->id(),
'type'=>$ban_type,
'start_time'=>$start_time,
'end_time'=>$end_time,
'content'=>$content,
'pics'=>json_encode($pics),
]);
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器开小差,请稍后再试');
}
}
//解封用户
public function cancelBanBlessUser(Request $request,$b_user_id){
try {
$content = $request->input('content');
if(!$content) return $this->failure('请输入解封理由');
$pics = $request->pics??[];
$type = $request->type;
BannedHistory::where('user_id',$b_user_id)->where('type',$type)->delete();
//创建解封记录
BannedHistory::create([
'user_id'=>$b_user_id,
'admin_user_id'=>auth()->id(),
'type'=>'cancel_ban_'.$type,
'start_time'=>date('Y-m-d H:i:s'),
'end_time'=>date('Y-m-d H:i:s'),
'content'=>$content,
'pics'=>json_encode($pics),
]);
return $this->success('ok');
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器开小差,请稍后再试');
}
}
//用户封禁记录
public function blessUserBanHistories(Request $request,$b_user_id){
try {
//bless_send:禁止发祝福,bless_chat:禁止聊天,cancel_ban_bless_chat:解除禁止聊天,cancel_ban_bless_send:解除禁止发祝福 risk_warning风险警告
$arr = ['bless_send','bless_chat','cancel_ban_bless_chat','cancel_ban_bless_send','risk_warning','cancel_ban_risk_warning'];
$histories = BannedHistory::withTrashed()->with('operator')->whereIn('type',$arr)->where('user_id',$b_user_id)->orderBy('id','desc')->get();
foreach ($histories as $key => $history) {
$history->pics = json_decode($history->pics,true);
}
return $this->success('ok',$histories);
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器开小差,请稍后再试');
}
}
// 创建渠道
public function addChannel(Request $request)
{
$channel = new Channels();
$channel->title = $request->title;
$channel->type= 'bless';
$channel->save();
// $share_url = '/pages/tabBar/home';
// $pre = 'channel='.$channel->id;
// $channel->share_url = $this->getminiQrcode($share_url,$pre);
$channel->share_qr_code = $this->ShareBlessMp( $channel);
$channel->save();
return $this->success('ok',$channel);
}
// 渠道列表
public function ChannelList(Request $request)
{
$result = Channels::orderBy('id','desc')->paginate();
foreach ($result as $key => $value) {
$value->user_num = BlessingUsers::where('channel_id',$value->id)->count();
}
return $this->success('ok',$result);
}
// 用户详情
public function User(Request $request,$b_user_id)
{
$user = BlessingUsers::where('id',$b_user_id)->first();
$user->receive_num = BottleReceivingRecord::where('receiving_user_id',$user->id)->count();
$user->send_num =DriftingBottle::where('b_id',$user->id)->count();
//检查用户是否处于被封禁状态
$time = date('Y-m-d H:i:s');
$ban_send = 0;
$ban_chat = 0;
$ban_risk = 0;
//判断用户是否被封禁
$send_history = BannedHistory::where('user_id',$b_user_id)->where('type','bless_send')->get();
foreach ($send_history as $key => $value) {
if($value->start_time < $time && $value->end_time > $time) $ban_send = 1;
}
$chat_history = BannedHistory::where('user_id',$b_user_id)->where('type','bless_chat')->get();
foreach ($chat_history as $key => $value) {
if($value->start_time < $time && $value->end_time > $time) $ban_chat = 1;
}
$risk_history = BannedHistory::where('user_id',$b_user_id)->where('type','risk_warning')->get();
foreach ($risk_history as $key => $value) {
if($value->start_time < $time && $value->end_time > $time) $ban_risk = 1;
}
$user->ban_send = $ban_send;
$user->ban_chat = $ban_chat;
$user->ban_risk = $ban_risk;
return $this->success('ok',$user);
}
// 生成二维码
public function getminiQrcode($path,$pre)
{
$app_id = 'wx1d3301c485402a21';
$secret = '0bf57514998d4b8cfd602d8ca49c1fd1';
$config = [
'app_id' => $app_id,
'secret' => $secret,
// 下面为可选项
// 指定 API 调用返回结果的类型array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => storage_path('logs/wechat.log'), //这个必须要有,要不调试有问题,你都会找不到原因
],
];
$app = Factory::miniProgram($config)->access_token;
$token = $app->getToken(true);
$data = [];
$data['jump_wxa']['path'] = $path;
$data['jump_wxa']['query'] = $pre ;
$data = json_encode($data);
$wechaturl = 'https://api.weixin.qq.com/wxa/generatescheme?access_token=' . $token['access_token'];
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_URL, $wechaturl);
// curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$" . $wechaturl, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
$result = json_decode($result, true);
$jump_url = '';
if(array_key_exists('openlink',$result)){
$jump_url = $result['openlink'];
}
return $jump_url;
}
/*
* 生成用户分享海报 跳转小程序
*/
public function ShareBlessMp($channel)
{
$app_id = 'wx1d3301c485402a21';
$secret = '0bf57514998d4b8cfd602d8ca49c1fd1';
$config = [
'app_id' => $app_id,
'secret' => $secret,
// 下面为可选项
// 指定 API 调用返回结果的类型array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => storage_path('logs/wechat.log'), //这个必须要有,要不调试有问题,你都会找不到原因
],
];
$app = Factory::miniProgram($config);
$data = [];
$data['is_hyaline'] = true;
$response = $app->app_code->get('/pages/tabBar/home?channel='.$channel->id, $data);
$path = time().'activity_qrocde.png';
$response->saveAs(storage_path('qrcode'), $path);
$qrcode_path = storage_path("qrcode/".$path);
$path = date('Y') . date('m') . "/" . date('d');
$rand = \CommonUtilsService::getTradeNO();
$object = $path . "/" . $rand.'.png';
$ossClient = new \OSS\OssClient(config('alioss.id'), config('alioss.secret'), config('alioss.host'));
$ossClient->uploadFile(config('alioss.buckets.picture'), $object, $qrcode_path);
$file_url = 'https://' . config('alioss.picture_domain') . '/' . $object;
return $file_url;
}
}