123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?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\services\system\config\SystemUserLevelServices;
- use app\services\system\config\SystemUserLevelTaskServices;
- use qiniu\basic\BaseServices;
- use app\model\user\UserLevel;
- use qiniu\exceptions\AdminException;
- use qiniu\services\SystemConfigService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- /**
- * 用户等级
- * Class UserLevelServices
- * @package app\services\user\level
- * @mixin UserLevel
- */
- class UserLevelServices extends BaseServices
- {
- /**
- * UserLevelServices constructor.
- * @param UserLevel $model
- */
- public function __construct(UserLevel $model)
- {
- $this->model = $model;
- }
- /**
- * 某些条件获取单个
- * @param array $where
- * @param string $field
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getWhereLevel(array $where, string $field = '*')
- {
- return $this->getOne($where, $field);
- }
- /**
- * 获取一些用户等级信息
- * @param array $uids
- * @return array
- */
- public function getUsersLevelInfo(array $uids)
- {
- return $this->getColumn([['uid', 'in', $uids]], 'level_id,is_forever,valid_time', 'uid');
- }
- /**
- * 清除会员等级
- * @param $uids
- * @return mixed
- */
- public function delUserLevel($uids)
- {
- if (is_array($uids)) {
- $re = $this->batchUpdate($uids, ['status' => 0, 'delete_time' => time()], 'uid');
- } else {
- $re = $this->update($uids, ['status' => 0, 'delete_time' => time()], 'uid');
- }
- if (!$re)
- throw new AdminException('修改会员信息失败');
- return true;
- }
- /**
- * 个人中心获取用户等级信息
- * @param int $uid
- * @param array $userInfo
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function homeGetUserLevel(int $uid, $userInfo = [])
- {
- $data = ['vip' => false, 'vip_id' => 0, 'vip_icon' => '', 'vip_name' => ''];
- //用户存在
- if ($uid) {
- if (!$userInfo) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userInfo = $userServices->getUserInfo($uid);
- }
- if ($userInfo) {
- $levelInfo = $this->getUserLevelInfo($uid);
- if (!$levelInfo) {//不存在等级 展示最低等级
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $allLevelInfo = $systemUserLevel->getList([['is_show', '=', 1]], 'id,name,icon,grade,task', 1, 1);
- $levelInfo = $allLevelInfo[0] ?? [];
- if ($levelInfo) {
- $levelInfo['level_id'] = 0;
- }
- }
- if ($levelInfo) {
- $data['vip'] = true;
- $data['vip_id'] = $levelInfo['level_id'];
- $data['vip_icon'] = $levelInfo['icon'];
- $data['vip_name'] = $levelInfo['name'];
- if ($levelInfo['id'] == 0) {
- $data['task'] = $levelInfo['levelInfo']['task'];
- }
- }
- }
- }
- return $data;
- }
- /**
- * 根据用户uid 获取会员详细信息
- * @param int $uid
- * @param string $field
- * @return array|int|mixed|string
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getUserLevelInfo(int $uid, string $field = '')
- {
- $userLevelInfo = $this->model->search(['valid' => 1, 'uid' => $uid])
- ->field($field)
- ->with(['levelInfo'])
- ->order('grade desc,add_time desc')
- ->find();
- $userLevelInfo['name'] = $userLevelInfo['levelInfo']['name'] ?? '';
- $userLevelInfo['icon'] = set_file_url($userLevelInfo['levelInfo']['icon'] ?? '');
- $userLevelInfo['image'] = set_file_url($userLevelInfo['levelInfo']['image'] ?? '');
- $userLevelInfo['task'] = set_file_url($userLevelInfo['levelInfo']['image'] ?? '');
- if ($field) return $userLevelInfo[$field] ?? '';
- return $userLevelInfo;
- }
- /**
- * 设置会员等级
- * @param int $uid 用户uid
- * @param int $level_id 等级id
- * @return UserLevel|bool|\think\Model
- * @throws DataNotFoundException
- * @throws ModelNotFoundException|DbException
- */
- public function setUserLevel(int $uid, int $level_id, $vipinfo = [])
- {
- /** @var SystemUserLevelServices $systemLevelServices */
- $systemLevelServices = app()->make(SystemUserLevelServices::class);
- if (!$vipinfo) {
- $vipinfo = $systemLevelServices->getLevel($level_id);
- if (!$vipinfo) {
- throw new AdminException('会员等级不存在');
- }
- }
- /** @var UserServices $user */
- $user = app()->make(UserServices::class);
- $userinfo = $user->getUserInfo($uid);
- $userVipInfo = $this->getWhereLevel(['uid' => $uid, 'level_id' => $level_id]);
- $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
- $data['add_time'] = time();
- if ($userVipInfo) {
- if ($vipinfo['valid_time'] > 0) $data['valid_time'] = ($userVipInfo['valid_time'] > time() ? $userVipInfo['valid_time'] : time()) + ($vipinfo['valid_time'] * 86400);
- $this->search(['uid' => $uid])->where('id', '<>', $userVipInfo['id'])->update(['status' => 0, 'delete_time' => time()]);
- if (!$this->update(['id' => $userVipInfo['id']], $data))
- throw new AdminException('修改会员信息失败');
- } else {
- $data = array_merge($data, [
- 'is_forever' => $vipinfo->is_forever,
- 'status' => 1,
- 'is_del' => 0,
- 'grade' => $vipinfo->grade,
- 'uid' => $uid,
- 'level_id' => $level_id,
- 'discount' => $vipinfo->discount,
- 'valid_time' => 0
- ]);
- if ($vipinfo['valid_time'] > 0) $data['valid_time'] = time() + ($vipinfo['valid_time'] * 86400);
- if (!$this->save($data)) throw new AdminException('写入会员信息失败');
- }
- if (!$user->update(['uid' => $uid], ['level' => $level_id]))
- throw new AdminException('修改用户会员等级失败');
- return true;
- }
- /**
- * 检测用户会员升级
- * @param int $uid
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function detection(int $uid, $pass = [])
- {
- //商城会员是否开启
- if (!sys_config('member_func_status')) {
- return true;
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('没有此用户,无法检测升级会员');
- }
- //没有激活暂不升级
- if (!$user['level_status']) {
- return true;
- }
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $userAllLevel = $systemUserLevel->getList([['is_show', '=', 1]]);
- if (!$userAllLevel) {
- return true;
- }
- foreach ($userAllLevel as $vipinfo) {
- /** @var SystemUserLevelTaskServices $taskService */
- $taskService = app()->make(SystemUserLevelTaskServices::class);
- $status = $taskService->checkLevelTask($vipinfo['id'], $uid, $user['clean_time']);
- if (!$status) break;
- if (sys_config('down_grade', 0)) {
- $this->delUserLevel($uid);
- $userServices->update(['uid' => $uid], ['level' => 0]);
- }
- $this->setUserLevel($uid, $vipinfo['id']);
- }
- $spread_uid = $userServices->where('uid', $uid)->value('spread_uid');
- if ($spread_uid && !in_array($spread_uid, $pass)) {
- $pass[] = $spread_uid;
- $this->detection($spread_uid, $pass);
- }
- return true;
- }
- /**
- * 会员等级列表
- * @param int $uid
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function grade(int $uid)
- {
- //商城会员是否开启
- if (!sys_config('member_func_status')) {
- return [];
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('没有此用户,无法检测升级会员');
- }
- $userLevelInfo = $this->getUserLevelInfo($uid);
- if (empty($userLevelInfo)) {
- $level_id = 0;
- } else {
- $level_id = $userLevelInfo['level_id'];
- }
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- return $systemUserLevel->getLevelListAndGrade($level_id);
- }
- }
|