Browse Source

添加文件

master
Mike 6 years ago
parent
commit
3a2f898812
  1. 38
      app/Controller/BaseController.php
  2. 99
      app/Controller/CouponController.php
  3. 4
      app/Model/Coupon.php
  4. 4
      app/Model/CouponRec.php
  5. 4
      app/Model/CouponUserRecType.php
  6. 9
      config/routes.php

38
app/Controller/BaseController.php

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class BaseController extends AbstractController
{
public function result($code, $data, $message = '成功'):Psr7ResponseInterface
{
$status = 'ok';
if($code>0){
$status = 'error';
}
$content = [
"status"=>$status,
"code" => $code,
"result" => $data ? collect($data)->toArray() : [],
"message" => $message
];
return $this->response->json($content);
}
public function success($data, $message = '成功')
{
return $this->result(0,$data, $message);
}
}

99
app/Controller/CouponController.php

@ -11,16 +11,99 @@ declare(strict_types=1);
*/
namespace App\Controller;
class CouponController extends AbstractController
use App\Model\CouponUserRecType;
use App\Model\Coupon;
use App\Model\CouponRec;
use Hyperf\DbConnection\Db;
class CouponController extends BaseController
{
public function index()
/**
* 获取用户可领取优惠卷接口
*/
public function getSystemCouponUserList(){
$user_id = $this->request->input('user_id');
$receive_type = $this->request->input('receive_type');
$c_ids = CouponUserRecType::where('receive_type',$receive_type)->pluck('system_coupon_user_id');
$nowTime = time();
$cr_ids = CouponRec::where('user_id',$user_id)->pluck('system_coupon_user_id');
$ids = array_merge($c_ids->toArray(),$cr_ids->toArray());
$ids = collect($ids)->unique();
$c = Coupon::where('start_time','<=',$nowTime)
->where('end_time','>',$nowTime)
->whereRaw('inventory_use < inventory')
->whereIn('id',$ids)
->orderBy('weigh','desc')
->limit(4)
->get();
return $this->success(['not_reveive'=>$c]);
}
public function userCouponAccount()
{
$user_id = $this->request->input('user_id');
$nowTime = time();
$userCouponCount = DB::table('ims_system_coupon_user_receive')
->leftJoin('ims_system_coupon_user', 'ims_system_coupon_user_receive.system_coupon_user_id', '=', 'ims_system_coupon_user.id')
->count();
// $userCouponCount = CouponRec::with('coupon')->where('user_id',$user_id)->where('usable_start_time','<=',$nowTime)
// ->where('usable_end_time','>',$nowTime)->count();
return $this->success(['total'=>$userCouponCount]);
}
/**
* 获取用户当前订单可用的优惠券列表
* 按分类(1订单 等优惠)分组返回
*/
public function getUserAvailableCoupons()
{
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
return [
'method' => $method,
'message' => "Hello {$user}.",
];
$userId = $this->request->input("user_id");
$receiveType = $this->request->input("receive_type");
$ids = $this->request->input("ids");
}
/**
* 获取用户已经领取的优惠卷列表
*/
public function getUserReceiveCouponList()
{
$userId = $this->request->input("user_id");
$nowTime = time();
$couponIds = CouponRec::where('user_id',$userId)
->whereIn('status',[0,1])
->orderBy('receive_time','desc')
->get()
->pluck('system_coupon_user_id');
$not_expired = [];
$expired = [];
$couponIds = $couponIds->toArray();
$coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get();
foreach ($coupons as $key => $coupon) {
if($coupon->usable_end_time < $nowTime){
$expired[] = $coupon;
}else{
$not_expired[] = $coupon;
}
}
$ret = ['not_expired'=>$not_expired,'expired'=>$expired];
return $this->success($ret);
}
}

4
app/Model/Coupon.php

@ -1,8 +1,8 @@
<?php
namespace App\Models;
namespace App\Model;
use App\Models\Model;
use App\Model\Model;
class Coupon extends Model
{

4
app/Model/CouponRec.php

@ -1,8 +1,8 @@
<?php
namespace App\Models;
namespace App\Model;
use App\Models\Model;
use App\Model\Model;
class CouponRec extends Model
{

4
app/Model/CouponUserRecType.php

@ -1,8 +1,8 @@
<?php
namespace App\Models;
namespace App\Model;
use App\Models\Model;
use App\Model\Model;
class CouponUserRecType extends Model
{

9
config/routes.php

@ -11,4 +11,11 @@ declare(strict_types=1);
*/
use Hyperf\HttpServer\Router\Router;
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
// Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@index');
Router::addGroup('/v1/',function (){
Router::post('CouponUser/getSystemCouponUserListst', 'App\Controller\CouponController@getSystemCouponUserList');
Router::post('CouponUser/userCouponAccount', 'App\Controller\CouponController@userCouponAccount');
Router::post('CouponUserReceive/getUserReceiveCouponList', 'App\Controller\CouponController@getUserReceiveCouponList');
});
Loading…
Cancel
Save