1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- <?php
- namespace app\models\user;
- use app\models\store\StoreOrder;
- use app\models\store\StoreOrderCartInfo;
- use app\models\store\StoreProduct;
- use crmeb\services\SystemConfigService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Session;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use crmeb\traits\JwtAuthModelTrait;
- /**
- * TODO 用户Model
- * Class User
- * @package app\models\user
- */
- class User extends BaseModel
- {
- use JwtAuthModelTrait;
- use ModelTrait;
- protected $pk = 'uid';
- protected $name = 'user';
- protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
- protected $hidden = [
- 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
- ];
- protected function setAddTimeAttr($value)
- {
- return time();
- }
- protected function setAddIpAttr($value)
- {
- return app('request')->ip();
- }
- protected function setLastTimeAttr($value)
- {
- return time();
- }
- protected function setLastIpAttr($value)
- {
- return app('request')->ip();
- }
- /**
- * @param $wechatUser
- * @param int $spread_uid
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function setWechatUser($wechatUser, $spread_uid = 0)
- {
- $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
- $res = self::create([
- 'account' => 'wx' . $wechatUser['uid'] . time(),
- 'pwd' => md5(123456),
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'add_time' => time(),
- 'add_ip' => app('request')->ip(),
- 'last_time' => time(),
- 'last_ip' => app('request')->ip(),
- 'uid' => $wechatUser['uid'],
- 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
- 'user_type' => 'wechat'
- ]);
- return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
- }
- /**
- * TODO 获取会员是否被清除过的时间
- * @param $uid
- * @return int|mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function getCleanTime($uid)
- {
- $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
- if (!$user) return 0;
- return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
- }
- /**
- * 更新用户信息
- * @param array $wechatUser 用户信息
- * @param int $uid 用户uid
- * @return bool|void
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function updateWechatUser($wechatUser, $uid)
- {
- $userInfo = self::where('uid', $uid)->find();
- if (!$userInfo) return;
- //增加成为分销权限
- if (!$userInfo->is_promoter) {
- $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
- $status = is_brokerage_statu($price);
- } else {
- $status = false;
- }
- if ($userInfo->spread_uid) {
- return self::edit([
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
- 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
- ], $uid, 'uid');
- } else {
- $data = [
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
- 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
- // 'spread_uid' => 0,
- // 'spread_time' => 0,
- 'last_time' => time(),
- 'last_ip' => request()->ip(),
- ];
- //TODO 获取后台分销类型
- // $storeBrokerageStatus = sys_config('store_brokerage_statu');
- // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
- // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
- // if ($storeBrokerageStatus == 1) {
- // $spreadCount = self::where('uid', $wechatUser['code'])->count();
- // if ($spreadCount) {
- // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
- // if ($spreadInfo->is_promoter) {
- // //TODO 只有扫码才可以获得推广权限
- //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
- // }
- // }
- // }
- // $data['spread_uid'] = $wechatUser['code'];
- // $data['spread_time'] = time();
- // }
- return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
- }
- }
- /**
- * 设置推广关系
- * @param $spread
- * @param $uid
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function setSpread($spread, $uid)
- {
- return UserSpread::setSpread($uid, $spread);
- //当前用户信息
- // $userInfo = self::where('uid', $uid)->find();
- // if (!$userInfo) return true;
- // //当前用户有上级直接返回
- // if ($userInfo->spread_uid) return true;
- // //没有推广编号直接返回
- // if (!$spread) return true;
- // if ($spread == $uid) return true;
- // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
- // //TODO 获取后台分销类型
- // $storeBrokerageStatus = sys_config('store_brokerage_statu');
- // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
- // if ($storeBrokerageStatus == 1) {
- // $spreadCount = self::where('uid', $spread)->count();
- // if ($spreadCount) {
- // $spreadInfo = self::where('uid', $spread)->find();
- // if ($spreadInfo->is_promoter) {
- // //TODO 只有扫码才可以获得推广权限
- //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
- // }
- // }
- // }
- // $data['spread_uid'] = $spread;
- // $data['spread_time'] = time();
- // return self::edit($data, $uid, 'uid');
- }
- /**
- * 小程序用户添加
- * @param $routineUser
- * @param int $spread_uid
- * @return object
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function setRoutineUser($routineUser, $spread_uid = 0)
- {
- self::beginTrans();
- $res1 = true;
- if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
- $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
- $res2 = self::create([
- 'account' => 'rt' . $routineUser['uid'] . time(),
- 'pwd' => md5(123456),
- 'nickname' => $routineUser['nickname'] ?: '',
- 'avatar' => $routineUser['headimgurl'] ?: '',
- // 'spread_uid' => $spread_uid,
- 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
- // 'spread_time' => $spread_uid ? time() : 0,
- 'uid' => $routineUser['uid'],
- 'add_time' => $routineUser['add_time'],
- 'add_ip' => request()->ip(),
- 'last_time' => time(),
- 'last_ip' => request()->ip(),
- 'user_type' => $routineUser['user_type']
- ]);
- $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
- self::checkTrans($res);
- return $res2;
- }
- /**
- * 获得当前登陆用户UID
- * @return int $uid
- */
- public static function getActiveUid()
- {
- $uid = null;
- $uid = Session::get('LoginUid');
- if ($uid) return $uid;
- else return 0;
- }
- /**
- * TODO 查询当前用户信息
- * @param $uid $uid 用户编号
- * @param string $field $field 查询的字段
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function getUserInfo($uid, $field = '')
- {
- if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
- else $userInfo = self::where('uid', $uid)->find();
- if (!$userInfo) return [];
- return $userInfo->toArray();
- }
- /**
- * 判断当前用户是否推广员
- * @param int $uid
- * @return bool
- */
- public static function isUserSpread($uid = 0)
- {
- if (!$uid) return false;
- $status = (int)sys_config('store_brokerage_statu');
- $isPromoter = true;
- if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
- if ($isPromoter) return true;
- else return false;
- }
- /**
- * TODO 一级返佣
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function backOrderBrokerage($orderInfo, bool $open = true)
- {
- //TODO 营销产品不返佣金
- if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
- if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
- if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
- $userInfo = User::getUserInfo($orderInfo['uid']);
- //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
- if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
- if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
- $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
- list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
- //TODO 返佣金额小于等于0 直接返回不返佣金
- if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
- //TODO 获取上级推广员信息
- // $open && self::beginTrans();
- $res = true;
- if ($virtualBrokeragePrice > 0) {
- $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
- $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
- //TODO 添加推广记录
- $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
- //TODO 添加用户余额
- $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
- //TODO 一级返佣成功 跳转二级返佣
- $res = $res1 && $res2;
- }
- if ($realBrokeragePrice > 0) {
- $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
- //TODO 添加推广记录
- $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfo['brokerage_price'], $mark, 0);
- }
- $res = $res && self::backOrderBrokerageTwo($orderInfo);
- // $open && self::checkTrans($res);
- return $res;
- }
- /**
- * TODO 二级推广
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
- {
- //TODO 获取购买商品的用户
- $userInfo = User::getUserInfo($orderInfo['uid']);
- //TODO 获取上推广人
- $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
- //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
- if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
- //TODO 获取后台分销类型 1 指定分销 2 人人分销
- if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
- $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
- list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
- //TODO 返佣金额小于等于0 直接返回不返佣金
- if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
- // $open && self::beginTrans();
- $res = true;
- if ($virtualBrokeragePrice > 0) {
- $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
- $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
- //TODO 添加推广记录
- $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
- //TODO 添加用户余额
- $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
- //TODO 一级返佣成功 跳转二级返佣
- $res = $res1 && $res2;
- }
- if ($realBrokeragePrice > 0) {
- $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
- //TODO 添加推广记录
- $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
- }
- // $res = $res && self::backOrderBrokerageTwo($orderInfo);
- // $open && self::checkTrans($res);
- return $res;
- }
- /**
- * 发放实际商品佣金
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendBackOrderBrokerage($orderInfo)
- {
- $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
- if ($list) {
- self::beginTrans();
- try {
- foreach ($list as $v) {
- self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
- $balance = self::getUserInfo($v['uid'])['brokerage_price'];
- UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
- }
- self::commitTrans();
- return true;
- } catch (Exception $e) {
- self::rollbackTrans();
- return false;
- }
- }
- return true;
- }
- /**
- * 获取推荐人 暂无使用
- * @param $uid
- * @param $page
- * @param $limit
- * @return mixed
- */
- public static function getSpreadList($uid, $page, $limit)
- {
- $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
- foreach ($list as $k => $user) {
- $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
- $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
- }
- $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
- $data['count'] = $count;
- $data['list'] = $list;
- return $data;
- }
- /**
- * 获取某个用户的下级uid
- * @param $uid
- * @return array
- */
- public static function getOneSpreadUid($uid)
- {
- return self::where('spread_uid', $uid)->column('uid');
- }
- /**
- * 修改个人信息
- * @param $avatar 头像
- * @param $nickname 昵称
- * @param $uid 用户uid
- * @return bool
- */
- public static function editUser($avatar, $nickname, $uid)
- {
- return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
- }
- /**
- * TODO 获取推广人数 一级
- * @param int $uid
- * @return bool|int|string
- */
- public static function getSpreadCount($uid = 0)
- {
- if (!$uid) return false;
- return self::where('spread_uid', $uid)->count();
- }
- /**
- * 修改当前用户的推广人数
- * @param $uid
- */
- public static function setUserSpreadCount($uid)
- {
- if (!$uid) return true;
- if (self::getSpreadCount($uid) > 0) {
- self::where('uid', $uid)->update(['spread_count' => 0]);
- } else {
- self::where('uid', $uid)->update(['spread_count' => 0]);
- return true;
- }
- return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
- }
- /**
- * TODO 获取推广人数 二级
- * @param int $uid
- * @return bool|int|string
- */
- public static function getSpreadLevelCount($uid = 0)
- {
- if (!$uid) return false;
- $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
- if (!count($uidSubordinate)) return 0;
- return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
- }
- /**
- * 获取用户下级推广人
- * @param int $uid 当前用户
- * @param int $grade 等级 0 一级 1 二级
- * @param string $orderBy 排序
- * @param string $keyword
- * @param int $page
- * @param int $limit
- * @return array|bool
- */
- public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
- {
- if (!$uid) return [];
- $gradeGroup = [0, 1];
- if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
- $userStair = self::where('spread_uid', $uid)->column('uid');
- if (!count($userStair)) return [];
- if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
- $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
- return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
- }
- /**
- * 获取团队信息
- * @param $uid
- * @param string $orderBy
- * @param string $keyword
- * @param int $page
- * @param int $limit
- * @return array
- */
- public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
- {
- $model = new self;
- if ($orderBy === '') $orderBy = 'u.add_time desc';
- $model = $model->alias(' u');
- $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
- ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
- $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
- $model = $model->where('u.uid', 'IN', $uid);
- $model = $model->field("u.uid,u.nickname,u.avatar,from_unixtime(u.add_time,'%Y/%m/%d') as time,u.spread_count as childCount,u.pay_count as orderCount,p.numberCount");
- if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
- $model = $model->group('u.uid');
- $model = $model->order($orderBy);
- $model = $model->page($page, $limit);
- $list = $model->select();
- if ($list) return $list->toArray();
- else return [];
- }
- /**
- * 设置用户的上级关系
- * @param $uid 用户uid
- * @param $spreadUid 上级用户uid
- * @return User|bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function setSpreadUid($uid, $spreadUid)
- {
- return UserSpread::setSpread($uid, $spreadUid);
- // 自己不能绑定自己为上级
- // if ($uid == $spreadUid) return false;
- // //TODO 获取后台分销类型
- // $storeBrokerageStatus = sys_config('store_brokerage_statu');
- // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
- // if ($storeBrokerageStatus == 1) {
- // $spreadCount = self::where('uid', $spreadUid)->count();
- // if ($spreadCount) {
- // $spreadInfo = self::where('uid', $spreadUid)->find();
- // if ($spreadInfo->is_promoter) {
- // //TODO 只有扫码才可以获得推广权限
- // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
- // }
- // }
- // }
- // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
- }
- /**
- * 判断上下级关系是否存在
- * @param $uid
- * @param $spreadUid
- * @return bool|int
- */
- public static function validSpread($uid, $spreadUid)
- {
- if (!$uid || !$spreadUid) return false;
- return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
- }
- /**
- * H5用户注册
- * @param $account
- * @param $password
- * @param $spread
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function register($account, $password, $spread)
- {
- $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
- if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
- $phone = $account;
- $data['account'] = $account;
- $data['pwd'] = md5($password);
- $data['phone'] = $phone;
- // if ($spread) {
- // $data['spread_uid'] = $spread;
- // $data['spread_time'] = time();
- // }
- $data['real_name'] = '';
- $data['birthday'] = 0;
- $data['card_id'] = '';
- $data['mark'] = '';
- $data['addres'] = '';
- $data['user_type'] = 'h5';
- $data['add_time'] = time();
- $data['add_ip'] = app('request')->ip();
- $data['last_time'] = time();
- $data['last_ip'] = app('request')->ip();
- $data['nickname'] = substr(md5($account . time()), 0, 12);
- $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
- $data['city'] = '';
- $data['language'] = '';
- $data['province'] = '';
- $data['country'] = '';
- $data['is_promoter'] = $storeBrokerageStatu != 1 ? 1 : 0;
- self::beginTrans();
- $res2 = WechatUser::create($data);
- $data['uid'] = $res2->uid;
- $res1 = self::create($data);
- $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
- self::checkTrans($res);
- return $res;
- }
- /**
- * 密码修改
- * @param $account
- * @param $password
- * @return User|bool
- */
- public static function reset($account, $password)
- {
- if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
- $count = self::where('account', $account)->where('pwd', md5($password))->count();
- if ($count) return true;
- return self::where('account', $account)->update(['pwd' => md5($password)]);
- }
- /**
- * 获取手机号是否注册
- * @param $phone
- * @return bool
- */
- public static function checkPhone($phone)
- {
- return self::be(['account' => $phone]);
- }
- /**
- * 获取推广人
- * @param $data 查询条件
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getRankList($data)
- {
- switch ($data['type']) {
- case 'week':
- $startTime = strtotime('this week');
- $endTime = time();
- break;
- case 'month':
- $startTime = strtotime('last month');
- $endTime = time();
- break;
- }
- $list = self::alias('t0')
- ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
- ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
- ->where('t1.spread_uid', '<>', 0)
- ->order('count desc')
- ->order('t0.uid desc')
- ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
- ->page($data['page'], $data['limit'])
- ->group('t0.uid')
- ->select();
- return count($list) ? $list->toArray() : [];
- }
- /**
- * 获取佣金排行
- * @param $data
- * @return array
- */
- public static function brokerageRank($data)
- {
- $model = self::where('status', 1);
- switch ($data['type']) {
- case 'week':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereWeek('add_time')->field('uid');
- });
- break;
- case 'month':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereMonth('add_time')->field('uid');
- });
- break;
- }
- $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
- ->page((int)$data['page'], (int)$data['limit'])->select();
- return count($users) ? $users->toArray() : [];
- }
- /**
- * 获取当前用户的佣金排行位置
- * @param $uid
- * @return int
- */
- public static function currentUserRank($type, $brokerage_price)
- {
- $model = self::where('status', 1);
- switch ($type) {
- case 'week':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereWeek('add_time')->field('uid');
- });
- break;
- case 'month':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereMonth('add_time')->field('uid');
- });
- break;
- }
- return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
- }
- public static function user_count()
- {
- $model = new self();
- $silver_user = $model->where([['level', '=', 1], ['brokerage_price', '<', 200]])->select();// 白银会员
- $data['silver'] = count($silver_user);// 白银会员总数
- $data['silver_user'] = $silver_user ? $silver_user->toArray() : [];
- $gold_user = $model->where([['level', '=', 2], ['brokerage_price', '<', 6000]])->select();// 黄金会员
- $data['gold'] = count($gold_user);// 黄金会员总数
- $data['gold_user'] = $gold_user ? $gold_user->toArray() : [];
- $diamonds_user = $model->where([['level', '=', 3]])->select();// 钻石会员总数
- $data['diamonds'] = count($diamonds_user);// 钻石会员总数
- $data['diamonds_user'] = $diamonds_user ? $diamonds_user->toArray() : [];
- return $data;
- }
- /**
- * 商家转账积分分红
- * @param $money
- * @return void
- * @throws \Exception
- */
- public static function bring_forward($money)
- {
- $data = self::user_count();
- $model = new self();
- $silver_user = $data['silver_user'];
- $silver = $data['silver'];
- $gold_user = $data['gold_user'];
- $gold = $data['gold'];
- $diamonds_user = $data['diamonds_user'];
- $diamonds = $data['diamonds'];
- //商家转账积分后
- $divide = ($money * 0.1)/($silver+$gold);//白银会员和黄金会员分百分之十的金额
- $diamonds_divide = ($money * 0.01)/$diamonds;//钻石会员销售的百分之1
- $user = array_merge($silver_user, $gold_user);
- if ($user){
- // 黄金会员和白银会员
- foreach ($user as &$item){
- if ($item['level'] == 1){
- if ($item['brokerage_price']+$divide > 200){
- $item['brokerage_price'] += 200 - $item['brokerage_price'];
- UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', 200 - $item['brokerage_price'], $item['spread_uid'], $item['brokerage_price'], '商家销售额分红,已达到上限');
- }else{
- $item['brokerage_price'] += $divide;
- UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $divide, $item['spread_uid'], $item['brokerage_price'], '商家销售额分红');
- }
- }elseif ($item['level'] == 2){
- if ($item['brokerage_price']+$divide > 6000){
- $item['brokerage_price'] += 6000 - $item['brokerage_price'];
- UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', 6000 - $item['brokerage_price'], $item['spread_uid'], $item['brokerage_price'], '商家销售额分红,已达到上限');
- }else{
- $item['brokerage_price'] += $divide;
- UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $divide, $item['spread_uid'], $item['brokerage_price'], '商家销售额分红');
- }
- }
- }
- $model->saveAll($user);
- }
- if ($diamonds_user){
- // 钻石会员
- foreach ($diamonds_user as &$item){
- $item['brokerage_price'] += $diamonds_divide;
- UserBill::income('佣金', $item['uid'], 'now_money', 'brokerage', $diamonds_divide, $item['spread_uid'], $item['brokerage_price'], '商家销售额分红');
- }
- $model->saveAll($diamonds_user);
- }
- }
- public static function gift_bag($order_id)
- {
- $data = self::user_count();
- $model = new self();
- $silver_user = $data['silver_user'];
- $silver = $data['silver'];
- $gold_user = $data['gold_user'];
- $gold = $data['gold'];
- $diamonds_user = $data['diamonds_user'];
- $diamonds = $data['diamonds'];
- // 礼包分红
- $order = StoreOrder::where('order_id', $order_id)->find();
- $user = User::where('uid', $order['uid'])->find();
- $info = StoreOrderCartInfo::where('oid', $order['id'])->where('product_id', '<=', 2)->select();
- foreach ($info as $item){
- $product = StoreProduct::where('id', $item['product_id'])->find();
- if ($user['spread_uid']){
- // 当前用户是否有上级
- $spread = User::where('uid', $user['spread_uid'])->find();
- if ($spread['level'] == 2){
- // 如果用户是黄金会员
- $push = $product['price'] * 0.2; //获得价格百分之20的佣金
- if ($spread['brokerage_price']+$push > 6000){
- $max = 6000 - $spread['brokerage_price'];
- $spread['brokerage_price'] += 6000 - $spread['brokerage_price'];
- UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $max, $spread['spread_uid'], $spread['brokerage_price'], '直推礼包销售额20%分红,已达到上限');
- }else{
- $spread['brokerage_price'] += $push;
- UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $push, $spread['spread_uid'], $spread['brokerage_price'], '直推礼包销售额20%分红');
- }
- $spread->save();
- }
- }
- if ($product['id'] == 1){
- $divide = ($product['price'] * 0.1)/$silver; // 200礼包的百分之十白银会员平分
- $diamonds_divide = ($product['price'] * 0.03)/$diamonds;//钻石会员200礼包的百分之3
- if ($silver_user){
- foreach ($silver_user as &$value){
- if ($value['brokerage_price']+$divide > 200){
- $max = 200 - $value['brokerage_price'];
- $value['brokerage_price'] += 200 - $value['brokerage_price'];
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '白银会员200礼包销售额分红,已达到上限');
- }else{
- $value['brokerage_price'] += $divide;
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '白银会员200礼包销售额分红');
- }
- }
- $model->saveAll($silver_user);
- }
- if ($diamonds_user){
- // 钻石会员
- foreach ($diamonds_user as &$value){
- $value['brokerage_price'] += $diamonds_divide;
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $diamonds_divide, $value['spread_uid'], $value['brokerage_price'], '钻石会员200礼包提成');
- }
- $model->saveAll($diamonds_user);
- }
- }elseif ($product['id'] == 2){
- //1991礼包 白银会员和黄金会员分百分之十的金额
- $divide = ($product['price'] * 0.1)/($silver+$gold);
- $user = array_merge($silver_user, $gold_user);
- $top_user = getParent($order['uid']); // 获取到上级所有钻石用户
- $count = count($top_user);
- if ($count > 0){
- $top_divide = ($product['price'] * 0.2)/$count;//1991礼包百分之二十上级所有钻石会员平分
- foreach ($top_user as $v){
- $details = self::where('uid', $v)->find();
- $details['brokerage_price'] += $top_divide;
- $details->save();
- UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $top_divide, $details['spread_uid'], $details['brokerage_price'], '钻石会员1991礼包销售额20%分红');
- }
- }
- if ($user){
- // 黄金会员和白银会员
- foreach ($user as &$value){
- if ($value['level'] == 1){
- if ($value['brokerage_price']+$divide > 200){
- $max = 200 - $value['brokerage_price'];
- $value['brokerage_price'] += 200 - $value['brokerage_price'];
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '白银会员1991礼包销售额分红,已达到上限');
- }else{
- $value['brokerage_price'] += $divide;
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '白银会员1991礼包销售额分红');
- }
- }elseif ($value['level'] == 2){
- if ($value['brokerage_price']+$divide > 6000){
- $max = 6000 - $value['brokerage_price']; // 到达上限
- $value['brokerage_price'] += 6000 - $value['brokerage_price'];
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $max, $value['spread_uid'], $value['brokerage_price'], '黄金会员1991礼包销售额分红,已达到上限');
- }else{
- $value['brokerage_price'] += $divide;
- UserBill::income('佣金', $value['uid'], 'now_money', 'brokerage', $divide, $value['spread_uid'], $value['brokerage_price'], '黄金会员1991礼包销售额分红');
- }
- }
- }
- $model->saveAll($user);
- }
- }
- }
- }
- /**
- * 普通商品确认收货分红
- * @param $uid
- * @param $order_id
- * @return void
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function receiving($uid, $order_id)
- {
- $order = StoreOrder::where('order_id', $order_id)->find();
- $user = User::where('uid', $order['uid'])->find();
- $info = StoreOrderCartInfo::where('oid', $order['id'])->where('product_id', '>', 2)->select(); // 普通商品
- if ($user['spread_uid']){
- $top_user = getParent($uid); // 获取到上级所有钻石用户
- $count = count($top_user);
- $s_user = getParents($uid);// 获取到上级所有黄金白银用户
- $s_count = count($s_user);
- foreach ($info as $item){
- $product = StoreProduct::where('id', $item['product_id'])->find();
- if ($s_count > 0){
- // 黄金白银会员分红
- $divide = ($product['price'] * 0.1 )/ $s_count; // 普通商品百分之十的价格黄金和白银会员平分
- foreach ($s_user as $k => $v){
- $details = User::where('uid', $v)->find();
- if ($details['level'] == 1){
- if ($details['brokerage_price'] < 200){
- if ($details['brokerage_price']+$divide > 200){
- $max = 200 - $details['brokerage_price'];
- $details['brokerage_price'] += $max;
- $details->save();
- UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $max, $details['spread_uid'], $details['brokerage_price'], '白银会员普通商品分红,已达到上限');
- }else{
- $details['brokerage_price'] += $divide;
- $details->save();
- UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $divide, $details['spread_uid'], $details['brokerage_price'], '白银会员普通商品分红');
- }
- }
- }elseif ($details['level'] == 2){
- if ($details['brokerage_price']+$divide > 6000){
- $max = 6000 - $details['brokerage_price'];
- $details['brokerage_price'] += $max;
- $details->save();
- UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $max, $details['spread_uid'], $details['brokerage_price'], '黄金会员普通商品分红,已达到上限');
- }else{
- $details['brokerage_price'] += $divide;
- $details->save();
- UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $divide, $details['spread_uid'], $details['brokerage_price'], '黄金会员普通商品分红');
- }
- }
- }
- }
- if ($count > 0){
- // 钻石会员分红
- $top_divide = ($product['price'] * 0.05 )/ $count; // 钻石会员平分普通商品百分之五
- foreach ($top_user as $v){
- $details = self::where('uid', $v)->find();
- $details['brokerage_price'] += $top_divide;
- $details->save();
- UserBill::income('佣金', $details['uid'], 'now_money', 'brokerage', $top_divide, $details['spread_uid'], $details['brokerage_price'], '钻石会员普通商品分成');
- }
- }
- }
- }
- }
- /**
- * 分红
- * @param $type
- * @param $money
- * @param $uid
- * @param $order_id
- * @return array
- * @throws \Exception
- */
- public static function bonus($type, $money, $uid = 0, $order_id = '')
- {
- switch ($type){
- case 1:
- self::bring_forward($money);
- break;
- case 2:
- self::gift_bag($order_id);
- break;
- case 3:
- self::receiving($uid, $order_id);
- break;
- }
- return [];
- }
- public static function release()
- {
- $model = Db::name('release');
- $time = strtotime('today');
- if (!$model->where('time', $time)->find()){
- $user = self::where('integral', '>', 0)->select();
- foreach ($user as &$item){
- $release = round($item['integral']) * 0.03;// 释放积分额度
- $item['integral'] -= $release;
- $item['voucher'] += $release;
- UserBill::expend('释放积分', $item['uid'], 'integral', 'release_integral', $release, 0, $item['integral'], '每天释放积分额度');
- UserBill::income('增加抵用券',$item['uid'], 'voucher', 'release_ivoucher', $release, 0, $item['voucher'], '每天释放积分,变换抵用券');
- $item->save();
- }
- $model->insert([
- 'time' => $time
- ]);
- }
- }
- }
|