UserLevelServices.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user\level;
  13. use app\services\activity\coupon\StoreCouponIssueServices;
  14. use app\services\activity\coupon\StoreCouponUserServices;
  15. use app\services\BaseServices;
  16. use app\services\user\AwardIntegralServices;
  17. use app\services\user\UserServices;
  18. use app\services\user\UserBillServices;
  19. use app\services\user\UserSignServices;
  20. use app\dao\user\level\UserLevelDao;
  21. use crmeb\exceptions\AdminException;
  22. use crmeb\services\FormBuilder as Form;
  23. use crmeb\services\SystemConfigService;
  24. use FormBuilder\Factory\Iview;
  25. use think\exception\ValidateException;
  26. use think\facade\Route as Url;
  27. /**
  28. * 用户等级
  29. * Class UserLevelServices
  30. * @package app\services\user\level
  31. * @mixin UserLevelDao
  32. */
  33. class UserLevelServices extends BaseServices
  34. {
  35. /**
  36. * UserLevelServices constructor.
  37. * @param UserLevelDao $dao
  38. */
  39. public function __construct(UserLevelDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 某些条件获取单个
  45. * @param array $where
  46. * @param string $field
  47. * @return mixed
  48. */
  49. public function getWhereLevel(array $where, string $field = '*')
  50. {
  51. return $this->getOne($where, $field);
  52. }
  53. /**
  54. * 获取一些用户等级信息
  55. * @param array $uids
  56. * @param string $field
  57. * @param string $key
  58. * @return array
  59. */
  60. public function getUsersLevelInfo(array $uids)
  61. {
  62. return $this->dao->getColumn([['uid', 'in', $uids]], 'level_id,is_forever,valid_time', 'uid');
  63. }
  64. /**
  65. * 清除会员等级
  66. * @param $uids
  67. * @return \crmeb\basic\BaseModel|mixed
  68. */
  69. public function delUserLevel($uids)
  70. {
  71. $where = [];
  72. if (is_array($uids)) {
  73. $where[] = ['uid', 'IN', $uids];
  74. $re = $this->dao->batchUpdate($uids, ['is_del' => 1, 'status' => 0], 'uid');
  75. } else {
  76. $where[] = ['uid', '=', $uids];
  77. $re = $this->dao->update($uids, ['is_del' => 1, 'status' => 0], 'uid');
  78. }
  79. if (!$re)
  80. throw new AdminException('修改会员信息失败');
  81. $where[] = ['category', 'IN', ['exp']];
  82. /** @var UserBillServices $userbillServices */
  83. $userbillServices = app()->make(UserBillServices::class);
  84. $userbillServices->update($where, ['status' => -1]);
  85. return true;
  86. }
  87. /**
  88. * 个人中心获取用户等级信息
  89. * @param int $uid
  90. * @return array
  91. */
  92. public function homeGetUserLevel(int $uid, $userInfo = [])
  93. {
  94. $data = ['vip' => false, 'vip_id' => 0, 'vip_icon' => '', 'vip_name' => ''];
  95. //用户存在
  96. if ($uid && sys_config('member_func_status', 0)) {
  97. if (!$userInfo) {
  98. /** @var UserServices $userServices */
  99. $userServices = app()->make(UserServices::class);
  100. $userInfo = $userServices->getUserInfo($uid);
  101. }
  102. if ($userInfo) {
  103. $levelInfo = $this->getUerLevelInfoByUid($uid);
  104. if (!$levelInfo) {//不存在等级 展示最低等级
  105. /** @var SystemUserLevelServices $systemUserLevel */
  106. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  107. $alllevelInfo = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1]], 'id,name,icon,grade', 1, 1);
  108. $levelInfo = $alllevelInfo[0] ?? [];
  109. if ($levelInfo) {
  110. $levelInfo['id'] = 0;
  111. }
  112. }
  113. if ($levelInfo) {
  114. $data['vip'] = true;
  115. $data['vip_id'] = $levelInfo['id'];
  116. $data['vip_icon'] = $levelInfo['icon'];
  117. $data['vip_name'] = $levelInfo['name'];
  118. }
  119. }
  120. }
  121. return $data;
  122. }
  123. /**
  124. * 根据用户uid 获取会员详细信息
  125. * @param int $uid
  126. * @param string $field
  127. */
  128. public function getUerLevelInfoByUid(int $uid, string $field = '')
  129. {
  130. $userLevelInfo = $this->dao->getUserLevel($uid);
  131. $data = [];
  132. if ($userLevelInfo) {
  133. $data = ['id' => $userLevelInfo['id'], 'level_id' => $userLevelInfo['level_id'], 'add_time' => $userLevelInfo['add_time']];
  134. $data['discount'] = $userLevelInfo['levelInfo']['discount'] ?? 0;
  135. $data['name'] = $userLevelInfo['levelInfo']['name'] ?? '';
  136. $data['money'] = $userLevelInfo['levelInfo']['money'] ?? 0;
  137. $data['icon'] = set_file_url($userLevelInfo['levelInfo']['icon'] ?? '');
  138. $data['image'] = set_file_url($userLevelInfo['levelInfo']['image'] ?? '');
  139. $data['is_pay'] = $userLevelInfo['levelInfo']['is_pay'] ?? 0;
  140. $data['grade'] = $userLevelInfo['levelInfo']['grade'] ?? 0;
  141. $data['exp_num'] = $userLevelInfo['levelInfo']['exp_num'] ?? 0;
  142. }
  143. if ($field) return $data[$field] ?? '';
  144. return $data;
  145. }
  146. /**
  147. * 设置会员等级
  148. * @param $uid 用户uid
  149. * @param $level_id 等级id
  150. * @return UserLevel|bool|\think\Model
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. * @throws \think\exception\DbException
  154. */
  155. public function setUserLevel(int $uid, int $level_id, $vipinfo = [])
  156. {
  157. /** @var SystemUserLevelServices $systemLevelServices */
  158. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  159. if (!$vipinfo) {
  160. $vipinfo = $systemLevelServices->getLevel($level_id);
  161. if (!$vipinfo) {
  162. throw new AdminException('会员等级不存在');
  163. }
  164. }
  165. /** @var UserServices $user */
  166. $user = app()->make(UserServices::class);
  167. $userinfo = $user->getUserInfo($uid);
  168. //把之前等级作废
  169. $this->dao->update(['uid' => $uid], ['status' => 0, 'is_del' => 1]);
  170. //检查是否购买过
  171. $uservipinfo = $this->getWhereLevel(['uid' => $uid, 'level_id' => $level_id]);
  172. $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  173. $data['add_time'] = time();
  174. if ($uservipinfo) {
  175. $data['status'] = 1;
  176. $data['is_del'] = 0;
  177. if (!$this->dao->update(['id' => $uservipinfo['id']], $data))
  178. throw new AdminException('修改会员信息失败');
  179. } else {
  180. $data = array_merge($data, [
  181. 'is_forever' => $vipinfo->is_forever,
  182. 'status' => 1,
  183. 'is_del' => 0,
  184. 'grade' => $vipinfo->grade,
  185. 'uid' => $uid,
  186. 'level_id' => $level_id,
  187. 'discount' => $vipinfo->discount,
  188. ]);
  189. $data['valid_time'] = 0;
  190. if (!$this->dao->save($data)) throw new AdminException('写入会员信息失败');
  191. }
  192. if (!$user->update(['uid' => $uid], ['level' => $level_id, 'exp' => $vipinfo['exp_num']]))
  193. throw new AdminException('修改用户会员等级失败');
  194. return true;
  195. }
  196. /**
  197. * 会员列表
  198. * @param $where
  199. * @return mixed
  200. */
  201. public function getSytemList($where)
  202. {
  203. /** @var SystemUserLevelServices $systemLevelServices */
  204. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  205. return $systemLevelServices->getLevelList($where);
  206. }
  207. /**
  208. * 获取添加修改需要表单数据
  209. * @param int $id
  210. * @return array
  211. * @throws \FormBuilder\Exception\FormBuilderException
  212. */
  213. public function edit(int $id)
  214. {
  215. if ($id) {
  216. $vipinfo = app()->make(SystemUserLevelServices::class)->getlevel($id);
  217. if (!$vipinfo) {
  218. throw new AdminException('数据不存在');
  219. }
  220. $field[] = Form::hidden('id', $id);
  221. $msg = '编辑会员等级';
  222. } else {
  223. $msg = '添加会员等级';
  224. }
  225. $field[] = Form::input('name', '等级名称', $vipinfo['name'] ?? '')->col(24)->required('请填写等级名称');
  226. // $field[] = Form::number('valid_date', '有效时间(天)', $vipinfo['valid_date'] ?? 0)->min(0)->col(12);
  227. $field[] = Form::number('grade', '等级', $vipinfo['grade'] ?? 0)->min(0)->precision(0)->col(8);
  228. $field[] = Form::number('discount', '享受折扣', $vipinfo['discount'] ?? 100)->min(0)->max(100)->col(8)->placeholder('输入折扣数100,代表原价,90代表9折');
  229. $field[] = Form::number('group_award', '团队奖(%)', $vipinfo['group_award'] ?? 100)->min(0)->max(100)->step(0.01)->col(8)->placeholder('输入团队奖百分比');
  230. // $field[] = Form::number('exp_num', '解锁需经验值达到', $vipinfo['exp_num'] ?? 0)->min(0)->precision(0)->col(8);
  231. $field[] = Form::number('achievement', '解锁需业绩达到', $vipinfo['achievement'] ?? 0)->min(0)->step(0.01)->col(8);
  232. $pre_level = false;
  233. if (isset($vipinfo)) {
  234. $pre_level = app()->make(SystemUserLevelServices::class)->getPreLevel($vipinfo['grade']);
  235. }
  236. $field[] = Form::number('sub_grade_num', $pre_level ? '解锁需存在' . $pre_level['name'] . '以上大区' : '解锁需存在低级别以上大区', $vipinfo['sub_grade_num'] ?? 0)->min(0)->precision(0)->col(8);
  237. $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('请选择图标'));
  238. $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('请选择背景'));
  239. $field[] = Form::radio('is_show', '是否显示', $vipinfo['is_show'] ?? 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  240. $field[] = Form::textarea('explain', '等级说明', $vipinfo['explain'] ?? '');
  241. return create_form($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
  242. }
  243. /*
  244. * 会员等级添加或者修改
  245. * @param $id 修改的等级id
  246. * @return json
  247. * */
  248. public function save(int $id, array $data)
  249. {
  250. /** @var SystemUserLevelServices $systemUserLevel */
  251. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  252. $levelOne = $systemUserLevel->getWhereLevel(['is_del' => 0, 'grade' => $data['grade']]);
  253. $levelThree = $systemUserLevel->getWhereLevel(['is_del' => 0, 'name' => $data['name']]);
  254. $levelPre = $systemUserLevel->getPreLevel($data['grade']);
  255. $levelNext = $systemUserLevel->getNextLevel($data['grade']);
  256. if ($levelPre && $data['achievement'] < $levelPre['achievement']) {
  257. throw new AdminException('会员业绩必须大于等于上一等级设置的业绩');
  258. }
  259. if ($levelNext && $data['achievement'] > $levelNext['achievement']) {
  260. throw new AdminException('会员业绩必须小于等于下一等级设置的业绩');
  261. }
  262. //修改
  263. if ($id) {
  264. if (($levelOne && $levelOne['id'] != $id) || ($levelThree && $levelThree['id'] != $id)) {
  265. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  266. }
  267. if (!$systemUserLevel->update($id, $data)) {
  268. throw new AdminException('修改失败');
  269. }
  270. $data['id'] = $id;
  271. $systemUserLevel->dao->cacheUpdate($data);
  272. return '修改成功';
  273. } else {
  274. if ($levelOne || $levelThree) {
  275. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  276. }
  277. //新增
  278. $data['add_time'] = time();
  279. $res = $systemUserLevel->save($data);
  280. if (!$res) {
  281. throw new AdminException('添加失败');
  282. }
  283. $data['id'] = $res->id;
  284. $systemUserLevel->cacheUpdate($data);
  285. return '添加成功';
  286. }
  287. }
  288. /**
  289. * 假删除
  290. * @param int $id
  291. * @return mixed
  292. */
  293. public function delLevel(int $id)
  294. {
  295. /** @var SystemUserLevelServices $systemUserLevel */
  296. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  297. $level = $systemUserLevel->getWhereLevel(['id' => $id]);
  298. if ($level && $level['is_del'] != 1) {
  299. /** @var UserServices $userServices */
  300. $userServices = app()->make(UserServices::class);
  301. if ($userServices->count(['level' => $level['id']])) {
  302. throw new AdminException('存在用户已是该等级,无法删除');
  303. }
  304. if (!$systemUserLevel->update($id, ['is_del' => 1]))
  305. throw new AdminException('删除失败');
  306. if (!$this->dao->update(['level_id' => $id], ['is_del' => 1])) {
  307. throw new AdminException('删除失败');
  308. }
  309. }
  310. $systemUserLevel->cacheDelById($id);
  311. return '删除成功';
  312. }
  313. /**
  314. * 设置是否显示
  315. * @param int $id
  316. * @param $is_show
  317. * @return mixed
  318. */
  319. public function setShow(int $id, int $is_show)
  320. {
  321. /** @var SystemUserLevelServices $systemUserLevel */
  322. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  323. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  324. throw new AdminException('数据不存在');
  325. if ($systemUserLevel->update($id, ['is_show' => $is_show])) {
  326. $systemUserLevel->cacheSaveValue($id, 'is_show', $is_show);
  327. return $is_show == 1 ? '显示成功' : '隐藏成功';
  328. } else {
  329. throw new AdminException($is_show == 1 ? '显示失败' : '隐藏失败');
  330. }
  331. }
  332. /**
  333. * 快速修改
  334. * @param int $id
  335. * @param $is_show
  336. * @return mixed
  337. */
  338. public function setValue(int $id, array $data)
  339. {
  340. /** @var SystemUserLevelServices $systemUserLevel */
  341. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  342. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  343. throw new AdminException('数据不存在');
  344. if ($systemUserLevel->update($id, [$data['field'] => $data['value']])) {
  345. $systemUserLevel->cacheSaveValue($id, $data['field'], $data['value']);
  346. return true;
  347. } else {
  348. throw new AdminException('保存失败');
  349. }
  350. }
  351. /**
  352. * 检测用户会员升级
  353. * @param $uid
  354. * @return bool
  355. */
  356. public function detection(int $uid)
  357. {
  358. //商城会员是否开启
  359. if (!sys_config('member_func_status')) {
  360. return true;
  361. }
  362. /** @var UserServices $userServices */
  363. $userServices = app()->make(UserServices::class);
  364. $user = $userServices->getUserInfo($uid);
  365. if (!$user) {
  366. throw new ValidateException('没有此用户,无法检测升级会员');
  367. }
  368. //没有激活暂不升级
  369. if (!$user['level_status']) {
  370. return true;
  371. }
  372. /** @var SystemUserLevelServices $systemUserLevel */
  373. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  374. $achievement = app()->make(AwardIntegralServices::class)->getAchievement($uid);
  375. $userAllLevel = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']], ['achievement', '<=', (float)$achievement]]);
  376. if (!$userAllLevel) {
  377. return true;
  378. }
  379. $data = [];
  380. $data['add_time'] = time();
  381. $userLevel = $this->dao->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
  382. $max_level = 0;
  383. foreach ($userAllLevel as $vipinfo) {
  384. if (in_array($vipinfo['id'], $userLevel)) {
  385. continue;
  386. }
  387. $num = 0;
  388. $sub_user = $userServices->getColumn(['spread_uid' => $user['uid']], 'uid');
  389. if (!count($sub_user)) {
  390. $num = 0;
  391. } else {
  392. if ($vipinfo['grade'] == 1) {
  393. $num = count($sub_user);
  394. } else {
  395. foreach ($sub_user as $v) {
  396. $subUsers = $this->dao->getColumn([['grade', '>=', $vipinfo['grade'] - 1], ['status', '=', 1], ['is_del', '=', 0], ['uid', 'in', array_merge([$v], get_group_user($v))]], 'uid');
  397. if (count(array_unique($subUsers)) > 0) {
  398. $num++;
  399. }
  400. }
  401. }
  402. }
  403. if ($vipinfo['sub_grade_num'] > $num) {
  404. break;
  405. }
  406. $max_level = $vipinfo['id'];
  407. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  408. $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  409. if ($uservip) {
  410. //降级在升级情况
  411. $data['status'] = 1;
  412. $data['is_del'] = 0;
  413. if (!$this->dao->update($uservip['id'], $data, 'id')) {
  414. throw new ValidateException('检测升级失败');
  415. }
  416. } else {
  417. $data = array_merge($data, [
  418. 'is_forever' => $vipinfo['is_forever'],
  419. 'status' => 1,
  420. 'is_del' => 0,
  421. 'grade' => $vipinfo['grade'],
  422. 'uid' => $uid,
  423. 'level_id' => $vipinfo['id'],
  424. 'discount' => $vipinfo['discount'],
  425. ]);
  426. if (!$this->dao->save($data)) {
  427. throw new ValidateException('检测升级失败');
  428. }
  429. }
  430. $data['add_time'] += 1;
  431. }
  432. $res = true;
  433. if ($user['level'] < $max_level) {
  434. $res = $userServices->update($uid, ['level' => $max_level], 'uid');
  435. }
  436. if (!$res) {
  437. throw new ValidateException('检测升级失败');
  438. }
  439. return true;
  440. }
  441. /**
  442. * 会员等级列表
  443. * @param int $uid
  444. */
  445. public function grade(int $uid)
  446. {
  447. //商城会员是否开启
  448. if (!sys_config('member_func_status')) {
  449. return [];
  450. }
  451. /** @var UserServices $userServices */
  452. $userServices = app()->make(UserServices::class);
  453. $user = $userServices->getUserInfo($uid);
  454. if (!$user) {
  455. throw new ValidateException('没有此用户,无法检测升级会员');
  456. }
  457. $userLevelInfo = $this->getUerLevelInfoByUid($uid);
  458. if (empty($userLevelInfo)) {
  459. $level_id = 0;
  460. } else {
  461. $level_id = $userLevelInfo['level_id'];
  462. }
  463. /** @var SystemUserLevelServices $systemUserLevel */
  464. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  465. return $systemUserLevel->getLevelListAndGrade($level_id);
  466. }
  467. /**
  468. * 获取会员信息
  469. * @param int $uid
  470. * @return array[]
  471. */
  472. public function getUserLevelInfo(int $uid)
  473. {
  474. $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
  475. //商城会员是否开启
  476. if (!sys_config('member_func_status')) {
  477. return $data;
  478. }
  479. /** @var UserServices $userServices */
  480. $userServices = app()->make(UserServices::class);
  481. $user = $userServices->getUserInfo($uid);
  482. if (!$user) {
  483. throw new ValidateException('没有此会员');
  484. }
  485. /** @var StoreCouponUserServices $storeCoupon */
  486. $storeCoupon = app()->make(StoreCouponUserServices::class);
  487. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  488. try {
  489. //检测升级
  490. $this->detection($uid);
  491. } catch (\Throwable $e) {
  492. }
  493. $data['user'] = $user;
  494. /** @var SystemUserLevelServices $systemUserLevel */
  495. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  496. $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
  497. $i = 0;
  498. foreach ($levelList as &$level) {
  499. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  500. $level['image'] = set_file_url($level['image']);
  501. $level['icon'] = set_file_url($level['icon']);
  502. $i++;
  503. }
  504. $data['level_list'] = $levelList;
  505. $levelInfo = $this->getUerLevelInfoByUid($uid);
  506. if (!$levelInfo) {//不存在等级 展示最低等级
  507. /** @var SystemUserLevelServices $systemUserLevel */
  508. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  509. $alllevelInfo = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1]], 'id,name,icon,grade', 1, 1);
  510. $levelInfo = $alllevelInfo[0] ?? [];
  511. if ($levelInfo) {
  512. $levelInfo['id'] = 0;
  513. }
  514. }
  515. if ($levelInfo) {
  516. $levelInfo['vip'] = true;
  517. $levelInfo['vip_id'] = $levelInfo['id'];
  518. $levelInfo['vip_icon'] = $levelInfo['icon'];
  519. $levelInfo['vip_name'] = $levelInfo['name'];
  520. }
  521. $data['level_info'] = $levelInfo;
  522. $i = 0;
  523. foreach ($levelList as &$level) {
  524. if ($level['grade'] < $levelInfo['grade']) {
  525. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  526. } else {
  527. $level['next_exp_num'] = $level['exp_num'];
  528. }
  529. $level['image'] = set_file_url($level['image']);
  530. $level['icon'] = set_file_url($level['icon']);
  531. $i++;
  532. }
  533. $data['level_list'] = $levelList;
  534. $data['level_info']['exp'] = $user['exp'] ?? 0;
  535. /** @var UserBillServices $userBillservices */
  536. $userBillservices = app()->make(UserBillServices::class);
  537. $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
  538. $task = [];
  539. /** @var UserSignServices $userSignServices */
  540. $userSignServices = app()->make(UserSignServices::class);
  541. $task['sign_count'] = $userSignServices->getSignSumDay($uid);
  542. $config = SystemConfigService::more(['sign_give_exp', 'order_give_exp', 'invite_user_exp']);
  543. $task['sign'] = $config['sign_give_exp'] ?? 0;
  544. $task['order'] = $config['order_give_exp'] ?? 0;
  545. $task['invite'] = $config['invite_user_exp'] ?? 0;
  546. $data['task'] = $task;
  547. return $data;
  548. }
  549. /**
  550. * 经验列表
  551. * @param int $uid
  552. * @return array
  553. */
  554. public function expList(int $uid)
  555. {
  556. /** @var UserServices $userServices */
  557. $userServices = app()->make(UserServices::class);
  558. $user = $userServices->getUserInfo($uid);
  559. if (!$user) {
  560. throw new ValidateException('没有此用户');
  561. }
  562. /** @var UserBillServices $userBill */
  563. $userBill = app()->make(UserBillServices::class);
  564. $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
  565. $list = $data['list'] ?? [];
  566. return $list;
  567. }
  568. /**
  569. * 获取激活会员卡需要的信息
  570. * @return mixed
  571. */
  572. public function getActivateInfo()
  573. {
  574. //商城会员是否开启
  575. if (!sys_config('member_func_status')) {
  576. throw new ValidateException('会员卡功能暂未开启');
  577. }
  578. //是否需要激活
  579. if (!sys_config('level_activate_status')) {
  580. throw new ValidateException('会员卡功能暂不需要激活');
  581. }
  582. return SystemConfigService::get('level_extend_info');
  583. }
  584. /**
  585. * 激活会员卡
  586. * @param int $uid
  587. * @param array $data
  588. * @return array
  589. */
  590. public function userActivatelevel(int $uid, array $data)
  591. {
  592. /** @var UserServices $userServices */
  593. $userServices = app()->make(UserServices::class);
  594. $user = $userServices->getUserInfo($uid);
  595. if (!$user) {
  596. throw new ValidateException('用户已注销,或不存在');
  597. }
  598. if ($user['level_status']) {
  599. throw new ValidateException('不需要重复激活');
  600. }
  601. $extend_info = $userServices->handelExtendInfo($data, true) ?: [];
  602. $update = ['level_status' => 1];
  603. if ($extend_info) {
  604. $default = $userServices->defaultExtendInfo;
  605. $params = array_column($default, 'param');
  606. $sex = $userServices->sex;
  607. $update['level_extend_info'] = $extend_info;
  608. foreach ($extend_info as $info) {
  609. if (isset($info['param']) && in_array($info['param'], $params) && isset($info['value'])) {
  610. if ($info['param'] == 'sex') {
  611. $update['sex'] = $sex[$info['value']] ?? 0;
  612. } elseif ($info['param'] == 'birthday') {
  613. $update['birthday'] = strtotime($info['value']);
  614. } else {
  615. $update[$info['param']] = $info['value'];
  616. }
  617. }
  618. }
  619. }
  620. $userServices->update($uid, $update);
  621. $data = [];
  622. //获取激活送好礼
  623. $data = SystemConfigService::more([
  624. 'level_integral_status',
  625. 'level_give_integral',
  626. 'level_money_status',
  627. 'level_give_money',
  628. 'level_coupon_status',
  629. 'level_give_coupon',
  630. ]);
  631. $ids = $data['level_give_coupon'] ?? [];
  632. $data['level_give_coupon'] = [];
  633. if ($data['level_coupon_status'] && $ids) {
  634. /** @var StoreCouponIssueServices $couponServices */
  635. $couponServices = app()->make(StoreCouponIssueServices::class);
  636. $coupon = $couponServices->getList(['id' => $ids]);
  637. $data['level_give_coupon'] = $coupon;
  638. }
  639. if (!$data['level_integral_status']) {
  640. $data['level_give_integral'] = 0;
  641. }
  642. if (!$data['level_money_status']) {
  643. $data['level_give_money'] = 0;
  644. }
  645. //激活会员卡事件
  646. event('user.activate.level', [$uid]);
  647. return $data;
  648. }
  649. }