SystemAdminServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. ],
  132. 'logo' => sys_config('site_logo'),
  133. 'logo_square' => sys_config('site_logo_square'),
  134. 'version' => get_crmeb_version(),
  135. 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')),
  136. 'queue' => $queue ?? true,
  137. 'timer' => $timer ?? true
  138. ];
  139. }
  140. /**
  141. * 获取登陆前的login等信息
  142. * @return array
  143. */
  144. public function getLoginInfo()
  145. {
  146. $key = uniqid();
  147. event('admin.info', [$key]);
  148. return [
  149. 'slide' => sys_data('admin_login_slide') ?? [],
  150. 'logo_square' => sys_config('site_logo_square'),//透明
  151. 'logo_rectangle' => sys_config('site_logo'),//方形
  152. 'login_logo' => sys_config('login_logo'),//登陆
  153. 'site_name' => sys_config('site_name'),
  154. 'copyright' => sys_config('nncnL_crmeb_copyright'),
  155. 'version' => get_crmeb_version(),
  156. 'key' => $key
  157. ];
  158. }
  159. /**
  160. * 管理员列表
  161. * @param array $where
  162. * @return array
  163. */
  164. public function getAdminList(array $where)
  165. {
  166. [$page, $limit] = $this->getPageValue();
  167. $list = $this->dao->getList($where, $page, $limit);
  168. $count = $this->dao->count($where);
  169. /** @var SystemRoleServices $service */
  170. $service = app()->make(SystemRoleServices::class);
  171. $allRole = $service->getRoleArray();
  172. foreach ($list as &$item) {
  173. if ($item['roles']) {
  174. $roles = [];
  175. foreach ($item['roles'] as $id) {
  176. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  177. }
  178. if ($roles) {
  179. $item['roles'] = implode(',', $roles);
  180. } else {
  181. $item['roles'] = '';
  182. }
  183. }
  184. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  185. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  186. }
  187. return compact('list', 'count');
  188. }
  189. /**
  190. * 创建管理员表单
  191. * @param int $level
  192. * @param array $formData
  193. * @return mixed
  194. * @throws \FormBuilder\Exception\FormBuilderException
  195. */
  196. public function createAdminForm(int $level, array $formData = [])
  197. {
  198. $f[] = $this->builder->input('account', '管理员账号', $formData['account'] ?? '')->required('请填写管理员账号');
  199. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  200. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  201. $f[] = $this->builder->input('real_name', '管理员姓名', $formData['real_name'] ?? '')->required('请输入管理员姓名');
  202. /** @var SystemRoleServices $service */
  203. $service = app()->make(SystemRoleServices::class);
  204. $options = $service->getRoleFormSelect($level);
  205. if (isset($formData['roles'])) {
  206. foreach ($formData['roles'] as &$item) {
  207. $item = intval($item);
  208. }
  209. }
  210. $f[] = $this->builder->select('roles', '管理员身份', $formData['roles'] ?? [])->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员身份');
  211. $f[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  212. return $f;
  213. }
  214. /**
  215. * 添加管理员form表单获取
  216. * @param int $level
  217. * @return array
  218. * @throws \FormBuilder\Exception\FormBuilderException
  219. */
  220. public function createForm(int $level)
  221. {
  222. return create_form('管理员添加', $this->createAdminForm($level), $this->url('/setting/admin'));
  223. }
  224. /**
  225. * 创建管理员
  226. * @param array $data
  227. * @return bool
  228. */
  229. public function create(array $data)
  230. {
  231. if ($data['conf_pwd'] != $data['pwd']) {
  232. throw new AdminException(400264);
  233. }
  234. unset($data['conf_pwd']);
  235. if ($this->dao->count(['account' => $data['account'], 'is_del' => 0])) {
  236. throw new AdminException(400596);
  237. }
  238. $data['pwd'] = $this->passwordHash($data['pwd']);
  239. $data['add_time'] = time();
  240. $data['roles'] = implode(',', $data['roles']);
  241. return $this->transaction(function () use ($data) {
  242. if ($this->dao->save($data)) {
  243. \think\facade\Cache::clear();
  244. return true;
  245. } else {
  246. throw new AdminException(100022);
  247. }
  248. });
  249. }
  250. /**
  251. * 修改管理员表单
  252. * @param int $level
  253. * @param int $id
  254. * @return array
  255. * @throws \FormBuilder\Exception\FormBuilderException
  256. */
  257. public function updateForm(int $level, int $id)
  258. {
  259. $adminInfo = $this->dao->get($id);
  260. if (!$adminInfo) {
  261. throw new AdminException(400594);
  262. }
  263. if ($adminInfo->is_del) {
  264. throw new AdminException(400452);
  265. }
  266. return create_form('管理员修改', $this->createAdminForm($level, $adminInfo->toArray()), $this->url('/setting/admin/' . $id), 'PUT');
  267. }
  268. /**
  269. * 修改管理员
  270. * @param int $id
  271. * @param array $data
  272. * @return bool
  273. */
  274. public function save(int $id, array $data)
  275. {
  276. if (!$adminInfo = $this->dao->get($id)) {
  277. throw new AdminException(400594);
  278. }
  279. if ($adminInfo->is_del) {
  280. throw new AdminException(400452);
  281. }
  282. //修改密码
  283. if ($data['pwd']) {
  284. if (!$data['conf_pwd']) {
  285. throw new AdminException(400263);
  286. }
  287. if ($data['conf_pwd'] != $data['pwd']) {
  288. throw new AdminException(400264);
  289. }
  290. $adminInfo->pwd = $this->passwordHash($data['pwd']);
  291. }
  292. //修改账号
  293. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->dao->isAccountUsable($data['account'], $id)) {
  294. throw new AdminException(400596);
  295. }
  296. if (isset($data['roles'])) {
  297. $adminInfo->roles = implode(',', $data['roles']);
  298. }
  299. $adminInfo->real_name = $data['real_name'] ?? $adminInfo->real_name;
  300. $adminInfo->account = $data['account'] ?? $adminInfo->account;
  301. $adminInfo->status = $data['status'];
  302. if ($adminInfo->save()) {
  303. \think\facade\Cache::clear();
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. }
  309. /**
  310. * 修改当前管理员信息
  311. * @param int $id
  312. * @param array $data
  313. * @return bool
  314. */
  315. public function updateAdmin(int $id, array $data)
  316. {
  317. $adminInfo = $this->dao->get($id);
  318. if (!$adminInfo)
  319. throw new AdminException(400451);
  320. if ($adminInfo->is_del) {
  321. throw new AdminException(400452);
  322. }
  323. if (!$data['real_name'])
  324. throw new AdminException(400453);
  325. if ($data['pwd']) {
  326. if (!password_verify($data['pwd'], $adminInfo['pwd']))
  327. throw new AdminException(400597);
  328. if (!$data['new_pwd'])
  329. throw new AdminException(400598);
  330. if (!$data['conf_pwd'])
  331. throw new AdminException(400263);
  332. if ($data['new_pwd'] != $data['conf_pwd'])
  333. throw new AdminException(400264);
  334. $adminInfo->pwd = $this->passwordHash($data['new_pwd']);
  335. }
  336. if ($data['file_pwd']) {
  337. if ($adminInfo->level != 0) throw new AdminException(400611);
  338. if (!$data['conf_file_pwd'])
  339. throw new AdminException(400263);
  340. if ($data['file_pwd'] != $data['conf_file_pwd'])
  341. throw new AdminException(400264);
  342. $adminInfo->file_pwd = $this->passwordHash($data['file_pwd']);
  343. }
  344. $adminInfo->real_name = $data['real_name'];
  345. $adminInfo->head_pic = $data['head_pic'];
  346. if ($adminInfo->save())
  347. return true;
  348. else
  349. return false;
  350. }
  351. /** 后台订单下单,评论,支付成功,后台消息提醒
  352. * @param $event
  353. */
  354. public function adminNewPush()
  355. {
  356. try {
  357. /** @var StoreOrderServices $orderServices */
  358. $orderServices = app()->make(StoreOrderServices::class);
  359. $data['ordernum'] = $orderServices->count(['is_del' => 0, 'status' => 1, 'shipping_type' => 1]);
  360. /** @var StoreProductServices $productServices */
  361. $productServices = app()->make(StoreProductServices::class);
  362. $data['inventory'] = $productServices->count(['type' => 5]);
  363. /** @var StoreProductReplyServices $replyServices */
  364. $replyServices = app()->make(StoreProductReplyServices::class);
  365. $data['commentnum'] = $replyServices->count(['is_reply' => 0]);
  366. /** @var UserExtractServices $extractServices */
  367. $extractServices = app()->make(UserExtractServices::class);
  368. $data['reflectnum'] = $extractServices->getCount(['status' => 0]);//提现
  369. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  370. ChannelService::instance()->send('ADMIN_NEW_PUSH', $data);
  371. } catch (\Exception $e) {
  372. }
  373. }
  374. }