UserLevelServices.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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\UserAwardIntegralServices;
  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(UserAwardIntegralServices::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. foreach ($userAllLevel as $vipinfo) {
  383. if (in_array($vipinfo['id'], $userLevel)) {
  384. continue;
  385. }
  386. $num = 0;
  387. $sub_user = $userServices->getColumn(['spread_uid' => $user['uid']], 'uid');
  388. if (!count($sub_user)) {
  389. $num = 0;
  390. } else {
  391. foreach ($sub_user as $v) {
  392. $subUsers = $this->dao->getColumn([['grade', '>=', $vipinfo['grade'] - 1], ['status', '=', 1], ['is_del', '=', 0], ['uid', 'in', array_merge([$v], get_group_user($v))]], 'uid');
  393. if (count(array_unique($subUsers)) > 0) {
  394. $num++;
  395. }
  396. }
  397. }
  398. if ($vipinfo['sub_grade_num'] > $num) {
  399. continue;
  400. }
  401. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  402. $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  403. if ($uservip) {
  404. //降级在升级情况
  405. $data['status'] = 1;
  406. $data['is_del'] = 0;
  407. if (!$this->dao->update($uservip['id'], $data, 'id')) {
  408. throw new ValidateException('检测升级失败');
  409. }
  410. } else {
  411. $data = array_merge($data, [
  412. 'is_forever' => $vipinfo['is_forever'],
  413. 'status' => 1,
  414. 'is_del' => 0,
  415. 'grade' => $vipinfo['grade'],
  416. 'uid' => $uid,
  417. 'level_id' => $vipinfo['id'],
  418. 'discount' => $vipinfo['discount'],
  419. ]);
  420. if (!$this->dao->save($data)) {
  421. throw new ValidateException('检测升级失败');
  422. }
  423. }
  424. $data['add_time'] += 1;
  425. }
  426. if (!$userServices->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
  427. throw new ValidateException('检测升级失败');
  428. }
  429. return true;
  430. }
  431. /**
  432. * 会员等级列表
  433. * @param int $uid
  434. */
  435. public function grade(int $uid)
  436. {
  437. //商城会员是否开启
  438. if (!sys_config('member_func_status')) {
  439. return [];
  440. }
  441. /** @var UserServices $userServices */
  442. $userServices = app()->make(UserServices::class);
  443. $user = $userServices->getUserInfo($uid);
  444. if (!$user) {
  445. throw new ValidateException('没有此用户,无法检测升级会员');
  446. }
  447. $userLevelInfo = $this->getUerLevelInfoByUid($uid);
  448. if (empty($userLevelInfo)) {
  449. $level_id = 0;
  450. } else {
  451. $level_id = $userLevelInfo['level_id'];
  452. }
  453. /** @var SystemUserLevelServices $systemUserLevel */
  454. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  455. return $systemUserLevel->getLevelListAndGrade($level_id);
  456. }
  457. /**
  458. * 获取会员信息
  459. * @param int $uid
  460. * @return array[]
  461. */
  462. public function getUserLevelInfo(int $uid)
  463. {
  464. $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
  465. //商城会员是否开启
  466. if (!sys_config('member_func_status')) {
  467. return $data;
  468. }
  469. /** @var UserServices $userServices */
  470. $userServices = app()->make(UserServices::class);
  471. $user = $userServices->getUserInfo($uid);
  472. if (!$user) {
  473. throw new ValidateException('没有此会员');
  474. }
  475. /** @var StoreCouponUserServices $storeCoupon */
  476. $storeCoupon = app()->make(StoreCouponUserServices::class);
  477. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  478. try {
  479. //检测升级
  480. $this->detection($uid);
  481. } catch (\Throwable $e) {
  482. }
  483. $data['user'] = $user;
  484. /** @var SystemUserLevelServices $systemUserLevel */
  485. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  486. $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
  487. $i = 0;
  488. foreach ($levelList as &$level) {
  489. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  490. $level['image'] = set_file_url($level['image']);
  491. $level['icon'] = set_file_url($level['icon']);
  492. $i++;
  493. }
  494. $data['level_list'] = $levelList;
  495. $levelInfo = $this->getUerLevelInfoByUid($uid);
  496. if (!$levelInfo) {//不存在等级 展示最低等级
  497. /** @var SystemUserLevelServices $systemUserLevel */
  498. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  499. $alllevelInfo = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1]], 'id,name,icon,grade', 1, 1);
  500. $levelInfo = $alllevelInfo[0] ?? [];
  501. if ($levelInfo) {
  502. $levelInfo['id'] = 0;
  503. }
  504. }
  505. if ($levelInfo) {
  506. $levelInfo['vip'] = true;
  507. $levelInfo['vip_id'] = $levelInfo['id'];
  508. $levelInfo['vip_icon'] = $levelInfo['icon'];
  509. $levelInfo['vip_name'] = $levelInfo['name'];
  510. }
  511. $data['level_info'] = $levelInfo;
  512. $i = 0;
  513. foreach ($levelList as &$level) {
  514. if ($level['grade'] < $levelInfo['grade']) {
  515. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  516. } else {
  517. $level['next_exp_num'] = $level['exp_num'];
  518. }
  519. $level['image'] = set_file_url($level['image']);
  520. $level['icon'] = set_file_url($level['icon']);
  521. $i++;
  522. }
  523. $data['level_list'] = $levelList;
  524. $data['level_info']['exp'] = $user['exp'] ?? 0;
  525. /** @var UserBillServices $userBillservices */
  526. $userBillservices = app()->make(UserBillServices::class);
  527. $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
  528. $task = [];
  529. /** @var UserSignServices $userSignServices */
  530. $userSignServices = app()->make(UserSignServices::class);
  531. $task['sign_count'] = $userSignServices->getSignSumDay($uid);
  532. $config = SystemConfigService::more(['sign_give_exp', 'order_give_exp', 'invite_user_exp']);
  533. $task['sign'] = $config['sign_give_exp'] ?? 0;
  534. $task['order'] = $config['order_give_exp'] ?? 0;
  535. $task['invite'] = $config['invite_user_exp'] ?? 0;
  536. $data['task'] = $task;
  537. return $data;
  538. }
  539. /**
  540. * 经验列表
  541. * @param int $uid
  542. * @return array
  543. */
  544. public function expList(int $uid)
  545. {
  546. /** @var UserServices $userServices */
  547. $userServices = app()->make(UserServices::class);
  548. $user = $userServices->getUserInfo($uid);
  549. if (!$user) {
  550. throw new ValidateException('没有此用户');
  551. }
  552. /** @var UserBillServices $userBill */
  553. $userBill = app()->make(UserBillServices::class);
  554. $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
  555. $list = $data['list'] ?? [];
  556. return $list;
  557. }
  558. /**
  559. * 获取激活会员卡需要的信息
  560. * @return mixed
  561. */
  562. public function getActivateInfo()
  563. {
  564. //商城会员是否开启
  565. if (!sys_config('member_func_status')) {
  566. throw new ValidateException('会员卡功能暂未开启');
  567. }
  568. //是否需要激活
  569. if (!sys_config('level_activate_status')) {
  570. throw new ValidateException('会员卡功能暂不需要激活');
  571. }
  572. return SystemConfigService::get('level_extend_info');
  573. }
  574. /**
  575. * 激活会员卡
  576. * @param int $uid
  577. * @param array $data
  578. * @return array
  579. */
  580. public function userActivatelevel(int $uid, array $data)
  581. {
  582. /** @var UserServices $userServices */
  583. $userServices = app()->make(UserServices::class);
  584. $user = $userServices->getUserInfo($uid);
  585. if (!$user) {
  586. throw new ValidateException('用户已注销,或不存在');
  587. }
  588. if ($user['level_status']) {
  589. throw new ValidateException('不需要重复激活');
  590. }
  591. $extend_info = $userServices->handelExtendInfo($data, true) ?: [];
  592. $update = ['level_status' => 1];
  593. if ($extend_info) {
  594. $default = $userServices->defaultExtendInfo;
  595. $params = array_column($default, 'param');
  596. $sex = $userServices->sex;
  597. $update['level_extend_info'] = $extend_info;
  598. foreach ($extend_info as $info) {
  599. if (isset($info['param']) && in_array($info['param'], $params) && isset($info['value'])) {
  600. if ($info['param'] == 'sex') {
  601. $update['sex'] = $sex[$info['value']] ?? 0;
  602. } elseif ($info['param'] == 'birthday') {
  603. $update['birthday'] = strtotime($info['value']);
  604. } else {
  605. $update[$info['param']] = $info['value'];
  606. }
  607. }
  608. }
  609. }
  610. $userServices->update($uid, $update);
  611. $data = [];
  612. //获取激活送好礼
  613. $data = SystemConfigService::more([
  614. 'level_integral_status',
  615. 'level_give_integral',
  616. 'level_money_status',
  617. 'level_give_money',
  618. 'level_coupon_status',
  619. 'level_give_coupon',
  620. ]);
  621. $ids = $data['level_give_coupon'] ?? [];
  622. $data['level_give_coupon'] = [];
  623. if ($data['level_coupon_status'] && $ids) {
  624. /** @var StoreCouponIssueServices $couponServices */
  625. $couponServices = app()->make(StoreCouponIssueServices::class);
  626. $coupon = $couponServices->getList(['id' => $ids]);
  627. $data['level_give_coupon'] = $coupon;
  628. }
  629. if (!$data['level_integral_status']) {
  630. $data['level_give_integral'] = 0;
  631. }
  632. if (!$data['level_money_status']) {
  633. $data['level_give_money'] = 0;
  634. }
  635. //激活会员卡事件
  636. event('user.activate.level', [$uid]);
  637. return $data;
  638. }
  639. }