|
@@ -6,7 +6,7 @@ use think\facade\Db;
|
|
|
|
|
|
|
|
class User extends Backend
|
|
class User extends Backend
|
|
|
{
|
|
{
|
|
|
- protected array $noNeedLogin = ['*'];
|
|
|
|
|
|
|
+// protected array $noNeedLogin = ['*'];
|
|
|
/**
|
|
/**
|
|
|
* 用户列表
|
|
* 用户列表
|
|
|
* @return void
|
|
* @return void
|
|
@@ -14,61 +14,160 @@ class User extends Backend
|
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\DbException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
*/
|
|
*/
|
|
|
- public function index():void
|
|
|
|
|
|
|
+ public function index(): void
|
|
|
{
|
|
{
|
|
|
$where = [];
|
|
$where = [];
|
|
|
|
|
|
|
|
- // 姓名搜索
|
|
|
|
|
- if (!empty($this->request->get('name'))) {
|
|
|
|
|
- $name = $this->request->get('name');
|
|
|
|
|
- $where[] = ['wac.name', 'like', '%' . $name . '%'];
|
|
|
|
|
|
|
+ // 用户名搜索
|
|
|
|
|
+ if (!empty($this->request->get('username'))) {
|
|
|
|
|
+ $username = $this->request->get('username');
|
|
|
|
|
+ $where[] = ['bu.username', 'like', '%' . $username . '%'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 昵称搜索
|
|
|
|
|
+ if (!empty($this->request->get('nickname'))) {
|
|
|
|
|
+ $nickname = $this->request->get('nickname');
|
|
|
|
|
+ $where[] = ['bu.nickname', 'like', '%' . $nickname . '%'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 邮箱搜索
|
|
// 邮箱搜索
|
|
|
if (!empty($this->request->get('email'))) {
|
|
if (!empty($this->request->get('email'))) {
|
|
|
$email = $this->request->get('email');
|
|
$email = $this->request->get('email');
|
|
|
- $where[] = ['wac.email', 'like', '%' . $email . '%'];
|
|
|
|
|
|
|
+ $where[] = ['bu.email', 'like', '%' . $email . '%'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 企业搜索
|
|
|
|
|
- if (!empty($this->request->get('company'))) {
|
|
|
|
|
- $company = $this->request->get('company');
|
|
|
|
|
- $where[] = ['wac.company', 'like', '%' . $company . '%'];
|
|
|
|
|
|
|
+ // 手机号搜索
|
|
|
|
|
+ if (!empty($this->request->get('mobile'))) {
|
|
|
|
|
+ $mobile = $this->request->get('mobile');
|
|
|
|
|
+ $where[] = ['bu.mobile', 'like', '%' . $mobile . '%'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 状态筛选
|
|
// 状态筛选
|
|
|
- if (!empty($this->request->get('status'))) {
|
|
|
|
|
|
|
+ if ($this->request->has('status') && $this->request->get('status') !== '') {
|
|
|
$status = $this->request->get('status');
|
|
$status = $this->request->get('status');
|
|
|
- $where['wac.status'] = $status;
|
|
|
|
|
|
|
+ $where[] = ['bu.status', '=', $status];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 企业搜索(通过企业成员表关联)
|
|
|
|
|
+ if (!empty($this->request->get('company'))) {
|
|
|
|
|
+ $company = $this->request->get('company');
|
|
|
|
|
+ $companyIds = Db::name('ba_company')
|
|
|
|
|
+ ->where('name', 'like', '%' . $company . '%')
|
|
|
|
|
+ ->column('id');
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($companyIds)) {
|
|
|
|
|
+ $userIds = Db::name('ba_company_member')
|
|
|
|
|
+ ->where('company_id', 'in', $companyIds)
|
|
|
|
|
+ ->column('user_id');
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($userIds)) {
|
|
|
|
|
+ $where[] = ['bu.id', 'in', $userIds];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果没有匹配的用户,返回空结果
|
|
|
|
|
+ $where[] = ['bu.id', '=', 0];
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果没有匹配的企业,返回空结果
|
|
|
|
|
+ $where[] = ['bu.id', '=', 0];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 排序
|
|
// 排序
|
|
|
$order = [];
|
|
$order = [];
|
|
|
if (!empty($this->request->get('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];
|
|
|
|
|
|
|
+ $orderParam = $this->request->get('order');
|
|
|
|
|
+ $orderArray = explode(',', $orderParam);
|
|
|
|
|
+ if (count($orderArray) === 2) {
|
|
|
|
|
+ $order[$orderArray[0]] = $orderArray[1];
|
|
|
|
|
+ }
|
|
|
} else {
|
|
} else {
|
|
|
- $order['wac.id'] = 'desc';
|
|
|
|
|
|
|
+ $order['bu.id'] = 'desc';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 分页
|
|
// 分页
|
|
|
$page = $this->request->get('page', 1);
|
|
$page = $this->request->get('page', 1);
|
|
|
$limit = $this->request->get('limit', 20);
|
|
$limit = $this->request->get('limit', 20);
|
|
|
|
|
|
|
|
- $userList = Db::name('user_on')
|
|
|
|
|
- ->alias('wac')
|
|
|
|
|
- ->leftJoin('company u', 'wac.company_id = u.id')
|
|
|
|
|
|
|
+ // 查询用户列表
|
|
|
|
|
+ $query = Db::table('ba_user')
|
|
|
|
|
+ ->alias('bu')
|
|
|
|
|
+ ->field([
|
|
|
|
|
+ 'bu.id',
|
|
|
|
|
+ 'bu.username',
|
|
|
|
|
+ 'bu.nickname',
|
|
|
|
|
+ 'bu.email',
|
|
|
|
|
+ 'bu.mobile',
|
|
|
|
|
+ 'bu.avatar',
|
|
|
|
|
+ 'bu.gender',
|
|
|
|
|
+ 'bu.status',
|
|
|
|
|
+ 'bu.create_time',
|
|
|
|
|
+ 'bu.last_login_time',
|
|
|
|
|
+ 'bu.last_login_ip',
|
|
|
|
|
+ 'bu.join_ip',
|
|
|
|
|
+ 'bu.motto',
|
|
|
|
|
+ 'bu.invitation_code',
|
|
|
|
|
+ 'GROUP_CONCAT(DISTINCT bc.name SEPARATOR ", ") as company_names',
|
|
|
|
|
+ 'GROUP_CONCAT(DISTINCT inviter.username SEPARATOR ", ") as inviter_names'
|
|
|
|
|
+ ])
|
|
|
|
|
+ ->leftJoin('ba_company_member bcm', 'bu.id = bcm.user_id')
|
|
|
|
|
+ ->leftJoin('ba_company bc', 'bcm.company_id = bc.id AND bc.status = 1')
|
|
|
|
|
+ ->leftJoin('ba_user_invitation bui', 'bu.id = bui.invitee_id')
|
|
|
|
|
+ ->leftJoin('ba_user inviter', 'bui.inviter_id = inviter.id')
|
|
|
->where($where)
|
|
->where($where)
|
|
|
- ->field('wac.*, u.name as company_name')
|
|
|
|
|
- ->order($order)
|
|
|
|
|
- ->paginate([
|
|
|
|
|
- 'list_rows' => $limit,
|
|
|
|
|
- 'page' => $page
|
|
|
|
|
- ]);
|
|
|
|
|
|
|
+ ->group('bu.id')
|
|
|
|
|
+ ->order($order);
|
|
|
|
|
+
|
|
|
|
|
+ $userList = $query->paginate([
|
|
|
|
|
+ 'list_rows' => $limit,
|
|
|
|
|
+ 'page' => $page
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 处理时间戳字段
|
|
|
|
|
+ $items = $userList->items();
|
|
|
|
|
+ foreach ($items as &$item) {
|
|
|
|
|
+ // 转换时间戳为日期时间格式
|
|
|
|
|
+ if (!empty($item['create_time'])) {
|
|
|
|
|
+ $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($item['last_login_time'])) {
|
|
|
|
|
+ $item['last_login_time'] = date('Y-m-d H:i:s', $item['last_login_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($item['join_time'])) {
|
|
|
|
|
+ $item['join_time'] = date('Y-m-d H:i:s', $item['join_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理性别显示
|
|
|
|
|
+ $genderMap = [
|
|
|
|
|
+ 0 => '未知',
|
|
|
|
|
+ 1 => '男',
|
|
|
|
|
+ 2 => '女'
|
|
|
|
|
+ ];
|
|
|
|
|
+ $item['gender_text'] = $genderMap[$item['gender']] ?? '未知';
|
|
|
|
|
+
|
|
|
|
|
+ // 处理状态显示
|
|
|
|
|
+ if ($item['status'] === 'enable') {
|
|
|
|
|
+ $item['status_text'] = '启用';
|
|
|
|
|
+ } elseif ($item['status'] === 'disable') {
|
|
|
|
|
+ $item['status_text'] = '禁用';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $item['status_text'] = $item['status'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果企业名为空,设置为空字符串
|
|
|
|
|
+ if (empty($item['company_names'])) {
|
|
|
|
|
+ $item['company_names'] = '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果邀请人为空,设置为空字符串
|
|
|
|
|
+ if (empty($item['inviter_names'])) {
|
|
|
|
|
+ $item['inviter_names'] = '';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
$this->success('查询成功', [
|
|
$this->success('查询成功', [
|
|
|
- 'list' => $userList->items(),
|
|
|
|
|
|
|
+ 'list' => $items,
|
|
|
'total' => $userList->total()
|
|
'total' => $userList->total()
|
|
|
]);
|
|
]);
|
|
|
}
|
|
}
|
|
@@ -82,15 +181,16 @@ class User extends Backend
|
|
|
public function edit():void
|
|
public function edit():void
|
|
|
{
|
|
{
|
|
|
$id = $this->request->post('id');
|
|
$id = $this->request->post('id');
|
|
|
- $name = $this->request->post('name');
|
|
|
|
|
|
|
+ $username = $this->request->post('name');
|
|
|
$email = $this->request->post('email');
|
|
$email = $this->request->post('email');
|
|
|
$password = $this->request->post('password');
|
|
$password = $this->request->post('password');
|
|
|
$company = $this->request->post('company');
|
|
$company = $this->request->post('company');
|
|
|
$company_id = $this->request->post('company_id');
|
|
$company_id = $this->request->post('company_id');
|
|
|
$status = $this->request->post('status', 1);
|
|
$status = $this->request->post('status', 1);
|
|
|
|
|
|
|
|
- if (empty($name) || empty($email)) {
|
|
|
|
|
- $this->error(__('姓名和邮箱不能为空'));
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($username) || empty($email)) {
|
|
|
|
|
+ $this->error(__('用户名和邮箱不能为空'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 验证邮箱格式
|
|
// 验证邮箱格式
|
|
@@ -98,8 +198,20 @@ class User extends Backend
|
|
|
$this->error(__('邮箱格式不正确'));
|
|
$this->error(__('邮箱格式不正确'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 检查用户名是否已存在(编辑时排除自己)
|
|
|
|
|
+ $checkUsername = Db::name('user')
|
|
|
|
|
+ ->where('username', $username);
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($id)) {
|
|
|
|
|
+ $checkUsername->where('id', '<>', $id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($checkUsername->find()) {
|
|
|
|
|
+ $this->error(__('用户名已存在'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 检查邮箱是否已存在(编辑时排除自己)
|
|
// 检查邮箱是否已存在(编辑时排除自己)
|
|
|
- $checkEmail = Db::name('user_on')
|
|
|
|
|
|
|
+ $checkEmail = Db::name('user')
|
|
|
->where('email', $email);
|
|
->where('email', $email);
|
|
|
|
|
|
|
|
if (!empty($id)) {
|
|
if (!empty($id)) {
|
|
@@ -111,12 +223,11 @@ class User extends Backend
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
$data = [
|
|
|
- 'name' => $name,
|
|
|
|
|
|
|
+ 'username' => $username,
|
|
|
|
|
+ 'nickname' => $username,
|
|
|
'email' => $email,
|
|
'email' => $email,
|
|
|
- 'company' => $company?: null,
|
|
|
|
|
- 'company_id' => $company_id?: null,
|
|
|
|
|
'status' => $status,
|
|
'status' => $status,
|
|
|
- 'update_time' => date('Y-m-d H:i:s')
|
|
|
|
|
|
|
+ 'update_time' => time()
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
// 如果有密码,进行加密
|
|
// 如果有密码,进行加密
|
|
@@ -128,19 +239,73 @@ class User extends Backend
|
|
|
try {
|
|
try {
|
|
|
if (!empty($id)) {
|
|
if (!empty($id)) {
|
|
|
// 编辑
|
|
// 编辑
|
|
|
- $result = Db::name('user_on')
|
|
|
|
|
|
|
+ $result = Db::name('user')
|
|
|
->where('id', $id)
|
|
->where('id', $id)
|
|
|
->update($data);
|
|
->update($data);
|
|
|
$message = '编辑成功';
|
|
$message = '编辑成功';
|
|
|
} else {
|
|
} else {
|
|
|
// 新增
|
|
// 新增
|
|
|
- $data['create_time'] = date('Y-m-d H:i:s');
|
|
|
|
|
- $result = Db::name('user_on')
|
|
|
|
|
|
|
+ $data['join_time'] = time();
|
|
|
|
|
+ $data['create_time'] = time();
|
|
|
|
|
+
|
|
|
|
|
+ // 生成邀请码(可选)
|
|
|
|
|
+ if (empty($data['invitation_code'])) {
|
|
|
|
|
+ $data['invitation_code'] = $this->generateInvitationCode();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $result = Db::name('user')
|
|
|
->insert($data);
|
|
->insert($data);
|
|
|
- $id = Db::name('user_on')->getLastInsID();
|
|
|
|
|
|
|
+ $id = Db::name('user')->getLastInsID();
|
|
|
$message = '新增成功';
|
|
$message = '新增成功';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 处理企业成员关系
|
|
|
|
|
+ if ($company_id > 0) {
|
|
|
|
|
+ // 检查企业是否存在
|
|
|
|
|
+ $company = Db::name('company')
|
|
|
|
|
+ ->where('id', $company_id)
|
|
|
|
|
+ ->where('status', 1)
|
|
|
|
|
+ ->find();
|
|
|
|
|
+
|
|
|
|
|
+ if ($company) {
|
|
|
|
|
+ // 检查是否已经是该企业成员
|
|
|
|
|
+ $member = Db::name('company_member')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->find();
|
|
|
|
|
+
|
|
|
|
|
+ if ($member) {
|
|
|
|
|
+ // 更新企业成员关系
|
|
|
|
|
+ Db::name('company_member')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->update([
|
|
|
|
|
+ 'company_id' => $company_id,
|
|
|
|
|
+ 'email' => $email,
|
|
|
|
|
+ 'name' =>$username,
|
|
|
|
|
+ 'update_time' => date('Y-m-d H:i:s')
|
|
|
|
|
+ ]);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 新增企业成员关系
|
|
|
|
|
+ Db::name('company_member')
|
|
|
|
|
+ ->insert([
|
|
|
|
|
+ 'company_id' => $company_id,
|
|
|
|
|
+ 'user_id' => $id,
|
|
|
|
|
+ 'email' => $email,
|
|
|
|
|
+ 'name' =>$username,
|
|
|
|
|
+ 'role' => 2, // 普通成员
|
|
|
|
|
+ 'status' => 1,
|
|
|
|
|
+ 'join_time' => date('Y-m-d H:i:s'),
|
|
|
|
|
+ 'create_time' => date('Y-m-d H:i:s'),
|
|
|
|
|
+ 'update_time' => date('Y-m-d H:i:s')
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果企业ID为空,删除企业成员关系
|
|
|
|
|
+ Db::name('company_member')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Db::commit();
|
|
Db::commit();
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
@@ -155,6 +320,31 @@ class User extends Backend
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成邀请码
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ private function generateInvitationCode(): string
|
|
|
|
|
+ {
|
|
|
|
|
+ $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
|
+ $code = '';
|
|
|
|
|
+ for ($i = 0; $i < 6; $i++) {
|
|
|
|
|
+ $code .= $characters[rand(0, strlen($characters) - 1)];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查是否已存在
|
|
|
|
|
+ $exists = Db::name('user')
|
|
|
|
|
+ ->where('invitation_code', $code)
|
|
|
|
|
+ ->find();
|
|
|
|
|
+
|
|
|
|
|
+ if ($exists) {
|
|
|
|
|
+ return $this->generateInvitationCode();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $code;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 用户详情
|
|
* 用户详情
|
|
|
* @return void
|
|
* @return void
|
|
@@ -162,7 +352,7 @@ class User extends Backend
|
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\DbException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
*/
|
|
*/
|
|
|
- public function detail():void
|
|
|
|
|
|
|
+ public function detail(): void
|
|
|
{
|
|
{
|
|
|
$id = $this->request->get('id');
|
|
$id = $this->request->get('id');
|
|
|
|
|
|
|
@@ -171,7 +361,7 @@ class User extends Backend
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 获取用户基础信息
|
|
// 获取用户基础信息
|
|
|
- $userInfo = Db::name('user_on')
|
|
|
|
|
|
|
+ $userInfo = Db::name('user')
|
|
|
->where('id', $id)
|
|
->where('id', $id)
|
|
|
->find();
|
|
->find();
|
|
|
|
|
|
|
@@ -179,6 +369,31 @@ class User extends Backend
|
|
|
$this->error(__('用户不存在'));
|
|
$this->error(__('用户不存在'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // 转换时间戳字段
|
|
|
|
|
+ if (!empty($userInfo['create_time'])) {
|
|
|
|
|
+ $userInfo['create_time_str'] = date('Y-m-d H:i:s', $userInfo['create_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!empty($userInfo['last_login_time'])) {
|
|
|
|
|
+ $userInfo['last_login_time_str'] = date('Y-m-d H:i:s', $userInfo['last_login_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!empty($userInfo['join_time'])) {
|
|
|
|
|
+ $userInfo['join_time_str'] = date('Y-m-d H:i:s', $userInfo['join_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!empty($userInfo['update_time'])) {
|
|
|
|
|
+ $userInfo['update_time_str'] = date('Y-m-d H:i:s', $userInfo['update_time']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取用户所在企业信息
|
|
|
|
|
+ $companyInfo = Db::name('company_member')
|
|
|
|
|
+ ->alias('bcm')
|
|
|
|
|
+ ->leftJoin('ba_company bc', 'bcm.company_id = bc.id')
|
|
|
|
|
+ ->where('bcm.user_id', $id)
|
|
|
|
|
+ ->field('bc.id, bc.name')
|
|
|
|
|
+ ->find();
|
|
|
|
|
+
|
|
|
|
|
+ $userInfo['company_id'] = $companyInfo['id'] ?? 0;
|
|
|
|
|
+ $userInfo['company_name'] = $companyInfo['name'] ?? '';
|
|
|
|
|
+
|
|
|
// 获取用户佣金信息
|
|
// 获取用户佣金信息
|
|
|
$commissionInfo = Db::name('user_commission')
|
|
$commissionInfo = Db::name('user_commission')
|
|
|
->where('user_id', $id)
|
|
->where('user_id', $id)
|
|
@@ -187,40 +402,105 @@ class User extends Backend
|
|
|
if (!$commissionInfo) {
|
|
if (!$commissionInfo) {
|
|
|
// 如果没有佣金记录,创建默认记录
|
|
// 如果没有佣金记录,创建默认记录
|
|
|
$commissionInfo = [
|
|
$commissionInfo = [
|
|
|
- 'total_commission' => 0,
|
|
|
|
|
- 'current_commission' => 0,
|
|
|
|
|
- 'promotion_link' => 'xxxx.com?from=' . $id
|
|
|
|
|
|
|
+ 'total_commission' => '0.00',
|
|
|
|
|
+ 'current_commission' => '0.00',
|
|
|
|
|
+ 'frozen_commission' => '0.00',
|
|
|
|
|
+ 'withdrawn_commission' => '0.00',
|
|
|
|
|
+ 'promotion_link' => $this->generatePromotionLink($id),
|
|
|
|
|
+ 'invitation_count' => 0
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 获取用户购买的产品列表
|
|
|
|
|
- $productList = Db::name('user_product')
|
|
|
|
|
|
|
+ // 获取用户购买的产品列表(包括套餐信息)
|
|
|
|
|
+ $productList = Db::name('user_product_usage')
|
|
|
->alias('up')
|
|
->alias('up')
|
|
|
- ->leftJoin('product p', 'up.product_id = p.id')
|
|
|
|
|
|
|
+ ->leftJoin('ba_product p', 'up.product_id = p.id')
|
|
|
|
|
+ ->leftJoin('ba_package_price pp', 'up.package_price_id = pp.id')
|
|
|
|
|
+ ->leftJoin('ba_package pk', 'pp.package_id = pk.id')
|
|
|
->where('up.user_id', $id)
|
|
->where('up.user_id', $id)
|
|
|
- ->field('up.*, p.name as product_name, p.type as product_type')
|
|
|
|
|
|
|
+ ->field('up.*, p.name as product_name, pp.spec_name as package_name, pp.card_type, pk.name as package_full_name')
|
|
|
->order('up.create_time', 'desc')
|
|
->order('up.create_time', 'desc')
|
|
|
->select()
|
|
->select()
|
|
|
->toArray();
|
|
->toArray();
|
|
|
|
|
|
|
|
- // 计算产品状态
|
|
|
|
|
|
|
+ // 处理产品状态和显示信息
|
|
|
foreach ($productList as &$product) {
|
|
foreach ($productList as &$product) {
|
|
|
- if (!empty($product['expire_time'])) {
|
|
|
|
|
- $product['status'] = strtotime($product['expire_time']) < time() ? '已过期' : '正常';
|
|
|
|
|
|
|
+ // 确定产品名称
|
|
|
|
|
+ if (!empty($product['package_full_name'])) {
|
|
|
|
|
+ $product['display_name'] = $product['package_full_name'] . ' (' . $product['package_name'] . ')';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $product['display_name'] = $product['product_name'] ?? '未知产品';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理卡类型显示
|
|
|
|
|
+ $cardTypeMap = [
|
|
|
|
|
+ 1 => '次卡',
|
|
|
|
|
+ 2 => '积分卡',
|
|
|
|
|
+ 3 => '日卡',
|
|
|
|
|
+ 4 => '周卡',
|
|
|
|
|
+ 5 => '季卡',
|
|
|
|
|
+ 6 => '月卡',
|
|
|
|
|
+ 7 => '年卡'
|
|
|
|
|
+ ];
|
|
|
|
|
+ $product['card_type_text'] = $cardTypeMap[$product['card_type']] ?? '未知';
|
|
|
|
|
+
|
|
|
|
|
+ // 处理购买时间
|
|
|
|
|
+ if (!empty($product['create_time'])) {
|
|
|
|
|
+ $product['buy_time'] = date('Y-m-d H:i:s', strtotime($product['create_time']));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $product['buy_time'] = '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 处理到期时间
|
|
|
|
|
+ if (!empty($product['end_time'])) {
|
|
|
|
|
+ $product['expire_time'] = date('Y-m-d', strtotime($product['end_time']));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $product['expire_time'] = '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 判断状态
|
|
|
|
|
+ $currentTime = time();
|
|
|
|
|
+ $endTime = !empty($product['end_time']) ? strtotime($product['end_time']) : 0;
|
|
|
|
|
+
|
|
|
|
|
+ if ($product['status'] == 2) {
|
|
|
|
|
+ $product['status_text'] = '已过期';
|
|
|
|
|
+ } elseif ($product['status'] == 3) {
|
|
|
|
|
+ $product['status_text'] = '已用完';
|
|
|
|
|
+ } elseif ($endTime > 0 && $endTime < $currentTime) {
|
|
|
|
|
+ $product['status_text'] = '已过期';
|
|
|
|
|
+ } elseif ($product['remaining_times'] == 0 && $product['card_type'] == 1) {
|
|
|
|
|
+ $product['status_text'] = '已用完';
|
|
|
|
|
+ } elseif ($product['remaining_points'] == 0 && $product['card_type'] == 2) {
|
|
|
|
|
+ $product['status_text'] = '已用完';
|
|
|
} else {
|
|
} else {
|
|
|
- $product['status'] = '正常';
|
|
|
|
|
|
|
+ $product['status_text'] = '正常';
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 获取下级员工(关联其他用户)
|
|
|
|
|
- $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')
|
|
|
|
|
|
|
+ // 获取下级人员(通过邀请关系)
|
|
|
|
|
+ $subordinateList = Db::name('user_invitation')
|
|
|
|
|
+ ->alias('bui')
|
|
|
|
|
+ ->leftJoin('ba_user u', 'bui.invitee_id = u.id')
|
|
|
|
|
+ ->leftJoin('ba_user_commission uc', 'u.id = uc.user_id')
|
|
|
|
|
+ ->where('bui.inviter_id', $id)
|
|
|
|
|
+ ->field('u.id, u.username, u.nickname, u.email, uc.current_commission, bui.create_time')
|
|
|
|
|
+ ->order('bui.create_time', 'desc')
|
|
|
->select()
|
|
->select()
|
|
|
->toArray();
|
|
->toArray();
|
|
|
|
|
|
|
|
|
|
+ // 处理下级人员数据
|
|
|
|
|
+ foreach ($subordinateList as &$subordinate) {
|
|
|
|
|
+ if (!empty($subordinate['create_time'])) {
|
|
|
|
|
+ $subordinate['bind_time'] = date('Y-m-d H:i:s', strtotime($subordinate['create_time']));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $subordinate['bind_time'] = '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $subordinate['name'] = $subordinate['nickname'] ?: $subordinate['username'];
|
|
|
|
|
+ $subordinate['commission_amount'] = $subordinate['current_commission'] ?? '0.00';
|
|
|
|
|
+ $subordinate['status_text'] = '正常';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$result = [
|
|
$result = [
|
|
|
'user_info' => $userInfo,
|
|
'user_info' => $userInfo,
|
|
|
'commission_info' => $commissionInfo,
|
|
'commission_info' => $commissionInfo,
|
|
@@ -235,7 +515,7 @@ class User extends Backend
|
|
|
* 删除用户
|
|
* 删除用户
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
- public function delete():void
|
|
|
|
|
|
|
+ public function delete(): void
|
|
|
{
|
|
{
|
|
|
$id = $this->request->post('id');
|
|
$id = $this->request->post('id');
|
|
|
|
|
|
|
@@ -243,9 +523,18 @@ class User extends Backend
|
|
|
$this->error(__('参数错误'));
|
|
$this->error(__('参数错误'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 检查是否有下级员工
|
|
|
|
|
- $hasSubordinate = Db::name('user_relation')
|
|
|
|
|
- ->where('user_id', $id)
|
|
|
|
|
|
|
+ // 检查是否存在
|
|
|
|
|
+ $user = Db::name('user')
|
|
|
|
|
+ ->where('id', $id)
|
|
|
|
|
+ ->find();
|
|
|
|
|
+
|
|
|
|
|
+ if (!$user) {
|
|
|
|
|
+ $this->error(__('用户不存在'));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查是否有下级员工(通过邀请关系)
|
|
|
|
|
+ $hasSubordinate = Db::name('user_invitation')
|
|
|
|
|
+ ->where('inviter_id', $id)
|
|
|
->count();
|
|
->count();
|
|
|
|
|
|
|
|
if ($hasSubordinate > 0) {
|
|
if ($hasSubordinate > 0) {
|
|
@@ -255,7 +544,7 @@ class User extends Backend
|
|
|
Db::startTrans();
|
|
Db::startTrans();
|
|
|
try {
|
|
try {
|
|
|
// 删除用户
|
|
// 删除用户
|
|
|
- $result = Db::name('user_on')
|
|
|
|
|
|
|
+ $result = Db::name('user')
|
|
|
->where('id', $id)
|
|
->where('id', $id)
|
|
|
->delete();
|
|
->delete();
|
|
|
|
|
|
|
@@ -264,14 +553,47 @@ class User extends Backend
|
|
|
->where('user_id', $id)
|
|
->where('user_id', $id)
|
|
|
->delete();
|
|
->delete();
|
|
|
|
|
|
|
|
|
|
+ // 删除用户产品使用记录
|
|
|
|
|
+ Db::name('user_product_usage')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除企业成员关系
|
|
|
|
|
+ Db::name('company_member')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除邀请关系(如果该用户是别人的下级)
|
|
|
|
|
+ Db::name('user_invitation')
|
|
|
|
|
+ ->where('invitee_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除邀请码
|
|
|
|
|
+ Db::name('invitation_code')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+
|
|
|
// 删除用户产品记录
|
|
// 删除用户产品记录
|
|
|
Db::name('user_product')
|
|
Db::name('user_product')
|
|
|
->where('user_id', $id)
|
|
->where('user_id', $id)
|
|
|
->delete();
|
|
->delete();
|
|
|
|
|
|
|
|
- // 删除上级关系(如果该用户是别人的下级)
|
|
|
|
|
|
|
+ // 删除用户关系表记录
|
|
|
Db::name('user_relation')
|
|
Db::name('user_relation')
|
|
|
- ->where('subordinate_id', $id)
|
|
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->whereOr('subordinate_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除佣金流水记录
|
|
|
|
|
+ Db::name('commission_flow')
|
|
|
|
|
+ ->where('user_id', $id)
|
|
|
|
|
+ ->whereOr('from_user_id', $id)
|
|
|
|
|
+ ->delete();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除分销记录
|
|
|
|
|
+ Db::name('distribution_record')
|
|
|
|
|
+ ->where('buyer_id', $id)
|
|
|
|
|
+ ->whereOr('distributor_id', $id)
|
|
|
->delete();
|
|
->delete();
|
|
|
|
|
|
|
|
Db::commit();
|
|
Db::commit();
|
|
@@ -280,6 +602,7 @@ class User extends Backend
|
|
|
Db::rollback();
|
|
Db::rollback();
|
|
|
$this->error('删除失败: ' . $e->getMessage());
|
|
$this->error('删除失败: ' . $e->getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
if ($result) {
|
|
if ($result) {
|
|
|
$this->success('删除成功');
|
|
$this->success('删除成功');
|
|
|
} else {
|
|
} else {
|
|
@@ -291,18 +614,20 @@ class User extends Backend
|
|
|
* 解除下级关系
|
|
* 解除下级关系
|
|
|
* @return void
|
|
* @return void
|
|
|
*/
|
|
*/
|
|
|
- public function removeSubordinate():void
|
|
|
|
|
|
|
+ public function removeSubordinate(): void
|
|
|
{
|
|
{
|
|
|
- $relationId = $this->request->post('relation_id');
|
|
|
|
|
|
|
+ $inviteeId = $this->request->post('invitee_id');
|
|
|
|
|
+ $inviterId = $this->request->post('inviter_id');
|
|
|
|
|
|
|
|
- if (empty($relationId)) {
|
|
|
|
|
|
|
+ if (empty($inviteeId) || empty($inviterId)) {
|
|
|
$this->error(__('参数错误'));
|
|
$this->error(__('参数错误'));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Db::startTrans();
|
|
Db::startTrans();
|
|
|
try {
|
|
try {
|
|
|
- $result = Db::name('user_relation')
|
|
|
|
|
- ->where('id', $relationId)
|
|
|
|
|
|
|
+ $result = Db::name('ba_user_invitation')
|
|
|
|
|
+ ->where('inviter_id', $inviterId)
|
|
|
|
|
+ ->where('invitee_id', $inviteeId)
|
|
|
->delete();
|
|
->delete();
|
|
|
|
|
|
|
|
Db::commit();
|
|
Db::commit();
|
|
@@ -333,4 +658,30 @@ class User extends Backend
|
|
|
|
|
|
|
|
$this->success('获取成功', $options);
|
|
$this->success('获取成功', $options);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成推广链接
|
|
|
|
|
+ */
|
|
|
|
|
+ private function generatePromotionLink(int $userId): string
|
|
|
|
|
+ {
|
|
|
|
|
+ // 获取用户的邀请码
|
|
|
|
|
+ $user = Db::table('ba_user')
|
|
|
|
|
+ ->where('id', $userId)
|
|
|
|
|
+ ->field('invitation_code')
|
|
|
|
|
+ ->find();
|
|
|
|
|
+
|
|
|
|
|
+ $invitationCode = $user['invitation_code'] ?? '';
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($invitationCode)) {
|
|
|
|
|
+ // 如果用户还没有邀请码,先生成一个
|
|
|
|
|
+ $invitationCode = $this->generateInvitationCode();
|
|
|
|
|
+ Db::table('ba_user')
|
|
|
|
|
+ ->where('id', $userId)
|
|
|
|
|
+ ->update(['invitation_code' => $invitationCode]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $domain = request()->domain();
|
|
|
|
|
+ return $domain . '/#/user/register?invite=' . $invitationCode;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|