12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382 |
- <?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\User;
- use app\services\system\config\SystemUserLevelServices;
- use app\services\wechat\WechatUserServices;
- use qiniu\basic\BaseServices;
- use qiniu\exceptions\AdminException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\exception\ValidateException;
- use think\facade\Log;
- use think\Model;
- /**
- *
- * Class UserServices
- * @package app\services\user
- * @mixin User
- */
- class UserServices extends BaseServices
- {
- /**
- * 性别
- * @var array
- */
- public $sex = [
- 0 => '保密',//保密
- 1 => '男',//男
- 2 => '女',//女
- ];
- /**
- * UserServices constructor.
- * @param User $model
- */
- public function __construct(User $model)
- {
- $this->model = $model;
- }
- /**
- * 获取用户信息
- * @param int $uid
- * @param string|array $field
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getUserInfo(int $uid, $field = '*')
- {
- if (is_string($field)) $field = explode(',', $field);
- return $this->get($uid, $field);
- }
- /**
- * 是否存在
- * @param int $uid
- * @return bool
- * @throws DbException
- */
- public function userExist(int $uid)
- {
- return $this->be($uid);
- }
- /**
- * 获取用户列表
- * @param array $where
- * @param string $field
- * @return array
- * @throws DbException
- */
- public function getUserList(array $where, string $field = '*'): array
- {
- [$page, $limit] = $this->getPageValue();
- $list = $this->getList($where, $field, $page, $limit);
- $count = $this->getCount($where);
- return compact('list', 'count');
- }
- /**
- * 保存用户信息
- * @param $user
- * @param int $spreadUid
- * @param string $userType
- * @return User|Model
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function setUserInfo($user, int $spreadUid = 0, string $userType = 'wechat')
- {
- mt_srand();
- if (isset($user['pwd']) && $user['pwd']) {
- $password = $user['pwd'];
- $salt = $user['salt'] ?? '';
- } else {
- [$password, $salt] = password('123456');
- }
- $data = [
- 'account' => create_account(),
- 'pwd' => $password,
- 'salt' => $salt,
- 'nickname' => $user['nickname'] ?? '',
- 'avatar' => !empty($user['headimgurl']) ? $user['headimgurl'] : sys_config('h5_avatar'),
- 'phone' => $user['phone'] ?? '',
- 'birthday' => $user['birthday'] ?? '',
- 'user_type' => $userType
- ];
- $res = $this->create($data);
- if (!$res)
- throw new AdminException('保存用户信息失败');
- if ($spreadUid && $this->get($spreadUid)) {
- /** @var UserSpreadServices $spreadService */
- $spreadService = app()->make(UserSpreadServices::class);
- $spreadService->setSpread($res->uid, $spreadUid);
- }
- return $res;
- }
- /**
- * 设置用户登录类型
- * @param int $uid
- * @param string $type
- * @return bool
- * @throws Exception
- */
- public function setLoginType(int $uid, string $type = 'h5')
- {
- if (!$this->update($uid, ['login_type' => $type]))
- throw new Exception('设置登录类型失败');
- return true;
- }
- /**
- * 设置用户分组
- * @param $uids
- * @param int $group_id
- * @return Model|null
- */
- public function setUserGroup($uids, int $group_id)
- {
- return $this->batchUpdate($uids, ['group_id' => $group_id], 'uid');
- }
- /**
- * 显示资源列表头部
- * @return array[]
- */
- public function typeHead()
- {
- /** @var UserWechatuserServices $userWechatUser */
- $userWechatUser = app()->make(UserWechatuserServices::class);
- //全部会员
- $all = $this->getCount([]);
- //小程序会员
- $routine = $userWechatUser->getCount(['w.user_type' => 'routine']);
- //公众号会员
- $wechat = $userWechatUser->getCount(['w.user_type' => 'wechat']);
- //H5会员
- $h5 = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'h5']);
- //pc会员
- $pc = $userWechatUser->getCount(['w.openid' => '', 'u.user_type' => 'pc']);
- //app会员
- $app = $userWechatUser->getCount(['w.user_type' => 'app']);
- return [
- ['user_type' => '', 'name' => '全部会员', 'count' => $all],
- ['user_type' => 'routine', 'name' => '小程序会员', 'count' => $routine],
- ['user_type' => 'wechat', 'name' => '公众号会员', 'count' => $wechat],
- ['user_type' => 'h5', 'name' => 'H5会员', 'count' => $h5],
- ['user_type' => 'pc', 'name' => 'PC会员', 'count' => $pc],
- ['user_type' => 'app', 'name' => 'APP会员', 'count' => $app],
- ];
- }
- /**
- * 会员列表
- * @param array $where
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function userIndex(array $where)
- {
- /** @var UserWechatuserServices $userWechatUser */
- $userWechatUser = app()->make(UserWechatuserServices::class);
- $fields = 'u.*,w.country,w.province,w.city,u.sex,w.unionid,w.openid,w.user_type as w_user_type,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time';
- [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
- if ($list) {
- /** @var UserGroupServices $userGroup */
- $userGroup = app()->make(UserGroupServices::class)->getUsersGroupName(array_unique(array_column($list, 'group_id')));
- $spreadUids = array_unique(array_column($list, 'spread_uid'));
- $spread_names = $this->getColumn([['uid', 'in', $spreadUids]], 'nickname', 'uid');
- foreach ($list as &$item) {
- $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
- $item['delete'] = ($item['delete_time'] != null) ? '已注销' : '正常';
- $item['birthday'] = $item['birthday'] ? date('Y-m-d', (int)$item['birthday']) : '';
- $item['spread_uid_nickname'] = $item['spread_uid'] ? (($spread_names[$item['spread_uid']] ?? '') . '/' . $item['spread_uid']) : '无';
- //用户类型
- if ($item['user_type'] == 'routine') {
- $item['user_type'] = '小程序';
- } else if ($item['user_type'] == 'wechat') {
- $item['user_type'] = '公众号';
- } else if ($item['user_type'] == 'h5') {
- $item['user_type'] = 'H5';
- } else if ($item['user_type'] == 'pc') {
- $item['user_type'] = 'PC';
- } else if ($item['user_type'] == 'app') {
- $item['user_type'] = 'APP';
- } else $item['user_type'] = '其他';
- if ($item['sex'] == 1) {
- $item['sex'] = '男';
- } else if ($item['sex'] == 2) {
- $item['sex'] = '女';
- } else $item['sex'] = '保密';
- //等级名称
- /** @var UserLevelServices $levelService */
- $levelService = app()->make(UserLevelServices::class);
- $levelInfo = $levelService->getUserLevelInfo($item['uid']);
- //分组名称
- $item['group_id'] = $userGroup[$item['group_id']] ?? '无';
- //用户等级
- $item['vip_name'] = $levelInfo['name'] ?? '无';
- }
- }
- return compact('count', 'list');
- }
- /**
- * 修改提交处理
- * @param int $id
- * @param array $data
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function updateInfo(int $id, array $data)
- {
- $user = $this->getUserInfo($id);
- if (!$user) {
- throw new AdminException('数据不存在!');
- }
- $edit = [];
- if ($data['phone']) {
- $otherUser = $this->getOne(['phone' => $data['phone']], 'uid,phone');
- if ($otherUser && $otherUser['uid'] != $id) {
- throw new AdminException('该手机号码已被注册');
- }
- $otherUser = $this->getOne(['account' => $data['phone']], 'uid,account');
- if ($otherUser && $otherUser['uid'] != $id) {
- throw new AdminException('该手机号码已被注册');
- }
- if ($data['phone'] != $user['phone']) {
- $edit['phone'] = $data['phone'];
- $edit['account'] = $data['phone'];
- }
- }
- if (isset($data['pwd']) && $data['pwd'] && $data['pwd'] != $user['pwd']) {
- $edit['pwd'] = $data['pwd'];
- $edit['salt'] = $data['salt'];
- }
- if (isset($data['trade_pwd']) && $data['trade_pwd'] && $data['trade_pwd'] != $user['trade_pwd']) {
- $edit['trade_pwd'] = $data['trade_pwd'];
- $edit['trade_salt'] = $data['trade_salt'];
- }
- $edit['status'] = $data['status'];
- $edit['real_name'] = $data['real_name'];
- $edit['card_id'] = $data['card_id'];
- $edit['birthday'] = strtotime($data['birthday']);
- $edit['mark'] = $data['mark'];
- $edit['is_promoter'] = (isset($data['is_promoter']) && $data['is_promoter'] >= 0) ? $data['is_promoter'] : $user['is_promoter'];
- $edit['group_id'] = $data['group_id'];
- if ($data['spread_uid'] >= 0 && $data['spread_uid'] != $user['spread_uid']) {
- /** @var UserSpreadServices $spreadService */
- $spreadService = app()->make(UserSpreadServices::class);
- $spreadService->adminSetSpread($user['uid'], $data['spread_uid']);
- }
- $edit['sex'] = $data['sex'];
- $edit['provincials'] = $data['provincials'];
- $edit['province'] = $data['province'];
- $edit['city'] = $data['city'];
- $edit['area'] = $data['area'];
- $edit['street'] = $data['street'];
- if ($user['level'] != $data['level']) {
- $this->saveGiveLevel((int)$user['uid'], (int)$data['level']);
- }
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- $wechatUser->update(['uid' => $id], ['sex' => $data['sex']]);
- $res = $this->update($id, $edit);
- if ($res) {
- return true;
- } else
- throw new AdminException('修改失败');
- }
- public function updateAccount($uid, $money_type, $pm, $num, $mark = '', $admin_id = 0)
- {
- if ($money_type == 'now_money') {
- /** @var UserMoneyServices $service */
- $service = app()->make(UserMoneyServices::class);
- $type = ($pm == 1 ? 'system_add' : 'system_sub');
- } elseif ($money_type == 'brokerage_price') {
- /** @var UserBrokerageServices $service */
- $service = app()->make(UserBrokerageServices::class);
- $type = ($pm == 1 ? 'system_add' : 'system_sub');
- } else {
- /** @var UserBillServices $service */
- $service = app()->make(UserBillServices::class);
- $type = ($pm == 1 ? 'system_add_' . $money_type : 'system_sub_' . $money_type);
- }
- $user = $this->getUserInfo($uid);
- if (!isset($user[$money_type])) throw new AdminException('请选择要操作的数据');
- return $service->income($type, $uid, ['number' => $num, 'mark' => $mark ? ',' . $mark : ''], $admin_id);
- }
- /**
- * 执行赠送会员等级
- * @param int $id
- * @param int $level_id
- * @return mixed
- * @throws DbException
- */
- public function saveGiveLevel(int $id, int $level_id)
- {
- if (!$this->userExist($id)) {
- throw new AdminException('用户不存在');
- }
- /** @var SystemUserLevelServices $systemLevelServices */
- $systemLevelServices = app()->make(SystemUserLevelServices::class);
- /** @var UserLevelServices $userLevelServices */
- $userLevelServices = app()->make(UserLevelServices::class);
- //查询当前选择的会员等级
- $systemLevel = $systemLevelServices->getLevel($level_id);
- if (!$systemLevel) throw new AdminException('您选择赠送的用户等级不存在!');
- //检查是否拥有此会员等级
- $level = $userLevelServices->getOne(['uid' => $id, 'level_id' => $level_id, 'valid' => 1]);
- if ($level) {
- throw new AdminException('此用户已有该用户等级,无法再次赠送');
- }
- //保存会员信息
- if (!$userLevelServices->setUserLevel($id, $level_id, $systemLevel)) {
- throw new AdminException('赠送失败');
- }
- $spread_uid = $this->getUserInfo($id)['spread_uid'];
- if ($spread_uid > 0) {
- /** @var UserLevelServices $level */
- $level = app()->make(UserSpreadServices::class);
- $level->detection($spread_uid);
- }
- return true;
- }
- /**
- * 清除会员等级
- * @paran int $uid
- * @paran boolean
- * */
- public function cleanUpLevel($uid)
- {
- if (!$this->userExist($uid))
- throw new AdminException('用户不存在');
- /** @var UserLevelServices $services */
- $services = app()->make(UserLevelServices::class);
- return $this->transaction(function () use ($uid, $services) {
- $res = $services->delUserLevel($uid);
- $res1 = $this->update($uid, ['clean_time' => time(), 'level' => 0], 'uid');
- if (!$res && !$res1)
- throw new AdminException('清除失败');
- return true;
- });
- }
- /**
- * 获取单个用户信息
- * @param int $id 用户id
- * @param string $type
- * @param array $where
- * @return mixed
- * @throws DbException
- */
- public function oneUserInfo(int $id, string $type, $where = [])
- {
- switch ($type) {
- case 'spread':
- [$page, $limit] = $this->getPageValue();
- return $this->getList(['spread_uid' => $id], 'uid,avatar,nickname,real_name,spread_uid,spread_time,is_promoter,spread_count,user_type', $page, $limit);
- break;
- case 'bill_log':
- /** @var UserBillServices $services */
- $category = $where['category'] ?? 'integral';
- $time = $where['time'] ?? [];
- $services = app()->make(UserBillServices::class);
- return $services->getBillList($id, $category, $time, 'title,category,number,balance,mark,add_time');
- break;
- case 'balance_log':
- /** @var UserMoneyServices $services */
- $services = app()->make(UserMoneyServices::class);
- $where['uid'] = $id;
- return $services->getMoneyList($where, 'title,type,number,balance,mark,pm,status,add_time');
- break;
- case 'brokerage_log':
- /** @var UserBrokerageServices $services */
- $services = app()->make(UserBrokerageServices::class);
- $where['uid'] = $id;
- return $services->getBrokerageList($where, 'title,type,number,balance,mark,pm,status,add_time,frozen_time');
- break;
- default:
- throw new AdminException('type参数错误');
- }
- }
- /***********************************************/
- /************ 前端api services *****************/
- /***********************************************/
- /**
- * 用户信息
- * @param $info
- * @return mixed
- */
- public function userInfo($info)
- {
- $uid = (int)$info['uid'];
- return $info;
- }
- /**
- * 个人中心
- * @param array $user
- */
- public function personalHome(array $user, $tokenData)
- {
- $uid = (int)$user['uid'];
- event('user.login', [$uid, app('request')->ip()]);
- /** @var StoreCouponUserServices $storeCoupon */
- $storeCoupon = app()->make(StoreCouponUserServices::class);
- /** @var UserMoneyServices $userMoneyServices */
- $userMoneyServices = app()->make(UserMoneyServices::class);
- /** @var UserExtractServices $userExtract */
- $userExtract = app()->make(UserExtractServices::class);
- /** @var StoreOrderServices $storeOrder */
- $storeOrder = app()->make(StoreOrderServices::class);
- /** @var UserLevelServices $userLevel */
- $userLevel = app()->make(UserLevelServices::class);
- /** @var StoreServiceServices $storeService */
- $storeService = app()->make(StoreServiceServices::class);
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- /** @var UserRelationServices $productRelation */
- $productRelation = app()->make(UserRelationServices::class);
- /** @var SystemMessageServices $systemMessageServices */
- $systemMessageServices = app()->make(SystemMessageServices::class);
- /** @var DiyServices $diyServices */
- $diyServices = app()->make(DiyServices::class);
- /** @var AgentLevelServices $agentLevelServices */
- $agentLevelServices = app()->make(AgentLevelServices::class);
- /** @var StoreProductLogServices $storeProductLogServices */
- $storeProductLogServices = app()->make(StoreProductLogServices::class);
- //是否存在核销码
- if (!$user['bar_code']) {
- $bar_code = $this->getBarCode();
- $this->dao->update($uid, ['bar_code' => $bar_code], 'uid');
- $user['bar_code'] = $bar_code;
- }
- //获取配置参数
- $configData = SystemConfigService::more([
- 'member_card_status',
- 'brokerage_func_status',
- 'store_brokerage_statu',
- 'store_brokerage_price',
- 'member_func_status',
- 'recharge_switch',
- 'extract_time',
- 'balance_func_status',
- 'invoice_func_status',
- 'special_invoice_status',
- 'user_extract_bank_status',
- 'user_extract_wechat_status',
- 'user_extract_alipay_status',
- 'level_activate_status'
- ]);
- //看付费会员是否开启
- $user['is_open_member'] = $user['svip_open'] = !!($configData['member_card_status'] ?? 0);
- $user['agent_level_name'] = '';
- //分销等级信息
- if ($user['agent_level']) {
- $levelInfo = $agentLevelServices->getLevelInfo((int)$user['agent_level'], 'id,name');
- $user['agent_level_name'] = $levelInfo && $levelInfo['name'] ? $levelInfo['name'] : '';
- }
- $wechatUserInfo = $wechatUser->getOne(['uid' => $uid, 'user_type' => $tokenData['type']]);
- $user['is_complete'] = $wechatUserInfo['is_complete'] ?? 0;
- $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
- $user['like'] = $productRelation->getUserCount($uid, 0, 'like');
- $collectCategory = sys_config('video_func_status', 1) ? '' : 'product';
- $user['collectCount'] = $productRelation->getUserCount($uid, 0, 'collect', $collectCategory);
- $user['orderStatusNum'] = $storeOrder->getOrderData($uid);
- $user['notice'] = 0;
- $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
- $user['orderStatusSum'] = (float)$userMoneyServices->sum(['uid' => $uid, 'pm' => 0, 'status' => 1], 'number', true);
- $user['extractTotalPrice'] = $userExtract->getExtractSum(['uid' => $uid, 'status' => 1]);//累计提现
- $user['extractPrice'] = $user['brokerage_price'];//可提现
- $user['statu'] = (int)($configData['store_brokerage_statu'] ?? 0);
- $orderStatusSum = (float)$storeOrder->sum(['pid' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'uid' => $user['uid'], 'is_del' => 0], 'pay_price', true);//累计有效消费
- $user['spread_status'] = ($configData['brokerage_func_status'] ?? 1) && $this->checkUserPromoter($user['uid'], $user, $orderStatusSum);
- if (!$user['is_promoter'] && $user['spread_status']) {
- $this->dao->update($uid, ['is_promoter' => 1], 'uid');
- $user['is_promoter'] = 1;
- }
- if ($user['statu'] == 3) {
- $storeBrokeragePrice = $configData['store_brokerage_price'] ?? 0;
- $user['promoter_price'] = bcsub((string)$storeBrokeragePrice, (string)$user['orderStatusSum'], 2);
- }
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- $user['broken_commission'] = $userBrokerageServices->getUserFrozenPrice($uid);
- if ($user['broken_commission'] < 0)
- $user['broken_commission'] = 0;
- $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
- if ($user['commissionCount'] < 0)
- $user['commissionCount'] = 0;
- //用户等级信息
- $userLevelInfo = $userLevel->homeGetUserLevel((int)$user['uid'], $user);
- $user = array_merge($user, $userLevelInfo);
- $user['yesterDay'] = $userBrokerageServices->getUsersBokerageSum(['uid' => $uid, 'pm' => 1], 'yesterday');
- $user['recharge_switch'] = (int)($configData['recharge_switch'] ?? 0);//充值开关
- $user['adminid'] = $storeService->checkoutIsService(['uid' => $uid, 'status' => 1, 'customer' => 1]);
- $user['broken_day'] = (int)($configData['extract_time'] ?? 0);//佣金冻结时间
- $user['balance_func_status'] = (int)($configData['balance_func_status'] ?? 0);
- $user['invioce_func'] = !!($configData['invoice_func_status'] ?? 0);
- $user['special_invoice'] = $user['invioce_func'] && ($configData['special_invoice_status'] ?? 0);
- $user['pay_vip_status'] = $user['is_ever_level'] || ($user['is_money_level'] && $user['overdue_time'] > time());
- $user['member_style'] = (int)$diyServices->getColorChange('member');
- if ($user['is_ever_level']) {
- $user['vip_status'] = 1;//永久会员
- } else {
- if (!$user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] < time()) {
- $user['vip_status'] = -1;//开通过已过期
- } else if (!$user['overdue_time'] && !$user['is_money_level']) {
- $user['vip_status'] = 2;//没有开通过
- } else if ($user['is_money_level'] && $user['overdue_time'] && $user['overdue_time'] > time()) {
- $user['vip_status'] = 3;//开通了,没有到期
- }
- }
- /** @var StoreServiceRecordServices $servicesRecord */
- $servicesRecord = app()->make(StoreServiceRecordServices::class);
- $service_num = $servicesRecord->sum(['user_id' => $uid], 'mssage_num');
- $message = $systemMessageServices->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
- $user['service_num'] = (int)bcadd((string)$service_num, (string)$message);
- $user['is_agent_level'] = ($configData['brokerage_func_status'] ?? 1) && $agentLevelServices->count(['status' => 1, 'is_del' => 0]);
- $user['visit_num'] = $storeProductLogServices->getDistinctCount(['uid' => $uid, 'type' => 'visit'], 'product_id');
- $user['user_extract_bank_status'] = (int)($configData['user_extract_bank_status'] ?? 1);
- $user['user_extract_wechat_status'] = (int)($configData['user_extract_wechat_status'] ?? 1);
- $user['user_extract_alipay_status'] = (int)($configData['user_extract_alipay_status'] ?? 1);
- //是否享受新人专享
- /** @var StoreNewcomerServices $newcomerServices */
- $newcomerServices = app()->make(StoreNewcomerServices::class);
- $user['newcomer_status'] = $newcomerServices->checkUserNewcomer($uid);
- $user['level_activate_status'] = $configData['level_activate_status'];
- $user['member_func_status'] = $configData['member_func_status'];
- $extendInfo = SystemConfigService::get('user_extend_info', []);
- $user['register_extend_info'] = [];
- if (!$user['level_activate_status']) {//不需要激活,用户激活状态默认为1
- $user['level_status'] = 1;
- }
- if ($extendInfo) {
- foreach ($extendInfo as $item) {
- if (isset($item['use']) && $item['use'] && isset($item['user_show']) && $item['user_show']) $user['register_extend_info'][] = $item;
- }
- }
- if (isset($user['extend_info']) && $user['extend_info']) {
- $default = $this->defaultExtendInfo;
- $params = array_column($default, 'param');
- $sex = $this->rSex;
- foreach ($user['extend_info'] as &$info) {
- if (isset($info['param']) && in_array($info['param'], $params)) {
- if ($info['param'] == 'sex') {
- $info['value'] = $sex[$user['sex']] ?? 0;
- } elseif ($info['param'] == 'birthday') {
- $info['value'] = ($user['birthday'] ?? '') ? date('Y-m-d', $user['birthday']) : '';
- } elseif ($info['param'] == 'address') {
- $info['value'] = $user['addres'] ?? '';
- } else {
- $info['value'] = $user[$info['param']] ?? '';
- }
- }
- }
- }
- // /** @var UserAwardIntegralServices $award_integral_service */
- // $award_integral_service = app()->make(UserAwardIntegralServices::class);
- // $award_lack = $award_integral_service->getLake();
- // $integral_price = $award_integral_service->getPrice();
- // $static_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 0, 'uid' => $user['uid']]);
- // $action_integral = $award_integral_service->getIntegralSum(['status' => 0, 'type' => 1, 'uid' => $user['uid']]);
- // $sum_integral = $award_integral_service->getIntegralSum(['status' => 0]);
- $hidden_integral = sys_config('hidden_integral', 0);
- $user['is_default_avatar'] = $user['avatar'] == sys_config('h5_avatar') ? 1 : 0;
- // $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
- /** @var AwardIntegralServices $award_integral_service */
- $award_integral_service = app()->make(AwardIntegralServices::class);
- $user['achievement'] = $award_integral_service->getAchievement($user['uid']);
- $all_integral = $award_integral_service->sum(['uid' => $user['uid']], 'num');
- $sent_integral = $award_integral_service->sum(['uid' => $user['uid']], 'sent_num');
- $stand_integral = (float)bcsub($all_integral, $sent_integral, 2);
- $extractable_integral = $award_integral_service->sum(['uid' => $user['uid']], 'extract_num');
- $extracted_integral = $award_integral_service->sum(['uid' => $user['uid']], 'extracted_num');
- $shop_integral_trade_commission = sys_config('shop_integral_trade_commission');//微信提现到账方式
- $shop_integral_trade_switch = sys_config('shop_integral_trade_switch');//微信提现到账方式
- return array_merge($user, compact('shop_integral_trade_commission', 'shop_integral_trade_switch', 'hidden_integral', 'all_integral', 'sent_integral', 'stand_integral', 'extractable_integral', 'extracted_integral'));
- }
- /**
- * 用户资金统计
- * @param int $uid `
- */
- public function balance(int $uid)
- {
- $userInfo = $this->getUserInfo($uid);
- if (!$userInfo) {
- throw new ValidateException('数据不存在');
- }
- /** @var UserMoneyServices $userMoneyServices */
- $userMoneyServices = app()->make(UserMoneyServices::class);
- /** @var StoreOrderServices $storeOrder */
- $storeOrder = app()->make(StoreOrderServices::class);
- $user['now_money'] = $userInfo['now_money'];//当前总资金
- $user['recharge'] = $userMoneyServices->getRechargeSum($uid);//累计充值
- $user['orderStatusSum'] = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price', true);//累计消费
- return $user;
- }
- /**
- * 用户修改信息
- * @param Request $request
- * @return mixed
- */
- public function eidtNickname(int $uid, array $data)
- {
- if (!$this->userExist($uid)) {
- throw new ValidateException('用户不存在');
- }
- if (!$this->dao->update($uid, $data, 'uid')) {
- throw new ValidateException('修改失败');
- }
- return true;
- }
- /**
- * 获取推广人排行
- * @param $data 查询条件
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getRankList(array $data)
- {
- $startTime = $endTime = 0;
- switch ($data['type']) {
- case 'week':
- $startTime = strtotime('this week Monday');
- break;
- case 'month':
- $startTime = strtotime('first day of ' . date('F Y'));
- break;
- }
- $endTime = time();
- [$page, $limit] = $this->getPageValue();
- $field = 'spread_uid,count(uid) AS count,spread_time';
- /** @var UserSpreadServices $userSpreadServices */
- $userSpreadServices = app()->make(UserSpreadServices::class);
- $list = $userSpreadServices->getAgentRankList([$startTime, $endTime], $field, $page, $limit);
- $rank = 0;
- foreach ($list as $key => $item) {
- if ($item['spread_uid'] == $data['uid']) $rank = $key + 1;
- }
- $week = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('this week Monday'), time()], 'timeKey' => 'spread_time']);
- $month = $userSpreadServices->count(['spread_uid' => $data['uid'], 'time' => [strtotime('last month'), time()], 'timeKey' => 'spread_time']);
- $start = date('Y-m-d H:i', $startTime);
- $end = date('Y-m-d H:i', time());
- return compact('list', 'rank', 'week', 'month', 'start', 'end');
- }
- /**
- * 静默绑定推广人
- * @param Request $request
- * @return mixed
- */
- public function spread(int $uid, int $spreadUid, $code)
- {
- $userInfo = $this->getUserInfo($uid);
- if (!$userInfo) {
- throw new ValidateException('数据不存在');
- }
- if ($code && !$spreadUid) {
- /** @var QrcodeServices $qrCode */
- $qrCode = app()->make(QrcodeServices::class);
- if ($info = $qrCode->getOne(['id' => $code, 'status' => 1])) {
- $spreadUid = $info['third_id'];
- }
- }
- //记录好友关系
- if ($spreadUid && $uid && $spreadUid != $uid) {
- /** @var UserFriendsServices $serviceFriend */
- $serviceFriend = app()->make(UserFriendsServices::class);
- $serviceFriend->saveFriend($uid, $spreadUid);
- }
- $data = [];
- //永久绑定
- $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
- $spread_uid = isset($user['code']) && $user['code'] && $user['code'] != $userInfo->uid ? $user['code'] : ($userInfo['spread_uid'] ?? 0);
- if ($userInfo->spread_uid && $store_brokergae_binding_status == 1) {
- $data['login_type'] = $user['login_type'] ?? $userInfo->login_type;
- } else {
- //绑定分销关系 = 所有用户
- if (sys_config('brokerage_bindind', 1) == 1) {
- //分销绑定类型为时间段且过期 ||临时
- $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
- if (!$userInfo['spread_uid'] || $store_brokergae_binding_status == 3 || ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) < time())) {
- $spreadUid = $spread_uid;
- if ($spreadUid && $userInfo->uid == $this->dao->value(['uid' => $spreadUid], 'spread_uid')) {
- $spreadUid = 0;
- }
- if ($spreadUid && $this->dao->get((int)$spreadUid)) {
- $data['spread_uid'] = $spreadUid;
- $data['spread_time'] = time();
- }
- }
- }
- }
- if ($data && !$this->dao->update($userInfo['uid'], $data, 'uid')) {
- throw new ValidateException('修改信息失败');
- }
- if (isset($data['spread_uid']) && $data['spread_uid']) {
- /** @var UserBillServices $userBill */
- $userBill = app()->make(UserBillServices::class);
- //邀请新用户增加经验
- $userBill->inviteUserIncExp((int)$spreadUid);
- }
- return true;
- }
- /**
- * 添加访问记录
- * @param Request $request
- * @return mixed
- */
- public function setVisit(array $data)
- {
- $userInfo = $this->getUserInfo($data['uid']);
- if (!$userInfo) {
- throw new ValidateException('数据不存在');
- }
- if (isset($data['ip']) && $data['ip']) {
- $addressArr = $this->addressHandle($this->convertIp($data['ip']));
- $data['province'] = $addressArr['province'] ?? '';
- }
- $data['channel_type'] = $userInfo['user_type'];
- $data['add_time'] = time();
- /** @var UserVisitServices $userVisit */
- $userVisit = app()->make(UserVisitServices::class);
- if ($userVisit->save($data)) {
- return true;
- } else {
- throw new ValidateException('添加访问记录失败');
- }
- }
- /**
- * 获取活动状态
- * @return mixed
- */
- public function activity()
- {
- /** @var StoreBargainServices $storeBragain */
- $storeBragain = app()->make(StoreBargainServices::class);
- /** @var StoreCombinationServices $storeCombinaion */
- $storeCombinaion = app()->make(StoreCombinationServices::class);
- /** @var StoreSeckillServices $storeSeckill */
- $storeSeckill = app()->make(StoreSeckillServices::class);
- $data['is_bargin'] = (bool)$storeBragain->validBargain();
- $data['is_pink'] = (bool)$storeCombinaion->validCombination();
- $data['is_seckill'] = (bool)$storeSeckill->getSeckillCount();
- return $data;
- }
- /**
- * 获取用户下级推广人
- * @param int $uid 当前用户
- * @param int $grade 等级 0 一级 1 二级
- * @param string $orderBy 排序
- * @param string $keyword
- * @return array|bool
- */
- public function getUserSpreadGrade(int $uid = 0, $grade = 0, $orderBy = '', $keyword = '', $time = [])
- {
- $user = $this->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('数据不存在');
- }
- $spread_one_ids = $this->getUserSpredadUids($uid, 1);
- $spread_two_ids = $this->getUserSpredadUids($uid, 2);
- $data = [
- 'total' => count($spread_one_ids),
- 'totalLevel' => count($spread_two_ids),
- 'list' => []
- ];
- /** @var UserStoreOrderServices $userStoreOrder */
- $userStoreOrder = app()->make(UserStoreOrderServices::class);
- $list = [];
- $where = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
- if ($grade == 0) {
- if ($spread_one_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_one_ids, $orderBy, $keyword, $time);
- $where = $where + ['spread_uid' => $uid];
- } else {
- if ($spread_two_ids) $list = $userStoreOrder->getUserSpreadCountList($spread_two_ids, $orderBy, $keyword, $time);
- $where = $where + ['spread_two_uid' => $uid];
- }
- foreach ($list as &$item) {
- if (isset($item['spread_time']) && $item['spread_time']) {
- $item['time'] = date('Y/m/d', $item['spread_time']);
- }
- }
- $data['list'] = $list;
- $data['brokerage_level'] = (int)sys_config('brokerage_level', 2);
- $data['count'] = 0;
- $data['price'] = 0;
- if ($list) {
- $uids = array_column($list, 'uid');
- $data['count'] = count($uids);
- /** @var StoreOrderServices $storeOrder */
- $storeOrder = app()->make(StoreOrderServices::class);
- $data['price'] = $storeOrder->sum($where, $grade == 0 ? 'one_brokerage' : 'two_brokerage');
- }
- return $data;
- }
- /**
- * 获取推广人uids
- * @param int $uid
- * @param bool $one
- * @return array
- */
- public function getUserSpredadUids(int $uid, int $type = 0)
- {
- $uids = $this->dao->getColumn(['spread_uid' => $uid], 'uid');
- if ($type === 1) {
- return $uids;
- }
- if ($uids) {
- $uidsTwo = $this->dao->getColumn([['spread_uid', 'in', $uids]], 'uid');
- if ($type === 2) {
- return $uidsTwo;
- }
- if ($uidsTwo) {
- $uids = array_merge($uids, $uidsTwo);
- }
- }
- return $uids;
- }
- /**
- * 检测用户是否是推广员
- * @param int $uid
- * @param array $user
- * @param float $sumPrice
- * @return bool
- */
- public function checkUserPromoter(int $uid, $user = [], float $sumPrice = 0.00)
- {
- if (!$uid) {
- return false;
- }
- if (!$user) {
- $user = $this->getUserInfo($uid);
- }
- if (!$user) {
- return false;
- }
- //分销是否开启
- if (!sys_config('brokerage_func_status')) {
- return false;
- }
- //用户分校推广资格是否开启4.0.32
- if (isset($user['spread_open']) && !$user['spread_open']) {
- return false;
- }
- $store_brokerage_statu = sys_config('store_brokerage_statu');
- if ($user['is_promoter'] || $store_brokerage_statu == 2) {
- return true;
- }
- if ($store_brokerage_statu == 3) {
- if (!$sumPrice) {
- /** @var StoreOrderServices $storeOrder */
- $storeOrder = app()->make(StoreOrderServices::class);
- $sumPrice = $storeOrder->sum(['pid' => 0, 'uid' => $uid, 'paid' => 1, 'is_del' => 0, 'refund_status' => [0, 3]], 'pay_price');//累计消费
- }
- $store_brokerage_price = sys_config('store_brokerage_price');
- if ($sumPrice >= $store_brokerage_price) {
- return true;
- }
- }
- return false;
- }
- /**
- * 同步微信粉丝用户(后台接口)
- * @return bool
- */
- public function syncWechatUsers()
- {
- $key = md5('sync_wechat_users');
- //一天点击一次
- if (CacheService::get($key)) {
- return true;
- }
- $next_openid = null;
- do {
- $result = OfficialAccount::userService()->list($next_openid);
- $userOpenids = $result['data'];
- //拆分大数组
- $opemidArr = array_chunk($userOpenids, 100);
- foreach ($opemidArr as $openids) {
- //加入同步|更新用户队列
- UserJob::dispatch([$openids]);
- }
- $next_openid = $result['next_openid'];
- } while ($next_openid != null);
- CacheService::set($key, 1, 3600 * 24);
- return true;
- }
- /**
- * 导入微信粉丝用户
- * @param array $openids
- * @return bool
- */
- public function importUser(array $noBeOpenids)
- {
- if (!$noBeOpenids) {
- return true;
- }
- $dataAll = $data = [];
- $time = time();
- foreach ($noBeOpenids as $openid) {
- try {
- $info = OfficialAccount::userService()->get($openid);
- $info = is_object($info) ? $info->toArray() : $info;
- } catch (\Throwable $e) {
- $info = [];
- }
- if (!$info) continue;
- if (($info['subscribe'] ?? 1) == 1) {
- $data['nickname'] = $info['nickname'] ?? '';
- $data['headimgurl'] = $info['headimgurl'] ?? '';
- $userInfoData = $this->setUserInfo($data);
- if (!$userInfoData) {
- throw new AdminException('用户信息储存失败!');
- }
- $data['uid'] = $userInfoData['uid'];
- $data['subscribe'] = $info['subscribe'];
- $data['unionid'] = $info['unionid'] ?? '';
- $data['openid'] = $info['openid'] ?? '';
- $data['sex'] = $info['sex'] ?? 0;
- $data['language'] = $info['language'] ?? '';
- $data['city'] = $info['city'] ?? '';
- $data['province'] = $info['province'] ?? '';
- $data['country'] = $info['country'] ?? '';
- $data['subscribe_time'] = $info['subscribe_time'] ?? '';
- $data['groupid'] = $info['groupid'] ?? 0;
- $data['remark'] = $info['remark'] ?? '';
- $data['tagid_list'] = isset($info['tagid_list']) && $info['tagid_list'] ? implode(',', $info['tagid_list']) : '';
- $data['add_time'] = $time;
- $data['is_complete'] = 1;
- $dataAll[] = $data;
- }
- }
- if ($dataAll) {
- /** @var WechatUserServices $wechatUser */
- $wechatUser = app()->make(WechatUserServices::class);
- if (!$wechatUser->saveAll($dataAll)) {
- throw new ValidateException('保存用户信息失败');
- }
- }
- return true;
- }
- /**
- * 修改会员的时间及是否会员状态
- * @param int $vip_day 会员天数
- * @param array $user_id 用户id
- * @param int $is_money_level 会员来源途径
- * @param bool $member_type 会员卡类型
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function setMemberOverdueTime($vip_day, int $user_id, int $is_money_level, $member_type = false)
- {
- if ($vip_day == 0) throw new ValidateException('天数不能为0');
- $user_info = $this->getUserInfo($user_id);
- if (!$user_info) throw new ValidateException('用户数据不存在');
- if (!$member_type) $member_type = "month";
- if ($member_type == 'ever') {
- $overdue_time = 0;
- $is_ever_level = 1;
- } else {
- if ($user_info['is_money_level'] == 0) {
- $overdue_time = bcadd(bcmul($vip_day, 86400, 0), time(), 0);
- } else {
- $overdue_time = bcadd(bcmul($vip_day, 86400, 0), $user_info['overdue_time'], 0);
- }
- $is_ever_level = 0;
- }
- $setData['overdue_time'] = $overdue_time;
- $setData['is_ever_level'] = $is_ever_level;
- $setData['is_money_level'] = $is_money_level ? $is_money_level : 0;
- // if ($user_info['level'] == 0) $setData['level'] = 1;
- return $this->dao->update(['uid' => $user_id], $setData);
- }
- /**
- * 清空到期svip(分批加入队列)
- * @return bool
- */
- public function offUserSvip()
- {
- $users = $this->dao->getColumn([['is_ever_level', '=', 0], ['is_money_level', '>', 0], ['overdue_time', '<', time()]], 'uid');
- if ($users) {
- //拆分大数组
- $uidsArr = array_chunk($users, 100);
- foreach ($uidsArr as $uids) {
- //加入同步|更新用户队列
- UserSvipJob::dispatch([$uids]);
- }
- }
- return true;
- }
- /**
- * 会员过期改变状态,变为普通会员
- * @param $uid
- * @param null $userInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function offMemberLevel($uid, $userInfo = null)
- {
- if (!$uid) return false;
- $userInfo = $userInfo ?: $this->dao->get($uid);
- if (!$userInfo) return false;
- if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
- $this->dao->update(['uid' => $uid], ['is_money_level' => 0]);
- return false;
- }
- return true;
- }
- /**
- * @param array $where
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getUserInfoList(array $where, $field = "*")
- {
- return $this->dao->getUserInfoList($where, $field);
- }
- /**
- * 保存用户上级推广人
- * @param int $uid
- * @param int $spread_uid
- * @return bool
- */
- public function saveUserSpreadUid(int $uid, int $spread_uid)
- {
- if (!$uid || !$spread_uid) {
- return false;
- }
- if ($uid == $spread_uid) {
- throw new ValidateException('上级推广人不能为自己');
- }
- $userInfo = $this->getUserInfo($uid);
- if (!$userInfo) {
- throw new ValidateException('用户不存在');
- }
- //上级已经是这个uid
- if ($userInfo['spread_uid'] == $spread_uid) {
- return true;
- }
- $spreadInfo = $this->getUserInfo($spread_uid);
- if (!$spreadInfo) {
- throw new ValidateException('上级用户不存在');
- }
- if ($spreadInfo['spread_uid'] == $uid) {
- throw new ValidateException('上级推广人不能为自己下级');
- }
- $data = ['spread_uid' => $spread_uid, 'spread_time' => time()];
- $this->dao->update($uid, $data);
- //记录推广绑定关系
- UserSpreadJob::dispatch([$uid, $spread_uid]);
- //记录好友关系
- UserFriendsJob::dispatch([$uid, $spread_uid]);
- return true;
- }
- /**
- * 增加推广用户佣金
- * @param int $uid
- * @param int $spread_uid
- * @param array $userInfo
- * @param array $spread_user
- * @return bool|mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function addBrokeragePrice(int $uid, int $spread_uid, array $userInfo = [], array $spread_user = [])
- {
- if (!$uid || !$spread_uid) {
- return false;
- }
- //商城分销功能是否开启 0关闭1开启
- if (!sys_config('brokerage_func_status')) return true;
- //获取设置推广佣金单价
- $brokerage_price = sys_config('uni_brokerage_price', 0);
- //推广佣金是否开启
- if (!sys_config('brokerage_user_status', 0)) {
- return true;
- }
- //获取推广佣金当日限额
- $day_brokerage_price_upper = sys_config('day_brokerage_price_upper', 0);
- if (!floatval($brokerage_price) || !floatval($day_brokerage_price_upper)) {
- return true;
- }
- if (!$userInfo) {
- $userInfo = $this->getUserInfo($uid);
- }
- if (!$userInfo) {
- return false;
- }
- if (!$spread_user) {
- $spread_user = $this->dao->getOne(['uid' => $spread_uid, 'status' => 1]);
- }
- if (!$spread_user) {
- return false;
- }
- if (!$this->checkUserPromoter($spread_uid, $spread_user)) {
- return false;
- }
- /** @var UserBrokerageServices $userBrokerageServices */
- $userBrokerageServices = app()->make(UserBrokerageServices::class);
- // -1不限制
- if ($day_brokerage_price_upper != -1) {
- if ($day_brokerage_price_upper <= 0) {
- return true;
- } else {
- //获取上级用户今日获取推广用户佣金
- $spread_day_brokerage = $userBrokerageServices->getUserBillBrokerageSum($spread_uid, ['brokerage_user'], 'today');
- //超过上限
- if (($spread_day_brokerage + $brokerage_price) > $day_brokerage_price_upper) {
- return true;
- }
- }
- }
- $spreadPrice = $spread_user['brokerage_price'];
- // 上级推广员返佣之后的金额
- $balance = bcadd($spreadPrice, $brokerage_price, 2);
- return $this->transaction(function () use ($uid, $spread_uid, $brokerage_price, $userInfo, $balance, $userBrokerageServices) {
- // 添加返佣记录
- $res1 = $userBrokerageServices->income('get_user_brokerage', $spread_uid, [
- 'nickname' => $userInfo['nickname'],
- 'number' => floatval($brokerage_price)
- ], $balance, $uid);
- // 添加用户余额
- $res2 = $this->dao->bcInc($spread_uid, 'brokerage_price', $brokerage_price, 'uid');
- //给上级发送获得佣金的模板消息
- /** @var StoreOrderTakeServices $storeOrderTakeServices */
- $storeOrderTakeServices = app()->make(StoreOrderTakeServices::class);
- $storeOrderTakeServices->sendBackOrderBrokerage([], $spread_uid, $brokerage_price, 'user');
- return $res1 && $res2;
- });
- }
- /**
- * 获取上级uid
- * @param int $uid
- * @param array $userInfo
- * @param bool $is_spread
- * @return int|mixed
- */
- public function getSpreadUid(int $uid, $userInfo = [], $is_spread = true)
- {
- if (!$uid) {
- return 0;
- }
- //商城分销功能是否开启 0关闭1开启
- if (!sys_config('brokerage_func_status')) return -1;
- if (!$userInfo) {
- $userInfo = $this->getUserCacheInfo($uid);
- }
- if (!$userInfo) {
- return 0;
- }
- //上级的上级不需要检测自购
- if ($is_spread) {
- //开启自购
- $is_self_brokerage = sys_config('is_self_brokerage', 0);
- if ($is_self_brokerage && $is_spread) {
- return $uid;
- }
- }
- //绑定类型
- $store_brokergae_binding_status = sys_config('store_brokerage_binding_status', 1);
- if ($store_brokergae_binding_status == 1 || $store_brokergae_binding_status == 3) {
- return $userInfo['spread_uid'];
- }
- //分销绑定类型为时间段且没过期
- $store_brokerage_binding_time = sys_config('store_brokerage_binding_time', 30);
- if ($store_brokergae_binding_status == 2 && ($userInfo['spread_time'] + $store_brokerage_binding_time * 24 * 3600) > time()) {
- return $userInfo['spread_uid'];
- }
- return -1;
- }
- /**
- * 用户付款code
- * @param int $uid
- * @return bool|mixed|null
- */
- public function getRandCode(int $uid)
- {
- $key = 'user_rand_code' . $uid;
- return CacheService::redisHandler()->remember($key, function () {
- return substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 9), 1))), 0, 3) . str_pad((string)mt_rand(1, 999), 3, '0', STR_PAD_LEFT);
- }, 600);
- }
- /**
- * 获取barcode
- * @return bool|int|mixed|null
- */
- public function getBarCode()
- {
- mt_srand();
- $code = substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 9), 1))), 0, 4) . str_pad((string)mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
- if (!$this->dao->count(['bar_code' => $code])) {
- return $code;
- } else {
- return $this->getBarCode();
- }
- }
- /**
- * 获取用户推广用户列表
- * @param $uid
- * @param $type
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function agentUserList($uid, $type)
- {
- $where['spread_uid'] = $uid;
- if ($type == 1) {
- $where['pay_count'] = -1;
- }
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getList($where, 'uid,nickname,avatar,FROM_UNIXTIME(spread_time, \'%Y.%m.%d %H:%m\') as spread_time', $page, $limit);
- $count = $this->dao->count($where);
- return compact('list', 'count');
- }
- /**
- * 个人中心
- * @param $uid
- * @return array|mixed
- */
- public function bankInfo($uid)
- {
- $userService = app()->make(UserServices::class);
- $user = $userService->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('数据不存在');
- }
- if (!$user['enterprise_professional_facilitator_id']) {
- return [];
- }
- return WithdrawService::init()::contractInfo($user['enterprise_professional_facilitator_id']);
- }
- public function cidUser($rname, $cid, $uid)
- {
- //找出出生年月日
- $time = substr($cid, 6, 4) . '-' . substr($cid, 10, 2) . '-' . substr($cid, 12, 2);
- $brithday = strtotime($time);
- $rust = $this->update($uid, ['real_name' => $rname, 'card_id' => $cid, 'is_auth' => 1, 'off' => '', 'birthday' => $brithday], 'uid');
- if ($rust)
- return true;
- else
- return false;
- }
- /**
- * TODO 获取推广人数 一级
- * @param int $uid
- * @return bool|int|string
- */
- public function getSpreadCount($uid = 0)
- {
- if (!$uid) return false;
- return $this->search()->where('spread_uid', $uid)->count();
- }
- /**
- * 修改当前用户的推广人数
- * @param $uid
- * @return bool
- */
- public function setUserSpreadCount($uid)
- {
- if (!$uid) return true;
- if ($this->getSpreadCount($uid) > 0) {
- $this->update($uid, ['spread_count' => 0]);
- } else {
- $this->update($uid, ['spread_count' => 0]);
- return true;
- }
- return $this->update($uid, ['spread_count' => self::getSpreadCount($uid)]);
- }
- public function cancelUser(int $uid)
- {
- try {
- $user = $this->get($uid);
- /** @var WechatUserServices $wechatUserServices */
- $wechatUserServices = app()->make(WechatUserServices::class);
- $this->update(['spread_uid' => $uid], ['spread_uid' => 0]);// 清除用户与下级的关系
- $this->update($uid, ['spread_uid' => 0, 'integral' => 0, 'now_money' => 0, 'brokerage_price' => 0, 'status' => 0]);//清除
- $this->bcDec($user['spread_uid'], 'spread_count', 1, 'uid');
- $this->destroy($uid);// 软删除用户
- $wechatUserServices->update(['uid' => $uid], ['unionid' => '', 'openid' => time() . rand(1000, 9999)]);// 删除微信用户
- $wechatUserServices->destroy(['uid' => $uid]);// 删除微信用户
- } catch (\Throwable $e) {
- Log::error('注销用户失败,失败原因:' . $e->getMessage());
- }
- }
- }
|