SystemAdminServices.php 15 KB

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