阶段更新
This commit is contained in:
449
modules/User/Http/Controllers/Api/Identity/IndexController.php
Normal file
449
modules/User/Http/Controllers/Api/Identity/IndexController.php
Normal file
@@ -0,0 +1,449 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers\Api\Identity;
|
||||
|
||||
use App\Api\Controllers\Controller;
|
||||
use Carbon\Carbon;
|
||||
use Encore\Admin\Form\Field\Id;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Jason\Api\Api;
|
||||
use Modules\Coupon\Models\Coupon;
|
||||
use Modules\Coupon\Traits\WithCoupon;
|
||||
use Modules\User\Http\Resources\IdentityMiddleResource;
|
||||
use Modules\User\Http\Resources\UserIdentityResource;
|
||||
use Modules\User\Models\Identity;
|
||||
use Modules\User\Models\Order;
|
||||
use Modules\User\Models\UserInvite;
|
||||
|
||||
class IndexController extends Controller
|
||||
{
|
||||
use WithCoupon;
|
||||
|
||||
/**
|
||||
* Notes: 获取身份
|
||||
* type 1 体验官 2 正式身份
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/8/11 10:52
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
|
||||
$type = $request->type ?? '';
|
||||
$user = Api::user();
|
||||
$list = Identity::where('order', '>', 1)->where('can_buy', 1)->get();
|
||||
$data = [
|
||||
'user' => [
|
||||
'username' => $user->username,
|
||||
'nickname' => $user->info->nickname,
|
||||
'avatar' => $user->info->avatar,
|
||||
'identity' => $user->identities->count()
|
||||
? new IdentityMiddleResource($user->identityMiddle()->first())
|
||||
: '',
|
||||
],
|
||||
'identities' => UserIdentityResource::collection($list),
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取可开通的身份
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/6/4 9:37
|
||||
* @param Identity $identity
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function create(Identity $identity): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
$ended_at = '';
|
||||
//可开通的身份ids
|
||||
$ids = Identity::where('can_buy', 1)->pluck('id')->toArray();
|
||||
$identity_middles = $user->identityMiddle;
|
||||
|
||||
//是否可以前台开通
|
||||
if (in_array($identity->id, $ids)) {
|
||||
//有有效期
|
||||
if ($identity->years) {
|
||||
//已经开通的身份ids
|
||||
$identity_ids = $identity_middles->pluck('identity_id')->toArray();
|
||||
//是否已经开通了此身份
|
||||
if (in_array($identity->id, $identity_ids)) {
|
||||
$ended_at = $identity_middles->where('identity_id', $identity->id)
|
||||
->first()->ended_at ?? now()->format('Y-m-d H:i:s');
|
||||
|
||||
$ended_at = Carbon::parse($ended_at)->addMonths($identity->years)->format('Y/m/d');
|
||||
} else {
|
||||
$ended_at = now()->addMonths($identity->years)->format('Y/m/d');
|
||||
}
|
||||
} else {
|
||||
$ended_at = '永久';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// $price = $identity->getCondition('price', '0');
|
||||
// if ($identity->job == Identity::JOB_JK) {
|
||||
// $coupon =
|
||||
// }
|
||||
|
||||
$price = $identity->getCondition('price', '0');
|
||||
$cost = $identity->getCondition('cost', '0');
|
||||
if (! $cost) {
|
||||
$cost = $price;
|
||||
}
|
||||
|
||||
$coupon_price = 0;
|
||||
|
||||
|
||||
$data = [
|
||||
'identity' => [
|
||||
'identity_id' => $identity->id,
|
||||
'name' => $identity->name,
|
||||
'cover' => $identity->cover_url,
|
||||
'description' => $ended_at ? '有效期至'.$ended_at : $identity->description,
|
||||
'coupon_price' => $coupon_price,
|
||||
'cost' => $cost,
|
||||
'price' => $price,
|
||||
'can_buy' => (bool) $identity->can_buy,
|
||||
'ended_at' => $ended_at,
|
||||
'service' => config('user.experience.service'),
|
||||
'protocol_url' => (string) $identity->protocol_url,//协议地址
|
||||
],
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 开通会员
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/6/4 10:02
|
||||
* @param Identity $identity
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function store(Identity $identity, Request $request): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$price = $request->price ?? 0;
|
||||
if ($identity->job != Identity::JOB_TY) {
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required',
|
||||
// 'card_no' => 'required|numeric',
|
||||
'cover' => 'required',
|
||||
'price' => 'required',
|
||||
], [
|
||||
'name.required' => '缺少姓名',
|
||||
'card_no.required' => '缺少银行卡号',
|
||||
'card_no.numeric' => '银行卡号只能是数字',
|
||||
'cover.required' => '缺少打款凭证',
|
||||
'price.required' => '缺少打款金额',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->failed($validator->errors()->first());
|
||||
}
|
||||
|
||||
|
||||
// if ($identity->job == Identity::JOB_NK && ! $user->parent) {
|
||||
// return $this->failed('没有推荐人不可开通年卡会员');
|
||||
// }
|
||||
} else {
|
||||
$total = $identity->users()->count();
|
||||
if ($total >= 100) {
|
||||
return $this->failed('体验官最多可开通100人');
|
||||
}
|
||||
$end_at = $identity->end_at;
|
||||
if ($end_at) {
|
||||
$end_at = Carbon::parse($end_at)->endOfDay();
|
||||
if (now()->gt($end_at)) {
|
||||
return $this->failed('体验官活动已过期');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$tencentMap = app('xuanchen.tencent.map');
|
||||
$res = $tencentMap->api()->ip(request()->ip())->toArray();
|
||||
if (! in_array($res['ad_info']['city'], ['深圳', '深圳市'])) {
|
||||
// return $this->failed('体验官活动只限于深圳用户参加');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (! $identity->can_buy) {
|
||||
return $this->failed('当前身份不可开通,请联系客服开通。');
|
||||
}
|
||||
|
||||
$type = Order::TYPE_OPEN;
|
||||
$year = $request->year ?? 1;
|
||||
//是否可以开通多身份 false不可
|
||||
if (config('identity.can_has_many_identity') == false) {
|
||||
if ($user->identities->isNotEmpty()) {
|
||||
$this_identity = $user->identities->first();
|
||||
|
||||
if ($this_identity->order > $identity->order) {
|
||||
return $this->failed('不可降级开通');
|
||||
}
|
||||
|
||||
if ($this_identity->id == $identity->id) {
|
||||
$type = Order::TYPE_RENEW;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array($identity->job, [Identity::JOB_JK, Identity::JOB_NK])) {
|
||||
$hasOne = Order::query()
|
||||
->byUser($user)
|
||||
->where('identity_id', $identity->id)
|
||||
->where('state', Order::STATE_INIT)
|
||||
->exists();
|
||||
|
||||
if ($hasOne) {
|
||||
return $this->failed('您已经提交过了,请等待审核');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (! $price) {
|
||||
$price = $identity->getCondition('price', '0');
|
||||
$price = $price * $year;
|
||||
}
|
||||
|
||||
$coupon_price = 0;
|
||||
|
||||
$data = [
|
||||
'user_id' => $user->id,
|
||||
'identity_id' => $identity->id,
|
||||
'year' => $year,
|
||||
'type' => $type,
|
||||
'stock' => $identity->stock,
|
||||
'name' => $request->name,
|
||||
'card_no' => $request->card_no,
|
||||
'cover' => $request->cover,
|
||||
'state' => Order::STATE_INIT,
|
||||
'price' => $price,
|
||||
];
|
||||
|
||||
$order = Order::create($data);
|
||||
|
||||
if ($order) {
|
||||
return $this->success('提交成功,请等待后台审核');
|
||||
} else {
|
||||
return $this->failed('创建订单失败,请稍后再试');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 微信支付
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/6/7 14:42
|
||||
* @param Order $order
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function wechat(Order $order, Request $request): JsonResponse
|
||||
{
|
||||
$channel = $request->channel ?? 'mp';
|
||||
$openid = $request->openid ?? '';
|
||||
|
||||
$user = Api::user();
|
||||
|
||||
if (! $order->canPay()) {
|
||||
return $this->failed('支付失败,订单不可支付');
|
||||
}
|
||||
|
||||
if ($order->user()->isNot($user)) {
|
||||
return $this->failed('支付失败,您没有权限支付');
|
||||
}
|
||||
|
||||
$extends = [
|
||||
'notify_url' => route('api.payment.notify.wechat'),
|
||||
];
|
||||
|
||||
if (! $openid) {
|
||||
$openid = $user->wechat->getUserOpenid($channel);
|
||||
}
|
||||
|
||||
$payment = $order->createWechatPayment($user, $order->price, $channel);
|
||||
if (! $openid) {
|
||||
return $this->failed('支付失败,缺少openid');
|
||||
}
|
||||
|
||||
if (config('payment.version', 2) == 2) {
|
||||
$extends = array_merge($extends, [
|
||||
'openid' => $openid,
|
||||
]);
|
||||
} else {
|
||||
$extends = array_merge($extends, [
|
||||
'payer' => [
|
||||
'openid' => $openid,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
$notify = $payment->getPaymentParams('商品下单', $extends);
|
||||
|
||||
if (config('payment.version', 2) == 2) {
|
||||
$notify = $notify->getContent();
|
||||
} else {
|
||||
$notify = $notify->toJson();
|
||||
}
|
||||
|
||||
$data = [
|
||||
'wechat' => $notify,
|
||||
'identity' => [
|
||||
'identity_id' => $order->identity_id,
|
||||
'name' => $order->identity->name,
|
||||
],
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 支付宝支付
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/6/7 14:42
|
||||
* @param Order $order
|
||||
* @return JsonResponse
|
||||
* @throws Exception
|
||||
*/
|
||||
public function alipay(Order $order): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
if (! $order->canPay()) {
|
||||
return $this->failed('支付失败,订单不可支付');
|
||||
}
|
||||
|
||||
if ($order->user()->isNot($user)) {
|
||||
return $this->failed('支付失败,您没有权限支付');
|
||||
}
|
||||
|
||||
$payment = $order->createAlipayPayment($user, $order->price, 'app');
|
||||
|
||||
$notify = $payment->getPaymentParams('商品下单', [
|
||||
'notify_url' => route('api.payment.notify.alipay'),
|
||||
]);
|
||||
|
||||
$data = [
|
||||
'wechat' => $notify->getContent(),
|
||||
'identity' => [
|
||||
'identity_id' => $order->identity_id,
|
||||
'name' => $order->identity->name,
|
||||
],
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 根据不同身份判定用户折扣
|
||||
*
|
||||
* @Author: Mr.wang
|
||||
* @Date : 2021/6/10 11:00
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function rule(): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
$defaultIdentity = Identity::orderBy('order', 'desc')->first();
|
||||
$defaultDiscount = $defaultIdentity->getRule('buy_discount', 0);
|
||||
if (! $user) {
|
||||
return $this->success([
|
||||
'message' => '开通节点 最高享'.($defaultDiscount / 10).'折优惠',
|
||||
'not_vip' => true,
|
||||
'discount' => $defaultDiscount / 100,
|
||||
]);
|
||||
}
|
||||
|
||||
$identity = $user->identities->first();
|
||||
$discount = $identity->getRule('buy_discount', 0);
|
||||
if ($discount >= 100) {
|
||||
$message = '开通节点 最高享'.($defaultDiscount / 10).'折优惠';
|
||||
} else {
|
||||
$message = $identity->name.'尊享'.($discount / 10).'折优惠';
|
||||
}
|
||||
|
||||
return $this->success([
|
||||
'message' => $message,
|
||||
'not_vip' => (bool) $identity->default,
|
||||
'discount' => $discount / 100,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 获取身份权益
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/6/8 8:39
|
||||
* @param Identity $identity
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function show(Identity $identity): JsonResponse
|
||||
{
|
||||
return $this->success(new UserIdentityResource($identity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 激活码激活
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/9/21 10:06
|
||||
* @param Order $order
|
||||
* @param Request $request
|
||||
* @return JsonResponse|mixed
|
||||
*/
|
||||
public function invite(Order $order, Request $request)
|
||||
{
|
||||
$code = $request->code;
|
||||
|
||||
$user = Api::user();
|
||||
info($user->id);
|
||||
|
||||
if (! $order->canPay()) {
|
||||
return $this->failed('支付失败,订单不可支付');
|
||||
}
|
||||
info($order->toJson());
|
||||
|
||||
if ($order->user()->isNot($user)) {
|
||||
return $this->failed('支付失败,您没有权限支付');
|
||||
}
|
||||
|
||||
$invite = UserInvite::where('code', $code)->first();
|
||||
if (! $invite) {
|
||||
return $this->failed('未找到激活码信息');
|
||||
}
|
||||
|
||||
if ($invite->status != UserInvite::STATUS_ALLOT) {
|
||||
return $this->failed('激活码状态不对');
|
||||
}
|
||||
|
||||
try {
|
||||
$order->pay();
|
||||
$invite->use($user->id);
|
||||
return $this->success('激活成功');
|
||||
} catch (\Exception $exception) {
|
||||
return $this->failed($exception->getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
105
modules/User/Http/Controllers/Api/Identity/PartnerController.php
Normal file
105
modules/User/Http/Controllers/Api/Identity/PartnerController.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\User\Http\Controllers\Api\Identity;
|
||||
|
||||
use App\Api\Controllers\Controller;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Jason\Api\Api;
|
||||
use Modules\Mall\Http\Resources\Api\Activity\ActivityBaseResource;
|
||||
use Modules\Mall\Http\Resources\Api\Activity\ActivityResource;
|
||||
use Modules\Mall\Models\Activity;
|
||||
use Modules\User\Models\Identity;
|
||||
use Modules\User\Models\Order;
|
||||
|
||||
class PartnerController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* Notes: 开通合伙人
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/8/4 15:21
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function create(): JsonResponse
|
||||
{
|
||||
$user = Api::user();
|
||||
|
||||
$activity = Activity::find(2);
|
||||
$activityInfo = $activity ? new ActivityResource($activity) : [];
|
||||
$text = '开通合伙人';
|
||||
$order = Order::ByUser($user)->where('identity_id', 0)->first();
|
||||
$value = true;
|
||||
if ($order) {
|
||||
$value = false;
|
||||
|
||||
if ($order->state == Order::STATE_INIT) {
|
||||
$text = '等待审核';
|
||||
} else {
|
||||
$text = '您已开通';
|
||||
}
|
||||
}
|
||||
$data = [
|
||||
'activity' => $activityInfo,
|
||||
'status' => [
|
||||
'value' => $value,
|
||||
'text' => $text,
|
||||
]
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes: 开通会员
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date : 2021/6/4 10:02
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'name' => 'required',
|
||||
'card_no' => 'required|numeric',
|
||||
'cover' => 'required',
|
||||
], [
|
||||
'name.required' => '缺少姓名',
|
||||
'card_no.required' => '缺少银行卡号',
|
||||
'card_no.numeric' => '银行卡号只能是数字',
|
||||
'cover.required' => '缺少打款凭证',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return $this->failed($validator->errors()->first());
|
||||
}
|
||||
|
||||
$user = Api::user();
|
||||
|
||||
$data = [
|
||||
'user_id' => $user->id,
|
||||
'identity_id' => 0,
|
||||
'year' => 1,
|
||||
'type' => 1,
|
||||
'name' => $request->name,
|
||||
'card_no' => $request->card_no,
|
||||
'cover' => $request->cover,
|
||||
'state' => Order::STATE_INIT,
|
||||
'price' => 0,
|
||||
];
|
||||
|
||||
$order = Order::create($data);
|
||||
|
||||
if ($order) {
|
||||
return $this->success('提交成功,请等待后台审核');
|
||||
} else {
|
||||
return $this->failed('创建订单失败,请稍后再试');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user