You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
9.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?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 Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class SupplyDemandController extends CommonController
{
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']);
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="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
]);
return $this->success('删除成功');
}
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'));
}
}