Commit 0078b858 by 杨树贤

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

parent 889d5c2e
<?php
namespace App\Http\Controllers;
use App\Models\Integral;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class IntegralsController extends Controller
{
public function index()
{
$integrals = Integral::with('integralType:id,name')
->whereStatus(1)
->get()->toArray();
return $this->Export(0, 'ok', ['data' => $integrals]);
}
public function create(Request $request)
{
$res = DB::table('integrals')->insert([
'name' => $request->name,
'integral_type_id' => $request->integral_type_id,
'add_time' => time(),
'status' => 1,
]);
if ($res) {
return $this->Export(0, 'ok');
} else {
return $this->Export(ErrorCode(001, 5), '新增积分信息失败');
}
}
public function update(Request $request, $id)
{
$res = DB::table('integrals')->where('id', $id)
->update([
'name' => $request->name,
'integral_type_id' => $request->integral_type_id,
]);
if ($res) {
return $this->Export(0, 'ok');
} 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), '删除积分信息失败');
}
}
<?php
namespace App\Http\Controllers;
use App\Models\Integral;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class IntegralsController extends Controller
{
public function show($id)
{
$integral = Integral::find($id)->toArray();
return $this->Export(0, 'ok', ['data' => $integral]);
}
public function index(Request $request)
{
$integrals = Integral::with('integralType:id,name')
->page($request->page, $request->page_size)
->orderBy('id','desc')
->get()->toArray();
$count = Integral::count();
return $this->Export(0, 'ok', ['data' => $integrals, 'count' => $count]);
}
public function create(Request $request)
{
$res = DB::table('integrals')->insert([
'name' => $request->name,
'daily_limit' => $request->daily_limit,
'integral_amount' => $request->integral_amount,
'add_time' => time(),
'status' => $request->status,
]);
if ($res) {
return $this->Export(0, 'ok');
} else {
return $this->Export(ErrorCode(001, 5), '新增积分信息失败');
}
}
public function update(Request $request, $id)
{
$res = DB::table('integrals')->where('id', $id)
->update([
'name' => $request->name,
'daily_limit' => $request->daily_limit,
'integral_amount' => $request->integral_amount,
'update_time' => time(),
]);
if ($res) {
return $this->Export(0, 'ok');
} 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
{
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()
{
......
......@@ -21,6 +21,7 @@ $router->get('/key', function () {
//积分信息
$router->get('/integrals', 'IntegralsController@index');
$router->get('/integrals/{id}', 'IntegralsController@show');
$router->post('/integrals', 'IntegralsController@create');
$router->patch('/integrals/{id}', 'IntegralsController@update');
$router->delete('/integrals/{id}', 'IntegralsController@destroy');
......
3671
\ No newline at end of file
18189
\ 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