User.php 9.1 KB

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