123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\user;
- use app\model\user\UserSpread;
- use qiniu\basic\BaseServices;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * Class UserSpreadServices
- * @package app\services\user
- * @mixin UserSpread
- */
- class UserSpreadServices extends BaseServices
- {
- /**
- * UserSpreadServices constructor.
- * @param UserSpread $model
- */
- public function __construct(UserSpread $model)
- {
- $this->model = $model;
- }
- /**
- * 记录推广关系
- * @param int $uid
- * @param int $spread_uid
- * @param int $spread_time
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function setSpread(int $uid, int $spread_uid, int $spread_time = 0)
- {
- if (!$uid || !$spread_uid) return false;
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $sp = $spread_uid;
- $userList = $userServices->getColumn([], 'uid,spread_uid', 'uid');
- while ($sp) {
- if ($sp == $uid) {
- $spread_uid = 0;
- break;
- }
- $sp = $userList[$sp]['spread_uid'] ?? 0;
- }
- $user = $userServices->getUserInfo($uid);
- if (!$userServices->userExist($uid)) {
- return false;
- }
- $spread_user = $userServices->getUserInfo($spread_uid);
- if (!$userServices->userExist($spread_uid)) {
- return false;
- }
- if (!$user || !$spread_user || !$spread_user['is_promoter']) {
- return true;
- }
- //首先获取绑定方式
- $bind_type = sys_config('bind_type', 1);
- if (!$spread_time) $spread_time = time();
- switch ($bind_type) {
- case 2:
- if ($this->be(['uid' => $uid, 'lock' => 1])) {
- return true;
- }
- return $this->create([
- 'uid' => $uid,
- 'spread_uid' => $spread_uid,
- 'lock' => 0,
- 'spread_time' => $spread_time
- ]);
- case 3:
- if ($this->be(['uid' => $uid, 'lock' => 1])) {
- return true;
- }
- $this->create([
- 'uid' => $uid,
- 'spread_uid' => $spread_uid,
- 'lock' => 0,
- 'spread_time' => $spread_time
- ]);
- $user['spread_uid'] = $spread_uid;
- $user['spread_time'] = $spread_time;
- return $user->save() && $userServices->setUserSpreadCount($spread_uid);
- break;
- default:
- if ($this->be(['uid' => $uid, 'lock' => 1])) {
- return true;
- }
- self::create([
- 'uid' => $uid,
- 'spread_uid' => $spread_uid,
- 'lock' => 1,
- 'spread_time' => $spread_time
- ]);
- $user['spread_uid'] = $spread_uid;
- $user['spread_time'] = $spread_time;
- return $user->save() && $userServices->setUserSpreadCount($spread_uid);
- break;
- }
- }
- /**
- * 记录推广关系
- * @param int $uid
- * @param int $spread_uid
- * @param int $spread_time
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function adminSetSpread(int $uid, int $spread_uid, int $spread_time = 0)
- {
- if (!$uid) return false;
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if (!$spread_uid) {
- $this->search(['uid' => $uid])->delete();
- $user = $userServices->getUserInfo($uid);
- $old_spread = $user['spread_uid'];
- $user['spread_uid'] = 0;
- $user->save();
- if ($old_spread > 0) $userServices->setUserSpreadCount($old_spread);
- }
- $sp = $spread_uid;
- $userList = $userServices->getColumn([], 'uid,spread_uid', 'uid');
- while ($sp) {
- if ($sp == $uid) {
- $spread_uid = 0;
- break;
- }
- $sp = $userList[$sp]['spread_uid'] ?? 0;
- }
- $user = $userServices->getUserInfo($uid);
- if (!$userServices->userExist($uid)) {
- return false;
- }
- $spread_user = $userServices->getUserInfo($spread_uid);
- if (!$userServices->userExist($spread_uid)) {
- return false;
- }
- if (!$user || !$spread_user || !$spread_user['is_promoter']) {
- return true;
- }
- if ($user['spread_uid'] == $spread_uid) {
- return true;
- }
- $old_spread = $user['spread_uid'];
- if (!$spread_time) $spread_time = time();
- $this->search(['uid' => $uid])->delete();
- //首先获取绑定方式
- self::create([
- 'uid' => $uid,
- 'spread_uid' => $spread_uid,
- 'lock' => 1,
- 'spread_time' => $spread_time
- ]);
- $user['spread_uid'] = $spread_uid;
- $user['spread_time'] = $spread_time;
- $user->save() && $userServices->setUserSpreadCount($spread_uid);
- if ($old_spread > 0) $userServices->setUserSpreadCount($old_spread);
- return true;
- }
- /**
- * @param $uid
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function setSpreadSure($uid)
- {
- //首先获取绑定方式
- $bind_type = sys_config('bind_type', 1);
- if ($bind_type != 2) {
- return true;
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if ($this->be(['uid' => $uid, 'lock' => 1])) {
- return true;
- }
- $log = $this->search(['uid' => $uid])->order('spread_time', 'desc')->find();
- if (!$log) {
- return true;
- }
- $sp = $log['spread_uid'];
- $userList = $userServices->getColumn([], 'uid,spread_uid', 'uid');
- while ($sp) {
- if ($sp == $uid) {
- $log['spread_uid'] = 0;
- break;
- }
- $sp = $userList[$sp]['spread_uid'];
- }
- if (!$log['spread_uid']) {
- return true;
- }
- $log['lock'] = 1;
- return $log->save() && $userServices->update($uid, ['spread_uid' => $log['spread_uid'], 'spread_time' => $log['spread_time']]) && $userServices->setUserSpreadCount($log['spread_uid']);
- }
- }
|