123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- <?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\level;
- use app\services\activity\coupon\StoreCouponIssueServices;
- use app\services\activity\coupon\StoreCouponUserServices;
- use app\services\BaseServices;
- use app\services\user\AwardIntegralServices;
- use app\services\user\UserServices;
- use app\services\user\UserBillServices;
- use app\services\user\UserSignServices;
- use app\dao\user\level\UserLevelDao;
- use crmeb\exceptions\AdminException;
- use crmeb\services\FormBuilder as Form;
- use crmeb\services\SystemConfigService;
- use FormBuilder\Factory\Iview;
- use think\exception\ValidateException;
- use think\facade\Route as Url;
- /**
- * 用户等级
- * Class UserLevelServices
- * @package app\services\user\level
- * @mixin UserLevelDao
- */
- class UserLevelServices extends BaseServices
- {
- /**
- * UserLevelServices constructor.
- * @param UserLevelDao $dao
- */
- public function __construct(UserLevelDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 某些条件获取单个
- * @param array $where
- * @param string $field
- * @return mixed
- */
- public function getWhereLevel(array $where, string $field = '*')
- {
- return $this->getOne($where, $field);
- }
- /**
- * 获取一些用户等级信息
- * @param array $uids
- * @param string $field
- * @param string $key
- * @return array
- */
- public function getUsersLevelInfo(array $uids)
- {
- return $this->dao->getColumn([['uid', 'in', $uids]], 'level_id,is_forever,valid_time', 'uid');
- }
- /**
- * 清除会员等级
- * @param $uids
- * @return \crmeb\basic\BaseModel|mixed
- */
- public function delUserLevel($uids)
- {
- $where = [];
- if (is_array($uids)) {
- $where[] = ['uid', 'IN', $uids];
- $re = $this->dao->batchUpdate($uids, ['is_del' => 1, 'status' => 0], 'uid');
- } else {
- $where[] = ['uid', '=', $uids];
- $re = $this->dao->update($uids, ['is_del' => 1, 'status' => 0], 'uid');
- }
- if (!$re)
- throw new AdminException('修改会员信息失败');
- $where[] = ['category', 'IN', ['exp']];
- /** @var UserBillServices $userbillServices */
- $userbillServices = app()->make(UserBillServices::class);
- $userbillServices->update($where, ['status' => -1]);
- return true;
- }
- /**
- * 个人中心获取用户等级信息
- * @param int $uid
- * @return array
- */
- public function homeGetUserLevel(int $uid, $userInfo = [])
- {
- $data = ['vip' => false, 'vip_id' => 0, 'vip_icon' => '', 'vip_name' => ''];
- //用户存在
- if ($uid && sys_config('member_func_status', 0)) {
- if (!$userInfo) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $userInfo = $userServices->getUserInfo($uid);
- }
- if ($userInfo) {
- $levelInfo = $this->getUerLevelInfoByUid($uid);
- if (!$levelInfo) {//不存在等级 展示最低等级
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $alllevelInfo = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1]], 'id,name,icon,grade', 1, 1);
- $levelInfo = $alllevelInfo[0] ?? [];
- if ($levelInfo) {
- $levelInfo['id'] = 0;
- }
- }
- if ($levelInfo) {
- $data['vip'] = true;
- $data['vip_id'] = $levelInfo['id'];
- $data['vip_icon'] = $levelInfo['icon'];
- $data['vip_name'] = $levelInfo['name'];
- }
- }
- }
- return $data;
- }
- /**
- * 根据用户uid 获取会员详细信息
- * @param int $uid
- * @param string $field
- */
- public function getUerLevelInfoByUid(int $uid, string $field = '')
- {
- $userLevelInfo = $this->dao->getUserLevel($uid);
- $data = [];
- if ($userLevelInfo) {
- $data = ['id' => $userLevelInfo['id'], 'level_id' => $userLevelInfo['level_id'], 'add_time' => $userLevelInfo['add_time']];
- $data['discount'] = $userLevelInfo['levelInfo']['discount'] ?? 0;
- $data['name'] = $userLevelInfo['levelInfo']['name'] ?? '';
- $data['money'] = $userLevelInfo['levelInfo']['money'] ?? 0;
- $data['icon'] = set_file_url($userLevelInfo['levelInfo']['icon'] ?? '');
- $data['image'] = set_file_url($userLevelInfo['levelInfo']['image'] ?? '');
- $data['is_pay'] = $userLevelInfo['levelInfo']['is_pay'] ?? 0;
- $data['grade'] = $userLevelInfo['levelInfo']['grade'] ?? 0;
- $data['exp_num'] = $userLevelInfo['levelInfo']['exp_num'] ?? 0;
- }
- if ($field) return $data[$field] ?? '';
- return $data;
- }
- /**
- * 设置会员等级
- * @param $uid 用户uid
- * @param $level_id 等级id
- * @return UserLevel|bool|\think\Model
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\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);
- //把之前等级作废
- $this->dao->update(['uid' => $uid], ['status' => 0, 'is_del' => 1]);
- //检查是否购买过
- $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) {
- $data['status'] = 1;
- $data['is_del'] = 0;
- if (!$this->dao->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,
- ]);
- $data['valid_time'] = 0;
- if (!$this->dao->save($data)) throw new AdminException('写入会员信息失败');
- }
- if (!$user->update(['uid' => $uid], ['level' => $level_id, 'exp' => $vipinfo['exp_num']]))
- throw new AdminException('修改用户会员等级失败');
- return true;
- }
- /**
- * 会员列表
- * @param $where
- * @return mixed
- */
- public function getSytemList($where)
- {
- /** @var SystemUserLevelServices $systemLevelServices */
- $systemLevelServices = app()->make(SystemUserLevelServices::class);
- return $systemLevelServices->getLevelList($where);
- }
- /**
- * 获取添加修改需要表单数据
- * @param int $id
- * @return array
- * @throws \FormBuilder\Exception\FormBuilderException
- */
- public function edit(int $id)
- {
- if ($id) {
- $vipinfo = app()->make(SystemUserLevelServices::class)->getlevel($id);
- if (!$vipinfo) {
- throw new AdminException('数据不存在');
- }
- $field[] = Form::hidden('id', $id);
- $msg = '编辑会员等级';
- } else {
- $msg = '添加会员等级';
- }
- $field[] = Form::input('name', '等级名称', $vipinfo['name'] ?? '')->col(24)->required('请填写等级名称');
- // $field[] = Form::number('valid_date', '有效时间(天)', $vipinfo['valid_date'] ?? 0)->min(0)->col(12);
- $field[] = Form::number('grade', '等级', $vipinfo['grade'] ?? 0)->min(0)->precision(0)->col(8);
- $field[] = Form::number('discount', '享受折扣', $vipinfo['discount'] ?? 100)->min(0)->max(100)->col(8)->placeholder('输入折扣数100,代表原价,90代表9折');
- $field[] = Form::number('group_award', '团队奖(%)', $vipinfo['group_award'] ?? 100)->min(0)->max(100)->step(0.01)->col(8)->placeholder('输入团队奖百分比');
- // $field[] = Form::number('exp_num', '解锁需经验值达到', $vipinfo['exp_num'] ?? 0)->min(0)->precision(0)->col(8);
- $field[] = Form::number('achievement', '解锁需业绩达到', $vipinfo['achievement'] ?? 0)->min(0)->step(0.01)->col(8);
- $pre_level = false;
- if (isset($vipinfo)) {
- $pre_level = app()->make(SystemUserLevelServices::class)->getPreLevel($vipinfo['grade']);
- }
- $field[] = Form::number('sub_grade_num', $pre_level ? '解锁需存在' . $pre_level['name'] . '以上大区' : '解锁需存在低级别以上大区', $vipinfo['sub_grade_num'] ?? 0)->min(0)->precision(0)->col(8);
- $field[] = Form::frameImage('icon', '图标', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'icon')), $vipinfo['icon'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择图标'));
- $field[] = Form::frameImage('image', '会员背景', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')), $vipinfo['image'] ?? '')->icon('ios-add')->width('960px')->height('505px')->modal(['footer-hide' => true])->appendValidate(Iview::validateStr()->required()->message('请选择背景'));
- $field[] = Form::radio('is_show', '是否显示', $vipinfo['is_show'] ?? 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
- $field[] = Form::textarea('explain', '等级说明', $vipinfo['explain'] ?? '');
- return create_form($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
- }
- /*
- * 会员等级添加或者修改
- * @param $id 修改的等级id
- * @return json
- * */
- public function save(int $id, array $data)
- {
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $levelOne = $systemUserLevel->getWhereLevel(['is_del' => 0, 'grade' => $data['grade']]);
- $levelThree = $systemUserLevel->getWhereLevel(['is_del' => 0, 'name' => $data['name']]);
- $levelPre = $systemUserLevel->getPreLevel($data['grade']);
- $levelNext = $systemUserLevel->getNextLevel($data['grade']);
- if ($levelPre && $data['achievement'] < $levelPre['achievement']) {
- throw new AdminException('会员业绩必须大于等于上一等级设置的业绩');
- }
- if ($levelNext && $data['achievement'] > $levelNext['achievement']) {
- throw new AdminException('会员业绩必须小于等于下一等级设置的业绩');
- }
- //修改
- if ($id) {
- if (($levelOne && $levelOne['id'] != $id) || ($levelThree && $levelThree['id'] != $id)) {
- throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
- }
- if (!$systemUserLevel->update($id, $data)) {
- throw new AdminException('修改失败');
- }
- $data['id'] = $id;
- $systemUserLevel->dao->cacheUpdate($data);
- return '修改成功';
- } else {
- if ($levelOne || $levelThree) {
- throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
- }
- //新增
- $data['add_time'] = time();
- $res = $systemUserLevel->save($data);
- if (!$res) {
- throw new AdminException('添加失败');
- }
- $data['id'] = $res->id;
- $systemUserLevel->cacheUpdate($data);
- return '添加成功';
- }
- }
- /**
- * 假删除
- * @param int $id
- * @return mixed
- */
- public function delLevel(int $id)
- {
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $level = $systemUserLevel->getWhereLevel(['id' => $id]);
- if ($level && $level['is_del'] != 1) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if ($userServices->count(['level' => $level['id']])) {
- throw new AdminException('存在用户已是该等级,无法删除');
- }
- if (!$systemUserLevel->update($id, ['is_del' => 1]))
- throw new AdminException('删除失败');
- if (!$this->dao->update(['level_id' => $id], ['is_del' => 1])) {
- throw new AdminException('删除失败');
- }
- }
- $systemUserLevel->cacheDelById($id);
- return '删除成功';
- }
- /**
- * 设置是否显示
- * @param int $id
- * @param $is_show
- * @return mixed
- */
- public function setShow(int $id, int $is_show)
- {
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- if (!$systemUserLevel->getWhereLevel(['id' => $id]))
- throw new AdminException('数据不存在');
- if ($systemUserLevel->update($id, ['is_show' => $is_show])) {
- $systemUserLevel->cacheSaveValue($id, 'is_show', $is_show);
- return $is_show == 1 ? '显示成功' : '隐藏成功';
- } else {
- throw new AdminException($is_show == 1 ? '显示失败' : '隐藏失败');
- }
- }
- /**
- * 快速修改
- * @param int $id
- * @param $is_show
- * @return mixed
- */
- public function setValue(int $id, array $data)
- {
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- if (!$systemUserLevel->getWhereLevel(['id' => $id]))
- throw new AdminException('数据不存在');
- if ($systemUserLevel->update($id, [$data['field'] => $data['value']])) {
- $systemUserLevel->cacheSaveValue($id, $data['field'], $data['value']);
- return true;
- } else {
- throw new AdminException('保存失败');
- }
- }
- /**
- * 检测用户会员升级
- * @param $uid
- * @return bool
- */
- public function detection(int $uid)
- {
- //商城会员是否开启
- 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);
- $achievement = app()->make(AwardIntegralServices::class)->getAchievement($uid);
- $userAllLevel = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']], ['achievement', '<=', (float)$achievement]]);
- if (!$userAllLevel) {
- return true;
- }
- $data = [];
- $data['add_time'] = time();
- $userLevel = $this->dao->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
- $max_level = 0;
- foreach ($userAllLevel as $vipinfo) {
- if (in_array($vipinfo['id'], $userLevel)) {
- continue;
- }
- $num = 0;
- $sub_user = $userServices->getColumn(['spread_uid' => $user['uid']], 'uid');
- if (!count($sub_user)) {
- $num = 0;
- } else {
- if ($vipinfo['grade'] == 1) {
- $num = count($sub_user);
- } else {
- foreach ($sub_user as $v) {
- $subUsers = $this->dao->getColumn([['grade', '>=', $vipinfo['grade'] - 1], ['status', '=', 1], ['is_del', '=', 0], ['uid', 'in', array_merge([$v], get_group_user($v))]], 'uid');
- if (count(array_unique($subUsers)) > 0) {
- $num++;
- }
- }
- }
- }
- if ($vipinfo['sub_grade_num'] > $num) {
- break;
- }
- $max_level = $vipinfo['id'];
- $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
- $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
- if ($uservip) {
- //降级在升级情况
- $data['status'] = 1;
- $data['is_del'] = 0;
- if (!$this->dao->update($uservip['id'], $data, 'id')) {
- throw new ValidateException('检测升级失败');
- }
- } else {
- $data = array_merge($data, [
- 'is_forever' => $vipinfo['is_forever'],
- 'status' => 1,
- 'is_del' => 0,
- 'grade' => $vipinfo['grade'],
- 'uid' => $uid,
- 'level_id' => $vipinfo['id'],
- 'discount' => $vipinfo['discount'],
- ]);
- if (!$this->dao->save($data)) {
- throw new ValidateException('检测升级失败');
- }
- }
- $data['add_time'] += 1;
- }
- $res = true;
- if ($user['level'] < $max_level) {
- $res = $userServices->update($uid, ['level' => $max_level], 'uid');
- }
- if (!$res) {
- throw new ValidateException('检测升级失败');
- }
- return true;
- }
- /**
- * 会员等级列表
- * @param int $uid
- */
- 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->getUerLevelInfoByUid($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);
- }
- /**
- * 获取会员信息
- * @param int $uid
- * @return array[]
- */
- public function getUserLevelInfo(int $uid)
- {
- $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
- //商城会员是否开启
- if (!sys_config('member_func_status')) {
- return $data;
- }
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('没有此会员');
- }
- /** @var StoreCouponUserServices $storeCoupon */
- $storeCoupon = app()->make(StoreCouponUserServices::class);
- $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
- try {
- //检测升级
- $this->detection($uid);
- } catch (\Throwable $e) {
- }
- $data['user'] = $user;
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
- $i = 0;
- foreach ($levelList as &$level) {
- $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
- $level['image'] = set_file_url($level['image']);
- $level['icon'] = set_file_url($level['icon']);
- $i++;
- }
- $data['level_list'] = $levelList;
- $levelInfo = $this->getUerLevelInfoByUid($uid);
- if (!$levelInfo) {//不存在等级 展示最低等级
- /** @var SystemUserLevelServices $systemUserLevel */
- $systemUserLevel = app()->make(SystemUserLevelServices::class);
- $alllevelInfo = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1]], 'id,name,icon,grade', 1, 1);
- $levelInfo = $alllevelInfo[0] ?? [];
- if ($levelInfo) {
- $levelInfo['id'] = 0;
- }
- }
- if ($levelInfo) {
- $levelInfo['vip'] = true;
- $levelInfo['vip_id'] = $levelInfo['id'];
- $levelInfo['vip_icon'] = $levelInfo['icon'];
- $levelInfo['vip_name'] = $levelInfo['name'];
- }
- $data['level_info'] = $levelInfo;
- $i = 0;
- foreach ($levelList as &$level) {
- if ($level['grade'] < $levelInfo['grade']) {
- $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
- } else {
- $level['next_exp_num'] = $level['exp_num'];
- }
- $level['image'] = set_file_url($level['image']);
- $level['icon'] = set_file_url($level['icon']);
- $i++;
- }
- $data['level_list'] = $levelList;
- $data['level_info']['exp'] = $user['exp'] ?? 0;
- /** @var UserBillServices $userBillservices */
- $userBillservices = app()->make(UserBillServices::class);
- $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
- $task = [];
- /** @var UserSignServices $userSignServices */
- $userSignServices = app()->make(UserSignServices::class);
- $task['sign_count'] = $userSignServices->getSignSumDay($uid);
- $config = SystemConfigService::more(['sign_give_exp', 'order_give_exp', 'invite_user_exp']);
- $task['sign'] = $config['sign_give_exp'] ?? 0;
- $task['order'] = $config['order_give_exp'] ?? 0;
- $task['invite'] = $config['invite_user_exp'] ?? 0;
- $data['task'] = $task;
- return $data;
- }
- /**
- * 经验列表
- * @param int $uid
- * @return array
- */
- public function expList(int $uid)
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('没有此用户');
- }
- /** @var UserBillServices $userBill */
- $userBill = app()->make(UserBillServices::class);
- $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
- $list = $data['list'] ?? [];
- return $list;
- }
- /**
- * 获取激活会员卡需要的信息
- * @return mixed
- */
- public function getActivateInfo()
- {
- //商城会员是否开启
- if (!sys_config('member_func_status')) {
- throw new ValidateException('会员卡功能暂未开启');
- }
- //是否需要激活
- if (!sys_config('level_activate_status')) {
- throw new ValidateException('会员卡功能暂不需要激活');
- }
- return SystemConfigService::get('level_extend_info');
- }
- /**
- * 激活会员卡
- * @param int $uid
- * @param array $data
- * @return array
- */
- public function userActivatelevel(int $uid, array $data)
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $user = $userServices->getUserInfo($uid);
- if (!$user) {
- throw new ValidateException('用户已注销,或不存在');
- }
- if ($user['level_status']) {
- throw new ValidateException('不需要重复激活');
- }
- $extend_info = $userServices->handelExtendInfo($data, true) ?: [];
- $update = ['level_status' => 1];
- if ($extend_info) {
- $default = $userServices->defaultExtendInfo;
- $params = array_column($default, 'param');
- $sex = $userServices->sex;
- $update['level_extend_info'] = $extend_info;
- foreach ($extend_info as $info) {
- if (isset($info['param']) && in_array($info['param'], $params) && isset($info['value'])) {
- if ($info['param'] == 'sex') {
- $update['sex'] = $sex[$info['value']] ?? 0;
- } elseif ($info['param'] == 'birthday') {
- $update['birthday'] = strtotime($info['value']);
- } else {
- $update[$info['param']] = $info['value'];
- }
- }
- }
- }
- $userServices->update($uid, $update);
- $data = [];
- //获取激活送好礼
- $data = SystemConfigService::more([
- 'level_integral_status',
- 'level_give_integral',
- 'level_money_status',
- 'level_give_money',
- 'level_coupon_status',
- 'level_give_coupon',
- ]);
- $ids = $data['level_give_coupon'] ?? [];
- $data['level_give_coupon'] = [];
- if ($data['level_coupon_status'] && $ids) {
- /** @var StoreCouponIssueServices $couponServices */
- $couponServices = app()->make(StoreCouponIssueServices::class);
- $coupon = $couponServices->getList(['id' => $ids]);
- $data['level_give_coupon'] = $coupon;
- }
- if (!$data['level_integral_status']) {
- $data['level_give_integral'] = 0;
- }
- if (!$data['level_money_status']) {
- $data['level_give_money'] = 0;
- }
- //激活会员卡事件
- event('user.activate.level', [$uid]);
- return $data;
- }
- }
|