248 lines
7.7 KiB
PHP
248 lines
7.7 KiB
PHP
<?php
|
|
namespace web\ambap;
|
|
|
|
class member {
|
|
// 接口入口:通过 act 参数路由到不同方法
|
|
public static function index() {
|
|
$act = $_REQUEST['act'] ?? '';
|
|
switch($act) {
|
|
case 'member.list':
|
|
self::json_list();
|
|
break;
|
|
case 'member.add':
|
|
self::json_add();
|
|
break;
|
|
case 'member.edit':
|
|
self::json_edit();
|
|
break;
|
|
case 'member.detail':
|
|
self::json_detail();
|
|
break;
|
|
case 'member.del':
|
|
self::json_del();
|
|
break;
|
|
case 'member.audit':
|
|
self::json_audit();
|
|
break;
|
|
default:
|
|
echo json_encode(['code' => 0, 'errmsg' => '无效的接口动作']);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 1. 获取成员列表(适配 ciy\db 类)
|
|
public static function json_list() {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
$page = $post->getint('page', 1);
|
|
$pageSize = $post->getint('pageSize', 15);
|
|
$offset = ($page - 1) * $pageSize;
|
|
|
|
// 构建查询条件
|
|
$csql = new \ciy\sql('lab_user');
|
|
$csql->limit($offset, $pageSize); // 分页:偏移量、每页条数
|
|
$csql->order('addtimes DESC');
|
|
|
|
// 核心修改:使用 ciy\db 的 get 方法获取列表+总数
|
|
$total = -1; // 传入 -1 自动查询总数
|
|
$list = $db->get($csql, $total); // $total 会被赋值为总条数
|
|
|
|
// 处理查询失败
|
|
if ($list === false) {
|
|
return self::err('获取列表失败:' . $db->error);
|
|
}
|
|
|
|
// 格式化返回数据
|
|
$retList = [];
|
|
foreach ($list as $item) {
|
|
$retList[] = [
|
|
'id' => $item['id'],
|
|
'name' => $item['name'],
|
|
'mobile' => $item['mobile'],
|
|
'usertitle' => $item['usertitle'],
|
|
'status' => $item['stpstatus'] == 10 ? 1 : 2, // 1=在册 2=历史
|
|
'sex' => $item['sex'],
|
|
'email' => $item['email'],
|
|
'avatar' => $item['avatar'] ?? '',
|
|
'isLeader' => $item['usertitle'] == 1, // 是否负责人
|
|
'addtimes' => $item['addtimes']
|
|
];
|
|
}
|
|
|
|
echo json_encode([
|
|
'code' => 1,
|
|
'list' => $retList,
|
|
'total' => $total
|
|
]);
|
|
}
|
|
|
|
// 2. 新增成员
|
|
public static function json_add() {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
|
|
// 获取表单数据
|
|
$name = $post->get('name');
|
|
$mobile = $post->get('mobile');
|
|
$usertitle = $post->getint('usertitle');
|
|
$status = $post->getint('status');
|
|
$sex = $post->getint('sex');
|
|
$email = $post->get('email');
|
|
$password = $post->get('password');
|
|
|
|
// 基础验证
|
|
if (empty($name)) return self::err('请输入姓名');
|
|
if (empty($mobile)) return self::err('请输入手机号');
|
|
if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) return self::err('手机号格式错误');
|
|
if (empty($password)) return self::err('请设置密码');
|
|
|
|
// 检查手机号是否已注册
|
|
$csql = new \ciy\sql('lab_user');
|
|
$csql->where('mobile', $mobile);
|
|
$exist = $db->getone($csql);
|
|
if (is_array($exist)) return self::err('该手机号已注册');
|
|
|
|
// 组装数据
|
|
$data = [
|
|
'name' => $name,
|
|
'mobile' => $mobile,
|
|
'usertitle' => $usertitle,
|
|
'stpstatus' => $status == 1 ? 10 : 20, // 10=正常(在册) 20=禁用(历史)
|
|
'sex' => $sex,
|
|
'email' => $email,
|
|
'password' => $password, // 前端已加密
|
|
'userlevel' => 10, // 默认等级
|
|
'trytime' => 0,
|
|
'logintimes' => tostamp(),
|
|
'addtimes' => tostamp(),
|
|
'ip' => getip(),
|
|
'laborgid' => 0,
|
|
'sn' => '',
|
|
'totalpnt' => 0,
|
|
'dvotecnt' => 0
|
|
];
|
|
|
|
// 插入数据(使用 ciy\db 的 insert 方法)
|
|
$csql = new \ciy\sql('lab_user');
|
|
$result = $db->insert($csql, $data);
|
|
if ($result === false) {
|
|
return self::err('新增失败:' . $db->error);
|
|
}
|
|
|
|
echo json_encode(['code' => 1, 'msg' => '新增成功']);
|
|
}
|
|
|
|
// 3. 编辑成员
|
|
public static function json_edit() {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
|
|
$id = $post->getint('id');
|
|
$name = $post->get('name');
|
|
$usertitle = $post->getint('usertitle');
|
|
$status = $post->getint('status');
|
|
$sex = $post->getint('sex');
|
|
$email = $post->get('email');
|
|
|
|
if (empty($id)) return self::err('参数错误');
|
|
if (empty($name)) return self::err('请输入姓名');
|
|
|
|
// 组装更新数据
|
|
$data = [
|
|
'name' => $name,
|
|
'usertitle' => $usertitle,
|
|
'stpstatus' => $status == 1 ? 10 : 20,
|
|
'sex' => $sex,
|
|
'email' => $email,
|
|
'updatetime' => tostamp()
|
|
];
|
|
|
|
// 更新数据(使用 ciy\db 的 update 方法)
|
|
$csql = new \ciy\sql('lab_user');
|
|
$csql->where('id', $id);
|
|
$result = $db->update($csql, $data);
|
|
if ($result === false) {
|
|
return self::err('修改失败:' . $db->error);
|
|
}
|
|
|
|
echo json_encode(['code' => 1, 'msg' => '修改成功']);
|
|
}
|
|
|
|
// 4. 获取成员详情
|
|
public static function json_detail() {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
$id = $post->getint('id');
|
|
|
|
if (empty($id)) return self::err('参数错误');
|
|
|
|
// 查询详情(使用 ciy\db 的 getone 方法)
|
|
$csql = new \ciy\sql('lab_user');
|
|
$csql->where('id', $id);
|
|
$item = $db->getone($csql);
|
|
|
|
if (!is_array($item)) return self::err('成员不存在');
|
|
|
|
// 格式化返回数据
|
|
$data = [
|
|
'id' => $item['id'],
|
|
'name' => $item['name'],
|
|
'mobile' => $item['mobile'],
|
|
'usertitle' => $item['usertitle'],
|
|
'status' => $item['stpstatus'] == 10 ? 1 : 2,
|
|
'sex' => $item['sex'],
|
|
'email' => $item['email']
|
|
];
|
|
|
|
echo json_encode(['code' => 1, 'data' => $data]);
|
|
}
|
|
|
|
// 5. 删除成员
|
|
public static function json_del() {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
$id = $post->getint('id');
|
|
|
|
if (empty($id)) return self::err('参数错误');
|
|
|
|
// 删除数据(使用 ciy\db 的 delete 方法)
|
|
$csql = new \ciy\sql('lab_user');
|
|
$csql->where('id', $id);
|
|
$result = $db->delete($csql);
|
|
if ($result === false) {
|
|
return self::err('删除失败:' . $db->error);
|
|
}
|
|
|
|
echo json_encode(['code' => 1, 'msg' => '删除成功']);
|
|
}
|
|
|
|
// 6. 审核成员
|
|
public static function json_audit() {
|
|
global $db;
|
|
$post = new \ciy\post();
|
|
$id = $post->getint('id');
|
|
$status = $post->getint('status');
|
|
|
|
if (empty($id)) return self::err('参数错误');
|
|
|
|
$data = ['stpstatus' => $status == 10 ? 10 : 20];
|
|
$csql = new \ciy\sql('lab_user');
|
|
$csql->where('id', $id);
|
|
$result = $db->update($csql, $data);
|
|
if ($result === false) {
|
|
return self::err('审核失败:' . $db->error);
|
|
}
|
|
|
|
echo json_encode(['code' => 1, 'msg' => '审核成功']);
|
|
}
|
|
|
|
// 通用错误返回
|
|
private static function err($msg) {
|
|
echo json_encode(['code' => 0, 'errmsg' => $msg]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// 执行入口
|
|
member::index();
|
|
?>
|