diff --git a/niucloud/app/api/controller/apiController/Campus.php b/niucloud/app/api/controller/apiController/Campus.php new file mode 100644 index 00000000..126c41a3 --- /dev/null +++ b/niucloud/app/api/controller/apiController/Campus.php @@ -0,0 +1,42 @@ +param('personnel_id',''); + if(empty($personnel_id)){ + return fail('缺少参数'); + } + $where = [ + 'personnel_id'=>$personnel_id + ]; + $res = (new CampusService())->getAll($where); + if(!$res['code']){ + return fail($res['msg']); + } + return success($res['data']); + } +} diff --git a/niucloud/app/api/controller/apiController/CommunicationRecords.php b/niucloud/app/api/controller/apiController/CommunicationRecords.php new file mode 100644 index 00000000..4814a2e3 --- /dev/null +++ b/niucloud/app/api/controller/apiController/CommunicationRecords.php @@ -0,0 +1,49 @@ + $request->param('staff_id', ''),// + 'resource_id' => $request->param('resource_id', ''),// + 'resource_type' => $request->param('resource_type', ''),//资源类型(如设备、文件、系统等) + 'communication_type' => $request->param('communication_type', ''),// + 'communication_result' => $request->param('communication_result', ''),//沟通结果: success-成功, failure-失败, pending-待定 + 'communication_time' => $request->param('communication_time', $date),//沟通时间 + 'remarks' => $request->param('remarks', ''),//备注 + 'tag' => $request->param('tag', null),//标签:|默认null high-高, medium-中, low-低 + ]; + + $res = (new CommunicationRecordsService())->add($data); + if(!$res['code']){ + return fail('操作失败'); + } + return success('操作成功'); + } +} diff --git a/niucloud/app/api/controller/apiController/Personnel.php b/niucloud/app/api/controller/apiController/Personnel.php index b5f63cdf..96e18ff8 100644 --- a/niucloud/app/api/controller/apiController/Personnel.php +++ b/niucloud/app/api/controller/apiController/Personnel.php @@ -81,5 +81,36 @@ class Personnel extends BaseApiService return success($res); } + //验证新旧密码是否正确 + public function checkOldPwd(Request $request){ + //获取员工信息 + $old_password = $request->param('old_password',''); + if(empty($old_password)){ + return fail('请输入旧密码'); + } + $res = (new PersonnelService())->checkOldPwd($old_password); + if(!$res['code']){ + return fail('旧密码不正确'); + } + return success($res['data']); + } + + //员工端-修改登录密码 + public function edidPassword(Request $request){ + $new_password = $request->param('new_password','');//新密码 + $key_value = $request->param('key_value','');//修改密码的key_value + $phone = $request->param('phone','');//登录账号 + + if(empty($new_password) || empty($key_value)){ + return fail('缺少参数'); + } + + $res = (new PersonnelService())->edidPassword($phone,$new_password,$key_value); + if(!$res['code']){ + return fail($res['msg']); + } + return success($res['data']); + + } } diff --git a/niucloud/app/api/route/route.php b/niucloud/app/api/route/route.php index 1e0e64e6..c88b5ab9 100644 --- a/niucloud/app/api/route/route.php +++ b/niucloud/app/api/route/route.php @@ -173,6 +173,8 @@ Route::group(function () { Route::post('personnelLogin', 'login.Login/personnelLogin'); //获取字典 Route::get('common/getDictionary', 'apiController.Common/getDictionary'); + //员工端-修改密码操作 + Route::post('personnel/edidPassword', 'apiController.Personnel/edidPassword'); @@ -202,6 +204,10 @@ Route::group(function () { Route::get('personnel/info', 'apiController.Personnel/info'); //员工端-修改 Route::post('personnel/edit', 'apiController.Personnel/edit'); + //员工端-验证旧密码是否正确 + Route::post('personnel/checkOldPwd', 'apiController.Personnel/checkOldPwd'); + + //员工端-获取全部人员列表 Route::get('personnel/getPersonnelAll', 'apiController.Personnel/getPersonnelAll'); //客户资源-添加 @@ -220,6 +226,13 @@ Route::group(function () { Route::post('resourceSharing/assign', 'apiController.ResourceSharing/assign'); + //沟通记录-添加 + Route::post('communicationRecords/add', 'apiController.CommunicationRecords/add'); + + //校区-获取员工下的全部校区 + Route::get('campus/getPersonnelCampus', 'apiController.campus/getPersonnelCampus'); + + diff --git a/niucloud/app/service/api/apiService/CampusService.php b/niucloud/app/service/api/apiService/CampusService.php new file mode 100644 index 00000000..d84ede64 --- /dev/null +++ b/niucloud/app/service/api/apiService/CampusService.php @@ -0,0 +1,91 @@ +model = (new Campus()); + } + + //获取全部校区 + public function getAll(array $where){ + $res = [ + 'code'=>0, + 'msg'=>'缺少筛选条件', + 'data'=>[] + ]; + if(!$where){ + return $res; + } + + $model = $this->model; + //判断用没有员工id + if(!empty($where['personnel_id'])){ + //获取员工归属的校区id + $campus_id = $this->getPersonnelCamousId($where['personnel_id']); + if(!$campus_id){ + $res = [ + 'code'=>0, + 'msg'=>'暂无归属校区', + 'data'=>[] + ]; + return $res; + } + $model = $model->whereIn('id',$campus_id); + } + $data = $model->select()->toArray(); + + foreach ($data as &$v){ + $campus_coordinates_arr = json_decode($v['campus_coordinates'],true); + $v['campus_coordinates_arr'] = [ + 'address'=>$campus_coordinates_arr['address'] ?? '', + 'lat'=>$campus_coordinates_arr['lat'] ?? '', + 'lng'=>$campus_coordinates_arr['lng'] ?? '', + ]; + } + + $res = [ + 'code'=>1, + 'msg'=>'操作成功', + 'data'=>$data + ]; + return $res; + } + + /** + * 查询员工归属的校区id + * @param $personnel_id 员工表id + */ + private function getPersonnelCamousId($personnel_id){ + //这里要去重 + $campus_id = CampusPersonRole::where('person_id',$personnel_id) + ->distinct(true) + ->column('campus_id'); + return $campus_id; + } + + + +} diff --git a/niucloud/app/service/api/apiService/CommunicationRecordsService.php b/niucloud/app/service/api/apiService/CommunicationRecordsService.php new file mode 100644 index 00000000..a0f2986c --- /dev/null +++ b/niucloud/app/service/api/apiService/CommunicationRecordsService.php @@ -0,0 +1,89 @@ +model = (new CommunicationRecords()); + } + + //添加沟通记录 + public function add(array $data) + { + $model = $this->model; + $add = $model->create($data);//员工信息 + if ($add) { + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => [] + ]; + } else { + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + } + return $res; + } + + /** + * 对比新旧数据改变 + * @param array $old_data 旧数据 + * @param array $new_data 新数据 + * @param array $ignoreFields 忽略验证的字段|默认[] + * @return array + */ + public function compareData(array $old_data, array $new_data, array $ignoreFields = ['updated_at']) + { + $changedFields = [];//改了那些字段 + $oldChanges = [];//数据修改前的样子 + $newChanges = [];//数据修改后的样子 + + foreach ($new_data as $key => $value) { + // 如果字段在忽略列表中,则跳过 + if (in_array($key, $ignoreFields)) { + continue; + } + + if (!isset($old_data[$key]) || $old_data[$key] != $value) { + $changedFields[] = $key; + $oldChanges[$key] = $old_data[$key] ?? null; + $newChanges[$key] = $value; + } + } + + return [ + 'changed_fields' => $changedFields, + 'old_values' => $oldChanges, + 'new_values' => $newChanges, + + 'changed_fields_json' => json_encode($changedFields, JSON_UNESCAPED_UNICODE), + 'old_values_json' => json_encode($oldChanges, JSON_UNESCAPED_UNICODE), + 'new_values_json' => json_encode($newChanges, JSON_UNESCAPED_UNICODE) + ]; + } + +} diff --git a/niucloud/app/service/api/apiService/PersonnelService.php b/niucloud/app/service/api/apiService/PersonnelService.php index be3833b4..8ac1516e 100644 --- a/niucloud/app/service/api/apiService/PersonnelService.php +++ b/niucloud/app/service/api/apiService/PersonnelService.php @@ -16,10 +16,9 @@ use app\model\departments\Departments; use app\model\member\Member; use app\model\personnel\Personnel; use app\model\sys\SysRole; -use app\service\core\member\CoreMemberService; +use app\model\sys\SysUser; use core\base\BaseApiService; -use core\exception\ApiException; -use core\util\Barcode; +use think\facade\Cache; use think\Model; /** @@ -36,57 +35,105 @@ class PersonnelService extends BaseApiService } //获取员工信息 - public function info(array $where,string $field = '*'){ + public function info(array $where, string $field = '*') + { $model = $this->model; $res = [ - 'code'=>0, - 'msg'=>'请添加检索条件', - 'data'=>[] + 'code' => 0, + 'msg' => '请添加检索条件', + 'data' => [] ]; - if(!$where){ + if (!$where) { return $res; } - if(!empty($where['id'])){ - $model = $model->where('id',$where['id']); + if (!empty($where['id'])) { + $model = $model->where('id', $where['id']); } $data = $model->field($field)->find();//员工信息 - //查询部门信息 - $campus_person_role = CampusPersonRole::where('person_id',$where['id'])->select()->toArray(); + //查询用户角色校区部门信息 + $campus_person_role = CampusPersonRole::where('person_id', $where['id']) + ->with([ + 'campus' => function ($query) { + }, + 'sysRole' => function ($query) { + }, + 'departments' => function ($query) { + } + ]) + ->select()->toArray(); $role_id = array_unique(array_column($campus_person_role, 'role_id')); $dept_id = array_unique(array_column($campus_person_role, 'dept_id')); //查询用户角色 - $role = SysRole::whereIn('role_id',$role_id)->where('status',1)->field('role_id,role_name,role_key,addon_keys,status')->select()->toArray(); + $role = SysRole::whereIn('role_id', $role_id)->where('status', 1)->field('role_id,role_name,role_key,addon_keys,status')->select()->toArray(); $role_name_arr = []; $role_key_arr = []; - foreach($role as $v){ + foreach ($role as $v) { $role_name_arr[] = $v['role_name']; $role_key_arr[] = $v['role_key']; } - $role_name_str = implode(',',$role_name_arr);// + $role_name_str = implode(',', $role_name_arr);// + + //校区部门 + // 初始化新的校区-部门合并数组 和 部门名称字符串 + $cameus_dept_arr = []; + $department_name_arr = []; + foreach ($campus_person_role as $v) { + $campusId = $v['campus_id']; + $deptId = $v['dept_id']; + $deptName = $v['dept_id_name']; + + // 收集部门名称 + $department_name_arr[] = $deptName; + + // 构建校区+部门结构 + if (!isset($cameus_dept_arr[$campusId])) { + $cameus_dept_arr[$campusId] = [ + 'campus_id' => $campusId, + 'campus_id_name' => $v['campus_id_name'], + 'dept_arr' => [] + ]; + } + + // 检查去重 + $exists = false; + foreach ($cameus_dept_arr[$campusId]['dept_arr'] as $dept) { + if ($dept['dept_id'] === $deptId) { + $exists = true; + break; + } + } - //查询部门 - $department_name_arr = Departments::whereIn('id',$dept_id)->column('department_name'); - $department_name_str = implode(',',$department_name_arr); + if (!$exists) { + $cameus_dept_arr[$campusId]['dept_arr'][] = [ + 'dept_id' => $deptId, + 'dept_name' => $deptName + ]; + } + } + // 最终转换 + $cameus_dept_arr = array_values($cameus_dept_arr); + $department_name_str = implode(',', $department_name_arr); - if($data){ + if ($data) { $data = $data->toArray(); - $data['role']=$role; + $data['role'] = $role; $data['role_name_str'] = $role_name_str; $data['role_key_arr'] = $role_key_arr; $data['department_name_str'] = $department_name_str; + $data['cameus_dept_arr'] = $cameus_dept_arr;//校区+部门的组合 $res['code'] = 1; $res['msg'] = '操作成功'; $res['data'] = $data; - }else{ + } else { $data = []; $res['code'] = 0; - $res['msg'] = '为找到数据'; + $res['msg'] = '未找到数据'; $res['data'] = $data; } @@ -95,31 +142,32 @@ class PersonnelService extends BaseApiService } //员工信息-修改 - public function edit(array $where,array $data){ + public function edit(array $where, array $data) + { $data['update_time'] = date('Y-m-d H:i:s'); - if(!$where){ + if (!$where) { return [ - 'code'=>0, - 'msg'=>'查询条件不能为空' + 'code' => 0, + 'msg' => '查询条件不能为空' ]; } $model = $this->model; - if(!empty($where['id'])){ - $model = $model->where('id',$where['id']); + if (!empty($where['id'])) { + $model = $model->where('id', $where['id']); } $res = $model->update($data);//员工信息 - if($res){ + if ($res) { $res = [ - 'code'=>1, - 'msg'=>'操作成功' + 'code' => 1, + 'msg' => '操作成功' ]; - }else{ + } else { $res = [ - 'code'=>0, - 'msg'=>'操作失败' + 'code' => 0, + 'msg' => '操作失败' ]; } return $res; @@ -127,7 +175,7 @@ class PersonnelService extends BaseApiService } //员工信息-获取全部用户 - public function getAll(array $where,string $field = '*') + public function getAll(array $where, string $field = '*') { if (!$where) { return [ @@ -165,7 +213,96 @@ class PersonnelService extends BaseApiService return $res; } + //验证旧密码 + public function checkOldPwd(string $old_passowrd) + { + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + $personnel_id = $this->member_id; + $phone = $this->model->where('id', $personnel_id)->value('phone'); + $password = (new SysUser())->where('username', $phone)->value('password'); + if (!check_password($old_passowrd, $password)) { + $res['msg'] = '旧密码错误'; + return $res; + } + $res['code'] = 1; + $res['msg'] = '密码正确'; + $res['data'] = [ + 'key_value' => $this->setEditPasswordKey($phone) + ]; + return $res; + } + + //设置新密码 + public function edidPassword($phone, $new_password, $key_value) + { + $checkEditPasswordKey = $this->checkEditPasswordKey($phone, $key_value);//验证修改密码允许的缓存key + if (!$checkEditPasswordKey['code']) { + return $checkEditPasswordKey; + } + //查询用户修改密码 + $update = (new SysUser())->where('username', $phone)->update([ + 'password' => create_password($new_password),//创建密码 + 'update_time' => time(), + ]); + if (!$update) { + $res = [ + 'code' => 0, + 'msg' => '操作失败', + 'data' => [] + ]; + } else { + $res = [ + 'code' => 1, + 'msg' => '操作成功', + 'data' => [] + ]; + } + return $res; + } + + + //生成修改密码允许的缓存key + public function setEditPasswordKey(string $phone) + { + $key_name = 'edit_password_' . $phone; + //生成字符串,存入cache中 + //check_password()//验证 + //create_password()//创建 + + $key_value = create_password($key_name); + // 缓存在3600秒之后过期 + Cache::set($key_name, $key_value, 3600); + return $key_value; + } + + //验证修改密码允许的缓存key + public function checkEditPasswordKey(string $phone, string $key_value) + { + $res = [ + 'code' => 0, + 'msg' => '' + ]; + $key_name = 'edit_password_' . $phone; + $key_value_cache = Cache::get($key_name); + if (empty($key_value_cache)) { + $res['msg'] = '参数已过期,请重新输入旧密码进行验证'; + return $res; + } + + //验证 + if ($key_value_cache != $key_value) { + $res['msg'] = '参数不正确,请重新输入旧密码进行验证'; + return $res; + } + $res['code'] = 1; + $res['msg'] = '操作成功'; + return $res; + } /** @@ -173,28 +310,29 @@ class PersonnelService extends BaseApiService * @param array $data * @return Member|array|mixed|Model !!! 仔细看,返回值是模型对象 如果想要判断是否为空 请用 $member->isEmpty() */ - public function findMemberInfo(array $data){ + public function findMemberInfo(array $data) + { //会员账号 - if(!empty($data['username'])) + if (!empty($data['username'])) $where[] = ['username', '=', $data['username']]; //会员手机号 - if(!empty($data['mobile'])) + if (!empty($data['mobile'])) $where[] = ['mobile', '=', $data['mobile']]; //会员id - if(!empty($data['id'])) + if (!empty($data['id'])) $where[] = ['id', '=', $data['id']]; //微信公众号openid - if(!empty($data['wx_openid'])) + if (!empty($data['wx_openid'])) $where[] = ['wx_openid', '=', $data['wx_openid']]; //微信小程序openid - if(!empty($data['weapp_openid'])) + if (!empty($data['weapp_openid'])) $where[] = ['weapp_openid', '=', $data['weapp_openid']]; // 微信unionid - if(!empty($data['wx_unionid'])) + if (!empty($data['wx_unionid'])) $where[] = ['wx_unionid', '=', $data['wx_unionid']]; - if(!empty($data['username|mobile'])) + if (!empty($data['username|mobile'])) $where[] = ['username|mobile', '=', $data['username|mobile']]; - if(empty($where)){ + if (empty($where)) { $where[] = ['member_id', '=', -1]; } return $this->model->where($where)->findOrEmpty();