MerchantAdminRepository.php 10 KB

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