92 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			92 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
|  | <?php | ||
|  | namespace app\api\controller; | ||
|  | 
 | ||
|  | use app\common\basics\Api; | ||
|  | use app\common\model\Demand As thisModel; | ||
|  | use app\common\model\DemandReport; | ||
|  | use app\common\server\JsonServer; | ||
|  | use think\facade\Validate; | ||
|  | use think\response\Json; | ||
|  | 
 | ||
|  | //需求相关
 | ||
|  | class  Demand extends Api | ||
|  | { | ||
|  |     public $like_not_need_login = ['lists', 'detail']; | ||
|  | 
 | ||
|  |     /*** | ||
|  |      * 列表 | ||
|  |      */ | ||
|  |     public function lists() | ||
|  |     { | ||
|  |         try { | ||
|  |             $page = input('page_size/d', 1); | ||
|  |             $limit = input('page_no/d', 10); | ||
|  | 
 | ||
|  |             $page = $page ?: 1; | ||
|  |             $limit = $limit ?: 10; | ||
|  |             $where = []; | ||
|  | 
 | ||
|  |             $order = [ | ||
|  |                 'id' => 'desc' | ||
|  |             ]; | ||
|  | 
 | ||
|  |             $count = thisModel::where($where)->count(); | ||
|  | 
 | ||
|  |             $list =  thisModel::field(['id', 'name', 'create_time']) | ||
|  |                 ->where($where) | ||
|  |                 ->order($order) | ||
|  |                 ->page($page,$limit) | ||
|  |                 ->select() | ||
|  |                 ->toArray(); | ||
|  | 
 | ||
|  |             $data = [ | ||
|  |                 'list'          => $list, | ||
|  |                 'page_no'       => $page, | ||
|  |                 'page_size'     => $limit, | ||
|  |                 'count'         => $count, | ||
|  |             ]; | ||
|  |             return JsonServer::success('获取成功', $data); | ||
|  |         } catch (\Exception $e) { | ||
|  |             return JsonServer::error('获取失败'); | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     public function detail() | ||
|  |     { | ||
|  |         $id = input('id/d', 0); | ||
|  |         if (!$item = thisModel::where('id', $id)->find()) { | ||
|  |             return JsonServer::error('记录不存在'); | ||
|  |         } | ||
|  |         return JsonServer::success('获取成功', $item->toArray()); | ||
|  |     } | ||
|  | 
 | ||
|  |     public function report(): Json | ||
|  |     { | ||
|  |         $input = input('post.'); | ||
|  | 
 | ||
|  |         $rule = [ | ||
|  |             'demand_id|需求' => 'require|number', | ||
|  |             'name|姓名' => 'require|min:2|max:50', | ||
|  |             'phone|手机' => 'require|mobile', | ||
|  | //            'company|公司' => '',
 | ||
|  | //            'price|报价' => '',
 | ||
|  |         ]; | ||
|  | 
 | ||
|  |         $validate = Validate::rule($rule); | ||
|  |         if (!$validate->check($input)) { | ||
|  |             return JsonServer::error($validate->getError()); | ||
|  |         } | ||
|  | 
 | ||
|  |         DemandReport::create([ | ||
|  |             'demand_id' => $input['demand_id'], | ||
|  |             'name' => $input['name'], | ||
|  |             'phone' => $input['phone'], | ||
|  |             'company' => $input['company'] ?: '', | ||
|  |             'price' => $input['price'] ?: '', | ||
|  |             'create_time' => time(), | ||
|  |         ]); | ||
|  | 
 | ||
|  |         return JsonServer::success('报名成功'); | ||
|  |     } | ||
|  | 
 | ||
|  | } |