User.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace app\admin\controller\userCenter;
  3. use app\common\controller\Backend;
  4. use think\facade\Db;
  5. class User extends Backend
  6. {
  7. protected array $noNeedLogin = ['*'];
  8. /**
  9. * 用户列表
  10. * @return void
  11. * @throws \think\db\exception\DataNotFoundException
  12. * @throws \think\db\exception\DbException
  13. * @throws \think\db\exception\ModelNotFoundException
  14. */
  15. public function index():void
  16. {
  17. $where = [];
  18. // 姓名搜索
  19. if (!empty($this->request->get('name'))) {
  20. $name = $this->request->get('name');
  21. $where[] = ['wac.name', 'like', '%' . $name . '%'];
  22. }
  23. // 邮箱搜索
  24. if (!empty($this->request->get('email'))) {
  25. $email = $this->request->get('email');
  26. $where[] = ['wac.email', 'like', '%' . $email . '%'];
  27. }
  28. // 企业搜索
  29. if (!empty($this->request->get('company'))) {
  30. $company = $this->request->get('company');
  31. $where[] = ['wac.company', 'like', '%' . $company . '%'];
  32. }
  33. // 状态筛选
  34. if (!empty($this->request->get('status'))) {
  35. $status = $this->request->get('status');
  36. $where['wac.status'] = $status;
  37. }
  38. // 排序
  39. $order = [];
  40. if (!empty($this->request->get('order'))) {
  41. $order_a = $this->request->get('order');
  42. $order_a = explode(',', $order_a);
  43. $order[$order_a[0]] = $order_a[1];
  44. } else {
  45. $order['wac.id'] = 'desc';
  46. }
  47. // 分页
  48. $page = $this->request->get('page', 1);
  49. $limit = $this->request->get('limit', 20);
  50. $userList = Db::name('user_on')
  51. ->alias('wac')
  52. ->leftJoin('company u', 'wac.company_id = u.id')
  53. ->where($where)
  54. ->field('wac.*, u.name as company_name')
  55. ->order($order)
  56. ->paginate([
  57. 'list_rows' => $limit,
  58. 'page' => $page
  59. ]);
  60. $this->success('查询成功', [
  61. 'list' => $userList->items(),
  62. 'total' => $userList->total()
  63. ]);
  64. }
  65. /**
  66. * 新增/编辑用户
  67. * @return void
  68. */
  69. public function edit():void
  70. {
  71. $id = $this->request->post('id');
  72. $name = $this->request->post('name');
  73. $email = $this->request->post('email');
  74. $password = $this->request->post('password');
  75. $company = $this->request->post('company');
  76. $company_id = $this->request->post('company_id');
  77. $status = $this->request->post('status', 1);
  78. if (empty($name) || empty($email)) {
  79. $this->error(__('姓名和邮箱不能为空'));
  80. }
  81. // 验证邮箱格式
  82. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  83. $this->error(__('邮箱格式不正确'));
  84. }
  85. // 检查邮箱是否已存在(编辑时排除自己)
  86. $checkEmail = Db::name('user_on')
  87. ->where('email', $email);
  88. if (!empty($id)) {
  89. $checkEmail->where('id', '<>', $id);
  90. }
  91. if ($checkEmail->find()) {
  92. $this->error(__('邮箱已存在'));
  93. }
  94. $data = [
  95. 'name' => $name,
  96. 'email' => $email,
  97. 'company' => $company?: null,
  98. 'company_id' => $company_id?: null,
  99. 'status' => $status,
  100. 'update_time' => date('Y-m-d H:i:s')
  101. ];
  102. // 如果有密码,进行加密
  103. if (!empty($password)) {
  104. $data['password'] = password_hash($password, PASSWORD_DEFAULT);
  105. }
  106. Db::startTrans();
  107. try {
  108. if (!empty($id)) {
  109. // 编辑
  110. $result = Db::name('user_on')
  111. ->where('id', $id)
  112. ->update($data);
  113. $message = '编辑成功';
  114. } else {
  115. // 新增
  116. $data['create_time'] = date('Y-m-d H:i:s');
  117. $result = Db::name('user_on')
  118. ->insert($data);
  119. $id = Db::name('user_on')->getLastInsID();
  120. $message = '新增成功';
  121. }
  122. Db::commit();
  123. } catch (\Exception $e) {
  124. Db::rollback();
  125. $this->error('操作失败: ' . $e->getMessage());
  126. }
  127. if ($result) {
  128. $this->success($message, ['id' => $id]);
  129. } else {
  130. $this->error(__('操作失败'));
  131. }
  132. }
  133. /**
  134. * 用户详情
  135. * @return void
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\DbException
  138. * @throws \think\db\exception\ModelNotFoundException
  139. */
  140. public function detail():void
  141. {
  142. $id = $this->request->get('id');
  143. if (empty($id)) {
  144. $this->error(__('参数错误'));
  145. }
  146. // 获取用户基础信息
  147. $userInfo = Db::name('user_on')
  148. ->where('id', $id)
  149. ->find();
  150. if (!$userInfo) {
  151. $this->error(__('用户不存在'));
  152. }
  153. // 获取用户佣金信息
  154. $commissionInfo = Db::name('user_commission')
  155. ->where('user_id', $id)
  156. ->find();
  157. if (!$commissionInfo) {
  158. // 如果没有佣金记录,创建默认记录
  159. $commissionInfo = [
  160. 'total_commission' => 0,
  161. 'current_commission' => 0,
  162. 'promotion_link' => 'xxxx.com?from=' . $id
  163. ];
  164. }
  165. // 获取用户购买的产品列表
  166. $productList = Db::name('user_product')
  167. ->alias('up')
  168. ->leftJoin('product p', 'up.product_id = p.id')
  169. ->where('up.user_id', $id)
  170. ->field('up.*, p.name as product_name, p.type as product_type')
  171. ->order('up.create_time', 'desc')
  172. ->select()
  173. ->toArray();
  174. // 计算产品状态
  175. foreach ($productList as &$product) {
  176. if (!empty($product['expire_time'])) {
  177. $product['status'] = strtotime($product['expire_time']) < time() ? '已过期' : '正常';
  178. } else {
  179. $product['status'] = '正常';
  180. }
  181. }
  182. // 获取下级员工(关联其他用户)
  183. $subordinateList = Db::name('user_relation')
  184. ->alias('ur')
  185. ->leftJoin('user_on u', 'ur.subordinate_id = u.id')
  186. ->where('ur.user_id', $id)
  187. ->field('u.name, ur.commission_amount, ur.status, ur.bind_time, ur.id as relation_id')
  188. ->select()
  189. ->toArray();
  190. $result = [
  191. 'user_info' => $userInfo,
  192. 'commission_info' => $commissionInfo,
  193. 'product_list' => $productList,
  194. 'subordinate_list' => $subordinateList
  195. ];
  196. $this->success('查询成功', $result);
  197. }
  198. /**
  199. * 删除用户
  200. * @return void
  201. */
  202. public function delete():void
  203. {
  204. $id = $this->request->post('id');
  205. if (empty($id)) {
  206. $this->error(__('参数错误'));
  207. }
  208. // 检查是否有下级员工
  209. $hasSubordinate = Db::name('user_relation')
  210. ->where('user_id', $id)
  211. ->count();
  212. if ($hasSubordinate > 0) {
  213. $this->error(__('该用户存在下级员工,请先处理下级关系'));
  214. }
  215. Db::startTrans();
  216. try {
  217. // 删除用户
  218. $result = Db::name('user_on')
  219. ->where('id', $id)
  220. ->delete();
  221. // 删除用户佣金记录
  222. Db::name('user_commission')
  223. ->where('user_id', $id)
  224. ->delete();
  225. // 删除用户产品记录
  226. Db::name('user_product')
  227. ->where('user_id', $id)
  228. ->delete();
  229. // 删除上级关系(如果该用户是别人的下级)
  230. Db::name('user_relation')
  231. ->where('subordinate_id', $id)
  232. ->delete();
  233. Db::commit();
  234. } catch (\Exception $e) {
  235. Db::rollback();
  236. $this->error('删除失败: ' . $e->getMessage());
  237. }
  238. if ($result) {
  239. $this->success('删除成功');
  240. } else {
  241. $this->error(__('删除失败'));
  242. }
  243. }
  244. /**
  245. * 解除下级关系
  246. * @return void
  247. */
  248. public function removeSubordinate():void
  249. {
  250. $relationId = $this->request->post('relation_id');
  251. if (empty($relationId)) {
  252. $this->error(__('参数错误'));
  253. }
  254. Db::startTrans();
  255. try {
  256. $result = Db::name('user_relation')
  257. ->where('id', $relationId)
  258. ->delete();
  259. Db::commit();
  260. if ($result) {
  261. $this->success('解除成功');
  262. } else {
  263. $this->error(__('解除失败'));
  264. }
  265. } catch (\Exception $e) {
  266. Db::rollback();
  267. $this->error('解除失败: ' . $e->getMessage());
  268. }
  269. }
  270. /**
  271. * 获取用户状态选项
  272. * @return void
  273. */
  274. public function statusOptions():void
  275. {
  276. $options = [
  277. ['value' => 1, 'label' => '启用'],
  278. ['value' => 2, 'label' => '禁用'],
  279. ['value' => 3, 'label' => '待审核'],
  280. ['value' => 4, 'label' => '审核拒绝']
  281. ];
  282. $this->success('获取成功', $options);
  283. }
  284. }