diff --git a/app/api/controller/Invite.php b/app/api/controller/Invite.php new file mode 100644 index 00000000..d26af915 --- /dev/null +++ b/app/api/controller/Invite.php @@ -0,0 +1,46 @@ +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')); + } +} \ No newline at end of file diff --git a/app/api/model/Invite/InviteLog.php b/app/api/model/Invite/InviteLog.php new file mode 100644 index 00000000..b5f30497 --- /dev/null +++ b/app/api/model/Invite/InviteLog.php @@ -0,0 +1,122 @@ +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(); + } +} \ No newline at end of file diff --git a/app/api/model/UserCoupon.php b/app/api/model/UserCoupon.php index adb189dd..7c8a292b 100644 --- a/app/api/model/UserCoupon.php +++ b/app/api/model/UserCoupon.php @@ -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 diff --git a/app/api/service/InviteService.php b/app/api/service/InviteService.php new file mode 100644 index 00000000..505dbba1 --- /dev/null +++ b/app/api/service/InviteService.php @@ -0,0 +1,20 @@ + 0) { + + } + } + } +} \ No newline at end of file diff --git a/app/api/service/passport/Login.php b/app/api/service/passport/Login.php index 16b151b4..49e7f983 100644 --- a/app/api/service/passport/Login.php +++ b/app/api/service/passport/Login.php @@ -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); } /** diff --git a/app/common/model/invite/InviteConfig.php b/app/common/model/invite/InviteConfig.php index 76518981..78a3a44d 100644 --- a/app/common/model/invite/InviteConfig.php +++ b/app/common/model/invite/InviteConfig.php @@ -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); } } \ No newline at end of file diff --git a/app/common/model/invite/InviteLog.php b/app/common/model/invite/InviteLog.php index e17ec1ec..10e2aefb 100644 --- a/app/common/model/invite/InviteLog.php +++ b/app/common/model/invite/InviteLog.php @@ -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 diff --git a/app/store/controller/Invite.php b/app/store/controller/Invite.php index 2c45cefa..517d623e 100644 --- a/app/store/controller/Invite.php +++ b/app/store/controller/Invite.php @@ -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() ?: '编辑失败');