123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <?php
- namespace app\admin\controller\borrow;
- use app\admin\model\Borrow as BorrowModel;
- use app\admin\model\BorrowAccess;
- use app\admin\model\BorrowNumber;
- use app\common\controller\Backend;
- use ba\Random;
- use modules\notification\Notification;
- use think\facade\Db;
- use app\common\controller\SnowflakeId;
- use Throwable;
- use app\common\library\xmwechat\offiaccount\OaService;
- use app\admin\controller\borrow\BorrowTools;
- class BorrowApply extends Backend
- {
- protected array|string $quickSearchField = ['username', 'mobile'];
- /**
- * 无需鉴权的方法
- * @var array
- */
- protected array $noNeedPermission = ['saveSign'];
- public function initialize(): void
- {
- parent::initialize();
- $this->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,8])
- ->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,8])
- ->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) {
- $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
- } else {
- $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();
- if (!isset($data['status']) && ($data['status'] !== 0 || $data['status'] !== 1 || $data['status'] == 8)) {
- $this->error("当前状态不可修改!!");
- }
- $data = $this->excludeFields($data);
- if($data['status'] != 8){
- $data['status'] = 2;
- $this->BorrowTools->checkRules($data);
- $data = $this->BorrowTools->checkText($data);
- $this->BorrowTools->checkAsset($data);
- }else{
- $data['status'] = 0;
- }
- $this->model->startTrans();
- $result = false;
- try {
- $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("失败!");
- }
- }
- }
|