| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace app\admin\controller\user_center;
- use app\common\controller\Backend;
- use think\facade\Db;
- class User extends Backend
- {
- protected array $noNeedLogin = ['*'];
- /**
- * 用户列表
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function index():void
- {
- $where = [];
- // 姓名搜索
- if (!empty($this->request->get('name'))) {
- $name = $this->request->get('name');
- $where[] = ['name', 'like', '%' . $name . '%'];
- }
- // 邮箱搜索
- if (!empty($this->request->get('email'))) {
- $email = $this->request->get('email');
- $where[] = ['email', 'like', '%' . $email . '%'];
- }
- // 企业搜索
- if (!empty($this->request->get('company'))) {
- $company = $this->request->get('company');
- $where[] = ['company', 'like', '%' . $company . '%'];
- }
- // 状态筛选
- if (!empty($this->request->get('status'))) {
- $status = $this->request->get('status');
- $where['status'] = $status;
- }
- // 排序
- $order = [];
- if (!empty($this->request->get('order'))) {
- $order_a = $this->request->get('order');
- $order_a = explode(',', $order_a);
- $order[$order_a[0]] = $order_a[1];
- } else {
- $order['id'] = 'desc';
- }
- // 分页
- $page = $this->request->get('page', 1);
- $limit = $this->request->get('limit', 20);
- $userList = Db::name('user_on')
- ->where($where)
- ->order($order)
- ->paginate([
- 'list_rows' => $limit,
- 'page' => $page
- ]);
- $this->success('查询成功', [
- 'list' => $userList->items(),
- 'total' => $userList->total()
- ]);
- }
- /**
- * 新增/编辑用户
- * @return void
- */
- public function edit():void
- {
- $id = $this->request->post('id');
- $name = $this->request->post('name');
- $email = $this->request->post('email');
- $password = $this->request->post('password');
- $company = $this->request->post('company');
- $status = $this->request->post('status', 1);
- if (empty($name) || empty($email)) {
- $this->error(__('姓名和邮箱不能为空'));
- }
- // 验证邮箱格式
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $this->error(__('邮箱格式不正确'));
- }
- // 检查邮箱是否已存在(编辑时排除自己)
- $checkEmail = Db::name('user_on')
- ->where('email', $email);
- if (!empty($id)) {
- $checkEmail->where('id', '<>', $id);
- }
- if ($checkEmail->find()) {
- $this->error(__('邮箱已存在'));
- }
- $data = [
- 'name' => $name,
- 'email' => $email,
- 'company' => $company ?: null,
- 'status' => $status,
- 'update_time' => date('Y-m-d H:i:s')
- ];
- // 如果有密码,进行加密
- if (!empty($password)) {
- $data['password'] = password_hash($password, PASSWORD_DEFAULT);
- }
- Db::startTrans();
- try {
- if (!empty($id)) {
- // 编辑
- $result = Db::name('user_on')
- ->where('id', $id)
- ->update($data);
- $message = '编辑成功';
- } else {
- // 新增
- $data['create_time'] = date('Y-m-d H:i:s');
- $result = Db::name('user_on')
- ->insert($data);
- $id = Db::name('user_on')->getLastInsID();
- $message = '新增成功';
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('操作失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success($message, ['id' => $id]);
- } else {
- $this->error(__('操作失败'));
- }
- }
- /**
- * 用户详情
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function detail():void
- {
- $id = $this->request->get('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- // 获取用户基础信息
- $userInfo = Db::name('user_on')
- ->where('id', $id)
- ->find();
- if (!$userInfo) {
- $this->error(__('用户不存在'));
- }
- // 获取用户佣金信息
- $commissionInfo = Db::name('user_commission')
- ->where('user_id', $id)
- ->find();
- if (!$commissionInfo) {
- // 如果没有佣金记录,创建默认记录
- $commissionInfo = [
- 'total_commission' => 0,
- 'current_commission' => 0,
- 'promotion_link' => 'xxxx.com?from=' . $id
- ];
- }
- // 获取用户购买的产品列表
- $productList = Db::name('user_product')
- ->alias('up')
- ->leftJoin('product p', 'up.product_id = p.id')
- ->where('up.user_id', $id)
- ->field('up.*, p.name as product_name, p.type as product_type')
- ->order('up.create_time', 'desc')
- ->select()
- ->toArray();
- // 计算产品状态
- foreach ($productList as &$product) {
- if (!empty($product['expire_time'])) {
- $product['status'] = strtotime($product['expire_time']) < time() ? '已过期' : '正常';
- } else {
- $product['status'] = '正常';
- }
- }
- // 获取下级员工(关联其他用户)
- $subordinateList = Db::name('user_relation')
- ->alias('ur')
- ->leftJoin('user_on u', 'ur.subordinate_id = u.id')
- ->where('ur.user_id', $id)
- ->field('u.name, ur.commission_amount, ur.status, ur.bind_time, ur.id as relation_id')
- ->select()
- ->toArray();
- $result = [
- 'user_info' => $userInfo,
- 'commission_info' => $commissionInfo,
- 'product_list' => $productList,
- 'subordinate_list' => $subordinateList
- ];
- $this->success('查询成功', $result);
- }
- /**
- * 删除用户
- * @return void
- */
- public function delete():void
- {
- $id = $this->request->post('id');
- if (empty($id)) {
- $this->error(__('参数错误'));
- }
- // 检查是否有下级员工
- $hasSubordinate = Db::name('user_relation')
- ->where('user_id', $id)
- ->count();
- if ($hasSubordinate > 0) {
- $this->error(__('该用户存在下级员工,请先处理下级关系'));
- }
- Db::startTrans();
- try {
- // 删除用户
- $result = Db::name('user_on')
- ->where('id', $id)
- ->delete();
- // 删除用户佣金记录
- Db::name('user_commission')
- ->where('user_id', $id)
- ->delete();
- // 删除用户产品记录
- Db::name('user_product')
- ->where('user_id', $id)
- ->delete();
- // 删除上级关系(如果该用户是别人的下级)
- Db::name('user_relation')
- ->where('subordinate_id', $id)
- ->delete();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('删除失败: ' . $e->getMessage());
- }
- if ($result) {
- $this->success('删除成功');
- } else {
- $this->error(__('删除失败'));
- }
- }
- /**
- * 解除下级关系
- * @return void
- */
- public function removeSubordinate():void
- {
- $relationId = $this->request->post('relation_id');
- if (empty($relationId)) {
- $this->error(__('参数错误'));
- }
- Db::startTrans();
- try {
- $result = Db::name('user_relation')
- ->where('id', $relationId)
- ->delete();
- Db::commit();
- if ($result) {
- $this->success('解除成功');
- } else {
- $this->error(__('解除失败'));
- }
- } catch (\Exception $e) {
- Db::rollback();
- $this->error('解除失败: ' . $e->getMessage());
- }
- }
- /**
- * 获取用户状态选项
- * @return void
- */
- public function statusOptions():void
- {
- $options = [
- ['value' => 1, 'label' => '启用'],
- ['value' => 2, 'label' => '禁用'],
- ['value' => 3, 'label' => '待审核'],
- ['value' => 4, 'label' => '审核拒绝']
- ];
- $this->success('获取成功', $options);
- }
- }
|