MerchantAdminRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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\common\repositories\system\merchant;
  12. use app\common\dao\BaseDao;
  13. use app\common\dao\system\merchant\MerchantAdminDao;
  14. use app\common\model\system\merchant\Merchant;
  15. use app\common\model\system\merchant\MerchantAdmin;
  16. use app\common\repositories\BaseRepository;
  17. use app\common\repositories\system\auth\RoleRepository;
  18. use crmeb\exceptions\AuthException;
  19. use crmeb\services\JwtTokenService;
  20. use FormBuilder\Exception\FormBuilderException;
  21. use FormBuilder\Factory\Elm;
  22. use FormBuilder\Form;
  23. use think\db\exception\DataNotFoundException;
  24. use think\db\exception\DbException;
  25. use think\db\exception\ModelNotFoundException;
  26. use think\exception\ValidateException;
  27. use think\facade\Cache;
  28. use think\facade\Config;
  29. use think\facade\Route;
  30. use think\Model;
  31. /**
  32. * Class MerchantRepository
  33. * @package app\common\repositories\system\merchant
  34. * @mixin MerchantAdminDao
  35. * @author xaboy
  36. * @day 2020-04-16
  37. */
  38. class MerchantAdminRepository extends BaseRepository
  39. {
  40. const PASSWORD_TYPE_ADMIN = 1;
  41. const PASSWORD_TYPE_MERCHANT = 2;
  42. const PASSWORD_TYPE_SELF = 3;
  43. /**
  44. * MerchantAdminRepository constructor.
  45. * @param MerchantAdminDao $dao
  46. */
  47. public function __construct(MerchantAdminDao $dao)
  48. {
  49. $this->dao = $dao;
  50. }
  51. /**
  52. * @param $merId
  53. * @param array $where
  54. * @param $page
  55. * @param $limit
  56. * @return array
  57. * @throws DataNotFoundException
  58. * @throws DbException
  59. * @throws ModelNotFoundException
  60. * @author xaboy
  61. * @day 2020-04-18
  62. */
  63. public function getList($merId, array $where, $page, $limit)
  64. {
  65. $query = $this->dao->search($merId, $where, 1);
  66. $count = $query->count();
  67. $list = $query->page($page, $limit)->hidden(['pwd', 'is_del'])->select();
  68. $topAccount = $this->dao->merIdByAccount($merId);
  69. foreach ($list as $k => $role) {
  70. if ($topAccount)
  71. $list[$k]['account'] = $topAccount . '@' . $role['account'];
  72. $list[$k]['rule_name'] = $role->roleNames();
  73. }
  74. return compact('list', 'count');
  75. }
  76. /**
  77. * @param int $merId
  78. * @param int|null $id
  79. * @param array $formData
  80. * @return Form
  81. * @throws FormBuilderException
  82. * @author xaboy
  83. * @day 2020-04-18
  84. */
  85. public function form(int $merId, ?int $id = null, array $formData = []): Form
  86. {
  87. $form = Elm::createForm(is_null($id) ? Route::buildUrl('merchantAdminCreate')->build() : Route::buildUrl('merchantAdminUpdate', ['id' => $id])->build());
  88. $rules = [
  89. Elm::select('roles', '身份', [])->options(function () use ($merId) {
  90. $data = app()->make(RoleRepository::class)->getAllOptions($merId);
  91. $options = [];
  92. foreach ($data as $value => $label) {
  93. $options[] = compact('value', 'label');
  94. }
  95. return $options;
  96. })->multiple(true),
  97. Elm::input('real_name', '管理员姓名'),
  98. Elm::input('account', '账号')->required(),
  99. Elm::input('phone', ' 联系电话'),
  100. ];
  101. if (!$id) {
  102. $rules[] = Elm::password('pwd', '密码')->required();
  103. $rules[] = Elm::password('againPassword', '确认密码')->required();
  104. }
  105. $rules[] = Elm::switches('status', '是否开启', 1)->inactiveValue(0)->activeValue(1)->inactiveText('关')->activeText('开');
  106. $form->setRule($rules);
  107. return $form->setTitle(is_null($id) ? '添加管理员' : '编辑管理员')->formData($formData);
  108. }
  109. /**
  110. * @param int $merId
  111. * @param int $id
  112. * @return Form
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws FormBuilderException
  116. * @throws ModelNotFoundException
  117. * @author xaboy
  118. * @day 2020-04-18
  119. */
  120. public function updateForm(int $merId, int $id)
  121. {
  122. return $this->form($merId, $id, $this->dao->get($id)->toArray());
  123. }
  124. /**
  125. * @param Merchant $merchant
  126. * @param $account
  127. * @param $pwd
  128. * @return BaseDao|Model
  129. * @author xaboy
  130. * @day 2020-04-17
  131. */
  132. public function createMerchantAccount(Merchant $merchant, $account, $pwd)
  133. {
  134. $pwd = $this->passwordEncode($pwd);
  135. $data = compact('pwd', 'account') + [
  136. 'mer_id' => $merchant->mer_id,
  137. 'real_name' => $merchant->real_name,
  138. 'phone' => $merchant->mer_phone,
  139. 'level' => 0
  140. ];
  141. return $this->create($data);
  142. }
  143. /**
  144. * @param $password
  145. * @return bool|string
  146. * @author xaboy
  147. * @day 2020-04-17
  148. */
  149. public function passwordEncode($password)
  150. {
  151. return password_hash($password, PASSWORD_BCRYPT);
  152. }
  153. /**
  154. * @param string $account
  155. * @param string $password
  156. * @return array|Model|null
  157. * @throws DataNotFoundException
  158. * @throws DbException
  159. * @throws ModelNotFoundException
  160. * @author xaboy
  161. * @day 2020-04-17
  162. */
  163. public function login(string $account, string $password)
  164. {
  165. event('admin.merLogin.before',compact('account', 'password'));
  166. $accountInfo = explode('@', $account, 2);
  167. if (count($accountInfo) === 1) {
  168. $adminInfo = $this->dao->accountByTopAdmin($accountInfo[0]);
  169. } else {
  170. $merId = $this->dao->accountByMerchantId($accountInfo[0]);
  171. if (!$merId){
  172. $key = 'mer_login_failuree_'.$account;
  173. $numb = Cache::get($key) ?? 0;
  174. $numb++;
  175. Cache::set($key,$numb,15*60);
  176. throw new ValidateException('账号或密码错误');
  177. }
  178. $adminInfo = $this->dao->accountByAdmin($accountInfo[1], $merId);
  179. }
  180. if (!$adminInfo || !password_verify($password, $adminInfo->pwd)){
  181. $key = 'mer_login_failuree_'.$account;
  182. $numb = Cache::get($key) ?? 0;
  183. $numb++;
  184. Cache::set($key,$numb,15*60);
  185. throw new ValidateException('账号或密码错误');
  186. }
  187. if ($adminInfo['status'] != 1)
  188. throw new ValidateException('账号已关闭');
  189. /**
  190. * @var MerchantRepository $merchantRepository
  191. */
  192. $merchantRepository = app()->make(MerchantRepository::class);
  193. $merchant = $merchantRepository->get($adminInfo->mer_id);
  194. if (!$merchant)
  195. throw new ValidateException('商户不存在');
  196. if (!$merchant['status'])
  197. throw new ValidateException('商户已被锁定');
  198. $adminInfo->last_time = date('Y-m-d H:i:s');
  199. $adminInfo->last_ip = app('request')->ip();
  200. $adminInfo->login_count++;
  201. $adminInfo->save();
  202. event('admin.merLogin',compact('adminInfo'));
  203. return $adminInfo;
  204. }
  205. /**
  206. * @param string $token
  207. * @param int $exp
  208. * @author xaboy
  209. * @day 2020-04-10
  210. */
  211. public function cacheToken(string $token, int $exp)
  212. {
  213. Cache::set('mer_' . $token, time() + $exp, $exp);
  214. }
  215. /**
  216. * @param string $token
  217. * @author xaboy
  218. * @day 2020-04-17
  219. */
  220. public function checkToken(string $token)
  221. {
  222. $has = Cache::has('mer_' . $token);
  223. if (!$has)
  224. throw new AuthException('无效的token');
  225. $lastTime = Cache::get('mer_' . $token);
  226. if (($lastTime + (intval(Config::get('admin.token_valid_exp', 15))) * 60) < time())
  227. throw new AuthException('token 已过期');
  228. }
  229. /**
  230. * @param string $token
  231. * @author xaboy
  232. * @day 2020-04-17
  233. */
  234. public function updateToken(string $token)
  235. {
  236. Cache::set('mer_' . $token, time(), intval(Config::get('admin.token_valid_exp', 15)) * 60);
  237. }
  238. /**
  239. * @param string $token
  240. * @author xaboy
  241. * @day 2020-04-17
  242. */
  243. public function clearToken(string $token)
  244. {
  245. Cache::delete('mer_' . $token);
  246. }
  247. /**
  248. * @param MerchantAdmin $admin
  249. * @return array
  250. * @author xaboy
  251. * @day 2020-04-17
  252. */
  253. public function createToken(MerchantAdmin $admin)
  254. {
  255. $service = new JwtTokenService();
  256. $exp = intval(Config::get('admin.token_exp', 3));
  257. $token = $service->createToken($admin->merchant_admin_id, 'mer', strtotime("+ {$exp}hour"));
  258. $this->cacheToken($token['token'], $token['out']);
  259. return $token;
  260. }
  261. /**
  262. * @param string $key
  263. * @param string $code
  264. * @author xaboy
  265. * @day 2020-04-17
  266. */
  267. public function checkCode(string $key, string $code)
  268. {
  269. if (!env('DEVELOPMENT',false)){
  270. $_code = Cache::get('am_captcha' . $key);
  271. if (!$_code) {
  272. throw new ValidateException('验证码过期');
  273. }
  274. if (strtolower($_code) != strtolower($code)) {
  275. throw new ValidateException('验证码错误');
  276. }
  277. //删除code
  278. Cache::delete('am_captcha' . $key);
  279. }
  280. }
  281. /**
  282. * @param string $code
  283. * @return string
  284. * @author xaboy
  285. * @day 2020-04-17
  286. */
  287. public function createLoginKey(string $code)
  288. {
  289. $key = uniqid(microtime(true), true);
  290. Cache::set('am_captcha' . $key, $code, Config::get('admin.captcha_exp', 5) * 60);
  291. return $key;
  292. }
  293. /**
  294. * @param int $id
  295. * @param int $userType
  296. * @return Form
  297. * @throws FormBuilderException
  298. * @author xaboy
  299. * @day 2020-04-20
  300. */
  301. public function passwordForm(int $id, $userType = 2)
  302. {
  303. $action = 'merchantAdminPassword';
  304. if ($userType == self::PASSWORD_TYPE_ADMIN)
  305. $action = 'systemMerchantAdminPassword';
  306. else if ($userType == self::PASSWORD_TYPE_SELF)
  307. $action = 'merchantAdminEditPassword';
  308. $form = Elm::createForm(Route::buildUrl($action, $userType == self::PASSWORD_TYPE_SELF ? [] : compact('id'))->build(), [
  309. $rules[] = Elm::password('pwd', '密码')->required(),
  310. $rules[] = Elm::password('againPassword', '确认密码')->required(),
  311. ]);
  312. return $form->setTitle('修改密码');
  313. }
  314. /**
  315. * @param array $formData
  316. * @return Form
  317. * @throws FormBuilderException
  318. * @author xaboy
  319. * @day 2020-04-20
  320. */
  321. public function editForm(array $formData)
  322. {
  323. $form = Elm::createForm(Route::buildUrl('merchantAdminEdit')->build());
  324. $form->setRule([
  325. Elm::input('real_name', '管理员姓名')->required(),
  326. Elm::input('phone', '联系电话')
  327. ]);
  328. return $form->setTitle('修改信息')->formData($formData);
  329. }
  330. /**
  331. * @param int $id
  332. * @param array $data
  333. * @return int
  334. * @throws DbException
  335. * @author xaboy
  336. * @day 2020-04-18
  337. */
  338. public function update(int $id, array $data)
  339. {
  340. if (isset($data['roles']))
  341. $data['roles'] = implode(',', $data['roles']);
  342. return $this->dao->update($id, $data);
  343. }
  344. }