c5_labsci/web/ambap/member.php
2026-01-29 21:27:08 +08:00

342 lines
11 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace web\ambap;
/**
* 依赖:全局$db对象、ciy\post、errjson/succjson、savelog、tostamp函数
*/
class member {
/**
* 获取成员列表(支持分页/筛选/字典值转换)
* @return array
*/
public static function json_get_list() {
global $db;
$post = new \ciy\post();
// 1. 初始化参数(解决未定义变量)
$page = $post->getint('page', 1); // 当前页
$pagesize = $post->getint('pagesize', 20); // 每页条数
$offset = ($page - 1) * $pagesize; // 偏移量
$mobile = trim($post->get('mobile', '')); // 手机号筛选
$name = trim($post->get('name', '')); // 姓名筛选
$status = $post->getint('status', 0); // 状态筛选
$role = $post->getint('role', 60); // 角色筛选
// 2. 构建SQL修复精简JOIN语句格式避免语法错误
$csql = new \ciy\sql("lab_user u
LEFT JOIN zc_cata s ON u.sex = s.codeid AND s.cbid = 10
LEFT JOIN zc_cata st ON u.stpstatus = st.codeid AND st.cbid = 11
LEFT JOIN zc_cata e ON u.education = e.codeid AND e.cbid = 102
LEFT JOIN zc_cata ut ON u.usertitle = ut.codeid AND ut.cbid = 12001701
LEFT JOIN zc_cata r ON u.role = r.codeid AND r.cbid = 12001703
LEFT JOIN zc_cata rr ON u.userlevel=rr.codeid AND rr.cbid=12001704");
// 配置查询字段(含字典关联中文名称)
$csql->column("u.id, u.mobile, u.name, u.usertitle, u.education, u.sex,
u.role, u.userlevel, u.stpstatus, u.addtimes, u.logintimes, u.ip,
s.name as sex_name, st.name as stpstatus_name, e.name as education_name,
ut.name as usertitle_name, r.name as role_name, rr.name as userlevel_name");
// 3. 拼接筛选条件
if (!empty($mobile)) {
$csql->where('u.mobile', $mobile);
}
if (!empty($name)) {
$csql->where('u.name', '%' . $name . '%', 'like');
}
if ($status > 0) {
$csql->where('u.stpstatus', $status);
}
if ($role > 0) {
$csql->where('u.role', $role);
}
// 4. 排序+分页(正确传参)
$csql->order('u.logintimes desc');
$csql->limit($offset, $pagesize);
// 5. 执行查询
$list = $db->get($csql);
if ($list === false) {
return errjson('查询成员列表失败: ' . $db->error);
}
// 6. 查询总数修复移除limit方法而非传0,0
$countSql = clone $csql;
$countSql->column('count(DISTINCT u.id) as total');
// 关键修复删除limit(0,0)ciy\sql默认无limit
$total = $db->get1($countSql);
// 7. 返回结果
return succjson([
'list' => $list ?: [],
'total' => $total ?: 0,
'page' => $page,
'pagesize' => $pagesize
]);
}
/**
* 获取成员详情(含字典中文名称)
* @return array
*/
public static function json_get_detail() {
global $db;
$post = new \ciy\post();
$id = $post->getint('id');
if ($id <= 0) {
return errjson('请传入有效的成员ID');
}
// 修复精简JOIN语句格式
$csql = new \ciy\sql("lab_user u
LEFT JOIN zc_cata s ON u.sex = s.codeid AND s.cbid = 10
LEFT JOIN zc_cata st ON u.stpstatus = st.codeid AND st.cbid = 11
LEFT JOIN zc_cata e ON u.education = e.codeid AND e.cbid = 102
LEFT JOIN zc_cata ut ON u.usertitle = ut.codeid AND ut.cbid = 12001701
LEFT JOIN zc_cata r ON u.role = r.codeid AND r.cbid = 12001703
LEFT JOIN zc_cata rr ON u.userlevel=rr.codeid AND rr.cbid=12001704");
$csql->column("u.*, s.name as sex_name, st.name as stpstatus_name,
e.name as education_name, ut.name as usertitle_name,
r.name as role_name, rr.name as userlevel_name");
$csql->where('u.id', $id);
$detail = $db->getone($csql);
if ($detail === false) {
return errjson('查询成员详情失败: ' . $db->error);
}
if (!is_array($detail)) {
return errjson('该成员不存在');
}
// 补充扩展信息(单表直接传构造函数)
$extSql = new \ciy\sql('ap_usr_ext');
$extSql->where('id', $id);
$extDetail = $db->getone($extSql);
if (is_array($extDetail)) {
$detail['appcid'] = $extDetail['appcid'];
}
return succjson($detail);
}
/**
* 编辑成员信息
* @return array
*/
public static function json_edit() {
global $db;
$post = new \ciy\post();
// 参数校验
$id = $post->getint('id');
$name = trim($post->get('name', ''));
$usertitle = $post->getint('usertitle', 0);
$education = $post->getint('education', 0);
$sex = $post->getint('sex', 90);
// 优化role默认值改为0避免无字典数据
$role = $post->getint('role', 0);
$userlevel = $post->getint('userlevel', 10);
if ($id <= 0) {
return errjson('请传入有效的成员ID');
}
if (empty($name)) {
return errjson('请填写成员姓名');
}
// 组装更新数据
$updata = [
'name' => $name,
'usertitle' => $usertitle,
'education' => $education,
'sex' => $sex,
'role' => $role,
'userlevel' => $userlevel
];
// 单表操作:构造函数直接传表名
$csql = new \ciy\sql('lab_user');
$csql->where('id', $id);
$res = $db->update($csql, $updata);
if ($res === false) {
savelog($db, 0, 'MEMBEREDIT', '编辑成员[' . $id . ']失败: ' . $db->error);
return errjson('编辑成员失败: ' . $db->error);
}
savelog($db, $id, 'MEMBEREDIT', '编辑成员[' . $id . ']成功');
return succjson(['msg' => '编辑成功']);
}
/**
* 禁用/启用成员
* @return array
*/
public static function json_change_status() {
global $db;
$post = new \ciy\post();
$id = $post->getint('id');
$status = $post->getint('status');
if ($id <= 0) {
return errjson('请传入有效的成员ID');
}
$updata = ['stpstatus' => $status];
$csql = new \ciy\sql('lab_user');
$csql->where('id', $id);
$res = $db->update($csql, $updata);
if ($res === false) {
$action = $status == 10 ? '启用' : '禁用';
savelog($db, 0, 'MEMBERSTATUS', $action . '成员[' . $id . ']失败: ' . $db->error);
return errjson($action . '成员失败: ' . $db->error);
}
$action = $status == 10 ? '启用' : '禁用';
savelog($db, $id, 'MEMBERSTATUS', $action . '成员[' . $id . ']成功');
return succjson(['msg' => $action . '成功']);
}
/**
* 逻辑删除成员标记deletetimes
* @return array
*/
public static function json_delete() {
global $db;
$post = new \ciy\post();
$id = $post->getint('id');
if ($id <= 0) {
return errjson('请传入有效的成员ID');
}
// 逻辑删除:标记删除时间+禁用状态
$updata = [
'stpstatus' => 99, // 99:已删除
'deletetimes' => tostamp() // 时间戳
];
$csql = new \ciy\sql('lab_user');
$csql->where('id', $id);
$res = $db->update($csql, $updata);
if ($res === false) {
savelog($db, 0, 'MEMBERDELETE', '删除成员[' . $id . ']失败: ' . $db->error);
return errjson('删除成员失败: ' . $db->error);
}
savelog($db, $id, 'MEMBERDELETE', '删除成员[' . $id . ']成功');
return succjson(['msg' => '删除成功']);
}
/**
* 重置成员密码(和登录逻辑一致)
* @return array
*/
public static function json_reset_pass() {
global $db;
global $_token; // 确保全局_token包含salt
$post = new \ciy\post();
$id = $post->getint('id');
$newPass = trim($post->get('new_pass', ''));
if ($id <= 0) {
return errjson('请传入有效的成员ID');
}
if (empty($newPass)) {
return errjson('请填写新密码');
}
// 优化:校验$_token是否存在
if (empty($_token) || empty($_token['salt'])) {
return errjson('密码加密配置异常,请联系管理员');
}
// 密码加密(和注册逻辑对齐)
$encryptPass = md5($newPass . $_token['salt']);
$updata = [
'password' => $encryptPass,
'trytime' => 0 // 重置错误尝试次数
];
$csql = new \ciy\sql('lab_user');
$csql->where('id', $id);
$res = $db->update($csql, $updata);
if ($res === false) {
savelog($db, 0, 'MEMBERRESETPASS', '重置成员[' . $id . ']密码失败: ' . $db->error);
return errjson('重置密码失败: ' . $db->error);
}
savelog($db, $id, 'MEMBERRESETPASS', '重置成员[' . $id . ']密码成功');
return succjson(['msg' => '重置密码成功']);
}
/**
* 获取字典表数据(用于前端下拉选项)
* @return array
*/
public static function json_get_cata() {
global $db;
$post = new \ciy\post();
$cbid = $post->getint('cbid');
if ($cbid <= 0) {
return errjson('请传入有效的字典分类ID');
}
$csql = new \ciy\sql('zc_cata');
$csql->where('cbid', $cbid);
$csql->order('codeid asc');
$list = $db->get($csql);
if ($list === false) {
return errjson('查询字典失败: ' . $db->error);
}
return succjson(['list' => $list ?: []]);
}
}
global $db;
$post = new \ciy\post();
$action = trim($post->get('action', ''));
$result = [];
// 根据action调用对应方法
switch ($action) {
case 'get_list':
$result = member::json_get_list();
break;
case 'get_detail':
$result = member::json_get_detail();
break;
case 'edit':
$result = member::json_edit();
break;
case 'change_status':
$result = member::json_change_status();
break;
case 'delete':
$result = member::json_delete();
break;
case 'reset_pass':
$result = member::json_reset_pass();
break;
case 'get_cata':
$result = member::json_get_cata();
break;
default:
$result = errjson('无效的接口操作: ' . $action);
}
// 输出JSON结果前端才能接收数据
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit;