model = new BorrowModel; $this->BorrowAccess = new BorrowAccess(); $this->BorrowNumber = new BorrowNumber(); $this->BorrowTools = new BorrowTools($this->app); } public function index(): 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(); // halt($where, $alias, $limit, $order); if ($user_group_id == 1 || $user_group_id == 2) { $res = $this->model ->alias($alias) ->where($where) ->where("status", 'in', [0, 1, 6,7]) ->where("user_id",$user_id) ->order($order) ->paginate($limit); } else if ($user_group_id == 3) { $res = $this->model ->alias($alias) ->where($where) ->where("status", 'in', [0,1, 6,7]) ->where('purpose', 0) ->where("user_id",$user_id) ->order($order) ->paginate($limit); } else if ($user_group_id == 4) { $res = $this->model ->alias($alias) ->where($where) ->where('user_id', $user_id) ->order($order) ->paginate($limit); } $this->success('', [ 'list' => $res->items(), 'total' => $res->total(), 'remark' => get_route_remark(), ]); } public function add(): void { if ($this->request->isPost()) { $data = $this->request->post(); $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; //尝试存储 try { $data['status'] = 0; $data['encoding'] = $uniID->generateParticle(); $result = $this->model->save($data); if (!empty($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'] = ['科研借单']; $leader = Db::name('admin')->where('switch', 1)->find(); $data['college_leader'] = $leader['nickname']; } else { $text['data']['const6'] = ['教学借单']; } $groupAccess = []; foreach ($data['asset'] as $datum) { $groupAccess[] = [ 'borrow_id' => $this->model->id, 'asset_name' => $datum['model'], 'origin' => $datum['origin'], 'num' => $datum['num'] ]; } $data['accessories']['borrow_id'] = $this->model->id; Db::name('accessories')->insert($data['accessories']); $this->BorrowNumber->saveAll($groupAccess); $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($result !== false) { if (!empty($open_idArr) && $open_idArr !== null) { foreach ($open_idArr as $k => $v){ $text['openid'] = $open_idArr[$k]; if($open_idArr[$k] != null){ $res = self::wxAuditMessage($text); }else{ $res = false; } } if ($res === true) { $this->success(__('Update successful')); } else { $this->success("更新成功,但消息发送失败,请检查网络或联系管理员"); } } else { $this->success('更新成功!但对方未配置公众号,消息提示失败!'); } } else { $this->error(__('No rows were added')); } } } public function del($ids = null): void { if (!$this->request->isDelete() || !$ids) { $this->error(__('Parameter error')); } $pk = $this->model->getPk(); $data = $this->model->where($pk, 'in', $ids)->select(); $count = 0; if ($data[0]['status'] !== 0) { $this->error("仅待实验室审核状态可以删除!!"); } $this->model->startTrans(); try { foreach ($data as $v) { $count += $v->delete(); $this->BorrowAccess->where('borrow_id', 'in', $ids)->select(); Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete(); Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete(); } $this->model->commit(); } catch (Throwable $e) { $this->model->rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(__('Deleted successfully')); } else { $this->error(__('No rows were deleted')); } } public function edit($id = null): void { $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) { // halt(1); $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select(); } else { // halt($id); $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id', $id)->select(); } $row['asset'] = $rows; $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find(); // halt(123); if ($this->request->isPost()) { $data = $this->request->post(); $this->BorrowTools->checkRules($data); $data = $this->BorrowTools->checkText($data); if (!isset($data['status']) && ($data['status'] !== 0 || $data['status'] !== 1)) { $this->error("仅待实验室审核与被驳回状态可以修改!!"); } $data['status'] = 0; $data = $this->excludeFields($data); $this->model->startTrans(); $result = false; try { $this->BorrowTools->checkAsset($data); $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) { $this->success(__('Update successful')); } else { $this->error(__('No rows updated')); } } $this->success('', [ 'row' => $row ]); } //保存电子签名 public function saveSign(): void { if ($this->request->isPost()) { $data = $this->request->post(); if (!$data) { $this->error(__('Parameter %s can not be empty', [''])); } $id = $this->auth->id; $result = false; if (isset($data['sign']) && $data['sign']) { $result = Db::name('sign')->where('uid', $id)->find(); if ($result) { $result['sign'] = $data['sign']; if (Db::name('sign')->update($result)) { $this->success(__('Avatar modified successfully!')); } } else { $result = Db::name('sign')->insert(['uid' => $id, 'sign' => $data['sign']]); if ($result) { $this->success(__('Avatar modified successfully!')); } } } if ($result == false) { $this->error('更新失败!'); } } } /** * 仪器预约待审核通知 * @param * @return void * @throws \Exception */ public function wxAuditMessage($res = null): bool { $openid = $res["openid"]; $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; } function getPlainTextFromHtml($html) { // Remove the HTML tags $html = strip_tags($html); // Convert HTML entities to single characters $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8'); $html_len = mb_strlen($html, 'UTF-8'); // Make the string the desired number of characters // Note that substr is not good as it counts by bytes and not characters $html = mb_substr($html, 0, strlen($html), 'UTF-8'); return $html; } public function getProtocol(){ $data = Db::name('protocol')->where("switch",1)->value('editor'); if($data){ $this->success('', [ 'data' => $data, 'data1' => self::getPlainTextFromHtml($data) ]); }else{ $this->error("失败!"); } } }