UserLevelServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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;
  13. use app\services\system\config\SystemUserLevelServices;
  14. use app\services\system\config\SystemUserLevelTaskServices;
  15. use qiniu\basic\BaseServices;
  16. use app\model\user\UserLevel;
  17. use qiniu\exceptions\AdminException;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\exception\ValidateException;
  22. /**
  23. * 用户等级
  24. * Class UserLevelServices
  25. * @package app\services\user\level
  26. * @mixin UserLevel
  27. */
  28. class UserLevelServices extends BaseServices
  29. {
  30. /**
  31. * UserLevelServices constructor.
  32. * @param UserLevel $model
  33. */
  34. public function __construct(UserLevel $model)
  35. {
  36. $this->model = $model;
  37. }
  38. /**
  39. * 某些条件获取单个
  40. * @param array $where
  41. * @param string $field
  42. * @return mixed
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws ModelNotFoundException
  46. */
  47. public function getWhereLevel(array $where, string $field = '*')
  48. {
  49. return $this->getOne($where, $field);
  50. }
  51. /**
  52. * 获取一些用户等级信息
  53. * @param array $uids
  54. * @return array
  55. */
  56. public function getUsersLevelInfo(array $uids)
  57. {
  58. return $this->getColumn([['uid', 'in', $uids]], 'level_id,is_forever,valid_time', 'uid');
  59. }
  60. /**
  61. * 清除会员等级
  62. * @param $uids
  63. * @return mixed
  64. */
  65. public function delUserLevel($uids)
  66. {
  67. if (is_array($uids)) {
  68. $re = $this->batchUpdate($uids, ['status' => 0, 'delete_time' => time()], 'uid');
  69. } else {
  70. $re = $this->update($uids, ['status' => 0, 'delete_time' => time()], 'uid');
  71. }
  72. if (!$re)
  73. throw new AdminException('修改会员信息失败');
  74. return true;
  75. }
  76. /**
  77. * 个人中心获取用户等级信息
  78. * @param int $uid
  79. * @param array $userInfo
  80. * @return array
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. */
  85. public function homeGetUserLevel(int $uid, $userInfo = [])
  86. {
  87. $data = ['vip' => false, 'vip_id' => 0, 'vip_icon' => '', 'vip_name' => ''];
  88. //用户存在
  89. if ($uid) {
  90. if (!$userInfo) {
  91. /** @var UserServices $userServices */
  92. $userServices = app()->make(UserServices::class);
  93. $userInfo = $userServices->getUserInfo($uid);
  94. }
  95. if ($userInfo) {
  96. $levelInfo = $this->getUserLevelInfo($uid);
  97. if (!$levelInfo) {//不存在等级 展示最低等级
  98. /** @var SystemUserLevelServices $systemUserLevel */
  99. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  100. $allLevelInfo = $systemUserLevel->getList([['is_show', '=', 1]], 'id,name,icon,grade,task', 1, 1);
  101. $levelInfo = $allLevelInfo[0] ?? [];
  102. if ($levelInfo) {
  103. $levelInfo['level_id'] = 0;
  104. }
  105. }
  106. if ($levelInfo) {
  107. $data['vip'] = true;
  108. $data['vip_id'] = $levelInfo['level_id'];
  109. $data['vip_icon'] = $levelInfo['icon'];
  110. $data['vip_name'] = $levelInfo['name'];
  111. if ($levelInfo['id'] == 0) {
  112. $data['task'] = $levelInfo['levelInfo']['task'];
  113. }
  114. }
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * 根据用户uid 获取会员详细信息
  121. * @param int $uid
  122. * @param string $field
  123. * @return array|int|mixed|string
  124. * @throws DataNotFoundException
  125. * @throws DbException
  126. * @throws ModelNotFoundException
  127. */
  128. public function getUserLevelInfo(int $uid, string $field = '')
  129. {
  130. $userLevelInfo = $this->model->search(['valid' => 1, 'uid' => $uid])
  131. ->field($field)
  132. ->with(['levelInfo'])
  133. ->order('grade desc,add_time desc')
  134. ->find();
  135. $userLevelInfo['name'] = $userLevelInfo['levelInfo']['name'] ?? '';
  136. $userLevelInfo['icon'] = set_file_url($userLevelInfo['levelInfo']['icon'] ?? '');
  137. $userLevelInfo['image'] = set_file_url($userLevelInfo['levelInfo']['image'] ?? '');
  138. $userLevelInfo['task'] = $userLevelInfo['levelInfo']['task'] ?? [];
  139. if ($field) return $userLevelInfo[$field] ?? '';
  140. return $userLevelInfo;
  141. }
  142. /**
  143. * 设置会员等级
  144. * @param int $uid 用户uid
  145. * @param int $level_id 等级id
  146. * @return UserLevel|bool|\think\Model
  147. * @throws DataNotFoundException
  148. * @throws ModelNotFoundException|DbException
  149. */
  150. public function setUserLevel(int $uid, int $level_id, $vipinfo = [])
  151. {
  152. /** @var SystemUserLevelServices $systemLevelServices */
  153. $systemLevelServices = app()->make(SystemUserLevelServices::class);
  154. if (!$vipinfo) {
  155. $vipinfo = $systemLevelServices->getLevel($level_id);
  156. if (!$vipinfo) {
  157. throw new AdminException('会员等级不存在');
  158. }
  159. }
  160. /** @var UserServices $user */
  161. $user = app()->make(UserServices::class);
  162. $userinfo = $user->getUserInfo($uid);
  163. $userVipInfo = $this->getWhereLevel(['uid' => $uid, 'level_id' => $level_id]);
  164. $data['mark'] = '尊敬的用户' . $userinfo['nickname'] . '在' . date('Y-m-d H:i:s', time()) . '成为了' . $vipinfo['name'];
  165. $data['add_time'] = time();
  166. if ($userVipInfo) {
  167. if ($vipinfo['valid_time'] > 0) $data['valid_time'] = ($userVipInfo['valid_time'] > time() ? $userVipInfo['valid_time'] : time()) + ($vipinfo['valid_time'] * 86400);
  168. $this->search(['uid' => $uid])->where('id', '<>', $userVipInfo['id'])->update(['status' => 0, 'delete_time' => time()]);
  169. if (!$this->update(['id' => $userVipInfo['id']], $data))
  170. throw new AdminException('修改会员信息失败');
  171. } else {
  172. $data = array_merge($data, [
  173. 'is_forever' => $vipinfo->is_forever,
  174. 'status' => 1,
  175. 'is_del' => 0,
  176. 'grade' => $vipinfo->grade,
  177. 'uid' => $uid,
  178. 'level_id' => $level_id,
  179. 'discount' => $vipinfo->discount,
  180. 'valid_time' => 0
  181. ]);
  182. if ($vipinfo['valid_time'] > 0) $data['valid_time'] = time() + ($vipinfo['valid_time'] * 86400);
  183. if (!$this->save($data)) throw new AdminException('写入会员信息失败');
  184. }
  185. if (!$user->update(['uid' => $uid], ['level' => $level_id]))
  186. throw new AdminException('修改用户会员等级失败');
  187. return true;
  188. }
  189. /**
  190. * 检测用户会员升级
  191. * @param int $uid
  192. * @return bool
  193. * @throws DataNotFoundException
  194. * @throws DbException
  195. * @throws ModelNotFoundException
  196. */
  197. public function detection(int $uid, $pass = [])
  198. {
  199. //商城会员是否开启
  200. if (!sys_config('member_func_status')) {
  201. return true;
  202. }
  203. /** @var UserServices $userServices */
  204. $userServices = app()->make(UserServices::class);
  205. $user = $userServices->getUserInfo($uid);
  206. if (!$user) {
  207. throw new ValidateException('没有此用户,无法检测升级会员');
  208. }
  209. //没有激活暂不升级
  210. if (!$user['level_status']) {
  211. return true;
  212. }
  213. /** @var SystemUserLevelServices $systemUserLevel */
  214. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  215. $userAllLevel = $systemUserLevel->getList([['is_show', '=', 1]]);
  216. if (!$userAllLevel) {
  217. return true;
  218. }
  219. foreach ($userAllLevel as $vipinfo) {
  220. /** @var SystemUserLevelTaskServices $taskService */
  221. $taskService = app()->make(SystemUserLevelTaskServices::class);
  222. $status = $taskService->checkLevelTask($vipinfo['id'], $uid, $user['clean_time']);
  223. if (!$status) break;
  224. if (sys_config('down_grade', 0)) {
  225. $this->delUserLevel($uid);
  226. $userServices->update(['uid' => $uid], ['level' => 0]);
  227. }
  228. $this->setUserLevel($uid, $vipinfo['id']);
  229. }
  230. $spread_uid = $userServices->where('uid', $uid)->value('spread_uid');
  231. if ($spread_uid && !in_array($spread_uid, $pass)) {
  232. $pass[] = $spread_uid;
  233. $this->detection($spread_uid, $pass);
  234. }
  235. return true;
  236. }
  237. /**
  238. * 会员等级列表
  239. * @param int $uid
  240. * @return array
  241. * @throws DataNotFoundException
  242. * @throws DbException
  243. * @throws ModelNotFoundException
  244. */
  245. public function grade(int $uid)
  246. {
  247. //商城会员是否开启
  248. if (!sys_config('member_func_status')) {
  249. return [];
  250. }
  251. /** @var UserServices $userServices */
  252. $userServices = app()->make(UserServices::class);
  253. $user = $userServices->getUserInfo($uid);
  254. if (!$user) {
  255. throw new ValidateException('没有此用户,无法检测升级会员');
  256. }
  257. $userLevelInfo = $this->getUserLevelInfo($uid);
  258. if (empty($userLevelInfo)) {
  259. $level_id = 0;
  260. } else {
  261. $level_id = $userLevelInfo['level_id'];
  262. }
  263. /** @var SystemUserLevelServices $systemUserLevel */
  264. $systemUserLevel = app()->make(SystemUserLevelServices::class);
  265. return $systemUserLevel->getLevelListAndGrade($level_id);
  266. }
  267. }