model = new BorrowModel; $this->BorrowAccess = new BorrowAccess(); $this->BorrowNumber = new BorrowNumber(); $this->adminModel = $this->auth->getAdmin(); $this->BorrowTools = new BorrowTools($this->app); } public function apply(): void { if ($this->request->param('select')) { $this->select(); } $type = $this->request->get()['type']; $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); list($where, $alias, $limit, $order) = $this->queryBuilder(); if($type == 1){ if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [0,2,6]] ]); }) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "=", 6], ['purpose', '=', 0] ]); }) ->whereOr(function($query) { $query->where([ ["status", "=", 3], ['purpose', '=', 0], ['college_leader_id', '=', $this->auth->id] ]); }) ->whereOr(function($query) { $query->where([ ["status", "in", [0,2,6]], ['user_id', '=', $this->auth->id] ]); }) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [0,2,6]], ['user_id', '=', $this->auth->id] ]); }) ->order($order) ->paginate($limit); } }else if($type == 2){ if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [3,5]] ]); }) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [3,5]], ['purpose', '=', 0], ['college_leader_id', '=', $this->auth->id] ]); }) ->whereOr(function($query) { $query->where([ ["status", "in", [3,5]], ['user_id', '=', $this->auth->id] ]); }) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [3,5]], ['user_id', '=', $this->auth->id] ]); }) ->order($order) ->paginate($limit); } }else if($type == 3){ if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [1,4,7,8]] ]); }) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [1,4,7,8]], ['purpose', '=', 0], ['college_leader_id', '=', $this->auth->id] ]); }) ->whereOr(function($query) { $query->where([ ["status", "in", [1,4,7,8]], ['user_id', '=', $this->auth->id] ]); }) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->alias($alias) ->where($where) ->where(function($query) { $query->where([ ["status", "in", [1,4,7,8]], ['user_id', '=', $this->auth->id] ]); }) ->order($order) ->paginate($limit); } } $this->success('', [ 'list' => $res, 'remark' => get_route_remark(), ]); } public function Useinng(): void { if ($this->request->param('select')) { $this->select(); } $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); list($where, $alias, $limit, $order) = $this->queryBuilder(); if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->where($where) ->where("status", 'in', [3, 5]) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->where($where) ->where("status", 'in', [3, 5]) ->where('college_leader_id', $user_id) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->where($where) ->where("status", 'in', [3, 5]) ->where('user_id', $user_id) ->order($order) ->paginate($limit); } $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } public function toEnd(): void { if ($this->request->param('select')) { $this->select(); } $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); list($where, $alias, $limit, $order) = $this->queryBuilder(); if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->where($where) ->where("status", 'in', [1, 4]) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->where($where) ->where("status", 'in', [1, 4]) ->where('college_leader_id', $user_id) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->where($where) ->where("status", 'in', [1, 4]) ->where('user_id', $user_id) ->order($order) ->paginate($limit); } $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } public function consoleInformation(): void { $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); $res = [ 'damage_num' => Db::name('asset')->where("status", 2)->count(), 'asset_count' => Db::name('asset')->count(), 'asset_lent' => Db::name('asset')->where('status', 1)->count(), 'asset_value' => Db::name('asset')->sum('unit_price'), 'damage_people' => Db::name('asset')->where("status", 2)->count(), 'damage_nature' => Db::name('asset')->where("status", 2)->count(), 'apply' => $this->model->where("status", 0)->count(), 'useing' => $this->model->where("status", 2)->count(), 'overdue' => $this->model->where("status", 5)->count(), ]; $this->success('', $res); } public function edit($id = null): void { if ($this->request->isPost()) { $data = $this->request->post()['data']; $this->BorrowTools->checkRules($data); $data = $this->BorrowTools->checkText($data); //获取用户类型 $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); if ($data['status'] == 1 ) { if(!isset($data['annotation'])){ $this->error("驳回时请填写批注!"); } } if ( $data['status'] == 7 ) { if(!isset($data['college_annotation'])){ $this->error("驳回时请填写批注!"); } } if (!(array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '')) { $this->error("请检查仪器状态"); } $data = $this->excludeFields($data); $this->model->startTrans(); $result = false; //判断是否是科研类借取并判断是否需要领导审核 try { // 调整仪器的变化 $this->BorrowTools->checkAsset($data); $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid'); if ($open_id !== null) { $text = [ "openid" => $open_id, "pagepath" => "pages/login/index", "data" => [ 'character_string1' => $data['id'], 'thing5' => $this->auth->nickname, ] ]; } //判断是否是科研类借取并判断是否需要领导审核 if ($data['purpose'] == 0) { $text['data']['const3'] = '科研借单'; if ($user_group_id !== 3) { $data['approval_person'] = $this->auth->nickname; $data['approval_person_id'] = $this->auth->id; if ($data['status'] == 6) { $text['data']['const4'] = ['审批通过']; $data['status'] = 6; } else if ($data['status'] == 1) { $text['data']['const4'] = ['已驳回']; $data['status'] = 1; } try { $result = $this->model->update($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } else { $data['college_leader_id'] = $user_id; $data['college_leader'] = $this->auth->nickname; if ($data['status'] == '1') { $text['data']['const4'] = ['已驳回']; $data['status'] = 7; } else { $text['data']['const4'] = ['审批通过']; } $result = false; $this->model->startTrans(); try { $result = $this->model->update($data); if ($data['status'] == 2 && array_key_exists('asset', $data)) { // $num = count($data['asset']); foreach ($data['asset'] as $v) { for ($i = 0; $i < $v['num']; $i++) { $this->BorrowAccess->insert( [ 'asset_name' => $v['model'], 'origin' => $v['origin'], 'borrow_id' => $data['id'], 'status' => -1, ] ); } } } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } } else { $data['approval_person'] = $this->auth->nickname; $data['approval_person_id'] = $this->auth->id; $text['data']['const3'] = ['教学借单']; if ($data['status'] == 2) { $text['data']['const4'] = ['审批通过']; } else if ($data['status'] == 1) { $text['data']['const4'] = ['已驳回']; } $result = $this->model->update($data); if ($data['status'] == 2 && array_key_exists('asset', $data)) { foreach ($data['asset'] as $v) { for ($i = 0; $i < $v['num']; $i++) { $this->BorrowAccess->insert( [ 'asset_name' => $v['model'], 'origin' => $v['origin'], 'borrow_id' => $data['id'], 'status' => -1, ] ); } } } } // 提交事务 $this->model->commit(); } catch (Throwable $e) { // 回滚事务 $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { // $this->success(__('Update successful')); $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid'); if ($open_id !== null) { // halt($text); $res = self::wxResultMessage($text); if ($res === true) { $this->success(__('Update successful')); } else { $this->success("更新成功,但消息发送失败,请检查网络或联系管理员"); } }else{ $this->success('更新成功!但对方未配置公众号,消息提示失败!'); } } else { $this->error(__('No rows updated')); } } $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) { $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select(); } else { $rows = $this->BorrowAccess->field('a.*,s.student_name,a.asset_name as model')->alias('a')->leftjoin('student s', 'a.student_id = s.id')->where('borrow_id', $id)->select(); } $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find(); $row['asset'] = $rows; $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select(); $this->success('', [ 'row' => $row ]); } public function editAccessories($id = null): void { if ($this->request->isPost()) { $data = $this->request->post()['data']; //获取用户类型 $user_id = $this->auth->id; $this->model->startTrans(); $result = false; //判断是否是科研类借取并判断是否需要领导审核 try { $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid'); if ($open_id !== null) { $text = [ "openid" => $open_id, "pagepath" => "pages/login/index", "data" => [ 'character_string1' => $data['id'], 'thing5' => $this->auth->nickname, ] ]; } //判断是否是科研类借取并判断是否需要领导审核 if ($data['purpose'] == 0) { $text['data']['const3'] = '科研借单'; $text['data']['const4'] = ['需要修改配件数量']; $data['status'] = 8; try { $result = $this->model->update($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } else { $data['approval_person'] = $this->auth->nickname; $data['approval_person_id'] = $this->auth->id; $text['data']['const3'] = ['教学借单']; $text['data']['const4'] = ['需要修改配件数量']; $data['status'] = 8; try { $result = $this->model->update($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } // 提交事务 $this->model->commit(); } catch (Throwable $e) { // 回滚事务 $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { // $this->success(__('Update successful')); $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid'); if ($open_id !== null) { // halt($text); $res = self::wxResultMessage($text); if ($res === true) { $this->success(__('Update successful')); } else { $this->success("更新成功,但消息发送失败,请检查网络或联系管理员"); } }else{ $this->success('更新成功!但对方未配置公众号,消息提示失败!'); } } else { $this->error(__('No rows updated')); } } } public function changeAssetNum($id = null): void { if ($this->request->isPost()) { $data = $this->request->post()['data']; // 调整仪器的变化 if($data['status'] != 8){ $this->BorrowTools->checkRules($data); $data = $this->BorrowTools->checkText($data); $this->BorrowTools->checkAsset($data); $asset_userArr = []; $admin_idArr = []; $open_idArr = []; //获取消息推送目标人 foreach ($data['asset'] as $k => $v){ $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user'); $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id'); $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid'); if(!in_array($asset_user,$asset_userArr)){ $asset_userArr[] = $asset_user; } if(!in_array($admin_id,$admin_idArr)){ $admin_idArr[] = $admin_id; } if(!in_array($open_id,$open_idArr)){ $open_idArr[] = $open_id; } } $data['approval_person_id'] = implode(',',$admin_idArr); $data['approval_person'] = implode(',',$asset_userArr); if (!empty($open_idArr) && $open_idArr !== null ) { $text = [ "pagepath" => "pages/login/index", "data" => [ 'character_string7' => $this->model->id, 'thing4' => $data['username'], ] ]; } } //获取用户类型 $user_id = $this->auth->id; $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id"); $data = $this->excludeFields($data); $this->model->startTrans(); $result = false; //判断是否是科研类借取并判断是否需要领导审核 try { if($data['status'] == 8){ $data['status'] = 2; }else{ $data['status'] = 0; $data['annotation'] = ''; $data['college_annotation'] = ''; } //判断是否是科研类借取并判断是否需要领导审核 if ($data['purpose'] == 0) { $text['data']['const6'] = ['科研借单']; $leader = Db::name('admin')->where('switch', 1)->find(); $data['college_leader'] = $leader['nickname']; if ($user_group_id !== 3) { try { $result = $this->model->update($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } else { $data['college_leader_id'] = $user_id; $data['college_leader'] = $this->auth->nickname; if ($data['status'] == '1') { $data['status'] = 7; } $result = false; $this->model->startTrans(); try { $result = $this->model->update($data); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } } } else { $text['data']['const6'] = ['教学借单']; $result = $this->model->update($data); } Db::name('accessories')->update($data['accessories']); // 提交事务 $this->model->commit(); } catch (Throwable $e) { // 回滚事务 $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { if (!empty($open_idArr) && $open_idArr !== null && $data['status'] != 8) { foreach ($open_idArr as $k => $v){ $text['open_id'] = $open_idArr[$k]; if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) { // halt($text); $res = self::wxAuditMessage($text); } } if ($res === true) { $this->success(__('Update successful')); } else { $this->success("更新成功,但消息发送失败,请检查网络或联系管理员"); } } else { $this->success('更新成功!但对方未配置公众号,消息提示失败!'); } } else { $this->error(__('No rows were added')); } } $row = $this->model->find($id); if (!$row) { $this->error(__('Record not found')); } if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6 || $row['status'] == 7) { $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select(); } else { $rows = $this->BorrowAccess->field('a.*,s.student_name,a.asset_name as model')->alias('a')->leftjoin('student s', 'a.student_id = s.id')->where('borrow_id', $id)->select(); } $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find(); $row['asset'] = $rows; $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select(); $this->success('', [ 'row' => $row ]); } public function add(): void { if ($this->request->isPost()) { $data = $this->request->post()['data']; $this->BorrowTools->checkRules($data); $data = $this->BorrowTools->checkText($data); $asset_userArr = []; $admin_idArr = []; $open_idArr = []; //获取消息推送目标人 foreach ($data['asset'] as $k => $v){ $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user'); $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id'); $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid'); if(!in_array($asset_user,$asset_userArr)){ $asset_userArr[] = $asset_user; } if(!in_array($admin_id,$admin_idArr)){ $admin_idArr[] = $admin_id; } if(!in_array($open_id,$open_idArr)){ $open_idArr[] = $open_id; } } $data['approval_person_id'] = implode(',',$admin_idArr); $data['approval_person'] = implode(',',$asset_userArr); //校验数据 $uniID = new SnowflakeId; $data['user_id'] = $this->auth->id; $this->model->startTrans(); //尝试存储 try { $data['status'] = 0; $data['encoding'] = $uniID->generateParticle(); if ($open_idArr !== [] && $open_idArr !== null) { $text = [ "pagepath" => "pages/login/index", "data" => [ 'character_string7' => $this->model->id, 'thing4' => $data['username'] ] ]; } if ($data['purpose'] == 0) { $text['data']['const6'] = ['科研借单']; $data['college_leader'] = Db::name('admin')->where('switch',1)->value('nickname'); } else { $text['data']['const6'] = ['教学借单']; } $result = $this->model->save($data); $data['accessories']['borrow_id'] = $this->model->id; Db::name('accessories')->insert($data['accessories']); $groupAccess = []; foreach ($data['asset'] as $datum) { if ($datum['model'] !== '') { $groupAccess[] = [ 'borrow_id' => $this->model->id, 'asset_name' => $datum['model'], 'origin' => $datum['origin'], 'num' => $datum['num'] ]; } } // halt($result); $this->BorrowNumber->saveAll($groupAccess); $this->model->commit(); } catch (Throwable $e) { // 回滚事务 $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { if ($open_idArr !== [] && $open_idArr !== null) { foreach ($open_idArr as $k => $v){ $text['open_id'] = $open_idArr[$k]; if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) { // halt($text); $res = self::wxAuditMessage($text); } } if (isset($res)) { $this->success(__('Update successful')); } else { $this->success("更新成功,但消息发送失败,请检查网络或联系管理员"); } }else{ // halt(2); $this->success('更新成功!但对方未配置公众号,消息提示失败!'); } } else { // halt(3); $this->error(__('No rows were added')); } } else { $res = $this->model->where('user_id',$this->auth->id)->find(); $this->success('',$res); } } public function editPassword(): void { if ($this->request->isPost()) { $data = $this->request->param()['data']; if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } $result = $this->adminModel->resetPassword($this->auth->id, $data['_value']); if ($result !== false) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } } /** * 仪器预约待审核通知 * @param * @return void * @throws \Exception */ public function wxAuditMessage($res = null): bool { $openid = $res["open_id"]; $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU'; $data = [ 'character_string7' => ['value' => $res["data"]["character_string7"]], 'thing4' => ['value' => $res["data"]["thing4"]], 'const6' => ['value' => $res["data"]["const6"]], 'time9' => ['value' => date('Y-m-d H:i')], ]; $url = 'http://weixin.qq.com/download'; $miniprogram = [ 'appid' => 'wx58633590ab59b7a1', 'pagepath' => '' ]; $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram); return $result; } public function getProtocol(){ $data = Db::name('protocol')->where("switch",1)->value('editor'); if($data){ $this->success('', [ 'data' => $data, 'data1' => htmlspecialchars_decode($data) ]); }else{ $this->error("失败!"); } } /** * 仪器预约审核结果通知 * @param * @return void * @throws \Exception */ public function wxResultMessage($res): bool { $openid = $res["openid"]; // halt($res); $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI'; $data = [ 'character_string1' => ['value' => $res["data"]["character_string1"]], 'const3' => ['value' => [$res["data"]["const3"]]], 'const4' => ['value' => $res["data"]["const4"]], 'thing5' => ['value' => $res["data"]["thing5"]], // 'time9' => ['value' => date('Y-m-d H:i')], ]; $url = 'http://weixin.qq.com/download'; $miniprogram = [ 'appid' => 'wx58633590ab59b7a1', 'pagepath' => "pages/login/index" ]; $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram); return $result; } // public function (): void // { // if ($this->request->param('select')) { // $this->select(); // } // $user_id = $this->auth->id; // $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id"); // $res = [ // 'damage_num' => Db::name('asset')->where("status",2)->count(), // 'damage_people' => Db::name('asset')->where("status",2)->where("damage_type",1)->count(), // 'damage_nature'=> Db::name('asset')->where("status",2)->where("damage_type",0)->count(), // 'apply'=> $this->model->where("status",0)->count(), // 'useing'=> $this->model->where("status",2)->count(), // 'overdue'=> $this->model->where("status",5)->count(), // ]; // $this->success('', $res); // } }