UserLevelServices.php 10 KB

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