SystemAdminServices.php 13 KB

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