UserLevelServices.php 25 KB

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