AdminRepository.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\common\repositories\system\admin;
  12. //附件
  13. use app\common\dao\system\admin\AdminDao;
  14. use app\common\model\system\admin\Admin;
  15. use app\common\repositories\BaseRepository;
  16. use app\common\repositories\system\auth\RoleRepository;
  17. use crmeb\exceptions\AuthException;
  18. use crmeb\services\JwtTokenService;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use FormBuilder\Factory\Elm;
  21. use FormBuilder\Form;
  22. use think\db\exception\DataNotFoundException;
  23. use think\db\exception\DbException;
  24. use think\db\exception\ModelNotFoundException;
  25. use think\exception\ValidateException;
  26. use think\facade\Cache;
  27. use think\facade\Config;
  28. use think\facade\Route;
  29. use think\Model;
  30. /**
  31. * Class BaseRepository
  32. * @package common\repositories
  33. * @mixin AdminDao
  34. */
  35. class AdminRepository extends BaseRepository
  36. {
  37. public function __construct(AdminDao $dao)
  38. {
  39. /**
  40. * @var AdminDao
  41. */
  42. $this->dao = $dao;
  43. }
  44. /**
  45. * @param array $where
  46. * @param $page
  47. * @param $limit
  48. * @return array
  49. * @throws DataNotFoundException
  50. * @throws DbException
  51. * @throws ModelNotFoundException
  52. * @author xaboy
  53. * @day 2020-04-09
  54. */
  55. public function getList(array $where, $page, $limit)
  56. {
  57. $query = $this->dao->search($where);
  58. $count = $query->count();
  59. $list = $query->page($page, $limit)->hidden(['pwd', 'is_del', 'update_time'])->select();
  60. foreach ($list as $k => $role) {
  61. $list[$k]['rule_name'] = $role->roleNames();
  62. }
  63. return compact('list', 'count');
  64. }
  65. public function passwordEncode($password)
  66. {
  67. return password_hash($password, PASSWORD_BCRYPT);
  68. }
  69. /**
  70. * 更新
  71. * @param int $id id
  72. * @param array $data 数组
  73. * @return int
  74. * @throws DbException
  75. * @author 张先生
  76. * @date 2020-03-26
  77. */
  78. public function update(int $id, array $data)
  79. {
  80. if (isset($data['roles']))
  81. $data['roles'] = implode(',', $data['roles']);
  82. return $this->dao->update($id, $data);
  83. }
  84. /**
  85. * @param int $id
  86. * @param $isSelf
  87. * @return Form
  88. * @throws FormBuilderException
  89. * @author xaboy
  90. * @day 2020-04-20
  91. */
  92. public function passwordForm(int $id, $isSelf = false)
  93. {
  94. $form = Elm::createForm(Route::buildUrl($isSelf ? 'systemAdminEditPassword' : 'systemAdminPassword', $isSelf ? [] : compact('id'))->build(), [
  95. $rules[] = Elm::password('pwd', '密码')->required(),
  96. $rules[] = Elm::password('againPassword', '确认密码')->required(),
  97. ]);
  98. return $form->setTitle('修改密码');
  99. }
  100. /**
  101. * @param array $formData
  102. * @return Form
  103. * @throws FormBuilderException
  104. * @author xaboy
  105. * @day 2020-04-20
  106. */
  107. public function editForm(array $formData)
  108. {
  109. $form = Elm::createForm(Route::buildUrl('systemAdminEdit')->build());
  110. $form->setRule([
  111. Elm::input('real_name', '管理员姓名')->required(),
  112. Elm::input('phone', '联系电话')
  113. ]);
  114. return $form->setTitle('修改信息')->formData($formData);
  115. }
  116. /**
  117. * @param int|null $id
  118. * @param array $formData
  119. * @return Form
  120. * @throws FormBuilderException
  121. * @author xaboy
  122. * @day 2020-04-08
  123. */
  124. public function form(?int $id = null, array $formData = []): Form
  125. {
  126. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemAdminCreate')->build() : Route::buildUrl('systemAdminUpdate', ['id' => $id])->build());
  127. $rules = [
  128. Elm::select('roles', '身份', [])->options(function () {
  129. $data = app()->make(RoleRepository::class)->getAllOptions(0);
  130. $options = [];
  131. foreach ($data as $value => $label) {
  132. $options[] = compact('value', 'label');
  133. }
  134. return $options;
  135. })->multiple(true),
  136. Elm::input('real_name', '管理员姓名'),
  137. Elm::input('account', '账号')->required(),
  138. Elm::input('phone', ' 联系电话'),
  139. ];
  140. if (!$id) {
  141. $rules[] = Elm::password('pwd', '密码')->required();
  142. $rules[] = Elm::password('againPassword', '确认密码')->required();
  143. }
  144. $rules[] = Elm::switches('status', '是否开启', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启');
  145. $form->setRule($rules);
  146. return $form->setTitle(is_null($id) ? '添加管理员' : '编辑管理员')->formData($formData);
  147. }
  148. /**
  149. * @param int $id
  150. * @return Form
  151. * @throws DataNotFoundException
  152. * @throws DbException
  153. * @throws FormBuilderException
  154. * @throws ModelNotFoundException
  155. * @author xaboy
  156. * @day 2020-04-09
  157. */
  158. public function updateForm(int $id)
  159. {
  160. return $this->form($id, $this->dao->get($id)->toArray());
  161. }
  162. /**
  163. * @param string $account
  164. * @param string $password
  165. * @return array|Model|null
  166. * @throws DataNotFoundException
  167. * @throws DbException
  168. * @throws ModelNotFoundException
  169. * @author xaboy
  170. * @day 2020-04-10
  171. */
  172. public function login(string $account, string $password)
  173. {
  174. $adminInfo = $this->dao->accountByAdmin($account);
  175. if (!$adminInfo)
  176. throw new ValidateException('账号不存在');
  177. if ($adminInfo['status'] != 1)
  178. throw new ValidateException('账号已关闭');
  179. if (!password_verify($password, $adminInfo->pwd))
  180. throw new ValidateException('账号或密码错误');
  181. $adminInfo->last_time = date('Y-m-d H:i:s');
  182. $adminInfo->last_ip = app('request')->ip();
  183. $adminInfo->login_count++;
  184. $adminInfo->save();
  185. return $adminInfo;
  186. }
  187. /**
  188. * @param string $token
  189. * @param int $exp
  190. * @author xaboy
  191. * @day 2020-04-10
  192. */
  193. public function cacheToken(string $token, int $exp)
  194. {
  195. Cache::set('admin_' . $token, time() + $exp, $exp);
  196. }
  197. public function checkToken(string $token)
  198. {
  199. $has = Cache::has('admin_' . $token);
  200. if (!$has)
  201. throw new AuthException('无效的token');
  202. $lastTime = Cache::get('admin_' . $token);
  203. if (($lastTime + (intval(Config::get('admin.token_valid_exp', 15))) * 60) < time())
  204. throw new AuthException('token 已过期');
  205. }
  206. public function updateToken(string $token)
  207. {
  208. Cache::set('admin_' . $token, time(), intval(Config::get('admin.token_valid_exp', 15)) * 60);
  209. }
  210. public function clearToken(string $token)
  211. {
  212. Cache::delete('admin_' . $token);
  213. }
  214. /**
  215. * @param Admin $admin
  216. * @return array
  217. * @author xaboy
  218. * @day 2020-04-09
  219. */
  220. public function createToken(Admin $admin)
  221. {
  222. $service = new JwtTokenService();
  223. $exp = intval(Config::get('admin.token_exp', 3));
  224. $token = $service->createToken($admin->admin_id, 'admin', strtotime("+ {$exp}hour"));
  225. $this->cacheToken($token['token'], $token['out']);
  226. return $token;
  227. }
  228. /**
  229. * 检测验证码
  230. * @param string $key key
  231. * @param string $code 验证码
  232. * @author 张先生
  233. * @date 2020-03-26
  234. */
  235. public function checkCode(string $key, string $code)
  236. {
  237. $_code = Cache::get('am_captcha' . $key);
  238. if (!$_code) {
  239. throw new ValidateException('验证码过期');
  240. }
  241. if (strtolower($_code) != strtolower($code)) {
  242. throw new ValidateException('验证码错误');
  243. }
  244. //删除code
  245. Cache::delete('am_captcha' . $key);
  246. }
  247. /**
  248. * @param string $code
  249. * @return string
  250. * @author xaboy
  251. * @day 2020-04-09
  252. */
  253. public function createLoginKey(string $code)
  254. {
  255. $key = uniqid(microtime(true), true);
  256. Cache::set('am_captcha' . $key, $code, Config::get('admin.captcha_exp', 5) * 60);
  257. return $key;
  258. }
  259. }