MerchantAdmin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\system\admin;
  12. use app\common\repositories\system\auth\RoleRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\system\merchant\MerchantAdminRepository;
  15. use app\validate\admin\AdminEditValidate;
  16. use app\validate\admin\AdminValidate;
  17. use FormBuilder\Exception\FormBuilderException;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * Class MerchantAdmin
  24. * @package app\controller\admin\system\admin
  25. * @author xaboy
  26. * @day 2020-04-18
  27. */
  28. class MerchantAdmin extends BaseController
  29. {
  30. /**
  31. * @var MerchantAdminRepository
  32. */
  33. protected $repository;
  34. /**
  35. * @var int
  36. */
  37. protected $merId;
  38. /**
  39. * 构造函数
  40. *
  41. * @param App $app 应用实例
  42. * @param MerchantAdminRepository $repository 商家管理仓库实例
  43. */
  44. public function __construct(App $app, MerchantAdminRepository $repository)
  45. {
  46. // 调用父类构造函数
  47. parent::__construct($app);
  48. // 初始化商家管理仓库实例
  49. $this->repository = $repository;
  50. // 获取当前商家ID
  51. $this->merId = $this->request->merId();
  52. }
  53. /**
  54. * 获取列表
  55. *
  56. * @return mixed
  57. */
  58. public function getList()
  59. {
  60. // 获取查询条件
  61. $where = $this->request->params(['keyword', 'date', 'status']);
  62. // 获取分页参数
  63. [$page, $limit] = $this->getPage();
  64. // 调用商家管理仓库实例的获取列表方法并返回结果
  65. return app('json')->success($this->repository->getList($this->merId, $where, $page, $limit));
  66. }
  67. /**
  68. * 切换状态
  69. *
  70. * @param int $id 商品ID
  71. * @return mixed
  72. */
  73. public function switchStatus($id)
  74. {
  75. // 获取状态值
  76. $status = $this->request->param('status');
  77. // 判断商品是否存在
  78. if (!$this->repository->exists($id, $this->merId, 1))
  79. return app('json')->fail('数据不存在');
  80. // 更新商品状态
  81. $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
  82. // 返回操作成功结果
  83. return app('json')->success('编辑成功');
  84. }
  85. /**
  86. * 创建表单
  87. *
  88. * @return \Illuminate\Http\JsonResponse
  89. */
  90. public function createForm()
  91. {
  92. // 调用 formToData 方法将表单转换为数据并返回 JSON 格式的成功响应
  93. return app('json')->success(formToData($this->repository->form($this->merId)));
  94. }
  95. /**
  96. * 更新表单
  97. *
  98. * @param int $id 表单 ID
  99. * @return \Illuminate\Http\JsonResponse
  100. */
  101. public function updateForm($id)
  102. {
  103. // 判断表单是否存在,若不存在则返回 JSON 格式的失败响应
  104. if (!$this->repository->exists($id, $this->merId, 1))
  105. return app('json')->fail('数据不存在');
  106. // 调用 repository 的 updateForm 方法更新表单,并将结果转换为数据并返回 JSON 格式的成功响应
  107. return app('json')->success(formToData($this->repository->updateForm($this->merId, $id)));
  108. }
  109. /**
  110. * 密码表单
  111. *
  112. * @param int $id 表单 ID
  113. * @return \Illuminate\Http\JsonResponse
  114. */
  115. public function passwordForm($id)
  116. {
  117. if (!$this->repository->exists($id, $this->merId))
  118. return app('json')->fail('数据不存在');
  119. // 调用 repository 的 passwordForm 方法获取密码表单,并将结果转换为数据并返回 JSON 格式的成功响应
  120. return app('json')->success(formToData($this->repository->passwordForm($id)));
  121. }
  122. /**
  123. * 创建管理员
  124. *
  125. * @param AdminValidate $validate 验证器实例
  126. * @return \think\response\Json
  127. */
  128. public function create(AdminValidate $validate)
  129. {
  130. $data = $this->request->params(['account', 'phone', 'pwd', 'againPassword', 'real_name', ['roles', []], ['status', 0]]);
  131. // 验证参数
  132. $validate->check($data);
  133. // 判断两次密码是否一致
  134. if ($data['pwd'] !== $data['againPassword'])
  135. return app('json')->fail('两次密码输入不一致');
  136. // 删除再次确认密码字段
  137. unset($data['againPassword']);
  138. // 判断账号是否已存在
  139. if ($this->repository->merFieldExists($this->merId, 'account', $data['account']))
  140. return app('json')->fail('账号已存在');
  141. // 对密码进行加密
  142. $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
  143. // 设置商家ID和等级
  144. $data['mer_id'] = $this->merId;
  145. $data['level'] = 1;
  146. // 检查角色是否可用
  147. $check = app()->make(RoleRepository::class)->checkRole($data['roles'], $this->merId);
  148. if (!$check) {
  149. return app('json')->fail('未开启或者不存在的身份不能添加');
  150. }
  151. // 创建管理员
  152. $this->repository->create($data);
  153. return app('json')->success('添加成功');
  154. }
  155. /**
  156. * 编辑管理员
  157. *
  158. * @param int $id 管理员ID
  159. * @param AdminValidate $validate 验证器实例
  160. * @return \think\response\Json
  161. */
  162. public function update($id, AdminValidate $validate)
  163. {
  164. $data = $this->request->params(['account', 'phone', 'real_name', ['roles', []], ['status', 0]]);
  165. $validate->isUpdate()->check($data);
  166. if ($this->repository->merFieldExists($this->merId, 'account', $data['account'], $id))
  167. return app('json')->fail('账号已存在');
  168. // 检查角色是否可用
  169. $check = app()->make(RoleRepository::class)->checkRole($data['roles'], $this->merId);
  170. if (!$check) {
  171. return app('json')->fail('未开启或者不存在的身份不能添加');
  172. }
  173. $this->repository->update($id, $data);
  174. return app('json')->success('编辑成功');
  175. }
  176. /**
  177. * 修改管理员密码
  178. *
  179. * @param int $id 管理员ID
  180. * @param AdminValidate $validate 管理员验证器实例
  181. * @return \think\response\Json
  182. */
  183. public function password($id, AdminValidate $validate)
  184. {
  185. // 获取请求参数
  186. $data = $this->request->params(['pwd', 'againPassword']);
  187. // 验证密码是否符合规则
  188. $validate->isPassword()->check($data);
  189. // 判断两次密码是否一致
  190. if ($data['pwd'] !== $data['againPassword'])
  191. return app('json')->fail('两次密码输入不一致');
  192. // 判断管理员是否存在
  193. if (!$this->repository->exists($id, $this->merId))
  194. return app('json')->fail('管理员不存在');
  195. // 对密码进行加密
  196. $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
  197. // 删除再次确认密码字段
  198. unset($data['againPassword']);
  199. // 更新管理员密码
  200. $this->repository->update($id, $data);
  201. return app('json')->success('修改密码成功');
  202. }
  203. /**
  204. * 删除管理员
  205. *
  206. * @param int $id 管理员ID
  207. * @return \think\response\Json
  208. */
  209. public function delete($id)
  210. {
  211. if (!$this->repository->exists($id, $this->merId, 1))
  212. return app('json')->fail('数据不存在');
  213. // 更新管理员状态为已删除
  214. $this->repository->update($id, ['is_del' => 1]);
  215. return app('json')->success('删除成功');
  216. }
  217. /**
  218. * 编辑管理员信息
  219. *
  220. * @param AdminEditValidate $validate 管理员编辑验证器
  221. * @return \think\response\Json 返回操作结果
  222. */
  223. public function edit(AdminEditValidate $validate)
  224. {
  225. // 从请求参数中获取真实姓名和电话号码
  226. $data = $this->request->params(['real_name', 'phone']);
  227. // 对获取到的数据进行校验
  228. $validate->check($data);
  229. // 调用仓库的更新方法,更新管理员信息
  230. $this->repository->update($this->request->adminId(), $data);
  231. // 返回操作成功的JSON响应
  232. return app('json')->success('修改成功');
  233. }
  234. /**
  235. * 编辑表单页面
  236. *
  237. * @return \think\response\Json
  238. */
  239. public function editForm()
  240. {
  241. // 获取当前管理员信息
  242. $adminInfo = $this->request->adminInfo();
  243. // 最后通过 app('json') 函数返回一个 JSON 格式的响应
  244. return app('json')->success(formToData($this->repository->editForm(['real_name' => $adminInfo->real_name, 'phone' => $adminInfo->phone,'merchant_admin_id' => $adminInfo->merchant_admin_id])));
  245. }
  246. /**
  247. * 编辑管理员密码
  248. *
  249. * @param AdminValidate $validate 验证器实例
  250. * @return \think\response\Json 返回操作结果
  251. */
  252. public function editPassword(AdminValidate $validate)
  253. {
  254. // 获取表单提交的密码和确认密码
  255. $data = $this->request->params(['pwd', 'againPassword']);
  256. // 验证密码是否符合规则
  257. $validate->isPassword()->check($data);
  258. // 判断两次输入的密码是否一致
  259. if ($data['pwd'] !== $data['againPassword'])
  260. return app('json')->fail('两次密码输入不一致');
  261. // 对密码进行加密处理
  262. $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
  263. // 删除确认密码字段
  264. unset($data['againPassword']);
  265. // 更新管理员密码
  266. $this->repository->update($this->request->adminId(), $data);
  267. // 返回操作成功结果
  268. return app('json')->success('修改密码成功');
  269. }
  270. /**
  271. * 编辑密码表单
  272. *
  273. * @return \Illuminate\Http\JsonResponse
  274. */
  275. public function editPasswordForm()
  276. {
  277. return app('json')->success(formToData($this->repository->passwordForm($this->request->adminId(), 3)));
  278. }
  279. }