SystemAdminServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\admin;
  12. use app\jobs\CheckQueueJob;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\product\product\StoreProductReplyServices;
  16. use app\services\product\product\StoreProductServices;
  17. use app\services\user\UserExtractServices;
  18. use crmeb\exceptions\AdminException;
  19. use app\dao\system\admin\SystemAdminDao;
  20. use app\services\system\SystemMenusServices;
  21. use crmeb\services\CacheService;
  22. use crmeb\services\FormBuilder;
  23. use crmeb\services\workerman\ChannelService;
  24. use crmeb\utils\Rsa;
  25. use think\facade\Config;
  26. use think\facade\Event;
  27. use think\Model;
  28. /**
  29. * 管理员service
  30. * Class SystemAdminServices
  31. * @package app\services\system\admin
  32. * @method getAdminIds(int $level) 根据管理员等级获取管理员id
  33. * @method getOrdAdmin(string $field, int $level) 获取低于等级的管理员名称和id
  34. */
  35. class SystemAdminServices extends BaseServices
  36. {
  37. /**
  38. * form表单创建
  39. * @var FormBuilder
  40. */
  41. protected $builder;
  42. /**
  43. * SystemAdminServices constructor.
  44. * @param SystemAdminDao $dao
  45. */
  46. public function __construct(SystemAdminDao $dao, FormBuilder $builder)
  47. {
  48. $this->dao = $dao;
  49. $this->builder = $builder;
  50. }
  51. /**
  52. * 管理员登陆
  53. * @param string $account
  54. * @param string $password
  55. * @return array|bool|Model
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function verifyLogin(string $account, string $password)
  61. {
  62. $adminInfo = $this->dao->accountByAdmin($account);
  63. if (!$adminInfo || !password_verify($password, $adminInfo->pwd)) return false;
  64. if (!$adminInfo->status) {
  65. throw new AdminException(400595);
  66. }
  67. $adminInfo->last_time = time();
  68. $adminInfo->last_ip = app('request')->ip();
  69. $adminInfo->login_count++;
  70. $adminInfo->save();
  71. return $adminInfo;
  72. }
  73. /**
  74. * 文件管理员登陆
  75. * @param string $account
  76. * @param string $password
  77. * @return array|Model
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public function verifyFileLogin(string $account, string $password)
  83. {
  84. $adminInfo = $this->dao->accountByAdmin($account);
  85. if (!$adminInfo) {
  86. throw new AdminException(400594);
  87. }
  88. if (!$adminInfo->status) {
  89. throw new AdminException(400595);
  90. }
  91. if (!password_verify($password, $adminInfo->file_pwd)) {
  92. throw new AdminException(400140);
  93. }
  94. $adminInfo->last_time = time();
  95. $adminInfo->last_ip = app('request')->ip();
  96. $adminInfo->login_count++;
  97. $adminInfo->save();
  98. return $adminInfo;
  99. }
  100. /**
  101. * 后台登陆获取菜单获取token
  102. * @param string $account
  103. * @param string $password
  104. * @param string $type
  105. * @return array|bool
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function login(string $account, string $password, string $type, string $key = '')
  111. {
  112. $adminInfo = $this->verifyLogin($account, $password);
  113. if (!$adminInfo) return false;
  114. $tokenInfo = $this->createToken($adminInfo->id, $type, $adminInfo->pwd);
  115. /** @var SystemMenusServices $services */
  116. $services = app()->make(SystemMenusServices::class);
  117. [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']);
  118. $remind = Config::get('app.console_remind', false);
  119. if ($remind) {
  120. [$queue, $timer] = Event::until('AdminLoginListener', [$key]);
  121. }
  122. return [
  123. 'token' => $tokenInfo['token'],
  124. 'expires_time' => $tokenInfo['params']['exp'],
  125. 'menus' => $menus,
  126. 'unique_auth' => $uniqueAuth,
  127. 'user_info' => [
  128. 'id' => $adminInfo->getData('id'),
  129. 'account' => $adminInfo->getData('account'),
  130. 'head_pic' => get_file_link($adminInfo->getData('head_pic')),
  131. 'level' => $adminInfo->getData('level'),
  132. 'real_name' => $adminInfo->getData('real_name'),
  133. ],
  134. 'logo' => sys_config('site_logo'),
  135. 'logo_square' => sys_config('site_logo_square'),
  136. 'version' => get_crmeb_version(),
  137. 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')),
  138. 'queue' => $queue ?? true,
  139. 'timer' => $timer ?? true,
  140. 'site_name' => sys_config('site_name'),
  141. ];
  142. }
  143. /**
  144. * 获取登陆前的login等信息
  145. * @return array
  146. */
  147. public function getLoginInfo()
  148. {
  149. $key = uniqid();
  150. CheckQueueJob::dispatch([$key]);
  151. $data = [
  152. 'slide' => sys_data('admin_login_slide') ?? [],
  153. 'logo_square' => sys_config('site_logo_square'),//透明
  154. 'logo_rectangle' => sys_config('site_logo'),//方形
  155. 'login_logo' => sys_config('login_logo'),//登陆
  156. 'site_name' => sys_config('site_name'),
  157. 'copyright' => sys_config('nncnL_crmeb_copyright', ''),
  158. 'version' => get_crmeb_version(),
  159. 'key' => $key,
  160. 'login_captcha' => 0,
  161. 'publicKey' => app()->make(Rsa::class)->getPublicKey()
  162. ];
  163. if (CacheService::get('login_captcha', 1) > 1) {
  164. $data['login_captcha'] = 1;
  165. }
  166. return $data;
  167. }
  168. /**
  169. * 管理员列表
  170. * @param array $where
  171. * @return array
  172. */
  173. public function getAdminList(array $where)
  174. {
  175. [$page, $limit] = $this->getPageValue();
  176. $list = $this->dao->getList($where, $page, $limit);
  177. $count = $this->dao->count($where);
  178. /** @var SystemRoleServices $service */
  179. $service = app()->make(SystemRoleServices::class);
  180. $allRole = $service->getRoleArray();
  181. foreach ($list as &$item) {
  182. if ($item['roles']) {
  183. $roles = [];
  184. foreach ($item['roles'] as $id) {
  185. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  186. }
  187. if ($roles) {
  188. $item['roles'] = implode(',', $roles);
  189. } else {
  190. $item['roles'] = '';
  191. }
  192. }
  193. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  194. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  195. }
  196. return compact('list', 'count');
  197. }
  198. /**
  199. * 创建管理员表单
  200. * @param int $level
  201. * @param array $formData
  202. * @return mixed
  203. * @throws \FormBuilder\Exception\FormBuilderException
  204. */
  205. public function createAdminForm(int $level, array $formData = [])
  206. {
  207. $f[] = $this->builder->input('account', '管理员账号', $formData['account'] ?? '')->required('请填写管理员账号');
  208. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  209. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  210. $f[] = $this->builder->input('real_name', '管理员姓名', $formData['real_name'] ?? '')->required('请输入管理员姓名');
  211. /** @var SystemRoleServices $service */
  212. $service = app()->make(SystemRoleServices::class);
  213. $options = $service->getRoleFormSelect($level);
  214. if (isset($formData['roles'])) {
  215. foreach ($formData['roles'] as &$item) {
  216. $item = intval($item);
  217. }
  218. }
  219. $f[] = $this->builder->select('roles', '管理员身份', $formData['roles'] ?? [])->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员身份');
  220. $f[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  221. return $f;
  222. }
  223. /**
  224. * 添加管理员form表单获取
  225. * @param int $level
  226. * @return array
  227. * @throws \FormBuilder\Exception\FormBuilderException
  228. */
  229. public function createForm(int $level)
  230. {
  231. return create_form('管理员添加', $this->createAdminForm($level), $this->url('/setting/admin'));
  232. }
  233. /**
  234. * 创建管理员
  235. * @param array $data
  236. * @return bool
  237. */
  238. public function create(array $data)
  239. {
  240. if ($data['conf_pwd'] != $data['pwd']) {
  241. throw new AdminException(400264);
  242. }
  243. unset($data['conf_pwd']);
  244. if ($this->dao->count(['account' => $data['account'], 'is_del' => 0])) {
  245. throw new AdminException(400596);
  246. }
  247. $data['pwd'] = $this->passwordHash($data['pwd']);
  248. $data['add_time'] = time();
  249. $data['roles'] = implode(',', $data['roles']);
  250. $data['head_pic'] = '/statics/system_images/admin_head_pic.png';
  251. return $this->transaction(function () use ($data) {
  252. if ($this->dao->save($data)) {
  253. CacheService::clear();
  254. return true;
  255. } else {
  256. throw new AdminException(100022);
  257. }
  258. });
  259. }
  260. /**
  261. * 修改管理员表单
  262. * @param int $level
  263. * @param int $id
  264. * @return array
  265. * @throws \FormBuilder\Exception\FormBuilderException
  266. */
  267. public function updateForm(int $level, int $id)
  268. {
  269. $adminInfo = $this->dao->get($id);
  270. if (!$adminInfo) {
  271. throw new AdminException(400594);
  272. }
  273. if ($adminInfo->is_del) {
  274. throw new AdminException(400452);
  275. }
  276. return create_form('管理员修改', $this->createAdminForm($level, $adminInfo->toArray()), $this->url('/setting/admin/' . $id), 'PUT');
  277. }
  278. /**
  279. * 修改管理员
  280. * @param int $id
  281. * @param array $data
  282. * @return bool
  283. */
  284. public function save(int $id, array $data)
  285. {
  286. if (!$adminInfo = $this->dao->get($id)) {
  287. throw new AdminException(400594);
  288. }
  289. if ($adminInfo->is_del) {
  290. throw new AdminException(400452);
  291. }
  292. //修改密码
  293. if ($data['pwd']) {
  294. if (!$data['conf_pwd']) {
  295. throw new AdminException(400263);
  296. }
  297. if ($data['conf_pwd'] != $data['pwd']) {
  298. throw new AdminException(400264);
  299. }
  300. $adminInfo->pwd = $this->passwordHash($data['pwd']);
  301. }
  302. //修改账号
  303. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->dao->isAccountUsable($data['account'], $id)) {
  304. throw new AdminException(400596);
  305. }
  306. if (isset($data['roles'])) {
  307. $adminInfo->roles = implode(',', $data['roles']);
  308. }
  309. $adminInfo->real_name = $data['real_name'] ?? $adminInfo->real_name;
  310. $adminInfo->account = $data['account'] ?? $adminInfo->account;
  311. $adminInfo->status = $data['status'];
  312. if ($adminInfo->save()) {
  313. CacheService::clear();
  314. return true;
  315. } else {
  316. return false;
  317. }
  318. }
  319. /**
  320. * 修改当前管理员信息
  321. * @param int $id
  322. * @param array $data
  323. * @return bool
  324. */
  325. public function updateAdmin(int $id, array $data)
  326. {
  327. $adminInfo = $this->dao->get($id);
  328. if (!$adminInfo)
  329. throw new AdminException(400451);
  330. if ($adminInfo->is_del) {
  331. throw new AdminException(400452);
  332. }
  333. if (!$data['real_name'])
  334. throw new AdminException(400453);
  335. if ($data['pwd']) {
  336. if (!password_verify($data['pwd'], $adminInfo['pwd']))
  337. throw new AdminException(400597);
  338. if (!$data['new_pwd'])
  339. throw new AdminException(400598);
  340. if (!$data['conf_pwd'])
  341. throw new AdminException(400263);
  342. if ($data['new_pwd'] != $data['conf_pwd'])
  343. throw new AdminException(400264);
  344. $adminInfo->pwd = $this->passwordHash($data['new_pwd']);
  345. }
  346. $adminInfo->real_name = $data['real_name'];
  347. $adminInfo->head_pic = $data['head_pic'];
  348. if ($adminInfo->save())
  349. return true;
  350. else
  351. return false;
  352. }
  353. /**
  354. * 设置当前管理员文件管理密码
  355. * @param int $id
  356. * @param array $data
  357. * @return bool
  358. */
  359. public function setFilePassword(int $id, array $data)
  360. {
  361. $adminInfo = $this->dao->get($id);
  362. if (!$adminInfo)
  363. throw new AdminException(400451);
  364. if ($adminInfo->is_del) {
  365. throw new AdminException(400452);
  366. }
  367. if ($data['file_pwd']) {
  368. if ($adminInfo->level != 0) throw new AdminException(400611);
  369. if (!$data['conf_file_pwd'])
  370. throw new AdminException(400263);
  371. if ($data['file_pwd'] != $data['conf_file_pwd'])
  372. throw new AdminException(400264);
  373. $adminInfo->file_pwd = $this->passwordHash($data['file_pwd']);
  374. }
  375. if ($adminInfo->save())
  376. return true;
  377. else
  378. return false;
  379. }
  380. /** 后台订单下单,评论,支付成功,后台消息提醒
  381. * @param $event
  382. */
  383. public function adminNewPush()
  384. {
  385. try {
  386. /** @var StoreOrderServices $orderServices */
  387. $orderServices = app()->make(StoreOrderServices::class);
  388. $data['ordernum'] = $orderServices->count(['is_del' => 0, 'status' => 1, 'shipping_type' => 1]);
  389. /** @var StoreProductServices $productServices */
  390. $productServices = app()->make(StoreProductServices::class);
  391. $data['inventory'] = $productServices->count(['type' => 5]);
  392. /** @var StoreProductReplyServices $replyServices */
  393. $replyServices = app()->make(StoreProductReplyServices::class);
  394. $data['commentnum'] = $replyServices->count(['is_reply' => 0]);
  395. /** @var UserExtractServices $extractServices */
  396. $extractServices = app()->make(UserExtractServices::class);
  397. $data['reflectnum'] = $extractServices->getCount(['status' => 0]);//提现
  398. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  399. ChannelService::instance()->send('ADMIN_NEW_PUSH', $data);
  400. } catch (\Exception $e) {
  401. }
  402. }
  403. }