|
|
<?php
|
|
|
/**
|
|
|
* 供需
|
|
|
*/
|
|
|
|
|
|
namespace App\Http\Controllers\Mobile;
|
|
|
|
|
|
use App\Helpers\ResponseCode;
|
|
|
use App\Models\AppointmentConfig;
|
|
|
use App\Models\AppointmentType;
|
|
|
use App\Models\Banner;
|
|
|
use App\Models\Config;
|
|
|
use App\Models\Message;
|
|
|
use App\Models\SupplyDemand;
|
|
|
use App\Models\SupplyDemandKeep;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
class SupplyDemandController extends CommonController
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/mobile/supply-demand/index",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="供需列表",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="myself", in="query", @OA\Schema(type="integer"), required=true, description="是否只看自己的0否1是"),
|
|
|
* @OA\Parameter(name="type", in="query", @OA\Schema(type="integer"), required=true, description="类型"),
|
|
|
* @OA\Parameter(name="keyword", in="query", @OA\Schema(type="integer"), required=true, description="关键词"),
|
|
|
* @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), required=true, description="状态0待审核1通过2拒绝"),
|
|
|
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
|
|
|
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
|
|
|
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
|
|
|
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function index()
|
|
|
{
|
|
|
$all = request()->all();
|
|
|
$supplyDemands = SupplyDemand::where(function ($query) use ($all) {
|
|
|
if (isset($all['type'])) {
|
|
|
$query->where('type', $all['type']);
|
|
|
}
|
|
|
if (isset($all['status'])) {
|
|
|
$query->where('status', $all['status']);
|
|
|
}
|
|
|
if (isset($all['keyword'])) {
|
|
|
$query->where('content', 'like', '%' . $all['keyword'] . '%');
|
|
|
}
|
|
|
if (isset($all['myself']) && $all['myself'] == 1) {
|
|
|
$query->where('user_id', $this->getUserId());
|
|
|
}
|
|
|
})->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
|
|
|
->paginate($all['page_size'] ?? 20);
|
|
|
return $this->success(compact('supplyDemands'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/mobile/supply-demand/detail",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="详情",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=true, description="id"),
|
|
|
* @OA\Response(
|
|
|
* response=200,
|
|
|
* description="操作成功"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function detail()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'id.required' => 'Id必填',
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'id' => 'required'
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$detail = SupplyDemand::with([
|
|
|
'user' => function ($query) {
|
|
|
$query->select('id', 'nickname', 'name', 'headimgurl');
|
|
|
}
|
|
|
])->find($all['id']);
|
|
|
// 增加view_count
|
|
|
$detail->increment('view_count');
|
|
|
$detail->save();
|
|
|
return $this->success($detail);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/mobile/supply-demand/save",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="更新",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="integer"), required=false, description="需求供应表ID(存在则更新,不存在则新增)"),
|
|
|
* @OA\Parameter(name="title", in="query", @OA\Schema(type="string"), required=false, description="标题"),
|
|
|
* @OA\Parameter(name="supply_demand_type_id", in="query", @OA\Schema(type="integer"), required=false, description="分类ID"),
|
|
|
* @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), required=false, description="内容"),
|
|
|
* @OA\Parameter(name="tag", in="query", @OA\Schema(type="string"), required=false, description="标签"),
|
|
|
* @OA\Parameter(name="wechat", in="query", @OA\Schema(type="string"), required=false, description="微信号"),
|
|
|
* @OA\Parameter(name="mobile", in="query", @OA\Schema(type="string"), required=false, description="电话"),
|
|
|
* @OA\Parameter(name="email", in="query", @OA\Schema(type="string"), required=false, description="邮箱"),
|
|
|
* @OA\Parameter(name="expire_time", in="query", @OA\Schema(type="string"), required=false, description="过期时间"),
|
|
|
* @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), required=false, description="审核状态(0:待审核;1:通过;2:拒绝)"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function save()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
DB::beginTransaction();
|
|
|
try {
|
|
|
if (isset($all['id'])) {
|
|
|
$model = SupplyDemand::where('user_id', $this->getUserId())->find($all['id']);
|
|
|
if (empty($model)) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
|
|
|
}
|
|
|
} else {
|
|
|
$model = new SupplyDemand();
|
|
|
$all['user_id'] = $this->getUserId();
|
|
|
}
|
|
|
$model->fill($all);
|
|
|
$model->save();
|
|
|
DB::commit();
|
|
|
return $this->success($model);
|
|
|
} catch (\Exception $exception) {
|
|
|
DB::rollBack();
|
|
|
return $this->fail([$exception->getCode(), $exception->getMessage()]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/mobile/supply-demand/destroy",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="删除",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=true, description="id"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function destroy()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'id.required' => 'Id必填',
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'id' => 'required'
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$model = SupplyDemand::find($all['id']);
|
|
|
if (empty($model)) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '数据不存在']);
|
|
|
}
|
|
|
$model->delete();
|
|
|
return $this->success('删除成功');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/mobile/supply-demand/send-message",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="发送消息",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="supply_demand_id", in="query", @OA\Schema(type="string"), required=true, description="供需信息id"),
|
|
|
* @OA\Parameter(name="content", in="query", @OA\Schema(type="string"), required=true, description="内容"),
|
|
|
* @OA\Parameter(name="to_user_id", in="query", @OA\Schema(type="string"), required=true, description="接收人用户id"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function sendMessage()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'supply_demand_id.required' => '供需信息id必填',
|
|
|
'content.required' => '内容必填',
|
|
|
'to_user_id.required' => '接收人必填',
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'supply_demand_id' => 'required',
|
|
|
'content' => 'required',
|
|
|
'to_user_id' => 'required'
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$model = SupplyDemand::find($all['id']);
|
|
|
if (empty($model)) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '供需数据不存在']);
|
|
|
}
|
|
|
// 每天限制私信次数限制
|
|
|
$message_limit = Config::getValueByKey('message_limit');
|
|
|
$messageToday = Message::where('user_id', $this->getUserId())
|
|
|
->where('created_at', 'like', '%' . date('Y-m-d') . '%')
|
|
|
->get();
|
|
|
if ($messageToday > $message_limit) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '今天私信次数已达上限']);
|
|
|
}
|
|
|
// 有且仅有自己发的信息,则不能发再
|
|
|
$myMessage = Message::where('user_id', $this->getUserId())
|
|
|
->where('supply_demand_id', $all['supply_demand_id'])
|
|
|
->first();
|
|
|
// 对方的信息
|
|
|
$otherMessage = Message::where('user_id', $all['to_user_id'])
|
|
|
->where('supply_demand_id', $all['supply_demand_id'])
|
|
|
->first();
|
|
|
if ($myMessage && empty($otherMessage)) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '对方回复以后才可以再次发送消息']);
|
|
|
}
|
|
|
// 创建消息
|
|
|
Message::create([
|
|
|
'user_id' => $this->getUserId(),
|
|
|
'to_user_id' => $all['to_user_id'],
|
|
|
'supply_demand_id' => $all['supply_demand_id'],
|
|
|
'content' => $all['content'],
|
|
|
'is_read' => 0
|
|
|
]);
|
|
|
// 增加view_count
|
|
|
$model->increment('contact_count');
|
|
|
$model->save();
|
|
|
return $this->success('删除成功');
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/mobile/supply-demand/message-list",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="消息列表",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="supply_demand_id", in="query", @OA\Schema(type="string"), required=true, description="供需信息id"),
|
|
|
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
|
|
|
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
|
|
|
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
|
|
|
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
|
|
|
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function messageList()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'supply_demand_id.required' => '供需信息id必填'
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'supply_demand_id' => 'required',
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$model = SupplyDemand::find($all['id']);
|
|
|
if (empty($model)) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '供需数据不存在']);
|
|
|
}
|
|
|
$message = Message::with([
|
|
|
'user' => function ($query) {
|
|
|
$query->select('id', 'nickname', 'name', 'headimgurl');
|
|
|
},
|
|
|
'toUser' => function ($query) {
|
|
|
$query->select('id', 'nickname', 'name', 'headimgurl');
|
|
|
}
|
|
|
])->where(function ($query) use ($all) {
|
|
|
|
|
|
})->where('supply_demand_id', $all['supply_demand_id'])
|
|
|
->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
|
|
|
->paginate($all['page_size'] ?? 20);
|
|
|
return $this->success(compact('message'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Get(
|
|
|
* path="/api/mobile/supply-demand/keep-index",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="收藏列表",
|
|
|
* description="",
|
|
|
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
|
|
|
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
|
|
|
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
|
|
|
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
|
|
|
* @OA\Response(
|
|
|
* response="200",
|
|
|
* description="暂无"
|
|
|
* )
|
|
|
* )
|
|
|
*/
|
|
|
public function keepIndex()
|
|
|
{
|
|
|
$all = request()->all();
|
|
|
$supplyDemands = SupplyDemandKeep::where('user_id', $this->getUserId())
|
|
|
->orderBy($all['sort_name'] ?? 'id', $all['sort_type'] ?? 'desc')
|
|
|
->paginate($all['page_size'] ?? 20);
|
|
|
return $this->success(compact('supplyDemands'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/mobile/supply-demand/keep-supply-demand",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="收藏供需帖子",
|
|
|
* @OA\Parameter(name="supply_demand_id", in="query", @OA\Schema(type="integer"), required=true, description="供需帖子id"),
|
|
|
* @OA\Response(response=200, description="操作成功")
|
|
|
* )
|
|
|
*/
|
|
|
public function keepSupplyDemand()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'supply_demand_id.required' => '供需信息id必填'
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'supply_demand_id' => 'required',
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$data = [
|
|
|
'user_id' => $this->getUserId(),
|
|
|
'supply_demand_id' => $all['supply_demand_id']
|
|
|
];
|
|
|
SupplyDemandKeep::firstOrCreate($data);
|
|
|
return $this->success('收藏成功');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @OA\Post(
|
|
|
* path="/api/mobile/supply-demand/un-keep-supply-demand",
|
|
|
* tags={"小程序-供需"},
|
|
|
* summary="取消收藏供需帖子",
|
|
|
* @OA\Parameter(name="supply_demand_id", in="query", @OA\Schema(type="integer"), required=true, description="供需帖子id"),
|
|
|
* @OA\Response(response=200, description="操作成功")
|
|
|
* )
|
|
|
*/
|
|
|
public function unKeepSupplyDemand()
|
|
|
{
|
|
|
$all = \request()->all();
|
|
|
$messages = [
|
|
|
'supply_demand_id.required' => '供需信息id必填'
|
|
|
];
|
|
|
$validator = Validator::make($all, [
|
|
|
'supply_demand_id' => 'required',
|
|
|
], $messages);
|
|
|
if ($validator->fails()) {
|
|
|
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
|
|
|
}
|
|
|
$keep = SupplyDemandKeep::where('user_id', $this->getUserId())->where('supply_demand_id', $all['supply_demand_id'])->first();
|
|
|
if (empty($keep)) {
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '该信息没有收藏']);
|
|
|
}
|
|
|
$keep->delete();
|
|
|
return $this->success('取消收藏成功');
|
|
|
}
|
|
|
|
|
|
}
|