master
wangxinglong 2021-12-13 19:09:16 +08:00
parent d143b55d3a
commit a776f4c445
6 changed files with 226 additions and 8 deletions

View File

@ -853,8 +853,12 @@ class Business extends Base
->logoResizeToWidth(ceil($w/6))
->logoPunchoutBackground(true)
->build();
return $result->getDataUri();
header(
"Content-type: image/jpg"
);
$path = "/storage/business/" . $businessCode . ".jpg";
$result->saveToFile( public_path() . $path);
return $this->json(0,"success",["url"=>$this->request->domain().$path]);
}
}

View File

@ -3,12 +3,19 @@ namespace app\controller\api;
use app\exception\RepositoryException;
use app\model\Account;
use app\model\Coupon as CouponModel;
use app\model\CouponBill;
use app\model\CouponMain;
use app\model\Deduction;
use app\model\Redpack;
use app\model\Score;
use app\repository\AccountRepository;
use app\repository\BusinessRepository;
use app\repository\CouponRepository;
use app\service\wx\WechatPay;
use app\validate\CouponRelease;
use app\validate\CouponUsingRule;
use think\Exception;
use think\facade\Config;
use think\facade\Db;
@ -103,6 +110,57 @@ class Coupon extends Base
}
}
/**
* 我的优惠卷列表 指定商家
*
*/
public function getCouponListByBusinessCode()
{
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 10);
$businessCode = $this->request->param('business_code/s', '');
$page = $page < 1 ? 1 : $page;
$size = $size < 1 ? 10 : $size;
$accountCode = $this->request->user['user_code'] ?? '';
try {
$whereMap = [];
$fields = [
'id',
'is_verificated as isVerificated',
'money',
'name as couponName',
'business_code as businessCode',
'end_time as endTime',
'consumer_name as consumerName',
'verificate_time as verificateTime',
'(end_time > NOW()) as sort_weight'];
$whereMap[] = ['consumer_code', '=', $accountCode];
$whereMap[] = ['business_code', '=', $businessCode];
$sortOrder = ['sort_weight' => 'desc', 'end_time' => 'asc'];
$res = CouponRepository::getInstance()->findList($whereMap, $fields, $page, $size,function ($q){
return $q->with(["couponMain","scoreModel"]);
}, $sortOrder);
$res['list'] ->each(function ($item){
//重置优惠券名称
if(isset($item->couponMain) && $item->couponMain){
$item->couponName = $item->couponMain->name;
}
});
$res['list'] = multiTwoArrayKeysExcludeFilter($res['list']->toArray(), ['sort_weight']);
return $this->json(0, 'success', $res);
} catch (RepositoryException | \Exception $e) {
return $this->json(5001, '优惠卷查询失败!');
}
}
/**
* 领取优惠券
* */
@ -142,7 +200,6 @@ class Coupon extends Base
}
//检查通过 执行领取
$time = time();
Db::startTrans();
@ -181,6 +238,156 @@ class Coupon extends Base
}
/**
* 核验优惠券 程序----核心操作----
* */
public function verification()
{
$accountId = $this->request->user['user_id'] ?? 0;
$lat = input("lat/f",0);
$lng = input("lng/f",0);
$couponId = input("couponId/d",0);
$account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
return $q->with(['business', 'parent']);
});
$time = time();
if(empty($account)){
return $this->json(6001,"无效的用户");
}
if ($lat <= 0 || $lng <= 0) {
return $this->json(4001, "请授权定位");
}
$coupon = CouponRepository::getInstance()->findById($couponId,[],function ($q){
return $q->with(["couponMain"]);
});
if($coupon->consumer_code != $account->user_code ){
return $this->json(4001, "参数错误");
}
if(empty($coupon)){
return $this->json(4001, "优惠券不存在");
}
if($coupon->status != CouponMain::status_on){
return $this->json(4001, "优惠券已停用");
}
if($coupon->on_shelf != CouponMain::on_shelf_on){
return $this->json(4001, "优惠券下架");
}
if(strtotime($coupon->end_time) < $time){
return $this->json(4001, "优惠券已过期");
}
if(!isset($coupon->couponMain)||empty($coupon->couponMain)){
return $this->json(4001, "商家优惠券信息错误");
}
$business = BusinessRepository::getInstance()->getModel()->with(["agency"])->where(["code"=>$coupon->couponMain->business_code])->lock(true)->find();
if(empty($business)){
return $this->json(4001, "商家不存在");
}
if($business->balance < $coupon->couponMain->deduction_money){
return $this->json(4001, "商家余额不足");
}
//开始数据操作
Db::startTrans();
try {
// 1. 修改优惠券状态
$coupon->save([
"is_verificated"=>CouponModel::is_verificated_on,
"used_time"=>date("Y-m-d H:i:s" ,$time),
"verificate_time"=>date("Y-m-d H:i:s" ,$time)
]);
//可分配金额
$deductionMoney = $coupon->couponMain->deduction_money;
$agencyMoney = ($deductionMoney/100) * $coupon->couponMain->commission_agency;
$adminMoney = ($deductionMoney/100) * $coupon->couponMain->commission_admin;
$consumerMoney = ($deductionMoney/100) * $coupon->couponMain->commission_consumer;
// 2. 写入优惠券流水
$couponBillData = [
"coupon_main_id"=>$coupon->couponMain->id,
"coupon_id"=>$coupon->id,
"user_code"=>$account->user_code,
"agency_code"=>$business->agency_code,
"commission_agency"=>$coupon->couponMain->commission_agency,
"commission_admin"=>$coupon->couponMain->commission_admin,
"commission_consumer"=>$coupon->couponMain->commission_consumer,
"money"=>$coupon->couponMain->money,
"agency_money"=>$agencyMoney,
"admin_money"=>$adminMoney,
"consumer_money"=>$consumerMoney,
"lat"=>$lat,
"lng"=>$lng,
];
$couponBill = CouponBill::create($couponBillData);
// 3. 写入商家扣费记录
$deductionData = [
"money"=>$deductionMoney,
"business_code"=>$business->code,
"business_name"=>$business->business_name,
"balance"=>$business->balance - $deductionMoney,
"reason"=> sprintf("[%s]验证优惠券[%s]扣除[%s]",$account->nick_name, $coupon->couponMain->name,$deductionMoney),
"coupon_main_id"=> $coupon->couponMain->id,
"coupon_id"=> $coupon->id,
"bill_id"=> $couponBill->id,
"create_time"=> date("Y-m-d H:i:s",$time),
];
Deduction::create($deductionData);
//4. 商家扣钱
$business->save(["balance"=>$business->balance - $deductionMoney]);
//5. 渠道商加钱
if(isset($business->agency)&&$business->agency){
$business->agency->inc("balance",$agencyMoney)->update();
}
//6. 用户提现到零钱
$RedpackData =[
"mch_billno"=>createUuid(),
"openid"=>$account->open_id,
"user_code"=>$account->user_code,
"money"=>$consumerMoney*100,
];
$redpackModelData = Redpack::create($RedpackData);
$payment = WechatPay::getInstance();
$redpack = $payment->redpack;
$res = $redpackData = [
'mch_billno' => $time.randomStr(0,10),
'send_name' => '红包',
're_openid' => $redpackModelData->openid,
'total_num' => 1, //固定为1可不传
'total_amount' => $redpackModelData->money, //单位为分不小于100
'wishing' => '恭喜发财',
'client_ip' => $this->request->ip(), //可不传,不传则由 SDK 取当前客户端 IP
'act_name' => '验证优惠券得红包',
'remark' => '验证优惠券得红包',//测试备注
// ...
];
$result = $redpack->sendNormal($redpackData);
var_dump($result);
Db::rollback();
}catch (RepositoryException $e){
Db::rollback();
return $this->json(5001, "服务器错误");
}catch (\Exception $e){
echo $e->getMessage();
Db::rollback();
return $this->json(5002, "服务器错误");
}
}
/**
* 发布优惠券
* */

