UserLevelServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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::number('achievement', '解锁需业绩达到', $vipinfo['achievement'] ?? 0)->min(0)->step(0.01)->col(8);
  230. $pre_level = false;
  231. if (isset($vipinfo)) {
  232. $pre_level = app()->make(SystemUserLevelServices::class)->getPreLevel($vipinfo['grade']);
  233. }
  234. $field[] = Form::number('sub_grade_num', $pre_level ? '解锁需存在' . $pre_level['name'] . '以上大区' : '解锁需存在低级别以上大区', $vipinfo['sub_grade_num'] ?? 0)->min(0)->precision(0)->col(8);
  235. $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('请选择图标'));
  236. $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('请选择背景'));
  237. $field[] = Form::radio('is_show', '是否显示', $vipinfo['is_show'] ?? 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
  238. $field[] = Form::textarea('explain', '等级说明', $vipinfo['explain'] ?? '');
  239. return create_form($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
  240. }
  241. /*
  242. * 会员等级添加或者修改
  243. * @param $id 修改的等级id
  244. * @return json
  245. * */
  246. public function save(int $id, array $data)
  247. {
  248. /** @var SystemUserLevelServices $systemUserLevel */
  249. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  250. $levelOne = $systemUserLevel->getWhereLevel(['is_del' => 0, 'grade' => $data['grade']]);
  251. $levelThree = $systemUserLevel->getWhereLevel(['is_del' => 0, 'name' => $data['name']]);
  252. $levelPre = $systemUserLevel->getPreLevel($data['grade']);
  253. $levelNext = $systemUserLevel->getNextLevel($data['grade']);
  254. if ($levelPre && $data['achievement'] < $levelPre['achievement']) {
  255. throw new AdminException('会员业绩必须大于等于上一等级设置的业绩');
  256. }
  257. if ($levelNext && $data['achievement'] > $levelNext['achievement']) {
  258. throw new AdminException('会员业绩必须小于等于下一等级设置的业绩');
  259. }
  260. //修改
  261. if ($id) {
  262. if (($levelOne && $levelOne['id'] != $id) || ($levelThree && $levelThree['id'] != $id)) {
  263. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  264. }
  265. if (!$systemUserLevel->update($id, $data)) {
  266. throw new AdminException('修改失败');
  267. }
  268. $data['id'] = $id;
  269. $systemUserLevel->dao->cacheUpdate($data);
  270. return '修改成功';
  271. } else {
  272. if ($levelOne || $levelThree) {
  273. throw new AdminException('已检测到您设置过的会员等级,此等级不可重复');
  274. }
  275. //新增
  276. $data['add_time'] = time();
  277. $res = $systemUserLevel->save($data);
  278. if (!$res) {
  279. throw new AdminException('添加失败');
  280. }
  281. $data['id'] = $res->id;
  282. $systemUserLevel->cacheUpdate($data);
  283. return '添加成功';
  284. }
  285. }
  286. /**
  287. * 假删除
  288. * @param int $id
  289. * @return mixed
  290. */
  291. public function delLevel(int $id)
  292. {
  293. /** @var SystemUserLevelServices $systemUserLevel */
  294. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  295. $level = $systemUserLevel->getWhereLevel(['id' => $id]);
  296. if ($level && $level['is_del'] != 1) {
  297. /** @var UserServices $userServices */
  298. $userServices = app()->make(UserServices::class);
  299. if ($userServices->count(['level' => $level['id']])) {
  300. throw new AdminException('存在用户已是该等级,无法删除');
  301. }
  302. if (!$systemUserLevel->update($id, ['is_del' => 1]))
  303. throw new AdminException('删除失败');
  304. if (!$this->dao->update(['level_id' => $id], ['is_del' => 1])) {
  305. throw new AdminException('删除失败');
  306. }
  307. }
  308. $systemUserLevel->cacheDelById($id);
  309. return '删除成功';
  310. }
  311. /**
  312. * 设置是否显示
  313. * @param int $id
  314. * @param $is_show
  315. * @return mixed
  316. */
  317. public function setShow(int $id, int $is_show)
  318. {
  319. /** @var SystemUserLevelServices $systemUserLevel */
  320. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  321. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  322. throw new AdminException('数据不存在');
  323. if ($systemUserLevel->update($id, ['is_show' => $is_show])) {
  324. $systemUserLevel->cacheSaveValue($id, 'is_show', $is_show);
  325. return $is_show == 1 ? '显示成功' : '隐藏成功';
  326. } else {
  327. throw new AdminException($is_show == 1 ? '显示失败' : '隐藏失败');
  328. }
  329. }
  330. /**
  331. * 快速修改
  332. * @param int $id
  333. * @param $is_show
  334. * @return mixed
  335. */
  336. public function setValue(int $id, array $data)
  337. {
  338. /** @var SystemUserLevelServices $systemUserLevel */
  339. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  340. if (!$systemUserLevel->getWhereLevel(['id' => $id]))
  341. throw new AdminException('数据不存在');
  342. if ($systemUserLevel->update($id, [$data['field'] => $data['value']])) {
  343. $systemUserLevel->cacheSaveValue($id, $data['field'], $data['value']);
  344. return true;
  345. } else {
  346. throw new AdminException('保存失败');
  347. }
  348. }
  349. /**
  350. * 检测用户会员升级
  351. * @param $uid
  352. * @return bool
  353. */
  354. public function detection(int $uid)
  355. {
  356. //商城会员是否开启
  357. if (!sys_config('member_func_status')) {
  358. return true;
  359. }
  360. /** @var UserServices $userServices */
  361. $userServices = app()->make(UserServices::class);
  362. $user = $userServices->getUserInfo($uid);
  363. if (!$user) {
  364. throw new ValidateException('没有此用户,无法检测升级会员');
  365. }
  366. //没有激活暂不升级
  367. if (!$user['level_status']) {
  368. return true;
  369. }
  370. /** @var SystemUserLevelServices $systemUserLevel */
  371. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  372. $userAllLevel = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1], ['exp_num', '<=', (float)$user['exp']]]);
  373. if (!$userAllLevel) {
  374. return true;
  375. }
  376. $data = [];
  377. $data['add_time'] = time();
  378. $userLevel = $this->dao->getColumn(['uid' => $uid, 'status' => 1, 'is_del' => 0], 'level_id');
  379. foreach ($userAllLevel as $vipinfo) {
  380. if (in_array($vipinfo['id'], $userLevel)) {
  381. continue;
  382. }
  383. $data['mark'] = '尊敬的用户' . $user['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  384. $uservip = $this->dao->getOne(['uid' => $uid, 'level_id' => $vipinfo['id']]);
  385. if ($uservip) {
  386. //降级在升级情况
  387. $data['status'] = 1;
  388. $data['is_del'] = 0;
  389. if (!$this->dao->update($uservip['id'], $data, 'id')) {
  390. throw new ValidateException('检测升级失败');
  391. }
  392. } else {
  393. $data = array_merge($data, [
  394. 'is_forever' => $vipinfo['is_forever'],
  395. 'status' => 1,
  396. 'is_del' => 0,
  397. 'grade' => $vipinfo['grade'],
  398. 'uid' => $uid,
  399. 'level_id' => $vipinfo['id'],
  400. 'discount' => $vipinfo['discount'],
  401. ]);
  402. if (!$this->dao->save($data)) {
  403. throw new ValidateException('检测升级失败');
  404. }
  405. }
  406. $data['add_time'] += 1;
  407. }
  408. if (!$userServices->update($uid, ['level' => end($userAllLevel)['id']], 'uid')) {
  409. throw new ValidateException('检测升级失败');
  410. }
  411. return true;
  412. }
  413. /**
  414. * 会员等级列表
  415. * @param int $uid
  416. */
  417. public function grade(int $uid)
  418. {
  419. //商城会员是否开启
  420. if (!sys_config('member_func_status')) {
  421. return [];
  422. }
  423. /** @var UserServices $userServices */
  424. $userServices = app()->make(UserServices::class);
  425. $user = $userServices->getUserInfo($uid);
  426. if (!$user) {
  427. throw new ValidateException('没有此用户,无法检测升级会员');
  428. }
  429. $userLevelInfo = $this->getUerLevelInfoByUid($uid);
  430. if (empty($userLevelInfo)) {
  431. $level_id = 0;
  432. } else {
  433. $level_id = $userLevelInfo['level_id'];
  434. }
  435. /** @var SystemUserLevelServices $systemUserLevel */
  436. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  437. return $systemUserLevel->getLevelListAndGrade($level_id);
  438. }
  439. /**
  440. * 获取会员信息
  441. * @param int $uid
  442. * @return array[]
  443. */
  444. public function getUserLevelInfo(int $uid)
  445. {
  446. $data = ['user' => [], 'level_info' => [], 'level_list' => [], 'task' => []];
  447. //商城会员是否开启
  448. if (!sys_config('member_func_status')) {
  449. return $data;
  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. /** @var StoreCouponUserServices $storeCoupon */
  458. $storeCoupon = app()->make(StoreCouponUserServices::class);
  459. $user['couponCount'] = $storeCoupon->getUserValidCouponCount((int)$uid);
  460. try {
  461. //检测升级
  462. $this->detection($uid);
  463. } catch (\Throwable $e) {
  464. }
  465. $data['user'] = $user;
  466. /** @var SystemUserLevelServices $systemUserLevel */
  467. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  468. $levelList = $systemUserLevel->getList(['is_del' => 0, 'is_show' => 1]);
  469. $i = 0;
  470. foreach ($levelList as &$level) {
  471. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  472. $level['image'] = set_file_url($level['image']);
  473. $level['icon'] = set_file_url($level['icon']);
  474. $i++;
  475. }
  476. $data['level_list'] = $levelList;
  477. $levelInfo = $this->getUerLevelInfoByUid($uid);
  478. if (!$levelInfo) {//不存在等级 展示最低等级
  479. /** @var SystemUserLevelServices $systemUserLevel */
  480. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  481. $alllevelInfo = $systemUserLevel->getList([['is_del', '=', 0], ['is_show', '=', 1]], 'id,name,icon,grade', 1, 1);
  482. $levelInfo = $alllevelInfo[0] ?? [];
  483. if ($levelInfo) {
  484. $levelInfo['id'] = 0;
  485. }
  486. }
  487. if ($levelInfo) {
  488. $levelInfo['vip'] = true;
  489. $levelInfo['vip_id'] = $levelInfo['id'];
  490. $levelInfo['vip_icon'] = $levelInfo['icon'];
  491. $levelInfo['vip_name'] = $levelInfo['name'];
  492. }
  493. $data['level_info'] = $levelInfo;
  494. $i = 0;
  495. foreach ($levelList as &$level) {
  496. if ($level['grade'] < $levelInfo['grade']) {
  497. $level['next_exp_num'] = $levelList[$i + 1]['exp_num'] ?? $level['exp_num'];
  498. } else {
  499. $level['next_exp_num'] = $level['exp_num'];
  500. }
  501. $level['image'] = set_file_url($level['image']);
  502. $level['icon'] = set_file_url($level['icon']);
  503. $i++;
  504. }
  505. $data['level_list'] = $levelList;
  506. $data['level_info']['exp'] = $user['exp'] ?? 0;
  507. /** @var UserBillServices $userBillservices */
  508. $userBillservices = app()->make(UserBillServices::class);
  509. $data['level_info']['today_exp'] = $userBillservices->getExpSum($uid, 'today');
  510. $task = [];
  511. /** @var UserSignServices $userSignServices */
  512. $userSignServices = app()->make(UserSignServices::class);
  513. $task['sign_count'] = $userSignServices->getSignSumDay($uid);
  514. $config = SystemConfigService::more(['sign_give_exp', 'order_give_exp', 'invite_user_exp']);
  515. $task['sign'] = $config['sign_give_exp'] ?? 0;
  516. $task['order'] = $config['order_give_exp'] ?? 0;
  517. $task['invite'] = $config['invite_user_exp'] ?? 0;
  518. $data['task'] = $task;
  519. return $data;
  520. }
  521. /**
  522. * 经验列表
  523. * @param int $uid
  524. * @return array
  525. */
  526. public function expList(int $uid)
  527. {
  528. /** @var UserServices $userServices */
  529. $userServices = app()->make(UserServices::class);
  530. $user = $userServices->getUserInfo($uid);
  531. if (!$user) {
  532. throw new ValidateException('没有此用户');
  533. }
  534. /** @var UserBillServices $userBill */
  535. $userBill = app()->make(UserBillServices::class);
  536. $data = $userBill->getExpList($uid, [], 'id,title,number,pm,add_time');
  537. $list = $data['list'] ?? [];
  538. return $list;
  539. }
  540. /**
  541. * 获取激活会员卡需要的信息
  542. * @return mixed
  543. */
  544. public function getActivateInfo()
  545. {
  546. //商城会员是否开启
  547. if (!sys_config('member_func_status')) {
  548. throw new ValidateException('会员卡功能暂未开启');
  549. }
  550. //是否需要激活
  551. if (!sys_config('level_activate_status')) {
  552. throw new ValidateException('会员卡功能暂不需要激活');
  553. }
  554. return SystemConfigService::get('level_extend_info');
  555. }
  556. /**
  557. * 激活会员卡
  558. * @param int $uid
  559. * @param array $data
  560. * @return array
  561. */
  562. public function userActivatelevel(int $uid, array $data)
  563. {
  564. /** @var UserServices $userServices */
  565. $userServices = app()->make(UserServices::class);
  566. $user = $userServices->getUserInfo($uid);
  567. if (!$user) {
  568. throw new ValidateException('用户已注销,或不存在');
  569. }
  570. if ($user['level_status']) {
  571. throw new ValidateException('不需要重复激活');
  572. }
  573. $extend_info = $userServices->handelExtendInfo($data, true) ?: [];
  574. $update = ['level_status' => 1];
  575. if ($extend_info) {
  576. $default = $userServices->defaultExtendInfo;
  577. $params = array_column($default, 'param');
  578. $sex = $userServices->sex;
  579. $update['level_extend_info'] = $extend_info;
  580. foreach ($extend_info as $info) {
  581. if (isset($info['param']) && in_array($info['param'], $params) && isset($info['value'])) {
  582. if ($info['param'] == 'sex') {
  583. $update['sex'] = $sex[$info['value']] ?? 0;
  584. } elseif ($info['param'] == 'birthday') {
  585. $update['birthday'] = strtotime($info['value']);
  586. } else {
  587. $update[$info['param']] = $info['value'];
  588. }
  589. }
  590. }
  591. }
  592. $userServices->update($uid, $update);
  593. $data = [];
  594. //获取激活送好礼
  595. $data = SystemConfigService::more([
  596. 'level_integral_status',
  597. 'level_give_integral',
  598. 'level_money_status',
  599. 'level_give_money',
  600. 'level_coupon_status',
  601. 'level_give_coupon',
  602. ]);
  603. $ids = $data['level_give_coupon'] ?? [];
  604. $data['level_give_coupon'] = [];
  605. if ($data['level_coupon_status'] && $ids) {
  606. /** @var StoreCouponIssueServices $couponServices */
  607. $couponServices = app()->make(StoreCouponIssueServices::class);
  608. $coupon = $couponServices->getList(['id' => $ids]);
  609. $data['level_give_coupon'] = $coupon;
  610. }
  611. if (!$data['level_integral_status']) {
  612. $data['level_give_integral'] = 0;
  613. }
  614. if (!$data['level_money_status']) {
  615. $data['level_give_money'] = 0;
  616. }
  617. //激活会员卡事件
  618. event('user.activate.level', [$uid]);
  619. return $data;
  620. }
  621. }