// +---------------------------------------------------------------------- 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); } }