8
app/model/Redpack.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace app\model;
//红包记录
class Redpack extends Base
{
}

View File

@ -37,7 +37,6 @@ class CouponRepository extends Repository
*/
public function couponMainHasList($id, $keyword, $page, $size)
{
return Coupon::findList(["id" => $id], [], $page, $size, function ($q) use ($keyword) {
if (!empty($keyword)) {
return $q::hasWhere('account', function ($q) use ($keyword) {

View File

@ -35,8 +35,8 @@ class WechatPay
'key' => $conf['key'], // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => root_path().'cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => root_path().'cert/apiclient_key.pem', // XXX: 绝对路径!!!!
'cert_path' => config_path().'/extra/cert/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => config_path().'/extra/cert/apiclient_key.pem', // XXX: 绝对路径!!!!
'notify_url' => $conf['applets_notify_url'], // 你也可以在下单时单独设置来想覆盖它
];

View File

@ -154,11 +154,11 @@ trait CouponTrait
$time = time();
//一天的开始时间
if(strtotime(date("Y-m-d " . $usingRule->day_start_time)) > $time){
throw new RepositoryException("请在当天{$usingRule->day_start_time}后来领取");
throw new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}领取");
}
//一天的结束时间
if(strtotime(date("Y-m-d ".$usingRule->day_end_time)) < $time){
throw new RepositoryException("请在当天{$usingRule->day_start_time}前领取");
throw new RepositoryException("请在当天{$usingRule->day_start_time}-{$usingRule->day_start_time}前领取");
}
}