new-admin-api/app/controller/api/user/Svip.php

445 lines
20 KiB
PHP

<?php
namespace app\controller\api\user;
use app\common\dao\store\coupon\StoreCouponProductDao;
use app\common\model\user\UserBrokerageApplyFormal;
use app\common\model\user\UserInviteCode;
use app\common\model\user\UserOrder;
use app\common\model\user\UserVipExchangeCode;
use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\order\StoreOrderCreateRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\store\product\SpuRepository;
use app\common\repositories\system\groupData\GroupDataRepository;
use app\common\repositories\system\groupData\GroupRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\system\serve\ServeOrderRepository;
use app\common\repositories\user\MemberinterestsRepository;
use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserInviteCodeRepository;
use app\common\repositories\user\UserOrderRepository;
use app\common\repositories\user\UserRepository;
use app\common\repositories\user\VipExchangeCodeRepository;
use crmeb\basic\BaseController;
use app\common\repositories\user\FeedbackRepository;
use crmeb\services\LockService;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;
class Svip extends BaseController
{
protected $repository;
public function __construct(App $app, UserBillRepository $repository)
{
parent::__construct($app);
$this->repository = $repository;
if (!systemConfig('svip_switch_status')) throw new ValidateException('付费会员未开启');
}
/**
* TODO 会员卡类型列表
* @param GroupRepository $groupRepository
* @param GroupDataRepository $groupDataRepository
* @return \think\response\Json
* @author Qinii
* @day 2022/11/7
*/
public function getTypeLst(GroupRepository $groupRepository,GroupDataRepository $groupDataRepository)
{
$group_id = $groupRepository->getSearch(['group_key' => 'svip_pay'])->value('group_id');
$where['group_id'] = $group_id;
$where['status'] = 1;
$list = $groupDataRepository->getSearch($where)->field('group_data_id,value,sort,status')->order('sort DESC')->select();
if ($this->request->isLogin() && $this->request->userInfo()->is_svip != -1) {
foreach ($list as $item) {
if ($item['value']['svip_type'] != 1) $res[] = $item;
}
}
$list = $res ?? $list;
$def = [];
if ($list && isset($list[0])) {
$def = $list[0] ? (['group_data_id' => $list[0]['group_data_id']] + $list[0]['value']) : [];
}
return app('json')->success(['def' => $def, 'list' => $list]);
}
/**
* TODO 购买会员
* @param $id
* @param GroupDataRepository $groupDataRepository
* @param ServeOrderRepository $serveOrderRepository
* @return \think\response\Json|void
* @author Qinii
* @day 2022/11/7
*/
public function createOrder($id, GroupDataRepository $groupDataRepository, UserOrderRepository $userOrderRepository)
{
$params = $this->request->params(['pay_type','return_url','bind_mer_id','bind_staff_id']);
if (!in_array($params['pay_type'], ['weixin', 'routine', 'h5', 'alipay', 'alipayQr', 'weixinQr'], true))
return app('json')->fail('请选择正确的支付方式');
$res = $groupDataRepository->getWhere(['group_data_id' => $id, 'status' => 1]);
if (!$res) return app('json')->fail('参数有误~');
if ($this->request->userInfo()->is_svip == 3)
return app('json')->fail('您已经是终身会员~');
if ($this->request->userInfo()->is_svip !== -1 && $res['value']['svip_type'] == 1)
return app('json')->fail('请选择其他会员类型');
$params['is_app'] = $this->request->isApp();
return $userOrderRepository->add($res,$this->request->userInfo(),$params);
}
/**
* TODO 会员中心个人信息
* @return \think\response\Json
* @author Qinii
* @day 2022/11/9
*/
public function svipUserInfo()
{
if ($this->request->isLogin()) {
$user = app()->make(UserRepository::class)->getSearch([])->field('uid,nickname,avatar,is_svip,svip_endtime,svip_save_money')->find($this->request->uid());
if ($user && $user['is_svip'] == 3) $user['svip_endtime'] = date('Y-m-d H:i:s',strtotime("+100 year"));
}
$data['user'] = $user ?? new \stdClass();
$data['interests'] = systemConfig('svip_switch_status') ? app()->make(MemberinterestsRepository::class)->getInterestsByLevel(MemberinterestsRepository::TYPE_SVIP) : [];
return app('json')->success($data);
}
/**
* TODO 获取会员优惠券列表
* @param StoreCouponRepository $couponRepository
* @return \think\response\Json
* @author Qinii
* @day 2022/11/17
*/
public function svipCoupon(StoreCouponRepository $couponRepository)
{
$where['send_type'] = $couponRepository::GET_COUPON_TYPE_SVIP;
$uid = $this->request->isLogin() ? $this->request->uid() : null;
$data = $couponRepository->sviplist($where, $uid);
return app('json')->success($data);
}
/**
* TODO 领取会员优惠券
* @param $id
* @param StoreCouponRepository $couponRepository
* @return \think\response\Json
* @author Qinii
* @day 2022/11/17
*/
public function receiveCoupon($id, StoreCouponRepository $couponRepository)
{
if (!$this->request->userInfo()->is_svip)
return app('json')->fail('您还不是付费会员');
if (!$couponRepository->exists($id))
return app('json')->fail('优惠券不存在');
try {
$couponRepository->receiveSvipCounpon($id, $this->request->uid());
} catch (\Exception $e) {
return app('json')->fail('优惠券已被领完');
}
return app('json')->success('领取成功');
}
/**
* TODO 会员专属商品
* @param SpuRepository $spuRepository
* @return \think\response\Json
* @author Qinii
* @day 2022/11/17
*/
public function svipProductList(SpuRepository $spuRepository)
{
[$page, $limit] = $this->getPage();
$user = $this->request->isLogin() ? $this->request->userInfo() : null;
if (systemConfig('svip_product_switch') || (isset($user->is_svip) && $user->is_svip > 0)){
$where['is_gift_bag'] = 0;
$where['product_type'] = 0;
$where['order'] = 'star';
$where['svip'] = 1;
$data = $spuRepository->getApiSearch($where, $page, $limit, $user);
}
return app('json')->success($data ?? ['count' => 0,'list' => []]);
}
/**
* Common: 兑换码 - 使用
* Author: wu-hui
* Time: 2024/03/02 18:46
* @param GroupDataRepository $groupDataRepository
* @param UserOrderRepository $userOrderRepository
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function vipExchangeCode(GroupDataRepository $groupDataRepository, UserOrderRepository $userOrderRepository){
// 参数获取
$exchangeCode = $this->request->param('exchange_code', '');
if(empty($exchangeCode)) throw new ValidateException('兑换码不存在!');
$user = $this->request->userInfo();
$uid = $this->request->uid();
// 获取兑换码信息
$exchangeCodeInfo = app()->make(VipExchangeCodeRepository::class)
->getSearchModel(['exchange_code'=>$exchangeCode])
->findOrEmpty()
->toArray();
if(empty($exchangeCodeInfo)) throw new ValidateException('兑换码不存在!');
if($exchangeCodeInfo['status'] == 0) throw new ValidateException('兑换码未激活!');
if($exchangeCodeInfo['status'] == 2) throw new ValidateException('兑换码已被使用,请勿重复使用!');
if($exchangeCodeInfo['status'] == 3) throw new ValidateException('兑换码已作废!');
// 下单并且使用
$res = $groupDataRepository->getWhere(['group_data_id' => $exchangeCodeInfo['group_data_id'], 'status' => 1]);
if (!$res) throw new ValidateException('会员卡不存在!');
if ($user->is_svip == 3) throw new ValidateException('您已经是终身会员!');
if ($user->is_svip !== -1 && $res['value']['svip_type'] == 1) throw new ValidateException('兑换失败,您已开通该类型会员!');
// 模拟下单
$params['pay_type'] = 'free';
$params['return_url'] = '';
$params['is_app'] = $this->request->isApp();
$params['bind_mer_id'] = $exchangeCodeInfo['mer_id'] ?? 0;
$params['bind_staff_id'] = $exchangeCodeInfo['staff_id'] ?? 0;
$params['exchange_code'] = $exchangeCode;
$params['exchange_code_id'] = $exchangeCodeInfo['id'];
$params['source'] = 1;
$userOrderRepository->add($res, $user, $params);
// 开通成功 修改信息
UserVipExchangeCode::where('id',$exchangeCodeInfo['id'])->update([
'status' => 2,
'use_time' => date("Y-m-d H:i:s",time()),
'use_user_id' => $uid
]);
return app('json')->success();
}
/**
* Common: 兑换码 - 信息获取
* Author: wu-hui
* Time: 2024/06/19 9:43
* @param GroupDataRepository $groupDataRepository
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function vipExchangeCodeInfo(GroupDataRepository $groupDataRepository){
// 参数获取
$exchangeCode = $this->request->param('exchange_code', '');
$exchangeCodeInfo = $this->vipExchangeCodeGetInfo($exchangeCode);
return app('json')->success($exchangeCodeInfo);
}
/**
* Common: 兑换码 - 获取兑换码信息
* Author: wu-hui
* Time: 2024/06/19 10:42
* @param $exchangeCode
* @return mixed
*/
protected function vipExchangeCodeGetInfo($exchangeCode){
// 参数获取
if(empty($exchangeCode)) throw new ValidateException('兑换码不存在!');
$user = $this->request->userInfo();
$uid = $this->request->uid();
// 获取兑换码信息
$exchangeCodeInfo = app()->make(VipExchangeCodeRepository::class)
->getSearchModel(['exchange_code'=>$exchangeCode])
->findOrEmpty()
->toArray();
if(empty($exchangeCodeInfo)) throw new ValidateException('兑换码不存在!');
if($exchangeCodeInfo['status'] == 0) throw new ValidateException('兑换码未激活!');
if($exchangeCodeInfo['status'] == 2) throw new ValidateException('兑换码已被使用,请勿重复使用!');
if($exchangeCodeInfo['status'] == 3) throw new ValidateException('兑换码已作废!');
// 是否允许开通
$res = app()->make(GroupDataRepository::class)->getWhere(['group_data_id' => $exchangeCodeInfo['group_data_id'], 'status' => 1]);
if (!$res) throw new ValidateException('会员卡不存在!');
if ($user->is_svip == 3) throw new ValidateException('您已是该类型终身会员!');
if ($user->is_svip !== -1 && $res['value']['svip_type'] == 1) throw new ValidateException('您已开通该类型会员!');
return $exchangeCodeInfo;
}
/**
* Common: 兑换码 - 支付并且开通
* Author: wu-hui
* Time: 2024/06/19 11:42
* @param StoreOrderCreateRepository $orderCreateRepository
* @return mixed
*/
public function vipExchangeCodePayment(StoreOrderCreateRepository $orderCreateRepository){
// 参数获取
$uid = $this->request->uid();
$user = $this->request->userInfo();
$payInfo = $this->request->params(['pay_type', 'return_url', 'code']);
if (!in_array($payInfo['pay_type'], StoreOrderRepository::PAY_TYPE, true)) return app('json')->fail('请选择正确的支付方式');
// 兑换码信息
$exchangeCodeInfo = $this->vipExchangeCodeGetInfo($payInfo['code']);
$payInfo['money'] = (float)$exchangeCodeInfo['svip_price'] ?? 0;
return Db::transaction(function () use ($exchangeCodeInfo, $payInfo, $user) {
$res = app()->make(GroupDataRepository::class)->getWhere(['group_data_id' => $exchangeCodeInfo['group_data_id'], 'status' => 1]);
// 模拟下单
$params['pay_type'] = $payInfo['pay_type'] ?? 'free';
$params['return_url'] = $payInfo['return_url'];
$params['is_app'] = $this->request->isApp();
$params['bind_mer_id'] = $exchangeCodeInfo['mer_id'] ?? 0;
$params['bind_staff_id'] = $exchangeCodeInfo['staff_id'] ?? 0;
$params['exchange_code'] = $payInfo['code'];
$params['exchange_code_id'] = $exchangeCodeInfo['id'];
$params['source'] = 1;
return app()->make(UserOrderRepository::class)->add($res, $user, $params);
});
}
/**
* Common: 邀请码 - 信息获取
* Author: wu-hui
* Time: 2024/04/03 11:33
* @return mixed
*/
public function inviteCodeInfo(){
// 参数获取
$exchangeCode = $this->request->param('exchange_code');
if(empty($exchangeCode)) throw new ValidateException('邀请码不存在!');
// 获取邀请码信息
$exchangeCodeInfo = app()->make(UserInviteCodeRepository::class)
->getSearchModel(['exchange_code'=>$exchangeCode])
->findOrEmpty()
->toArray();
if(empty($exchangeCodeInfo)) throw new ValidateException('邀请码不存在!');
return app('json')->success($exchangeCodeInfo);
}
/**
* Common: 邀请码 - 下单并且支付
* Author: wu-hui
* Time: 2024/04/03 15:03
* @param StoreOrderCreateRepository $orderCreateRepository
* @return mixed
*/
public function inviteCodePayment(StoreOrderCreateRepository $orderCreateRepository){
// 参数获取
$uid = $this->request->uid();
$payInfo = $this->request->params(['pay_type', 'return_url', 'code']);
if (!in_array($payInfo['pay_type'], StoreOrderRepository::PAY_TYPE, true)) return app('json')->fail('请选择正确的支付方式');
// 获取邀请码信息
$exchangeCodeInfo = app()->make(UserInviteCodeRepository::class)
->getSearchModel(['exchange_code'=>$payInfo['code']])
->findOrEmpty()
->toArray();
$payInfo['money'] = (float)$exchangeCodeInfo['svip_price'] ?? 0;
// 判断:是否需要支付
if($payInfo['money'] <= 0){
// 无需支付 直接激活
UserInviteCode::where('exchange_code',$payInfo['code'])->update([
'activate_uid' => $uid,
'activate' => date('Y-m-d H:i:s'),
'status' => 1
]);
return app('json')->success("激活成功");
}
else{
// 发起支付
$groupOrder = app()
->make(LockService::class)
->exec('online_order.create',function() use ($payInfo,$orderCreateRepository){
$payType = array_search($payInfo['pay_type'],StoreOrderRepository::PAY_TYPE);
return $orderCreateRepository->onlinePayment($payType,$payInfo,$this->request->userInfo(),34);
});
// 记录订单信息
$orderId = app()->make(StoreOrderRepository::class)
->getSearch([])
->where('group_order_id',$groupOrder->group_order_id)
->value('order_id');
UserInviteCode::where('exchange_code',$payInfo['code'])->update([
'order_id' => $orderId,
]);
if ($groupOrder['pay_price'] == 0) {
app()->make(StoreOrderRepository::class)->paySuccess($groupOrder);
return app('json')->status('success', '支付成功', ['order_id' => $groupOrder['group_order_id']]);
}
try {
return app()->make(StoreOrderRepository::class)->pay($payInfo['pay_type'], $this->request->userInfo(), $groupOrder, $payInfo['return_url'], $this->request->isApp());
} catch (\Exception $e) {
return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
}
}
}
/**
* Common: 邀请码 - 使用
* Author: wu-hui
* Time: 2024/04/03 16:25
* @param GroupDataRepository $groupDataRepository
* @param UserOrderRepository $userOrderRepository
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function inviteUse(GroupDataRepository $groupDataRepository,UserOrderRepository $userOrderRepository){
// 参数获取
$exchangeCode = $this->request->param('exchange_code');
if(empty($exchangeCode)) throw new ValidateException('邀请码不存在!');
$user = $this->request->userInfo();
$uid = $this->request->uid();
Db::transaction(function () use ($groupDataRepository, $userOrderRepository, $user, $uid, $exchangeCode) {
// 获取邀请码信息
$exchangeCodeInfo = app()->make(UserInviteCodeRepository::class)
->getSearchModel(['exchange_code'=>$exchangeCode])
->findOrEmpty()
->toArray();
if(empty($exchangeCodeInfo)) throw new ValidateException('邀请码不存在!');
if($exchangeCodeInfo['status'] == 0) throw new ValidateException('邀请码未激活!');
if($exchangeCodeInfo['status'] == 2) throw new ValidateException('邀请码已被使用,请勿重复使用!');
if($exchangeCodeInfo['status'] == 3) throw new ValidateException('邀请码已作废!');
// 使用
$res = $groupDataRepository->getWhere(['group_data_id' => $exchangeCodeInfo['group_data_id'], 'status' => 1]);
if (!$res) throw new ValidateException('会员卡不存在!');
if ($user->is_svip == 3) throw new ValidateException('您已经是终身会员!');
if ($user->is_svip !== -1 && $res['value']['svip_type'] == 1) throw new ValidateException('兑换失败,您已开通该类型会员!');
// 模拟下单
$params['pay_type'] = 'free';
$params['return_url'] = '';
$params['is_app'] = $this->request->isApp();
$params['bind_mer_id'] = $exchangeCodeInfo['mer_id'] ?? 0;
$params['bind_staff_id'] = $exchangeCodeInfo['staff_id'] ?? 0;
$params['exchange_code'] = $exchangeCode;
$params['exchange_code_id'] = $exchangeCodeInfo['id'];
$params['source'] = 2;
$userOrderRepository->add($res, $user, $params);
// 开通成功 修改信息
UserInviteCode::where('exchange_code',$exchangeCode)->update([
'use_user_id' => $uid,
'use_time' => date('Y-m-d H:i:s'),
'status' => 2
]);
// 绑定上下级关系
if((int)$exchangeCodeInfo['activate_uid'] == (int)$uid) throw new ValidateException('邀请人和使用者不能是同一人!');
$bindRes = app()->make(UserRepository::class)->bindSpread($user, (int)$exchangeCodeInfo['activate_uid']);
if(!$bindRes) throw new ValidateException('领取失败,关系链错误!');
});
return app('json')->success();
}
}