StoreAdmin.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\controller\store\system;
  12. use think\facade\App;
  13. use app\controller\store\AuthController;
  14. use app\services\store\SystemStoreStaffServices;
  15. /**
  16. * Class StoreAdmin
  17. * @package app\controller\store\system
  18. */
  19. class StoreAdmin extends AuthController
  20. {
  21. /**
  22. * SystemRole constructor.
  23. * @param App $app
  24. * @param SystemStoreStaffServices $services
  25. */
  26. public function __construct(App $app, SystemStoreStaffServices $services)
  27. {
  28. parent::__construct($app);
  29. $this->services = $services;
  30. }
  31. /**
  32. * 显示管理员资源列表
  33. * @return mixed
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function index()
  39. {
  40. $where = $this->request->getMore([
  41. ['name', ''],
  42. ['roles', ''],
  43. ['is_del', 0],
  44. ['status', '']
  45. ]);
  46. $where['level'] = $this->storeStaffInfo['level'] + 1;
  47. $where['store_id'] = $this->storeId;
  48. $where['is_admin'] = 1;
  49. return app('json')->success($this->services->getStoreAdminList($where));
  50. }
  51. /**
  52. * 创建表单
  53. * @return mixed
  54. * @throws \FormBuilder\Exception\FormBuilderException
  55. */
  56. public function create()
  57. {
  58. return app('json')->success($this->services->createStoreAdminForm((int)$this->storeId, $this->storeStaffInfo['level'] + 1));
  59. }
  60. /**
  61. * 保存管理员
  62. * @return mixed
  63. */
  64. public function save()
  65. {
  66. $data = $this->request->postMore([
  67. ['account', ''],
  68. ['avatar', ''],
  69. ['phone', ''],
  70. ['conf_pwd', ''],
  71. ['pwd', ''],
  72. ['staff_name', ''],
  73. ['roles', []],
  74. ['status', 0],
  75. ]);
  76. $this->validate($data, \app\validate\store\StoreAdminValidate::class);
  77. $data['level'] = $this->storeStaffInfo['level'] + 1;
  78. if ($data['conf_pwd'] != $data['pwd']) {
  79. return app('json')->fail('两次输入的密码不相同');
  80. }
  81. if (!check_phone($data['phone'])) {
  82. return app('json')->fail('请输入正确的手机号');
  83. }
  84. if ($this->services->count(['store_id' => $this->storeId, 'account' => $data['account'], 'is_del' => 0])) {
  85. return app('json')->fail('该账号已经存在');
  86. }
  87. $admin = $this->services->getOne(['store_id' => $this->storeId, 'phone' => $data['phone'], 'is_del' => 0]);
  88. if ($admin && $admin['is_admin']) {
  89. return app('json')->fail('该手机号已经存在');
  90. }
  91. $data['store_id'] = $this->storeId;
  92. $data['is_admin'] = 1;
  93. $data['is_cashier'] = 1;
  94. unset($data['conf_pwd']);
  95. $data['pwd'] = $this->services->passwordHash($data['pwd']);
  96. $data['add_time'] = time();
  97. $data['roles'] = implode(',', $data['roles']);
  98. if ($admin) {//修改
  99. $res = $this->services->update($admin['id'], $data);
  100. } else {
  101. $res = $this->services->save($data);
  102. }
  103. if ($res) {
  104. return app('json')->success('添加成功');
  105. } else {
  106. app('json')->fail('添加失败');
  107. }
  108. }
  109. /**
  110. * 显示编辑资源表单页.
  111. *
  112. * @param int $id
  113. * @return \think\Response
  114. */
  115. public function edit($id)
  116. {
  117. if (!$id) {
  118. return $this->fail('管理员信息读取失败');
  119. }
  120. return app('json')->success($this->services->updateStoreAdminForm((int)$id, $this->storeStaffInfo['level'] + 1));
  121. }
  122. /**
  123. * 修改管理员信息
  124. * @param $id
  125. * @return mixed
  126. */
  127. public function update($id)
  128. {
  129. $data = $this->request->postMore([
  130. ['account', ''],
  131. ['avatar', ''],
  132. ['phone', ''],
  133. ['conf_pwd', ''],
  134. ['pwd', ''],
  135. ['staff_name', ''],
  136. ['roles', []],
  137. ['status', 0],
  138. ]);
  139. $this->validate($data, \app\validate\store\StoreAdminValidate::class, 'update');
  140. if (!check_phone($data['phone'])) {
  141. return app('json')->fail('请输入正确的手机号');
  142. }
  143. $storeAdmin = $this->services->get(['store_id' => $this->storeId, 'account' => $data['account'], 'is_del' => 0]);
  144. if ($storeAdmin && $storeAdmin['id'] != $id) {
  145. return app('json')->fail('该账号已经存在');
  146. }
  147. $storeAdmin = $this->services->getOne(['store_id' => $this->storeId, 'phone' => $data['phone'], 'is_del' => 0]);
  148. if ($storeAdmin && $storeAdmin['is_admin'] && $storeAdmin['id'] != $id) {
  149. return app('json')->fail('该手机号已经存在');
  150. }
  151. if ($data['pwd']) {
  152. if (!$data['conf_pwd']) {
  153. return $this->fail('请输入确认密码');
  154. }
  155. if ($data['pwd'] != $data['conf_pwd']) {
  156. return $this->fail('两次输入的密码不一致');
  157. }
  158. $data['pwd'] = $this->services->passwordHash($data['pwd']);
  159. } else {
  160. unset($data['pwd']);
  161. }
  162. unset($data['conf_pwd']);
  163. $data['is_admin'] = 1;
  164. if ($this->services->update((int)$id, $data)) {
  165. return app('json')->success('修改成功');
  166. } else {
  167. return $this->fail('修改失败');
  168. }
  169. }
  170. /**
  171. * 删除管理员
  172. * @param $id
  173. * @return mixed
  174. */
  175. public function delete($id)
  176. {
  177. if (!$id) return $this->fail('删除失败,缺少参数');
  178. $admin = $this->services->getStaffInfo((int)$id);
  179. if (!$admin['level']) {
  180. return app('json')->fail('门店超级管理员账号不能删除');
  181. }
  182. if ($this->services->update((int)$id, ['is_del' => 1, 'status' => 0]))
  183. return app('json')->success('删除成功!');
  184. else
  185. return $this->fail('删除失败');
  186. }
  187. /**
  188. * 修改状态
  189. * @param $id
  190. * @param $status
  191. * @return mixed
  192. */
  193. public function set_status($id, $status)
  194. {
  195. $this->services->update((int)$id, ['status' => $status]);
  196. return app('json')->success($status == 0 ? '关闭成功' : '开启成功');
  197. }
  198. /**
  199. * 获取当前登陆门店管理员的信息
  200. * @return mixed
  201. */
  202. public function info()
  203. {
  204. return app('json')->success($this->storeStaffInfo);
  205. }
  206. }