update: coupon generate

This commit is contained in:
Tokumeikoi 2020-10-24 23:08:14 +08:00
parent 4ca1b9e8ff
commit 1a94a48cf4
12 changed files with 157 additions and 23 deletions

View File

@ -3,22 +3,34 @@
namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\CouponSave;
use App\Http\Requests\Admin\CouponGenerate;
use App\Models\Plan;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Coupon;
use App\Utils\Helper;
use Illuminate\Support\Facades\DB;
class CouponController extends Controller
{
public function fetch(Request $request)
{
$coupon = Coupon::orderBy('created_at', 'desc');
$coupons = $coupon->get();
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
$sortType = in_array($request->input('sort_type'), ['ASC', 'DESC']) ? $request->input('sort_type') : 'DESC';
$sort = $request->input('sort') ? $request->input('sort') : 'created_at';
$builder = Coupon::orderBy($sort, $sortType);
$total = $builder->count();
$coupons = $builder->forPage($current, $pageSize)
->get();
foreach ($coupons as $k => $v) {
if ($coupons[$k]['limit_plan_ids']) $coupons[$k]['limit_plan_ids'] = json_decode($coupons[$k]['limit_plan_ids']);
}
return response([
'data' => $coupons
'data' => $coupons,
'total' => $total
]);
}
@ -48,6 +60,66 @@ class CouponController extends Controller
]);
}
public function generate(CouponGenerate $request)
{
if ($request->input('generate_count')) {
$this->multiGenerate($request);
return;
}
$params = $request->validated();
if (isset($params['limit_plan_ids'])) {
$params['limit_plan_ids'] = json_encode($params['limit_plan_ids']);
}
if (!$request->input('id')) {
if (!isset($params['code'])) {
$params['code'] = Helper::randomChar(8);
}
if (!Coupon::create($params)) {
abort(500, '创建失败');
}
} else {
try {
Coupon::find($request->input('id'))->update($params);
} catch (\Exception $e) {
abort(500, '保存失败');
}
}
return response([
'data' => true
]);
}
private function multiGenerate(CouponGenerate $request)
{
$coupons = [];
for ($i = 0;$i < $request->input('generate_count');$i++) {
$coupon = $request->validated();
$coupon['limit_plan_ids'] = json_encode($coupon['limit_plan_ids']);
$coupon['code'] = Helper::randomChar(8);
$coupon['created_at'] = $coupon['updated_at'] = time();
unset($coupon['generate_count']);
array_push($coupons, $coupon);
}
DB::beginTransaction();
if (!Coupon::insert($coupons)) {
DB::rollBack();
abort(500, '生成失败');
}
DB::commit();
$data = "名称,类型,金额或比例,开始时间,结束时间,可用次数,可用于订阅,券码,生成时间\r\n";
foreach($coupons as $coupon) {
$type = ['金额', '比例'][$coupon['type']];
$startTime = date('Y-m-d H:i:s', $coupon['started_at']);
$endTime = date('Y-m-d H:i:s', $coupon['ended_at']);
$limitUse = $coupon['limit_use'] ?? '不限制';
$createTime = date('Y-m-d H:i:s', $coupon['created_at']);
$data .= "{$coupon['name']},{$type},{$coupon['value']},{$startTime},{$endTime},{$limitUse},{$coupon['limit_plan_ids']},{$coupon['code']},{$createTime}\r\n";
}
echo $data;
}
public function drop(Request $request)
{
if (empty($request->input('id'))) {

View File

@ -22,10 +22,6 @@ class UserController extends Controller
{
if ($request->input('filter')) {
foreach ($request->input('filter') as $filter) {
if ($filter['key'] === 'email') {
$builder->where($filter['key'], 'like', '%' . $filter['value'] . '%');
continue;
}
if ($filter['key'] === 'invite_by_email') {
$user = User::where('email', $filter['value'])->first();
if (!$user) continue;
@ -35,6 +31,10 @@ class UserController extends Controller
if ($filter['key'] === 'd' || $filter['key'] === 'transfer_enable') {
$filter['value'] = $filter['value'] * 1073741824;
}
if ($filter['condition'] === '模糊') {
$filter['condition'] = 'like';
$filter['value'] = "%{$filter['value']}%";
}
$builder->where($filter['key'], $filter['condition'], $filter['value']);
}
}
@ -164,7 +164,7 @@ class UserController extends Controller
'data' => true
]);
}
if ($request->input('count')) {
if ($request->input('generate_count')) {
$this->multiGenerate($request);
}
}
@ -178,7 +178,7 @@ class UserController extends Controller
}
}
$users = [];
for ($i = 0;$i < $request->input('count');$i++) {
for ($i = 0;$i < $request->input('generate_count');$i++) {
$user = [
'email' => Helper::randomChar(6) . '@' . $request->input('email_suffix'),
'plan_id' => isset($plan->id) ? $plan->id : NULL,

View File

@ -20,31 +20,35 @@ class ClientController extends Controller
{
public function subscribe(Request $request)
{
$flag = $request->input('flag')
? $request->input('flag')
: isset($_SERVER['HTTP_USER_AGENT'])
? $_SERVER['HTTP_USER_AGENT']
: '';
$flag = strtolower($flag);
$user = $request->user;
// account not expired and is not banned.
$userService = new UserService();
if ($userService->isAvailable($user)) {
$serverService = new ServerService();
$servers = $serverService->getAllServers($user);
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$_SERVER['HTTP_USER_AGENT'] = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'quantumult%20x') !== false) {
if ($flag) {
if (strpos($flag, 'quantumult%20x') !== false) {
die($this->quantumultX($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'quantumult') !== false) {
if (strpos($flag, 'quantumult') !== false) {
die($this->quantumult($user, $servers['vmess']));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'clash') !== false) {
if (strpos($flag, 'clash') !== false) {
die($this->clash($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'surfboard') !== false) {
if (strpos($flag, 'surfboard') !== false) {
die($this->surfboard($user, $servers['shadowsocks'], $servers['vmess']));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'surge') !== false) {
if (strpos($flag, 'surge') !== false) {
die($this->surge($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'shadowrocket') !== false) {
if (strpos($flag, 'shadowrocket') !== false) {
die($this->shadowrocket($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
}
}

View File

@ -113,6 +113,7 @@ class OrderController extends Controller
DB::rollBack();
abort(500, '优惠券使用失败');
}
$order->coupon_id = $couponService->getId();
}
$orderService->setVipDiscount($user);

View File

@ -0,0 +1,47 @@
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class CouponGenerate extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'generate_count' => 'nullable|integer|max:500',
'name' => 'required',
'type' => 'required|in:1,2',
'value' => 'required|integer',
'started_at' => 'required|integer',
'ended_at' => 'required|integer',
'limit_use' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array',
'code' => ''
];
}
public function messages()
{
return [
'generate_count.integer' => '生成数量必须为数字',
'generate_count.max' => '生成数量最大为500个',
'name.required' => '名称不能为空',
'type.required' => '类型不能为空',
'type.in' => '类型格式有误',
'value.required' => '金额或比例不能为空',
'value.integer' => '金额或比例格式有误',
'started_at.required' => '开始时间不能为空',
'started_at.integer' => '开始时间格式有误',
'ended_at.required' => '结束时间不能为空',
'ended_at.integer' => '结束时间格式有误',
'limit_use.integer' => '使用次数格式有误',
'limit_plan_ids.array' => '指定订阅格式有误'
];
}
}

View File

@ -15,7 +15,7 @@ class UserFetch extends FormRequest
{
return [
'filter.*.key' => 'required|in:id,email,transfer_enable,d,expired_at,uuid,token,invite_by_email,invite_user_id',
'filter.*.condition' => 'required|in:>,<,=,>=,<=',
'filter.*.condition' => 'required|in:>,<,=,>=,<=,模糊',
'filter.*.value' => 'required'
];
}

View File

@ -14,7 +14,7 @@ class UserGenerate extends FormRequest
public function rules()
{
return [
'count' => 'nullable|integer|max:500',
'generate_count' => 'nullable|integer|max:500',
'expired_at' => 'nullable|integer',
'plan_id' => 'nullable|integer',
'email_prefix' => 'nullable',
@ -26,6 +26,8 @@ class UserGenerate extends FormRequest
public function messages()
{
return [
'generate_count.integer' => '生成数量必须为数字',
'generate_count.max' => '生成数量最大为500个'
];
}
}

View File

@ -84,7 +84,7 @@ class AdminRoute
$router->post('/ticket/close', 'Admin\\TicketController@close');
// Coupon
$router->get ('/coupon/fetch', 'Admin\\CouponController@fetch');
$router->post('/coupon/save', 'Admin\\CouponController@save');
$router->post('/coupon/generate', 'Admin\\CouponController@generate');
$router->post('/coupon/drop', 'Admin\\CouponController@drop');
// Knowledge
$router->get ('/knowledge/fetch', 'Admin\\KnowledgeController@fetch');

View File

@ -51,4 +51,9 @@ class CouponService
}
return true;
}
public function getId()
{
return $this->coupon->id;
}
}

View File

@ -339,3 +339,6 @@ CREATE TABLE `v2_knowledge` (
`created_at` int(11) NOT NULL COMMENT '創建時間',
`updated_at` int(11) NOT NULL COMMENT '更新時間'
) COMMENT='知識庫' COLLATE 'utf8mb4_general_ci';
ALTER TABLE `v2_order`
ADD `coupon_id` int(11) NULL AFTER `plan_id`;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long