Browse Source

结算时获取优惠券列表

master
ZLW-PC\Administrator 7 years ago
parent
commit
c6c7c6b1e9
  1. 28
      app/Http/Controllers/Api/UserBonusController.php
  2. 9
      app/Http/Controllers/Weixin/CartController.php
  3. 38
      app/Http/Model/UserBonus.php
  4. 2
      resources/org/wxJsSdk/jssdk.php
  5. 1
      routes/web.php

28
app/Http/Controllers/Api/UserBonusController.php

@ -44,13 +44,7 @@ class UserBonusController extends CommonController
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserBonus::add($data);
if($res == false)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
return UserBonus::add($data);
}
//用户删除优惠券
@ -70,4 +64,24 @@ class UserBonusController extends CommonController
return ReturnData::create(ReturnData::SUCCESS,$res);
}
public function userAvailableBonusList(Request $request)
{
//参数
$data['user_id'] = Token::$uid;
$data['min_amount'] = $request->input('min_amount','');
if($data['min_amount']=='')
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserBonus::getAvailableBonusList($data);
if($res == false)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
}

9
app/Http/Controllers/Weixin/CartController.php

@ -87,6 +87,15 @@ class CartController extends CommonController
if($data['checkout_goods']['total_price']>$data['user_info']['money']){$is_balance_enough = 0;}
$data['is_balance_enough'] = $is_balance_enough;
//获取用户优惠券列表
$postdata = array(
'min_amount' => $data['checkout_goods']['total_price'],
'access_token' => $_SESSION['weixin_user_info']['access_token']
);
$url = env('APP_API_URL')."/user_available_bonus_list";
$res = curl_request($url,$postdata,'GET');
$data['bonus_list'] = $res['data'];
return view('weixin.cart.cartCheckout', $data);
}
}

38
app/Http/Model/UserBonus.php

@ -1,5 +1,7 @@
<?php
namespace App\Http\Model;
use App\Common\ReturnData;
use DB;
class UserBonus extends BaseModel
{
@ -61,14 +63,21 @@ class UserBonus extends BaseModel
public static function add(array $data)
{
if(self::where(['bonus_id'=>$data['bonus_id'],'user_id'=>$data['user_id']])->first()){return '亲,您已获取!';}
if(!Bonus::where(['id'=>$data['bonus_id']])->where('num',-1)->first() && !Bonus::where(['id'=>$data['bonus_id']])->where('num','>',0)->first())
{
return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您来晚了啦,已被抢光了');
}
if(self::where(['bonus_id'=>$data['bonus_id'],'user_id'=>$data['user_id']])->first()){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'亲,您已获取!');}
if ($id = self::insertGetId($data))
{
return $id;
DB::table('bonus')->where(array('id'=>$data['bonus_id']))->decrement('num', 1);
return ReturnData::create(ReturnData::SUCCESS,$id);
}
return false;
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
public static function modify($where, array $data)
@ -91,4 +100,25 @@ class UserBonus extends BaseModel
return true;
}
//商品结算时,获取优惠券列表
public static function getAvailableBonusList(array $param)
{
extract($param);
$where['user_bonus.user_id'] = $user_id;
if(isset($status)){$where['bonus.status'] = 0;}
$model = new UserBonus;
if(isset($min_amount)){$model = $model->where('bonus.min_amount', '<=', $min_amount);} //满多少使用
if(isset($bonus_end_time)){$model = $model->where('bonus.end_time', '>=', date('Y-m-d H:i:s'));} //有效期
$bonus_list = $model->join('bonus', 'bonus.id', '=', 'user_bonus.bonus_id')->where($where)
->select('bonus.*', 'user_bonus.user_id', 'user_bonus.used_time', 'user_bonus.get_time', 'user_bonus.status as user_bonus_status', 'user_bonus.id as user_bonus_id')
->orderBy('bonus.money','desc')->get();
$res['list'] = $bonus_list;
return $res;
}
}

2
resources/org/wxJsSdk/jssdk.php

@ -17,7 +17,7 @@ class JSSDK
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();

1
routes/web.php

@ -183,6 +183,7 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web
Route::get('/user_point_list', 'UserPointController@userPointList'); //用户积分列表
Route::post('/user_point_add', 'UserPointController@userPointAdd');
//优惠券
Route::get('/user_available_bonus_list', 'UserBonusController@userAvailableBonusList'); //用户结算时获取可用优惠券列表
Route::get('/user_bonus_list', 'UserBonusController@userBonusList'); //用户优惠券列表
Route::post('/user_bonus_add', 'UserBonusController@userBonusAdd'); //用户获取优惠券
Route::get('/bonus_list', 'BonusController@bonusList'); //可用获取的优惠券列表

Loading…
Cancel
Save