model = new Personnel(); } //获取员工信息 public function info(array $where, string $field = '*') { $model = $this->model; $res = [ 'code' => 0, 'msg' => '请添加检索条件', 'data' => [] ]; if (!$where) { return $res; } if (!empty($where['id'])) { $model = $model->where('id', $where['id']); } $data = $model->field($field)->find();//员工信息 //查询用户角色校区部门信息 $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_name_arr = []; $role_key_arr = []; foreach ($role as $v) { $role_name_arr[] = $v['role_name']; $role_key_arr[] = $v['role_key']; } $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; } } 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) { $data = $data->toArray(); $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 { $data = []; $res['code'] = 0; $res['msg'] = '未找到数据'; $res['data'] = $data; } return $res; } //员工信息-修改 public function edit(array $where, array $data) { $data['update_time'] = date('Y-m-d H:i:s'); if (!$where) { return [ 'code' => 0, 'msg' => '查询条件不能为空' ]; } $model = $this->model; if (!empty($where['id'])) { $model = $model->where('id', $where['id']); } $res = $model->update($data);//员工信息 if ($res) { $res = [ 'code' => 1, 'msg' => '操作成功' ]; } else { $res = [ 'code' => 0, 'msg' => '操作失败' ]; } return $res; } //员工信息-获取全部用户 public function getAll(array $where, string $field = '*') { if (!$where) { return [ 'code' => 0, 'msg' => '查询条件不能为空' ]; } $model = $this->model; //存在员工id的时候 if ((!empty($where['personnel_id']) || isset($where['personnel_id'])) && $where['personnel_id'] !== '') { //查询这个员工的校区id $campus_id = CampusPersonRole::where('person_id', $where['personnel_id']) ->distinct(true) ->column('campus_id'); if ($campus_id) { $person_id_arr = CampusPersonRole::whereIn('campus_id', $campus_id) ->distinct(true) ->column('person_id'); if ($person_id_arr) { //根据校区id获取校区下的全部员工 $model = $model->whereIn('id', $person_id_arr); } } } if (empty($where['account_type'])) { $model = $model->where('account_type', $where['account_type']); } $res = $model->field($field) ->select() ->toArray();//员工信息 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; } /** * 获取会员的模型对象(todo 慎用!!! 现主要用于登录) * @param array $data * @return Member|array|mixed|Model !!! 仔细看,返回值是模型对象 如果想要判断是否为空 请用 $member->isEmpty() */ public function findMemberInfo(array $data) { //会员账号 if (!empty($data['username'])) $where[] = ['username', '=', $data['username']]; //会员手机号 if (!empty($data['mobile'])) $where[] = ['mobile', '=', $data['mobile']]; //会员id if (!empty($data['id'])) $where[] = ['id', '=', $data['id']]; //微信公众号openid if (!empty($data['wx_openid'])) $where[] = ['wx_openid', '=', $data['wx_openid']]; //微信小程序openid if (!empty($data['weapp_openid'])) $where[] = ['weapp_openid', '=', $data['weapp_openid']]; // 微信unionid if (!empty($data['wx_unionid'])) $where[] = ['wx_unionid', '=', $data['wx_unionid']]; if (!empty($data['username|mobile'])) $where[] = ['username|mobile', '=', $data['username|mobile']]; if (empty($where)) { $where[] = ['member_id', '=', -1]; } return $this->model->where($where)->findOrEmpty(); } }