SystemAdminServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. 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\Event;
  23. /**
  24. * 管理员service
  25. * Class SystemAdminServices
  26. * @package app\services\system\admin
  27. * @method getAdminIds(int $level) 根据管理员等级获取管理员id
  28. * @method getOrdAdmin(string $field, int $level) 获取低于等级的管理员名称和id
  29. */
  30. class SystemAdminServices extends BaseServices
  31. {
  32. /**
  33. * form表单创建
  34. * @var FormBuilder
  35. */
  36. protected $builder;
  37. /**
  38. * SystemAdminServices constructor.
  39. * @param SystemAdminDao $dao
  40. */
  41. public function __construct(SystemAdminDao $dao, FormBuilder $builder)
  42. {
  43. $this->dao = $dao;
  44. $this->builder = $builder;
  45. }
  46. /**
  47. * 管理员登陆
  48. * @param string $account
  49. * @param string $password
  50. * @return array|\think\Model
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function verifyLogin(string $account, string $password)
  56. {
  57. $adminInfo = $this->dao->accountByAdmin($account);
  58. if (!$adminInfo) {
  59. throw new AdminException(400594);
  60. }
  61. if (!$adminInfo->status) {
  62. throw new AdminException(400595);
  63. }
  64. if (!password_verify($password, $adminInfo->pwd)) {
  65. throw new AdminException(400140);
  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. * 后台登陆获取菜单获取token
  75. * @param string $account
  76. * @param string $password
  77. * @param string $type
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function login(string $account, string $password, string $type, string $key = '')
  84. {
  85. $adminInfo = $this->verifyLogin($account, $password);
  86. $tokenInfo = $this->createToken($adminInfo->id, $type);
  87. /** @var SystemMenusServices $services */
  88. $services = app()->make(SystemMenusServices::class);
  89. [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']);
  90. [$queue, $timer] = Event::until('admin.login', [$key]);
  91. return [
  92. 'token' => $tokenInfo['token'],
  93. 'expires_time' => $tokenInfo['params']['exp'],
  94. 'menus' => $menus,
  95. 'unique_auth' => $uniqueAuth,
  96. 'user_info' => [
  97. 'id' => $adminInfo->getData('id'),
  98. 'account' => $adminInfo->getData('account'),
  99. 'head_pic' => $adminInfo->getData('head_pic'),
  100. ],
  101. 'logo' => sys_config('site_logo'),
  102. 'logo_square' => sys_config('site_logo_square'),
  103. 'version' => get_crmeb_version(),
  104. 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')),
  105. 'queue' => $queue,
  106. 'timer' => $timer
  107. ];
  108. }
  109. /**
  110. * 获取登陆前的login等信息
  111. * @return array
  112. */
  113. public function getLoginInfo()
  114. {
  115. $key = uniqid();
  116. event('admin.info', [$key]);
  117. return [
  118. 'slide' => sys_data('admin_login_slide') ?? [],
  119. 'logo_square' => sys_config('site_logo_square'),//透明
  120. 'logo_rectangle' => sys_config('site_logo'),//方形
  121. 'login_logo' => sys_config('login_logo'),//登陆
  122. 'site_name' => sys_config('site_name'),
  123. 'key' => $key
  124. ];
  125. }
  126. /**
  127. * 管理员列表
  128. * @param array $where
  129. * @return array
  130. */
  131. public function getAdminList(array $where)
  132. {
  133. [$page, $limit] = $this->getPageValue();
  134. $list = $this->dao->getList($where, $page, $limit);
  135. $count = $this->dao->count($where);
  136. /** @var SystemRoleServices $service */
  137. $service = app()->make(SystemRoleServices::class);
  138. $allRole = $service->getRoleArray();
  139. foreach ($list as &$item) {
  140. if ($item['roles']) {
  141. $roles = [];
  142. foreach ($item['roles'] as $id) {
  143. if (isset($allRole[$id])) $roles[] = $allRole[$id];
  144. }
  145. if ($roles) {
  146. $item['roles'] = implode(',', $roles);
  147. } else {
  148. $item['roles'] = '';
  149. }
  150. }
  151. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  152. $item['_last_time'] = $item['last_time'] ? date('Y-m-d H:i:s', $item['last_time']) : '';
  153. }
  154. return compact('list', 'count');
  155. }
  156. /**
  157. * 创建管理员表单
  158. * @param int $level
  159. * @param array $formData
  160. * @return mixed
  161. * @throws \FormBuilder\Exception\FormBuilderException
  162. */
  163. public function createAdminForm(int $level, array $formData = [])
  164. {
  165. $f[] = $this->builder->input('account', '管理员账号', $formData['account'] ?? '')->required('请填写管理员账号');
  166. $f[] = $this->builder->input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
  167. $f[] = $this->builder->input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
  168. $f[] = $this->builder->input('real_name', '管理员姓名', $formData['real_name'] ?? '')->required('请输入管理员姓名');
  169. /** @var SystemRoleServices $service */
  170. $service = app()->make(SystemRoleServices::class);
  171. $options = $service->getRoleFormSelect($level);
  172. if (isset($formData['roles'])) {
  173. foreach ($formData['roles'] as &$item) {
  174. $item = intval($item);
  175. }
  176. }
  177. $f[] = $this->builder->select('roles', '管理员身份', $formData['roles'] ?? [])->setOptions(FormBuilder::setOptions($options))->multiple(true)->required('请选择管理员身份');
  178. $f[] = $this->builder->radio('status', '状态', $formData['status'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  179. return $f;
  180. }
  181. /**
  182. * 添加管理员form表单获取
  183. * @param int $level
  184. * @return array
  185. * @throws \FormBuilder\Exception\FormBuilderException
  186. */
  187. public function createForm(int $level)
  188. {
  189. return create_form('管理员添加', $this->createAdminForm($level), $this->url('/setting/admin'));
  190. }
  191. /**
  192. * 创建管理员
  193. * @param array $data
  194. * @return bool
  195. */
  196. public function create(array $data)
  197. {
  198. if ($data['conf_pwd'] != $data['pwd']) {
  199. throw new AdminException(400264);
  200. }
  201. unset($data['conf_pwd']);
  202. if ($this->dao->count(['account' => $data['account'], 'is_del' => 0])) {
  203. throw new AdminException(400596);
  204. }
  205. $data['pwd'] = $this->passwordHash($data['pwd']);
  206. $data['add_time'] = time();
  207. $data['roles'] = implode(',', $data['roles']);
  208. return $this->transaction(function () use ($data) {
  209. if ($this->dao->save($data)) {
  210. \think\facade\Cache::clear();
  211. return true;
  212. } else {
  213. throw new AdminException(100022);
  214. }
  215. });
  216. }
  217. /**
  218. * 修改管理员表单
  219. * @param int $level
  220. * @param int $id
  221. * @return array
  222. * @throws \FormBuilder\Exception\FormBuilderException
  223. */
  224. public function updateForm(int $level, int $id)
  225. {
  226. $adminInfo = $this->dao->get($id);
  227. if (!$adminInfo) {
  228. throw new AdminException(400594);
  229. }
  230. if ($adminInfo->is_del) {
  231. throw new AdminException(400452);
  232. }
  233. return create_form('管理员修改', $this->createAdminForm($level, $adminInfo->toArray()), $this->url('/setting/admin/' . $id), 'PUT');
  234. }
  235. /**
  236. * 修改管理员
  237. * @param int $id
  238. * @param array $data
  239. * @return bool
  240. */
  241. public function save(int $id, array $data)
  242. {
  243. if (!$adminInfo = $this->dao->get($id)) {
  244. throw new AdminException(400594);
  245. }
  246. if ($adminInfo->is_del) {
  247. throw new AdminException(400452);
  248. }
  249. //修改密码
  250. if ($data['pwd']) {
  251. if (!$data['conf_pwd']) {
  252. throw new AdminException(400263);
  253. }
  254. if ($data['conf_pwd'] != $data['pwd']) {
  255. throw new AdminException(400264);
  256. }
  257. $adminInfo->pwd = $this->passwordHash($data['pwd']);
  258. }
  259. //修改账号
  260. if (isset($data['account']) && $data['account'] != $adminInfo->account && $this->dao->isAccountUsable($data['account'], $id)) {
  261. throw new AdminException(400596);
  262. }
  263. if (isset($data['roles'])) {
  264. $adminInfo->roles = implode(',', $data['roles']);
  265. }
  266. $adminInfo->real_name = $data['real_name'] ?? $adminInfo->real_name;
  267. $adminInfo->account = $data['account'] ?? $adminInfo->account;
  268. $adminInfo->status = $data['status'];
  269. if ($adminInfo->save()) {
  270. \think\facade\Cache::clear();
  271. return true;
  272. } else {
  273. return false;
  274. }
  275. }
  276. /**
  277. * 修改当前管理员信息
  278. * @param int $id
  279. * @param array $data
  280. * @return bool
  281. */
  282. public function updateAdmin(int $id, array $data)
  283. {
  284. $adminInfo = $this->dao->get($id);
  285. if (!$adminInfo)
  286. throw new AdminException(400451);
  287. if ($adminInfo->is_del) {
  288. throw new AdminException(400452);
  289. }
  290. if (!$data['real_name'])
  291. throw new AdminException(400453);
  292. if ($data['pwd']) {
  293. if (!password_verify($data['pwd'], $adminInfo['pwd']))
  294. throw new AdminException(400597);
  295. if (!$data['new_pwd'])
  296. throw new AdminException(400598);
  297. if (!$data['conf_pwd'])
  298. throw new AdminException(400263);
  299. if ($data['new_pwd'] != $data['conf_pwd'])
  300. throw new AdminException(400264);
  301. $adminInfo->pwd = $this->passwordHash($data['new_pwd']);
  302. }
  303. $adminInfo->real_name = $data['real_name'];
  304. $adminInfo->head_pic = $data['head_pic'];
  305. if ($adminInfo->save())
  306. return true;
  307. else
  308. return false;
  309. }
  310. /** 后台订单下单,评论,支付成功,后台消息提醒
  311. * @param $event
  312. */
  313. public function adminNewPush()
  314. {
  315. try {
  316. /** @var StoreOrderServices $orderServices */
  317. $orderServices = app()->make(StoreOrderServices::class);
  318. $data['ordernum'] = $orderServices->count(['is_del' => 0, 'status' => 1, 'shipping_type' => 1]);
  319. /** @var StoreProductServices $productServices */
  320. $productServices = app()->make(StoreProductServices::class);
  321. $data['inventory'] = $productServices->count(['type' => 5]);
  322. /** @var StoreProductReplyServices $replyServices */
  323. $replyServices = app()->make(StoreProductReplyServices::class);
  324. $data['commentnum'] = $replyServices->count(['is_reply' => 0]);
  325. /** @var UserExtractServices $extractServices */
  326. $extractServices = app()->make(UserExtractServices::class);
  327. $data['reflectnum'] = $extractServices->getCount(['status' => 0]);//提现
  328. $data['msgcount'] = intval($data['ordernum']) + intval($data['inventory']) + intval($data['commentnum']) + intval($data['reflectnum']);
  329. ChannelService::instance()->send('ADMIN_NEW_PUSH', $data);
  330. } catch (\Exception $e) {
  331. }
  332. }
  333. }