pull/1/head
wanghousheng 10 months ago
parent 1363d75d75
commit 4ebfa6e43c
  1. 46
      app/api/controller/Invite.php
  2. 122
      app/api/model/Invite/InviteLog.php
  3. 12
      app/api/model/UserCoupon.php
  4. 20
      app/api/service/InviteService.php
  5. 8
      app/api/service/passport/Login.php
  6. 31
      app/common/model/invite/InviteConfig.php
  7. 12
      app/common/model/invite/InviteLog.php
  8. 6
      app/store/controller/Invite.php

@ -0,0 +1,46 @@
<?php
namespace app\api\controller;
use app\api\model\Invite\InviteLog;
use app\common\model\invite\InviteConfig;
use cores\exception\BaseException;
use think\db\exception\DbException;
use think\response\Json;
class Invite extends Controller
{
/**
* @notes:基本信息
* @return Json
* @throws BaseException
* @author: wanghousheng
*/
public function index(): Json
{
$info = InviteConfig::detail([], ['coupon']);
$data = [];
if ($info) {
$data['one_order_rate'] = $info['one_order_rate'];
$data['integral'] = $info['integral'];
$data['coupon_name'] = $info['coupon']['name'];
$model = new InviteLog();
$data['money'] = $model->sumMoney();
$data['count_people'] = $model->countPeople();
}
return $this->renderSuccess($data);
}
/**
* @notes:邀请记录
* @throws BaseException
* @throws DbException
* @author: wanghousheng
*/
public function getList(): Json
{
$model = new InviteLog();
$list = $model->getList();
return $this->renderSuccess(compact('list'));
}
}

@ -0,0 +1,122 @@
<?php
namespace app\api\model\Invite;
use app\api\model\User as UserModel;
use app\api\model\UserCoupon;
use app\api\service\User as UserService;
use app\common\model\invite\InviteConfig;
use cores\exception\BaseException;
use think\db\exception\DbException;
use think\Paginator;
class InviteLog extends \app\common\model\invite\InviteLog
{
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'store_id',
'update_time'
];
/**
* @notes:获取列表记录
* @param array $where The conditions to filter the items.
* @param int $listRows The number of rows to display per page. The default value is 15.
* @return Paginator The paginated items.
* @throws BaseException If an error occurred.
* @throws DbException If a database exception occurred.
*/
public function getList(array $where = [], int $listRows = 15): Paginator
{
// 当前用户ID
$userId = UserService::getCurrentLoginUserId();
$where['user_id'] = $userId;
return $this->where($where)
->with(['invitee'])
->order(['create_time' => 'desc'])
->paginate($listRows);
}
private static function isExistReferee(int $userId): bool
{
return (bool)self::get(['invitee_user_id' => $userId]);
}
/**
* @notes:新增
* @param $data
* @return bool
* @author: wanghousheng
*/
public function add($data): bool
{
$data['store_id'] = self::$storeId;
return $this->save($data);
}
public static function createRelation(int $userId, int $refereeId): bool
{
// 自邀请
if ($userId == $refereeId) {
return false;
}
// 判断当前用户是否已存在推荐关系
if (self::isExistReferee($userId)) {
return false;
}
$data['invitee_user_id'] = $userId;
$data['user_id'] = $refereeId;
$model = new self;
$model->add($data);
self::gifts($refereeId);
return true;
}
/**
* @notes:发送奖励
* @param int $userId
* @author: wanghousheng
*/
private static function gifts(int $userId)
{
$info = InviteConfig::detail(['store_id' => self::$storeId]);
if ($info) {
//是否有积分
if ($info['integral'] > 0) {
UserModel::setIncPoints($userId, (int)$info['integral'], '邀请好友', self::$storeId);
}
//优惠券
if ($info['coupon_id'] > 0) {
(new UserCoupon)->giveReceive(intval($info['coupon_id']), $userId);
}
}
}
/**获取金额
* @notes:
* @return float
* @throws BaseException
* @author: wanghousheng
*/
public function sumMoney(): float
{
// 当前用户ID
$userId = UserService::getCurrentLoginUserId();
return $this->where(['user_id' => $userId])->sum('money');
}
/**累计人数
* @notes:
* @return int
* @throws BaseException
* @author: wanghousheng
*/
public function countPeople(): int
{
$userId = UserService::getCurrentLoginUserId();
return $this->where(['user_id' => $userId])->count();
}
}

@ -138,6 +138,18 @@ class UserCoupon extends UserCouponModel
return $this->add($userId, $couponInfo);
}
public function giveReceive(int $couponId, int $userId): bool
{
// 获取优惠券信息
$couponInfo = Coupon::detail($couponId);
// 验证优惠券是否可领取
if (!$this->checkReceive($userId, $couponInfo)) {
return false;
}
// 添加领取记录
return $this->add($userId, $couponInfo);
}
/**
* 验证优惠券是否可领取
* @param int $userId 当前用户ID

@ -0,0 +1,20 @@
<?php
namespace app\api\service;
use app\common\model\invite\InviteConfig;
use app\common\service\BaseService;
class InviteService extends BaseService
{
private function getInfo(int $userId)
{
$info = InviteConfig::detail();
if ($info) {
//是否有积分
if ($info['integral'] > 0) {
}
}
}
}

@ -13,6 +13,7 @@ declare (strict_types=1);
namespace app\api\service\passport;
use app\api\model\{dealer\Referee as RefereeModel,
Invite\InviteLog,
Setting as SettingModel,
UploadFile as UploadFileModel,
User as UserModel};
@ -268,6 +269,11 @@ class Login extends BaseService
$refereeId > 0 && RefereeModel::createRelation($userId, $refereeId);
}
private function bindInvite(int $userId, int $refereeId): void
{
$refereeId > 0 && InviteLog::createRelation($userId, $refereeId);
}
/**
* 保存oauth信息(第三方用户信息)
* @param int $userId 用户ID
@ -371,6 +377,8 @@ class Login extends BaseService
$this->userInfo = $model;
// 记录推荐人关系
$this->bindRelation($this->getUserId(), (int)$refereeId);
//记录邀请关系
$this->bindInvite($this->getUserId(), (int)$refereeId);
}
/**

@ -2,7 +2,12 @@
namespace app\common\model\invite;
use app\common\model\Coupon;
use cores\BaseModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;
class InviteConfig extends BaseModel
{
@ -12,29 +17,26 @@ class InviteConfig extends BaseModel
// 定义主键
protected $pk = 'id';
/**
* @notes:编辑
* @param $data
* @return bool
* @author: wanghousheng
*/
public function edit($data): bool
public function coupon(): HasOne
{
if ($this->where(['store_id' => self::$storeId])->exists()) {
return $this->save($data) !== false;
}
$data['store_id'] = self::$storeId;
return $this->save($data);
return $this->hasOne(Coupon::class, 'coupon_id', 'coupon_id');
}
/**
* @notes:新增
* @param $data
* @return bool
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @author: wanghousheng
*/
public function add($data): bool
{
if ($this->where(['store_id' => self::$storeId])->find()) {
return $this->where(['store_id' => self::$storeId])->save($data) !== false;
}
$data['store_id'] = self::$storeId;
return $this->save($data);
}
@ -42,11 +44,12 @@ class InviteConfig extends BaseModel
/**
* 详情
* @param array $where
* @param array $with
* @return static|array|null
*/
public static function detail(array $where = [])
public static function detail(array $where = [], array $with = [])
{
return self::get($where);
return self::get($where, $with);
}
}

@ -31,18 +31,6 @@ class InviteLog extends BaseModel
return $this->hasOne(User::class, 'user_id', 'invitee_user_id');
}
/**
* @notes:新增
* @param $data
* @return bool
* @author: wanghousheng
*/
public function add($data): bool
{
$data['store_id'] = self::$storeId;
return $this->save($data);
}
/**
* 详情
* @param array $where

@ -18,14 +18,14 @@ class Invite extends Controller
return $this->renderSuccess(compact('info'));
}
public function editConfig(int $configId): Json
public function editConfig(): Json
{
$data = $this->postForm();
if (!$data) {
return $this->renderError('缺少必要参数');
}
$model = InviteConfig::detail($configId);
if ($model->edit($data)) {
$model = new InviteConfig();
if ($model->add($data)) {
return $this->renderSuccess('编辑成功');
}
return $this->renderError($model->getError() ?: '编辑失败');

Loading…
Cancel
Save