pull/1/head
parent
1363d75d75
commit
4ebfa6e43c
@ -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(); |
||||
} |
||||
} |
@ -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) { |
||||
|
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue