first commit

This commit is contained in:
2020-08-06 16:42:18 +08:00
commit eb792c34aa
12972 changed files with 1511424 additions and 0 deletions

2
packages/bonus/README.md Normal file
View File

@@ -0,0 +1,2 @@
# Bonus 奖金结算系统

View File

@@ -0,0 +1,28 @@
{
"name": "rulong/bonus",
"description": "",
"license": "MIT",
"authors": [
{
"name": "C.Jason",
"email": "chenjxlg@163.com"
}
],
"require": {
"php": ">=7.1.3",
"rulong/user-account": "^1.0",
"laravel/framework": "*"
},
"autoload": {
"psr-0": {
"RuLong\\Bonus": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"RuLong\\Bonus\\ServiceProvider"
]
}
}
}

View File

@@ -0,0 +1,13 @@
<?php
return [
'user_model' => \App\Models\User::class,
'Settlement' => [
'Direct' => [
'integral' => 100,
'layer' => 9,
'point' => 5,
],
],
'full_point' => 10,
];

View File

@@ -0,0 +1,20 @@
<?php
namespace RuLong\Bonus;
use RuLong\Bonus\Contracts\Settlement;
class Bonus
{
protected $settlement;
public function __construct(Settlement $settlement)
{
$this->settlement = $settlement;
}
public function __destruct()
{
$this->settlement->fire();
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace RuLong\Bonus\Contracts;
interface Settlement
{
/**
* 子规则的实际计算过程
* @Author:<C.Jason>
* @Date:2018-10-31T14:22:48+0800
*/
function fire();
}

View File

@@ -0,0 +1,14 @@
<?php
namespace RuLong\Bonus\Exceptions;
use RuntimeException;
class BonusException extends RuntimeException
{
public function __construct($message)
{
parent::__construct($message);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace RuLong\Bonus\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Bonus extends Model
{
use SoftDeletes;
protected $guarded = [];
protected $dates = [
'deleted_at',
];
protected $casts = [
'performs' => 'array',
'configs' => 'array',
];
public function logs()
{
return $this->hasMany(BonusLog::class);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace RuLong\Bonus\Models;
use Illuminate\Database\Eloquent\Model;
class BonusLog extends Model
{
protected $guarded = [];
public function bouns()
{
return $this->belongsTo(Bonus::class);
}
public function user()
{
return $this->belongsTo(config('rulong_bonus.user_model'));
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace RuLong\Bonus;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class ServiceProvider extends LaravelServiceProvider
{
/**
* 部署时加载
* @Author:<C.Jason>
* @Date:2018-06-22T16:01:20+0800
* @return [type] [description]
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/rulong_bonus.php' => config_path('rulong_bonus.php')]);
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations/');
}
}
/**
* 注册服务提供者
* @Author:<C.Jason>
* @Date:2018-06-22T16:01:12+0800
* @return [type] [description]
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/rulong_bonus.php', 'rulong_bonus');
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace RuLong\Bonus\Traits;
use RuLong\UserRelation\Models\UserRelation;
trait BloodLine
{
public function getUpBloodLine($user, $layer)
{
$lineArray = explode(',', $this->strLine);
return UserRelation::whereIn('user_id', $lineArray)->orderBy('layer', 'desc')->limit($layer)->get();
}
public function getDownBloodLine($user, $layer)
{
#Todo...
return UserRelation::where('bloodline', 'like', "%," . $user->id . ",%")->whereBetween('layer', [$user->relation->layer, $user->relation->layer + $layer])->orderBy('layer', 'asc')->get();
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace RuLong\Bonus\Traits;
use RuLong\Bonus\Bonus;
trait Settlementable
{
public static function settlement()
{
return new Bonus(new static(...func_get_args()));
}
}

View File

@@ -0,0 +1,8 @@
<?php
namespace RuLong\Bonus\Traits;
trait UserHasBonus
{
}

View File

@@ -0,0 +1,27 @@
{
"name": "xuanchen/coupon",
"description": "卡券相关",
"license": "MIT",
"authors": [
{
"name": "玄尘",
"email": "122383162@qq.com"
}
],
"require": {
"php": ">=7.1.3",
"laravel/framework": "*"
},
"autoload": {
"psr-0": {
"XuanChen\\Coupon": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"XuanChen\\Coupon\\ServiceProvider"
]
}
}
}

View File

@@ -0,0 +1,15 @@
<?php
return [
'coupon_model' => \App\Models\Coupon::class,
'rules' => [
'pingan' => [
'pattern' => '/^\d{12}$/',
'model' => \XuanChen\Coupon\Action\PinganAction::class,
],
'ysd' => [
'pattern' => '/^YSD/',
'model' => \XuanChen\Coupon\Action\YsdAction::class,
],
],
];

View File

@@ -0,0 +1,153 @@
<?php
namespace XuanChen\Coupon\Action;
use App\Models\Coupon;
use App\Models\Log as LogModel;
class Init
{
//渠道
public $user;
//卡券编号
public $redemptionCode;
//订单金额
public $total;
//网点编号
public $outletId;
//活动id
public $activityId;
//手机号
public $mobile;
//核销的卡券 创建的核销记录
public $coupon;
//查询返回卡券信息
public $query_coupon;
//设置渠道
public function setUser($user)
{
$this->user = $user;
return $this;
}
//设置核销码
public function setCode($redemptionCode)
{
$this->redemptionCode = $redemptionCode;
return $this;
}
//设置订单总额
public function setTotal($total)
{
$this->total = $total;
return $this;
}
//设置网点id
public function setOutletId($outletId)
{
$this->outletId = $outletId;
return $this;
}
//设置活动id
public function setActivityId($activityId)
{
$this->activityId = $activityId;
return $this;
}
//设置手机号
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Notes: 插入日志
* @Author: 玄尘
* @Date : 2020/6/30 10:29
* @param $url
* @param $method
* @param $params
* @param string $type
* @return mixed
*/
public function createLog($url, $method, $params, $type = 'pingan')
{
$data = [
'path' => $url,
'method' => $method,
'type' => $type,
'in_source' => $params,
];
$info = LogModel::create($data);
return $info;
}
/**
* Notes: 更新日志
* @Author: 玄尘
* @Date : 2020/6/30 10:29
* @param $log
* @param $params
*/
public static function updateLog($log, $params)
{
$log->out_source = $params;
$log->save();
}
//统一门店 相同金额 3分钟之内看作是一笔订单
public function CheckCount()
{
//排除本时生活渠道
if ($this->user && $this->user->id == 6) {
return true;
}
$check_count = Coupon::where('outletId', $this->outletId)
->where('total', $this->total)
->where('status', 2)
->where('created_at', '>=', now()->subMinutes(3)->format('Y-m-d H:i:s'))
->count();
$count = floor($this->total / 100);
if ($check_count > 0) {
if ($this->total < 100) {
return '核销失败订单金额少于100只能核销一张优惠券。';
}
if ($check_count >= $count) {
return "核销失败,此订单您只能使用 {$count} 张优惠券";
}
}
return true;
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace XuanChen\Coupon\Action;
use XuanChen\Coupon\Action\pingan\Query;
use XuanChen\Coupon\Action\pingan\Verification;
use XuanChen\Coupon\Contracts\CouponContracts;
class PinganAction extends Init implements CouponContracts
{
/**
* Notes: 核销执行入口
* @Author: 玄尘
* @Date : 2020/6/29 14:49
* @return mixed
*/
public function start()
{
return (new Verification)->setCode($this->redemptionCode)
->setUser($this->user)
->setOutletId($this->outletId)
->setTotal($this->total)
->start();
}
/**
* Notes: 查询卡券详情
* @Author: 玄尘
* @Date : 2020/6/29 15:15
* @return mixed
*/
public function detail()
{
$info = (new Query)->setOutletId($this->outletId)
->setCode($this->redemptionCode)
->start();
return $info;
}
//发券
function grant()
{
return '没这个接口';
}
//作废
function destroy()
{
return '没这个接口';
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace XuanChen\Coupon\Action;
use XuanChen\Coupon\Action\ysd\YsdDestroy;
use XuanChen\Coupon\Action\ysd\YsdGrant;
use XuanChen\Coupon\Action\ysd\YsdQuery;
use XuanChen\Coupon\Action\ysd\YsdVerification;
use XuanChen\Coupon\Contracts\CouponContracts;
/**
* Class YsdAction 自有卡券核销
* @Author : 玄尘
* @Date : 2020/7/21 9:41
* @package XuanChen\Coupon\Action
*/
class YsdAction extends Init implements CouponContracts
{
/**
* Notes: 发券
* @Author: 玄尘
* @Date : 2020/7/21 10:08
* @return mixed
*/
public function grant()
{
return (new YsdGrant)->setActivityId($this->activityId)
->setOutletId($this->outletId)
->setMobile($this->mobile)
->start();
}
/**
* Notes: 查询卡券详情
* @Author: 玄尘
* @Date : 2020/6/29 15:15
* @return mixed
*/
public function detail()
{
$query_coupon = (new YsdQuery)->setOutletId($this->outletId)
->setCode($this->redemptionCode)
->start();
if (!is_string($query_coupon)) {
return [
'name' => $query_coupon->activity->title,
'code' => $query_coupon->code,
'full' => $query_coupon->full,
'price' => $query_coupon->price,
'status' => $query_coupon->status,
'used_at' => (string)$query_coupon->used_at,
'startTime' => (string)$query_coupon->start_at,
'endTime' => (string)$query_coupon->end_at,
];
}
return $query_coupon;
}
/**
* Notes: 作废
* @Author: 玄尘
* @Date : 2020/7/21 11:32
*/
public function destroy()
{
return $res = (new YsdDestroy)->setCode($this->redemptionCode)
->start();
}
/**
* Notes: 核销执行入口
* @Author: 玄尘
* @Date : 2020/6/29 14:49
* @return mixed
*/
public function start()
{
return $res = (new YsdVerification)
->setCode($this->redemptionCode)
->setUser($this->user)
->setOutletId($this->outletId)
->setTotal($this->total)
->start();
}
}

View File

@@ -0,0 +1,205 @@
<?php
namespace XuanChen\Coupon\Action\pingan;
use App\Models\PinganToken;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use XuanChen\Coupon\Action\Init;
/**
* 超市购物券
*/
class PingAnInit extends Init
{
protected $this_type;
protected $baseUri;
protected $tokenUri;
protected $client_id;
protected $grant_type;
protected $client_secret;
protected $access_token;
protected $userName;
protected $error;
protected $aes_code; //aes 密钥
protected $log;//日志
public function __construct()
{
$this->this_type = config('pingan.this_type');
$pingan = config('pingan.' . $this->this_type);
$this->baseUri = $pingan['Uri'];
$this->tokenUri = $pingan['tokenUri'];
$this->client_id = $pingan['client_id'];
$this->grant_type = $pingan['grant_type'];
$this->userName = $pingan['userName'];
$this->client_secret = $pingan['client_secret'];
$this->aes_code = $pingan['AES_CODE'];
}
/**
* 获取access_token
* @return void [type] [description]
*/
public function getToken()
{
//从数据库里找token
$token = PinganToken::where('type', $this->this_type)->orderBy('id', 'desc')->first();
if ($token) {
$access_token = $token->access_token;
$expires_in = $token->expires_in;
$get_token_time = $token->get_token_time;
$diffMinutes = $get_token_time->diffInMinutes(now(), false);
if ($diffMinutes < $expires_in) {
$this->access_token = $access_token;
} else {
$this->getAjaxToken();
}
} else {
$this->getAjaxToken();
}
}
/**
* 获取毫秒级别的时间戳
*/
public function getMsecTime()
{
[$msec, $sec] = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
$msectime = explode('.', $msectime);
return $msectime[0];
}
/**
* 请求平台 access_token
* @return void [type] [description]
*/
public function getAjaxToken()
{
$params = [
'client_id' => $this->client_id,
'grant_type' => $this->grant_type,
'client_secret' => $this->client_secret,
];
try {
$log = $this->createLog($this->tokenUri, 'POST', $params, 'pingan');
$client = new Client();
$response = $client->request('POST', $this->tokenUri, [
'form_params' => $params,
]);
$body = $response->getBody();
$content = $body->getContents();
$result = json_decode($content, true);
$this->updateLog($log, $result); //更新日志
if ($result['ret'] > 0) {
$this->error = $result['msg'];
} else {
$data = $result['data'];
PinganToken::create([
'type' => $this->this_type,
'access_token' => $data['access_token'],
'expires_in' => $data['expires_in'],
'get_token_time' => now(),
]);
$this->access_token = $data['access_token'];
$this->error = '';
}
} catch (RequestException $e) {
$this->error = $e->getMessage();
$this->updateLog($log, [$this->error]); //更新日志
}
}
/**
* 通用获取数据接口
* @param [type] $url 请求地址
* @param array $query 传递参数
* @param array $json 需要传的json数据
* @param string $method 方式
* @return array|mixed [type] [description]
*/
public function getPingAnData($url, $query = [], $json = [], $method = 'POST')
{
$this->getToken();
if ($this->error) {
return $this->error;
}
$postData = [
'query' => array_merge([
'access_token' => $this->access_token,
'request_id' => $this->getMsecTime(),
'userName' => $this->userName,
], $query),
'json' => $json,
'headers' => [
'Content-Type' => 'application/json;charset=utf-8',
'accept' => 'application/json;charset=utf-8',
],
];
$log = $this->createLog($url, $method, $postData, 'pingan'); //日志
try {
$client = new Client();
$response = $client->request($method, $url, $postData);
$body = $response->getBody();
$content = $body->getContents();
$result = json_decode($content, true);
if ($result['ret'] > 0) {
$retData = $result['msg'];
} else {
$retData = $result['data'];
}
$this->updateLog($log, $retData);//更新日志
return $retData;
} catch (RequestException $e) {
$this->updateLog($log, [$e->getMessage()]);//更新日志
return ['ret' => '99999', $e->getMessage()];
}
}
//加密
public function encrypt($str)
{
if (is_array($str)) {
$str = json_encode($str);
}
$data = openssl_encrypt($str, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA);
return base64_encode($data);
}
//解密
public function decrypt($str)
{
$encrypted = base64_decode($str);
return openssl_decrypt($encrypted, 'aes-128-ecb', $this->aes_code, OPENSSL_RAW_DATA);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace XuanChen\Coupon\Action\pingan;
use App\Models\User;
class Query extends PingAnInit
{
public function start()
{
try {
//查询网点是否存在
$outlet = User::where('outlet_id', $this->outletId)->first();
if (!$outlet) {
throw new \Exception('网点编号错误,未查询到网点信息');
}
$url = $this->baseUri . 'partner/v2/coupondetail';
$params = [
'redemptionCode' => $this->redemptionCode,
'outletNo' => $outlet->PaOutletId,
'thirdOutletNo' => $outlet->outlet_id,
];
$res = $this->getPingAnData($url, $params);
if (!is_array($res)) {
throw new \Exception($res);
}
if ($res['code'] != 200) {
throw new \Exception($res['message']);
}
return collect($res['data']);
} catch (\Exception $e) {
return $e->getMessage();
}
}
}

View File

@@ -0,0 +1,296 @@
<?php
namespace XuanChen\Coupon\Action\pingan;
use App\Models\Coupon;
use App\Models\User;
use Carbon\Carbon;
class Verification extends PingAnInit
{
public $ticket;
//查询到的卡券规则和商品id
public $queryData;
public function start()
{
//检查可核销次数
$ret = $this->CheckCount();
if ($ret !== true) {
return $ret;
}
//查询卡券信息
$this->query_coupon = (new Query)->setOutletId($this->outletId)
->setCode($this->redemptionCode)
->start();
if (is_string($this->query_coupon)) {
return $this->query_coupon;
}
//校验卡券
$ticket = $this->checkCoupon();
if (!is_array($ticket)) {
return $ticket;
}
//增加核销记录
$coupon = $this->AddCoupon();
if (is_string($coupon)) {
return $coupon;
}
try {
$params = [
'couponNo' => $coupon->redemptionCode,
'partnerOrderId' => date('ymdHis') . sprintf("%0" . strlen(999999) . "d", mt_rand(0, 999999)),
'outletId' => $coupon->PaOutletId,
'productId' => $coupon->productId,
'timestamp' => $this->getMsecTime(),
];
$url = $this->baseUri . 'partner/redemption';
$str = $this->encrypt($params);
$res = $this->getPingAnData($url, [], ['data' => $str]);
if (!is_array($res)) {
$coupon->remark = $res;
$coupon->status = 3;
$coupon->save();
throw new \Exception($res);
}
if ($res['code'] != 200) {
$coupon->remark = $res['code'] . '--' . $res['message'];
$coupon->status = 3;
$coupon->save();
throw new \Exception($res['message']);
}
$data = $res['data'];
$coupon->remark = $data['message'];
$coupon->status = ($data['status'] == 1) ? 2 : 3;
$coupon->save();
//返回的数据
$resdata = [
'price' => $coupon->price,
'name' => $coupon->couponName,
'total' => $coupon->total,
];
//核销成功 执行分润
$coupon->profit();
return $resdata;
} catch (Exception $e) {
$coupon->status = 3;
$coupon->remark = '核销失败 ' . $e->getMessage();
$coupon->save();
return $coupon->remark;
}
}
/**
* Notes: 如可核销记录
* @Author: 玄尘
* @Date : 2020/7/21 15:03
* @return string
*/
public function AddCoupon()
{
try {
$couponData = [
'user_id' => $this->user->id,
'type' => Coupon::TYPE_PINGAN,
'outletId' => $this->outletId,
'PaOutletId' => $this->queryData['PaOutletId'],
'redemptionCode' => $this->redemptionCode,
'productId' => $this->queryData['productId'],
'thirdPartyGoodsId' => $this->queryData['thirdPartyGoodsId'],
'couponName' => $this->query_coupon['couponName'],
'price' => $this->ticket['price'],
'total' => $this->total,
'profit' => $this->ticket['profit'],
'status' => 0,
'startTime' => $this->query_coupon['startTime'],
'endTime' => $this->query_coupon['endTime'],
];
return $coupon = Coupon::create($couponData);
} catch (Exception $e) {
return $e->getMessage();
}
}
/**
* Notes: 检查卡券信息
* @Author: 玄尘
* @Date : 2020/6/29 16:40
* @return string
*/
public function checkCoupon()
{
//检查卡券是否已被核销
if ($this->query_coupon['status'] > 0) {
if (isset(config('pingan.coupon_status')[$this->query_coupon['status']])) {
return '核销失败,平安券' . config('pingan.coupon_status')[$this->query_coupon['status']];
}
return '核销失败,平安券不可用。';
}
$startTime = Carbon::parse($this->query_coupon['startTime']);
$endTime = Carbon::parse($this->query_coupon['endTime']);
$now = now();
if ($startTime->gt($now)) {
return '核销失败,平安券未开始使用。';
}
if ($now->gt($endTime)) {
return '核销失败,平安券已过期。';
}
//查找适配的商品id 和 网点id
$pinganData = $this->getPinganProduct();
if (is_string($pinganData)) {
return $pinganData;
}
//获取相关优惠信息
return $this->checkCode();
}
/**
* 校验平安券编号
* @return array|string [type] [description]
*/
public function checkCode()
{
$code = $this->user->code->where('code', $this->queryData['thirdPartyGoodsId'])->first();
if (!$code) {
return "核销失败,未找到此项平安券规则,请联系管理人员检查渠道配置。";
}
$ticket = explode('-', $this->queryData['thirdPartyGoodsId']);
if (!is_array($ticket) || count($ticket) != 3) {
return "核销失败,平安券规则格式不正确。";
}
$full = $ticket[1]; //full100
$price = $ticket[2];
preg_match('/\d+/', $full, $result);
if (empty($result) || !is_array($result)) {
return "核销失败,平安券规则格式不正确。";
}
if (!is_numeric($this->total)) {
return "核销失败,订单金额必须是数字";
}
if ($result[0] > $this->total) {
return '核销失败,订单金额不足,平安券不可使用。';
}
$this->ticket = [
'total' => $result[0],
'price' => $price,
'profit' => $code->profit,
];
return $this->ticket;
}
/**
* 查找平安商品id
* @author 玄尘 2020-04-04
* @return array|string [type] [description]
*/
public function getPinganProduct()
{
//查询网点是否存在
$outlet = User::where('outlet_id', $this->outletId)->first();
if (!$outlet) {
return '核销失败,网点编号错误,未查询到网点信息';
}
$PaOutletId = $outlet->PaOutletId;
$outlet_id = $outlet->outlet_id;
$profitOfferItemVersion = $this->query_coupon['profitOfferItemVersion'];
if (!$PaOutletId && $profitOfferItemVersion != 1) {
return '核销失败参数错误渠道信息缺少平安网点id';
}
$productItemList = $this->query_coupon['productItemList'];
if (!is_array($productItemList) || !is_array($productItemList[0])) {
return '核销失败,平安券数据有误,可能是未配置网点。';
}
//循环查找
$first = '';
foreach ($productItemList as $key => $item) {
$productId = $item['productId'];
$thirdPartyGoodsId = $item['thirdPartyGoodsId'];
$outletList = $item['outletList'];
if (!is_array($outletList) || !is_array($outletList[0])) {
return '核销失败,网点信息有误!请检查平安券配置信息。';
break;
}
$outletList = collect($outletList);
//判断是新版还是旧版
if ($profitOfferItemVersion) {
//新版通过第三方查询
$first = $outletList->firstWhere('thirdOutletNo', $outlet_id);
if ($first) {
break;
}
} else {
//旧版通过平安网点查询
$first = $outletList->firstWhere('outletNo', $PaOutletId);
if ($first) {
break;
}
}
}
if (!$first) {
return '核销失败,未找到可用网点信息。';
}
if (!$thirdPartyGoodsId) {
return '核销失败,平安券编号规则有误。';
}
if (!$productId) {
return '核销失败未查询到平安券商品id。';
}
return $this->queryData = [
'thirdPartyGoodsId' => $thirdPartyGoodsId,
'productId' => $productId,
'PaOutletId' => $first['outletNo'],
];
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace XuanChen\Coupon\Action\ysd;
use App\Models\ActivityCoupon;
use XuanChen\Coupon\Action\Init;
class YsdDestroy extends Init
{
public function start()
{
if ($this->redemptionCode) {
try {
$info = ActivityCoupon::where('code', $this->redemptionCode)->first();
if (!$info) {
throw new \Exception('未查询到卡券信息');
}
if (!$info->canDestroy()) {
throw new \Exception('作废失败,' . $info->status_text . '不能操作');
}
$info->status = ActivityCoupon::STATUS_CLOSE;
$info->save();
return true;
} catch (\Exception $e) {
return $e->getMessage();
}
} else {
return '未获取到券码。';
}
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace XuanChen\Coupon\Action\ysd;
use App\Models\Activity;
use App\Models\User;
use XuanChen\Coupon\Action\Init;
class YsdGrant extends Init
{
public function start()
{
try {
$activity = Activity::where('code', $this->activityId)->first();
if (!$activity) {
return '没有找到这个活动。';
}
if ($activity->user) {
$info = User::where('outlet_id', $this->outletId)->first();
if (!$info) {
return '未查询到此网点信息。';
}
if ($info->parent_id != $activity->user_id) {
return '发券失败,您没有权限参加这个活动!';
}
}
$coupon = $activity->grant($this->mobile, $this->outletId);
if (!is_string($coupon)) {
return [
'name' => $activity->title,
'code' => $coupon->code,
'full' => $coupon->full,
'price' => $coupon->price,
'startTime' => $coupon->start_at->format('Y-m-d H:i:s'),
'endTime' => $coupon->end_at->format('Y-m-d H:i:s'),
];
}
return $coupon;
} catch (\Exception $e) {
return $e->getMessage();
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace XuanChen\Coupon\Action\ysd;
use App\Models\ActivityCoupon;
use App\Models\User;
use XuanChen\Coupon\Action\Init;
class YsdQuery extends Init
{
public function start()
{
try {
$info = User::where('outlet_id', $this->outletId)->first();
if (!$info) {
throw new \Exception('网点编号错误,未查询到网点信息');
}
$coupon = ActivityCoupon::where('code', $this->redemptionCode)->first();
if (!$coupon) {
throw new \Exception('卡券编号错误,未查询到卡券信息');
}
if ($coupon->activity->user_id > 0 && $info->parent_id != $coupon->activity->user_id) {
throw new \Exception('您没有权限查询此卡券信息。');
}
return $coupon;
} catch (\Exception $e) {
return $e->getMessage();
}
}
}

View File

@@ -0,0 +1,189 @@
<?php
namespace XuanChen\Coupon\Action\ysd;
use App\Models\ActivityCoupon;
use App\Models\Coupon;
use Illuminate\Support\Facades\DB;
use XuanChen\Coupon\Action\Init;
class YsdVerification extends Init
{
public $ticket;
/**
* Notes: 核销具体流程
* @Author: 玄尘
* @Date : 2020/7/29 13:12
* @return array|string
*/
public function start()
{
//检查可核销次数
$ret = $this->CheckCount();
if ($ret !== true) {
return $ret;
}
//查询卡券信息
$this->query_coupon = (new YsdQuery)->setOutletId($this->outletId)
->setCode($this->redemptionCode)
->start();
if (is_string($this->query_coupon)) {
return $this->query_coupon;
}
//校验卡券
$ticket = $this->checkCoupon();
if (!is_array($ticket)) {
return $ticket;
}
//增加核销记录
$coupon = $this->AddCoupon();
if (is_string($coupon)) {
return $coupon;
}
DB::beginTransaction();
try {
$this->query_coupon->status = ActivityCoupon::STATUS_USED;
$this->query_coupon->used_at = now();
$this->query_coupon->save();
$this->coupon->status = 2;
$this->coupon->remark = '核销成功';
$this->coupon->save();
//返回的数据
$resdata = [
'name' => $this->coupon->couponName,
'total' => $this->coupon->total,
'price' => $this->coupon->price,
];
//核销成功 执行分润
$this->coupon->profit();
DB::commit();
return $resdata;
} catch (Exception $e) {
DB::rollback();
$this->coupon->status = 3;
$this->coupon->remark = '核销失败 ' . $e->getMessage();
$this->coupon->save();
return $this->coupon->remark;
}
}
/**
* Notes: 如可核销记录
* @Author: 玄尘
* @Date : 2020/7/21 15:03
* @return string
*/
public function AddCoupon()
{
DB::beginTransaction();
try {
$couponData = [
'user_id' => $this->user->id,
'type' => Coupon::TYPE_YSD,
'outletId' => $this->outletId,
'PaOutletId' => '',
'redemptionCode' => $this->redemptionCode,
'thirdPartyGoodsId' => $this->query_coupon->activity->rule->code,
'couponName' => $this->query_coupon->activity->title,
'price' => $this->ticket['price'],
'total' => $this->total,
'profit' => $this->ticket['profit'],
'status' => $this->query_coupon->status,
'startTime' => $this->query_coupon->start_at,
'endTime' => $this->query_coupon->end_at,
];
$this->coupon = Coupon::create($couponData);
DB::commit();
return $this->coupon;
} catch (Exception $e) {
DB::rollback();
return $e->getMessage();
}
}
/**
* Notes: 检查卡券信息
* @Author: 玄尘
* @Date : 2020/6/29 16:40
* @return string
*/
public function checkCoupon()
{
if (!$this->query_coupon->canRedemption()) {
return '核销失败,不可核销';
}
if ($this->query_coupon->activity->user_id > 0 && $this->user->id !== $this->query_coupon->activity->user_id) {
return '核销失败,您没有权限核销此优惠券。';
}
$now = now();
if ($this->query_coupon->start_at->gt($now)) {
return '核销失败,卡券未到可用时间。请在' . $this->query_coupon->start_at->format('Y-m-d H:i:s') . '后使用';
}
if ($now->gt($this->query_coupon->end_at)) {
return '核销失败,卡券已过期。';
}
$rule_code = $this->query_coupon->activity->rule->code;
info($rule_code);
$code = $this->user->code->where('code', $rule_code)->first();
info($this->user->code);
info($code);
if (!$code) {
return "核销失败,您没有权限使用此卡券优惠活动。";
}
$ticket = explode('-', $rule_code);
if (!is_array($ticket) || count($ticket) != 3) {
return "核销失败,卡券规则格式不正确";
}
$full = $ticket[1]; //full100
$price = $ticket[2];
// preg_match('/(\d{3}(\.\d+)?)/is', $full, $match);
preg_match('/\d+/', $full, $match);
if (!is_array($match)) {
return "核销失败,卡券规则格式不正确。";
}
if (!is_numeric($this->total)) {
return "核销失败,订单金额必须是数字";
}
if ($match[0] > $this->total) {
return '核销失败,订单金额不足。';
}
return $this->ticket = [
'total' => $match[0],
'price' => $price,
'profit' => $code->profit,
];
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace XuanChen\Coupon\Contracts;
interface CheckCouponContracts
{
/**
* start
* @return [type] [description]
*/
function start();
}

View File

@@ -0,0 +1,30 @@
<?php
namespace XuanChen\Coupon\Contracts;
interface CouponContracts
{
//发券接口
function grant();
/**
* Notes: 查询卡券详情
* @Author: 玄尘
* @Date : 2020/6/29 15:15
* @return mixed
*/
function detail();
//作废接口
function destroy();
/**
* Notes: 核销执行入口
* @Author: 玄尘
* @Date : 2020/6/29 14:49
* @return mixed
*/
function start();
}

View File

@@ -0,0 +1,137 @@
<?php
namespace XuanChen\Coupon;
use App\Models\Activity;
use App\Models\User;
use Illuminate\Support\Facades\DB;
/**
* 自有卡券系统
*/
class Coupon
{
/**
* Notes: 发券接口
* @Author: 玄尘
* @Date : 2020/6/28 15:07
* @param $activityId 活动编号
* @param $outletId 网点编号
* @param $mobile 手机号
*/
public static function Grant($activityId, $outletId, $mobile)
{
$model = config('xuanchen_coupon.rules.ysd.model');
return (new $model)->setActivityId($activityId)
->setOutletId($outletId)
->setMobile($mobile)
->grant();
}
/**
* Notes: 查询接口
* @Author: 玄尘
* @Date : 2020/7/21 11:58
* @param $redemptionCode
*/
public static function Query($redemptionCode, $outletId)
{
if (!$redemptionCode) {
return '查询失败,未获取到券码';
}
$model = self::getModelByCode($redemptionCode);
if (is_string($model)) {
return $model;
}
return $model->setCode($redemptionCode)
->setOutletId($outletId)
->detail();
}
/**
* Notes: 卡券作废
* @Author: 玄尘
* @Date : 2020/7/21 13:49
* @param $redemptionCode
* @return string
* @throws \Exception
*/
public static function Destroy($redemptionCode)
{
try {
$model = self::getModelByCode($redemptionCode);
if (is_string($model)) {
return $model;
}
return $model->setCode($redemptionCode)->destroy();
} catch (\Exception $e) {
return $e->getMessage();
}
}
/**
* Notes: 根据券码 获取class
* @Author: 玄尘
* @Date : 2020/7/21 12:00
* @param $code
* @return string
*/
public static function getModelByCode($code)
{
$rules = config('xuanchen_coupon.rules');
if (!$rules) {
return '系统出错,未找到配置文件';
}
$model = '';
foreach ($rules as $rule) {
if (preg_match($rule['pattern'], $code, $matches)) {
$model = $rule['model'];
break;
}
}
if (!$model) {
throw new \Exception('卡券核销失败。未查到卡券所属');
}
return new $model;
}
/**
* Notes: 核销卡券
* @Author: 玄尘
* @Date : 2020/6/29 15:24
* @param $user
* @return string
*/
public static function Redemption($user, $redemptionCode, $total, $outletId)
{
try {
$model = self::getModelByCode($redemptionCode);
if (is_string($model)) {
return $model;
}
return $model->setUser($user)
->setCode($redemptionCode)
->setTotal($total)
->setOutletId($outletId)
->start();
} catch (\Exception $e) {
return $e->getMessage();
}
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace XuanChen\Coupon;
use Illuminate\Support\ServiceProvider;
class CouponServiceProvider extends ServiceProvider
{
/**
* Register services.
* @return void
*/
public function register()
{
dd(1);
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/xuanchen_coupon.php' => config_path('xuanchen_coupon.php')]);
}
}
/**
* Bootstrap services.
* @return void
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__ . '/../config/xuanchen_coupon.php', 'xuanchen_coupon');
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace XuanChen\Coupon;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class ServiceProvider extends LaravelServiceProvider
{
/**
* Register services.
* @return void
*/
public function register()
{
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/xuanchen_coupon.php' => config_path('xuanchen_coupon.php')]);
}
}
/**
* Bootstrap services.
* @return void
*/
public function boot()
{
$this->mergeConfigFrom(__DIR__ . '/../config/xuanchen_coupon.php', 'xuanchen_coupon');
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace XuanChen\Coupon\Traits;
use App\Models\Log as LogModel;
trait Log
{
/**
* Notes: 插入日志
* @Author: 玄尘
* @Date : 2020/6/30 10:29
* @param $url
* @param $method
* @param $params
* @param string $type
* @return mixed
*/
public function createLog($url, $method, $params, $type = 'pingan')
{
$data = [
'path' => $url,
'method' => $method,
'type' => $type,
'in_source' => $params,
];
$info = LogModel::create($data);
return $info;
}
/**
* Notes: 更新日志
* @Author: 玄尘
* @Date : 2020/6/30 10:29
* @param $log
* @param $params
*/
public static function updateLog($log, $params)
{
$log->out_source = $params;
$log->save();
}
}

View File

@@ -0,0 +1,79 @@
<?php
namespace XuanChen\Coupon\Traits;
trait SetParams
{
//渠道
public $user;
//卡券编号
public $redemptionCode;
//订单金额
public $total;
//网点编号
public $outletId;
//活动id
public $activityId;
//手机号
public $mobile;
//设置渠道
public function setUser($user)
{
$this->user = $user;
return $this;
}
//设置核销码
public function setCode($redemptionCode)
{
$this->redemptionCode = $redemptionCode;
return $this;
}
//设置订单总额
public function setTotal($total)
{
$this->total = $total;
return $this;
}
//设置网点id
public function setOutletId($outletId)
{
$this->outletId = $outletId;
return $this;
}
//设置活动id
public function setActivityId($activityId)
{
$this->activityId = $activityId;
return $this;
}
//设置手机号
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
}

View File

@@ -0,0 +1,4 @@
# Identity 身份系统
# trait User 模型 涉及函数 RuLong\Identity\Traits\UserHasIdentity
# 身份变更 $User->identityUpdate(Identity_id,Channel) // (身份ID,变更渠道)
# 身份加权点数 $User->addPoint(Identity_id,Point) // (身份ID,加权数额默认1)

View File

@@ -0,0 +1,27 @@
{
"name": "rulong/identity",
"description": "",
"license": "MIT",
"authors": [
{
"name": "C.Jason",
"email": "chenjxlg@163.com"
}
],
"require": {
"php": ">=7.1.3",
"laravel/framework": "*"
},
"autoload": {
"psr-0": {
"RuLong\\Identity": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"RuLong\\Identity\\ServiceProvider"
]
}
}
}

View File

@@ -0,0 +1,9 @@
<?php
return [
'user_model' => \App\Models\User::class,
'channel' => [
'AutoUp' => '自动升级',
'EmptyUp' => '后台升级',
],
'default_identity' => '普通用户',
];

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateIdentitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('identities', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('identities');
}
}

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateIdentityLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('identity_logs', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('identity_logs');
}
}

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserIdentitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_identities', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_identities');
}
}

View File

@@ -0,0 +1,31 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateIdentityPointsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('identity_points', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('identity_points');
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace RuLong\Identity\Contracts;
interface IdentityContracts
{
/**
* 子规则的实际计算过程
* @Author:<C.Jason>
* @Date:2018-10-31T14:22:48+0800
*/
function fire();
}

View File

@@ -0,0 +1,47 @@
<?php
namespace RuLong\Identity\Events;
use App\User;
use RuLong\Identity\Contracts\IdentityContracts;
use RuLong\Identity\Models\IdentityPoint as IdentityPointModel;
use RuLong\Identity\Traits\Identityable;
class IdentityPoint implements IdentityContracts
{
use Identityable;
public $user;
public $identity_id;
public $point;
public function __construct($user, array $other = null)
{
$this->user = User::find($user->id);
$this->identity_id = $other['identity_id'];
if (isset($other['point']) && is_numeric($other['point'])) {
$this->point = $other['point'];
} else {
$this->point = 1;
}
}
/**
* 创建及修改身份
* @Author:<Leady>
* @Date:2018-11-05T13:21:47+0800
* @return [type] [description]
*/
public function fire()
{
if ($this->user->id === 1) {
return false;
}
IdentityPointModel::create([
'user_id' => $this->user->id,
'identity_id' => $this->identity_id,
'point' => $this->point,
]);
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace RuLong\Identity\Events;
use RuLong\Identity\Contracts\IdentityContracts;
use RuLong\Identity\Models\IdentityLog as IdentityLogModel;
use RuLong\Identity\Models\UserIdentity as UserIdentityModel;
use RuLong\Identity\Traits\Identityable;
class IdentityUpdate implements IdentityContracts
{
use Identityable;
public $user;
public $identity_id;
public $channel;
public $other;
public function __construct($user, array $other = null)
{
$this->user = $user;
$this->identity_id = $other['identity_id'];
$this->channel = $other['channel'] ?? '';
$this->other = $other;
}
/**
* 创建及修改身份
* @Author:<Leady>
* @Date:2018-11-05T13:21:47+0800
* @return [type] [description]
*/
public function fire()
{
$before = 0;
$identity_info = UserIdentityModel::where('user_id', $this->user->id)->first();
if ($identity_info) {
$before = $identity_info->identity_id;
$identity_info->identity_id = $this->identity_id;
$identity_info->save();
} else {
$data = [
'user_id' => $this->user->id,
'identity_id' => $this->identity_id,
];
UserIdentityModel::create($data);
}
$log = IdentityLogModel::create(['user_id' => $this->user->id, 'before' => $before, 'after' => $this->identity_id, 'channel' => $this->channel, 'other' => $this->other]);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace RuLong\Identity;
use RuLong\Identity\Contracts\IdentityContracts;
class Identity
{
protected $contracts;
public function __construct(IdentityContracts $contracts)
{
$this->contracts = $contracts;
}
public function __destruct()
{
$this->contracts->fire();
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace RuLong\Identity\Models;
use App\Models\IdentityGift;
use App\Models\IdentityPrice;
use Illuminate\Database\Eloquent\Model;
class Identity extends Model
{
//
protected $casts = [
'configs' => 'array',
];
public function prices()
{
return $this->hasMany(IdentityPrice::class);
}
public function codes()
{
return $this->hasMany(IdentityCode::class);
}
/**
* 获取身份指定日期的佣金设定值
* @Date:2019-09-16T13:53:36+0800
* @param string $date [Y-m-d H:i:s]
* @return [type] [description]
*/
public function getPrice($date = '')
{
$date = $date ?: now()->endOfDay()->format('Y-m-d H:i:s');
$info = IdentityPrice::where('identity_id', $this->id)->where('created_at', '<', $date)->orderBy('created_at', 'desc')->first();
if ($info) {
return $info->configs;
} else {
return false;
}
}
public function gifts()
{
return $this->hasMany(IdentityGift::class);
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace RuLong\Identity\Models;
use Illuminate\Database\Eloquent\Model;
class IdentityCode extends Model
{
protected $guarded = [];
public function identity()
{
return $this->belongsTo(Identity::class);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace RuLong\Identity\Models;
use Illuminate\Database\Eloquent\Model;
class IdentityLog extends Model
{
protected $guarded = [];
protected $casts = [
'other' => 'array',
];
//
public function user()
{
return $this->belongsTo(config('user_account.user_model'))->withDefault();
}
protected function getBeforeIdentityTitleAttribute()
{
return Identity::where('id', $this->before)->value('title') ?: '普通用户';
}
protected function getIdentityTitleAttribute()
{
return Identity::where('id', $this->after)->value('title') ?: '';
}
public function getChannelTextAttribute()
{
return config('rulong_identity.channel.' . $this->channel) ?? '无';
}
}

View File

@@ -0,0 +1,15 @@
<?php
namespace RuLong\Identity\Models;
use Illuminate\Database\Eloquent\Model;
class IdentityPoint extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(config('user_account.user_model'), 'user_id', 'id');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace RuLong\Identity\Models;
use Illuminate\Database\Eloquent\Model;
class UserIdentity extends Model
{
protected $primaryKey = 'user_id';
public $incrementing = false;
protected $guarded = [];
public function info()
{
return $this->belongsTo(Identity::class, 'identity_id');
}
public function logs()
{
return $this->hasMany(IdentityLog::class, 'user_id', 'user_id');
}
public function user()
{
return $this->belongsTo(config('user_account.user_model'))->withDefault();
}
public function getEmptyTextAttribute()
{
$info = IdentityLog::where('channel', 'EmptyUp')->first();
if ($info) {
return "";
} else {
return "";
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace RuLong\Identity;
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
class ServiceProvider extends LaravelServiceProvider
{
/**
* 部署时加载
* @Author:<C.Jason>
* @Date:2018-06-22T16:01:20+0800
* @return [type] [description]
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/rulong_identity.php' => config_path('rulong_identity.php')]);
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations/');
}
}
/**
* 注册服务提供者
* @Author:<C.Jason>
* @Date:2018-06-22T16:01:12+0800
* @return [type] [description]
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/rulong_identity.php', 'rulong_identity');
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace RuLong\Identity\Traits;
use RuLong\Identity\Identity;
trait Identityable
{
public static function start()
{
return new Identity(new static(...func_get_args()));
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace RuLong\Identity\Traits;
use RuLong\Identity\Models\Identity;
use RuLong\Identity\Models\UserIdentity;
use \RuLong\Identity\Events\IdentityPoint;
use \RuLong\Identity\Events\IdentityUpdate;
use \RuLong\Identity\Models\IdentityPoint as IdentityPointModel;
trait UserHasIdentity
{
/**
* 获取身份标题
* @Author:<C.Jason>
* @Date:2018-11-09T17:00:39+0800
* @return [type] [description]
*/
public function getIdentityTextAttribute()
{
return Identity::where('id', $this->identity->identity_id)->value('title') ?: config('rulong_identity.default_identity');
}
public function getIdentityIdAttribute()
{
return $this->identity->identity_id;
}
public function getNameTextAttribute()
{
return Identity::where('id', $this->identity->identity_id)->value('name') ?: config('rulong_identity.default_identity');
}
public function pointlogs()
{
return $this->hasMany(IdentityPointModel::class);
}
public function identity()
{
return $this->hasOne(UserIdentity::class)->withDefault(['identity_id' => 0]);
}
/**
* User模型涉及函数辅助快速执行修改身份
* [identityUpdate description]
* @Author:<Leady>
* @Date:2018-11-07T16:24:44+0800
* @param [type] $id [身份ID]
* @return [type] [description]
*/
public function identityUpdate($id, $channel = 'AutoUp', $other = [])
{
$data = ['identity_id' => $id, 'channel' => $channel];
if (!empty($other)) {
$data = array_merge($data, $other);
}
return IdentityUpdate::start($this, $data);
}
/**
* User模型设计函数辅助快速增加身份积分。
* @Author:<Leady>
* @Date:2018-11-07T16:25:45+0800
* @param [type] $id [description]
* @param integer $point [description]
*/
public function addPoint($id, $point = 1)
{
return IdentityPoint::start($this, ['identity_id' => $id, 'point' => $point]);
}
}