Commit 0078b858 by 杨树贤

完善积分信息所需要的服务接口

parent 889d5c2e
<?php <?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Integral; use App\Models\Integral;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
class IntegralsController extends Controller class IntegralsController extends Controller
{ {
public function index() public function show($id)
{ {
$integrals = Integral::with('integralType:id,name') $integral = Integral::find($id)->toArray();
->whereStatus(1)
->get()->toArray(); return $this->Export(0, 'ok', ['data' => $integral]);
}
return $this->Export(0, 'ok', ['data' => $integrals]);
} public function index(Request $request)
{
public function create(Request $request) $integrals = Integral::with('integralType:id,name')
{ ->page($request->page, $request->page_size)
$res = DB::table('integrals')->insert([ ->orderBy('id','desc')
'name' => $request->name, ->get()->toArray();
'integral_type_id' => $request->integral_type_id, $count = Integral::count();
'add_time' => time(),
'status' => 1, return $this->Export(0, 'ok', ['data' => $integrals, 'count' => $count]);
]); }
if ($res) { public function create(Request $request)
return $this->Export(0, 'ok'); {
} else { $res = DB::table('integrals')->insert([
return $this->Export(ErrorCode(001, 5), '新增积分信息失败'); 'name' => $request->name,
} 'daily_limit' => $request->daily_limit,
} 'integral_amount' => $request->integral_amount,
'add_time' => time(),
public function update(Request $request, $id) 'status' => $request->status,
{ ]);
$res = DB::table('integrals')->where('id', $id)
->update([ if ($res) {
'name' => $request->name, return $this->Export(0, 'ok');
'integral_type_id' => $request->integral_type_id, } else {
]); return $this->Export(ErrorCode(001, 5), '新增积分信息失败');
if ($res) { }
return $this->Export(0, 'ok'); }
} else {
return $this->Export(ErrorCode(002, 5), '更新积分信息失败'); public function update(Request $request, $id)
} {
} $res = DB::table('integrals')->where('id', $id)
->update([
public function destroy($ids) 'name' => $request->name,
{ 'daily_limit' => $request->daily_limit,
$ids = explode(',', trim($ids)); 'integral_amount' => $request->integral_amount,
$res = DB::table('integrals')->whereIn('id', $ids)->delete(); 'update_time' => time(),
if ($res) { ]);
return $this->Export(0, 'ok'); if ($res) {
} else { return $this->Export(0, 'ok');
return $this->Export(ErrorCode(003, 5), '删除积分信息失败'); } else {
} return $this->Export(ErrorCode(002, 5), '更新积分信息失败');
} }
}
public function destroy($ids)
{
$ids = explode(',', trim($ids));
$res = DB::table('integrals')->whereIn('id', $ids)->delete();
if ($res) {
return $this->Export(0, 'ok');
} else {
return $this->Export(ErrorCode(003, 5), '删除积分信息失败');
}
}
} }
\ No newline at end of file
<?php
namespace App\Http\Filters;
class IntegralFilter extends QueryFilter
{
}
\ No newline at end of file
<?php
namespace App\Http\Filters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
abstract class QueryFilter
{
protected $request;
protected $builder;
public function __construct(Request $request)
{
$this->request = $request;
}
public function apply(Builder $builder)
{
$this->builder = $builder;
foreach ($this->filters() as $name => $value) {
if (method_exists($this, $name)) {
call_user_func_array([$this, $name], array_filter([$value]));
}
}
return $this->builder;
}
public function filters()
{
return $this->request->all();
}
}
\ No newline at end of file
...@@ -10,7 +10,12 @@ class Integral extends Model ...@@ -10,7 +10,12 @@ class Integral extends Model
{ {
public $timestamps = false; public $timestamps = false;
protected $hidden = ['id']; protected $hidden = [];
public function scopePage($query, $page = 1, $pageSize = 10)
{
return $query->offset(($page - 1) * $pageSize)->limit($pageSize);
}
public function integralType() public function integralType()
{ {
......
...@@ -21,6 +21,7 @@ $router->get('/key', function () { ...@@ -21,6 +21,7 @@ $router->get('/key', function () {
//积分信息 //积分信息
$router->get('/integrals', 'IntegralsController@index'); $router->get('/integrals', 'IntegralsController@index');
$router->get('/integrals/{id}', 'IntegralsController@show');
$router->post('/integrals', 'IntegralsController@create'); $router->post('/integrals', 'IntegralsController@create');
$router->patch('/integrals/{id}', 'IntegralsController@update'); $router->patch('/integrals/{id}', 'IntegralsController@update');
$router->delete('/integrals/{id}', 'IntegralsController@destroy'); $router->delete('/integrals/{id}', 'IntegralsController@destroy');
......
3671 18189
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment