luosl пре 10 месеци
родитељ
комит
fb43dccf11

+ 1 - 1
.env-example

@@ -1,5 +1,5 @@
 APP_DEBUG = true
-
+APP_ENV = local
 [APP]
 DEFAULT_TIMEZONE = Asia/Shanghai
 

+ 224 - 0
app/admin/controller/company/Index.php

@@ -0,0 +1,224 @@
+<?php
+
+namespace app\admin\controller\company;
+
+use app\common\controller\Backend;
+use think\facade\Db;
+
+class Index extends  Backend
+{
+    protected array $noNeedLogin = ['*'];
+    /**
+     * 企业列表查询
+     * @return void
+     */
+    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('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);
+
+        $companyList = Db::name('company')
+            ->where($where)
+            ->order($order)
+            ->paginate([
+                'list_rows' => $limit,
+                'page' => $page
+            ]);
+
+        $this->success('查询成功', [
+            'list' => $companyList->items(),
+            'total' => $companyList->total()
+        ]);
+    }
+
+    /**
+     * 新增/编辑企业
+     * @return void
+     */
+    public function edit():void
+    {
+        $id = $this->request->post('id');
+        $name = $this->request->post('name');
+        $status = $this->request->post('status', 1);
+
+        if (empty($name)) {
+            $this->error(__('企业名称不能为空'));
+        }
+
+        // 验证企业名称是否已存在(编辑时排除自己)
+        $checkName = Db::name('company')
+            ->where('name', $name);
+
+        if (!empty($id)) {
+            $checkName->where('id', '<>', $id);
+        }
+
+        if ($checkName->find()) {
+            $this->error(__('企业名称已存在'));
+        }
+
+        $data = [
+            'name' => $name,
+            'status' => $status,
+            'update_time' => date('Y-m-d H:i:s')
+        ];
+        $adminInfo          = $this->auth->getInfo();
+        try {
+            if (!empty($id)) {
+                // 编辑
+                $result = Db::name('company')
+                    ->where('id', $id)
+                    ->update($data);
+                $message = '编辑成功';
+            } else {
+                // 新增
+                $data['creator_name'] = $adminInfo['nickname']; // 可以从登录信息获取
+                $data['create_time'] = date('Y-m-d H:i:s');
+                $result = Db::name('company')
+                    ->insert($data);
+                $id = Db::name('company')->getLastInsID();
+                $message = '新增成功';
+            }
+        } catch (\Exception $e) {
+            $this->error('操作失败: ' . $e->getMessage());
+        }
+
+        if ($result) {
+            $this->success($message, ['id' => $id]);
+        } else {
+            $this->error(__('操作失败'));
+        }
+    }
+
+    /**
+     * 删除企业
+     * @return void
+     */
+    public function delete():void
+    {
+        $id = $this->request->post('id');
+
+        if (empty($id)) {
+            $this->error(__('参数错误'));
+        }
+
+        try {
+            $result = Db::name('company')
+                ->where('id', $id)
+                ->delete();
+
+        } catch (\Exception $e) {
+            $this->error('删除失败: ' . $e->getMessage());
+        }
+
+        if ($result) {
+            $this->success('删除成功');
+        } else {
+            $this->error(__('删除失败'));
+        }
+    }
+
+    /**
+     * 禁用/启用企业
+     * @return void
+     */
+    public function toggleStatus():void
+    {
+        $id = $this->request->post('id');
+
+        if (empty($id)) {
+            $this->error(__('参数错误'));
+        }
+
+        try {
+            $companyInfo = Db::name('company')
+                ->where('id', $id)
+                ->find();
+
+            if (!$companyInfo) {
+                $this->error(__('企业不存在'));
+            }
+
+            $newStatus = $companyInfo['status'] == 1 ? 2 : 1;
+            $statusText = $newStatus == 2 ? '禁用' : '启用';
+
+            $result = Db::name('company')
+                ->where('id', $id)
+                ->update([
+                    'status' => $newStatus,
+                    'update_time' => date('Y-m-d H:i:s')
+                ]);
+
+        } catch (\Exception $e) {
+            $this->error('操作失败: ' . $e->getMessage());
+        }
+        if ($result) {
+            $this->success($statusText . '成功');
+        } else {
+            $this->error(__('操作失败'));
+        }
+    }
+
+    /**
+     * 获取企业详情
+     * @return void
+     */
+    public function detail():void
+    {
+        $id = $this->request->get('id');
+
+        if (empty($id)) {
+            $this->error(__('参数错误'));
+        }
+
+        $companyInfo = Db::name('company')
+            ->where('id', $id)
+            ->find();
+
+        if (!$companyInfo) {
+            $this->error(__('企业不存在'));
+        }
+
+        $this->success('查询成功', $companyInfo);
+    }
+
+    /**
+     * 获取企业状态选项
+     * @return void
+     */
+    public function statusOptions():void
+    {
+        $options = [
+            ['value' => 1, 'label' => '正常'],
+            ['value' => 2, 'label' => '禁用']
+        ];
+
+        $this->success('获取成功', $options);
+    }
+
+}

+ 329 - 0
app/admin/controller/user_center/User.php

@@ -0,0 +1,329 @@
+<?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);
+    }
+}

+ 8 - 0
public/.htaccess

@@ -0,0 +1,8 @@
+location ~* (runtime|application)/{
+	return 403;
+}
+location / {
+	if (!-e $request_filename){
+		rewrite  ^(.*)$  /index.php?s=$1  last;   break;
+	}
+}

+ 6 - 0
public/nginx.htaccess

@@ -0,0 +1,6 @@
+location / { 
+    if (!-e $request_filename) {
+        rewrite  ^(.*)$  /index.php?s=/$1  last;
+        break;
+    }
+}