BorrowApp.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <?php
  2. namespace app\admin\controller\borrow;
  3. use app\admin\model\Borrow as BorrowModel;
  4. use app\admin\model\BorrowAccess;
  5. use app\admin\model\BorrowNumber;
  6. use app\common\controller\Backend;
  7. use ba\Random;
  8. use modules\notification\Notification;
  9. use think\facade\Db;
  10. use app\common\controller\SnowflakeId;
  11. use think\migration\command\migrate\Status;
  12. use Throwable;
  13. use app\admin\controller\borrow\BorrowTools;
  14. use app\common\library\xmwechat\offiaccount\OaService;
  15. class BorrowApp extends Backend
  16. {
  17. protected array|string $quickSearchField = ['username', 'mobile'];
  18. public function initialize(): void
  19. {
  20. parent::initialize();
  21. $this->model = new BorrowModel;
  22. $this->BorrowAccess = new BorrowAccess();
  23. $this->BorrowNumber = new BorrowNumber();
  24. $this->adminModel = $this->auth->getAdmin();
  25. $this->BorrowTools = new BorrowTools($this->app);
  26. }
  27. public function apply(): void
  28. {
  29. if ($this->request->param('select')) {
  30. $this->select();
  31. }
  32. $type = $this->request->get()['type'];
  33. $user_id = $this->auth->id;
  34. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  35. list($where, $alias, $limit, $order) = $this->queryBuilder();
  36. if($type == 1){
  37. if ($user_group_id == 1 || $user_group_id == 2) {
  38. $res = $this->model
  39. ->alias($alias)
  40. ->where($where)
  41. ->where(function($query) {
  42. $query->where([
  43. ["status", "in", [0,2,6]]
  44. ]);
  45. })
  46. ->order($order)
  47. ->paginate($limit);
  48. } else if ($user_group_id == 3) {
  49. $res = $this->model
  50. ->alias($alias)
  51. ->where($where)
  52. ->where(function($query) {
  53. $query->where([
  54. ["status", "=", 6],
  55. ['purpose', '=', 0]
  56. ]);
  57. })
  58. ->whereOr(function($query) {
  59. $query->where([
  60. ["status", "=", 3],
  61. ['purpose', '=', 0],
  62. ['college_leader_id', '=', $this->auth->id]
  63. ]);
  64. })
  65. ->whereOr(function($query) {
  66. $query->where([
  67. ["status", "in", [0,2,6]],
  68. ['user_id', '=', $this->auth->id]
  69. ]);
  70. })
  71. ->order($order)
  72. ->paginate($limit);
  73. } else if ($user_group_id == 4) {
  74. $res = $this->model
  75. ->alias($alias)
  76. ->where($where)
  77. ->where(function($query) {
  78. $query->where([
  79. ["status", "in", [0,2,6]],
  80. ['user_id', '=', $this->auth->id]
  81. ]);
  82. })
  83. ->order($order)
  84. ->paginate($limit);
  85. }
  86. }else if($type == 2){
  87. if ($user_group_id == 1 || $user_group_id == 2) {
  88. $res = $this->model
  89. ->alias($alias)
  90. ->where($where)
  91. ->where(function($query) {
  92. $query->where([
  93. ["status", "in", [3,5]]
  94. ]);
  95. })
  96. ->order($order)
  97. ->paginate($limit);
  98. } else if ($user_group_id == 3) {
  99. $res = $this->model
  100. ->alias($alias)
  101. ->where($where)
  102. ->where(function($query) {
  103. $query->where([
  104. ["status", "in", [3,5]],
  105. ['purpose', '=', 0],
  106. ['college_leader_id', '=', $this->auth->id]
  107. ]);
  108. })
  109. ->whereOr(function($query) {
  110. $query->where([
  111. ["status", "in", [3,5]],
  112. ['user_id', '=', $this->auth->id]
  113. ]);
  114. })
  115. ->order($order)
  116. ->paginate($limit);
  117. } else if ($user_group_id == 4) {
  118. $res = $this->model
  119. ->alias($alias)
  120. ->where($where)
  121. ->where(function($query) {
  122. $query->where([
  123. ["status", "in", [3,5]],
  124. ['user_id', '=', $this->auth->id]
  125. ]);
  126. })
  127. ->order($order)
  128. ->paginate($limit);
  129. }
  130. }else if($type == 3){
  131. if ($user_group_id == 1 || $user_group_id == 2) {
  132. $res = $this->model
  133. ->alias($alias)
  134. ->where($where)
  135. ->where(function($query) {
  136. $query->where([
  137. ["status", "in", [1,4,7,8]]
  138. ]);
  139. })
  140. ->order($order)
  141. ->paginate($limit);
  142. } else if ($user_group_id == 3) {
  143. $res = $this->model
  144. ->alias($alias)
  145. ->where($where)
  146. ->where(function($query) {
  147. $query->where([
  148. ["status", "in", [1,4,7,8]],
  149. ['purpose', '=', 0],
  150. ['college_leader_id', '=', $this->auth->id]
  151. ]);
  152. })
  153. ->whereOr(function($query) {
  154. $query->where([
  155. ["status", "in", [1,4,7,8]],
  156. ['user_id', '=', $this->auth->id]
  157. ]);
  158. })
  159. ->order($order)
  160. ->paginate($limit);
  161. } else if ($user_group_id == 4) {
  162. $res = $this->model
  163. ->alias($alias)
  164. ->where($where)
  165. ->where(function($query) {
  166. $query->where([
  167. ["status", "in", [1,4,7,8]],
  168. ['user_id', '=', $this->auth->id]
  169. ]);
  170. })
  171. ->order($order)
  172. ->paginate($limit);
  173. }
  174. }
  175. $this->success('', [
  176. 'list' => $res,
  177. 'remark' => get_route_remark(),
  178. ]);
  179. }
  180. public function Useinng(): void
  181. {
  182. if ($this->request->param('select')) {
  183. $this->select();
  184. }
  185. $user_id = $this->auth->id;
  186. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  187. list($where, $alias, $limit, $order) = $this->queryBuilder();
  188. if ($user_group_id == 1 || $user_group_id == 2) {
  189. $res = $this->model
  190. ->where($where)
  191. ->where("status", 'in', [3, 5])
  192. ->order($order)
  193. ->paginate($limit);
  194. } else if ($user_group_id == 3) {
  195. $res = $this->model
  196. ->where($where)
  197. ->where("status", 'in', [3, 5])
  198. ->where('college_leader_id', $user_id)
  199. ->order($order)
  200. ->paginate($limit);
  201. } else if ($user_group_id == 4) {
  202. $res = $this->model
  203. ->where($where)
  204. ->where("status", 'in', [3, 5])
  205. ->where('user_id', $user_id)
  206. ->order($order)
  207. ->paginate($limit);
  208. }
  209. $this->success('', [
  210. 'list' => $res->items(),
  211. 'total' => $res->total(),
  212. 'remark' => get_route_remark(),
  213. ]);
  214. }
  215. public function toEnd(): void
  216. {
  217. if ($this->request->param('select')) {
  218. $this->select();
  219. }
  220. $user_id = $this->auth->id;
  221. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  222. list($where, $alias, $limit, $order) = $this->queryBuilder();
  223. if ($user_group_id == 1 || $user_group_id == 2) {
  224. $res = $this->model
  225. ->where($where)
  226. ->where("status", 'in', [1, 4])
  227. ->order($order)
  228. ->paginate($limit);
  229. } else if ($user_group_id == 3) {
  230. $res = $this->model
  231. ->where($where)
  232. ->where("status", 'in', [1, 4])
  233. ->where('college_leader_id', $user_id)
  234. ->order($order)
  235. ->paginate($limit);
  236. } else if ($user_group_id == 4) {
  237. $res = $this->model
  238. ->where($where)
  239. ->where("status", 'in', [1, 4])
  240. ->where('user_id', $user_id)
  241. ->order($order)
  242. ->paginate($limit);
  243. }
  244. $this->success('', [
  245. 'list' => $res->items(),
  246. 'total' => $res->total(),
  247. 'remark' => get_route_remark(),
  248. ]);
  249. }
  250. public function consoleInformation(): void
  251. {
  252. $user_id = $this->auth->id;
  253. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  254. $res = [
  255. 'damage_num' => Db::name('asset')->where("status", 2)->count(),
  256. 'asset_count' => Db::name('asset')->count(),
  257. 'asset_lent' => Db::name('asset')->where('status', 1)->count(),
  258. 'asset_value' => Db::name('asset')->sum('unit_price'),
  259. 'damage_people' => Db::name('asset')->where("status", 2)->count(),
  260. 'damage_nature' => Db::name('asset')->where("status", 2)->count(),
  261. 'apply' => $this->model->where("status", 0)->count(),
  262. 'useing' => $this->model->where("status", 2)->count(),
  263. 'overdue' => $this->model->where("status", 5)->count(),
  264. ];
  265. $this->success('', $res);
  266. }
  267. public function edit($id = null): void
  268. {
  269. if ($this->request->isPost()) {
  270. $data = $this->request->post()['data'];
  271. $this->BorrowTools->checkRules($data);
  272. $data = $this->BorrowTools->checkText($data);
  273. //获取用户类型
  274. $user_id = $this->auth->id;
  275. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  276. if ($data['status'] == 1 ) {
  277. if(!isset($data['annotation'])){
  278. $this->error("驳回时请填写批注!");
  279. }
  280. }
  281. if ( $data['status'] == 7 ) {
  282. if(!isset($data['college_annotation'])){
  283. $this->error("驳回时请填写批注!");
  284. }
  285. }
  286. if (!(array_key_exists('status', $data) && $data['status'] !== null && $data['status'] !== '')) {
  287. $this->error("请检查仪器状态");
  288. }
  289. $data = $this->excludeFields($data);
  290. $this->model->startTrans();
  291. $result = false;
  292. //判断是否是科研类借取并判断是否需要领导审核
  293. try {
  294. // 调整仪器的变化
  295. $this->BorrowTools->checkAsset($data);
  296. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  297. if ($open_id !== null) {
  298. $text = [
  299. "openid" => $open_id,
  300. "pagepath" => "pages/login/index",
  301. "data" => [
  302. 'character_string1' => $data['id'],
  303. 'thing5' => $this->auth->nickname,
  304. ]
  305. ];
  306. }
  307. //判断是否是科研类借取并判断是否需要领导审核
  308. if ($data['purpose'] == 0) {
  309. $text['data']['const3'] = '科研借单';
  310. if ($user_group_id !== 3) {
  311. $data['approval_person'] = $this->auth->nickname;
  312. $data['approval_person_id'] = $this->auth->id;
  313. if ($data['status'] == 6) {
  314. $text['data']['const4'] = ['审批通过'];
  315. $data['status'] = 6;
  316. } else if ($data['status'] == 1) {
  317. $text['data']['const4'] = ['已驳回'];
  318. $data['status'] = 1;
  319. }
  320. try {
  321. $result = $this->model->update($data);
  322. $this->model->commit();
  323. } catch (Throwable $e) {
  324. $this->model->rollback();
  325. $this->error($e->getMessage());
  326. }
  327. } else {
  328. $data['college_leader_id'] = $user_id;
  329. $data['college_leader'] = $this->auth->nickname;
  330. if ($data['status'] == '1') {
  331. $text['data']['const4'] = ['已驳回'];
  332. $data['status'] = 7;
  333. } else {
  334. $text['data']['const4'] = ['审批通过'];
  335. }
  336. $result = false;
  337. $this->model->startTrans();
  338. try {
  339. $result = $this->model->update($data);
  340. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  341. // $num = count($data['asset']);
  342. foreach ($data['asset'] as $v) {
  343. for ($i = 0; $i < $v['num']; $i++) {
  344. $this->BorrowAccess->insert(
  345. [
  346. 'asset_name' => $v['model'],
  347. 'origin' => $v['origin'],
  348. 'borrow_id' => $data['id'],
  349. 'status' => -1,
  350. ]
  351. );
  352. }
  353. }
  354. }
  355. $this->model->commit();
  356. } catch (Throwable $e) {
  357. $this->model->rollback();
  358. $this->error($e->getMessage());
  359. }
  360. }
  361. } else {
  362. $data['approval_person'] = $this->auth->nickname;
  363. $data['approval_person_id'] = $this->auth->id;
  364. $text['data']['const3'] = ['教学借单'];
  365. if ($data['status'] == 2) {
  366. $text['data']['const4'] = ['审批通过'];
  367. } else if ($data['status'] == 1) {
  368. $text['data']['const4'] = ['已驳回'];
  369. }
  370. $result = $this->model->update($data);
  371. if ($data['status'] == 2 && array_key_exists('asset', $data)) {
  372. foreach ($data['asset'] as $v) {
  373. for ($i = 0; $i < $v['num']; $i++) {
  374. $this->BorrowAccess->insert(
  375. [
  376. 'asset_name' => $v['model'],
  377. 'origin' => $v['origin'],
  378. 'borrow_id' => $data['id'],
  379. 'status' => -1,
  380. ]
  381. );
  382. }
  383. }
  384. }
  385. }
  386. // 提交事务
  387. $this->model->commit();
  388. } catch (Throwable $e) {
  389. // 回滚事务
  390. $this->model->rollback();
  391. $this->error($e->getMessage());
  392. }
  393. if ($result !== false) {
  394. // $this->success(__('Update successful'));
  395. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  396. if ($open_id !== null) {
  397. // halt($text);
  398. $res = self::wxResultMessage($text);
  399. if ($res === true) {
  400. $this->success(__('Update successful'));
  401. } else {
  402. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  403. }
  404. }else{
  405. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  406. }
  407. } else {
  408. $this->error(__('No rows updated'));
  409. }
  410. }
  411. $row = $this->model->find($id);
  412. if (!$row) {
  413. $this->error(__('Record not found'));
  414. }
  415. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6) {
  416. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  417. } else {
  418. $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();
  419. }
  420. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  421. $row['asset'] = $rows;
  422. $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select();
  423. $this->success('', [
  424. 'row' => $row
  425. ]);
  426. }
  427. public function editAccessories($id = null): void
  428. {
  429. if ($this->request->isPost()) {
  430. $data = $this->request->post()['data'];
  431. //获取用户类型
  432. $user_id = $this->auth->id;
  433. $this->model->startTrans();
  434. $result = false;
  435. //判断是否是科研类借取并判断是否需要领导审核
  436. try {
  437. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  438. if ($open_id !== null) {
  439. $text = [
  440. "openid" => $open_id,
  441. "pagepath" => "pages/login/index",
  442. "data" => [
  443. 'character_string1' => $data['id'],
  444. 'thing5' => $this->auth->nickname,
  445. ]
  446. ];
  447. }
  448. //判断是否是科研类借取并判断是否需要领导审核
  449. if ($data['purpose'] == 0) {
  450. $text['data']['const3'] = '科研借单';
  451. $text['data']['const4'] = ['需要修改配件数量'];
  452. $data['status'] = 8;
  453. try {
  454. $result = $this->model->update($data);
  455. $this->model->commit();
  456. } catch (Throwable $e) {
  457. $this->model->rollback();
  458. $this->error($e->getMessage());
  459. }
  460. } else {
  461. $data['approval_person'] = $this->auth->nickname;
  462. $data['approval_person_id'] = $this->auth->id;
  463. $text['data']['const3'] = ['教学借单'];
  464. $text['data']['const4'] = ['需要修改配件数量'];
  465. $data['status'] = 8;
  466. try {
  467. $result = $this->model->update($data);
  468. $this->model->commit();
  469. } catch (Throwable $e) {
  470. $this->model->rollback();
  471. $this->error($e->getMessage());
  472. }
  473. }
  474. // 提交事务
  475. $this->model->commit();
  476. } catch (Throwable $e) {
  477. // 回滚事务
  478. $this->model->rollback();
  479. $this->error($e->getMessage());
  480. }
  481. if ($result !== false) {
  482. // $this->success(__('Update successful'));
  483. $open_id = Db::name('oauth_log')->where('user_id',$user_id)->value('opid');
  484. if ($open_id !== null) {
  485. // halt($text);
  486. $res = self::wxResultMessage($text);
  487. if ($res === true) {
  488. $this->success(__('Update successful'));
  489. } else {
  490. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  491. }
  492. }else{
  493. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  494. }
  495. } else {
  496. $this->error(__('No rows updated'));
  497. }
  498. }
  499. }
  500. public function changeAssetNum($id = null): void
  501. {
  502. if ($this->request->isPost()) {
  503. $data = $this->request->post()['data'];
  504. // 调整仪器的变化
  505. if($data['status'] != 8){
  506. $this->BorrowTools->checkRules($data);
  507. $data = $this->BorrowTools->checkText($data);
  508. $this->BorrowTools->checkAsset($data);
  509. $asset_userArr = [];
  510. $admin_idArr = [];
  511. $open_idArr = [];
  512. //获取消息推送目标人
  513. foreach ($data['asset'] as $k => $v){
  514. $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user');
  515. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  516. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  517. if(!in_array($asset_user,$asset_userArr)){
  518. $asset_userArr[] = $asset_user;
  519. }
  520. if(!in_array($admin_id,$admin_idArr)){
  521. $admin_idArr[] = $admin_id;
  522. }
  523. if(!in_array($open_id,$open_idArr)){
  524. $open_idArr[] = $open_id;
  525. }
  526. }
  527. $data['approval_person_id'] = implode(',',$admin_idArr);
  528. $data['approval_person'] = implode(',',$asset_userArr);
  529. if (!empty($open_idArr) && $open_idArr !== null ) {
  530. $text = [
  531. "pagepath" => "pages/login/index",
  532. "data" => [
  533. 'character_string7' => $this->model->id,
  534. 'thing4' => $data['username'],
  535. ]
  536. ];
  537. }
  538. }
  539. //获取用户类型
  540. $user_id = $this->auth->id;
  541. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  542. $data = $this->excludeFields($data);
  543. $this->model->startTrans();
  544. $result = false;
  545. //判断是否是科研类借取并判断是否需要领导审核
  546. try {
  547. if($data['status'] == 8){
  548. $data['status'] = 2;
  549. }else{
  550. $data['status'] = 0;
  551. $data['annotation'] = '';
  552. $data['college_annotation'] = '';
  553. }
  554. //判断是否是科研类借取并判断是否需要领导审核
  555. if ($data['purpose'] == 0) {
  556. $text['data']['const6'] = ['科研借单'];
  557. $leader = Db::name('admin')->where('switch', 1)->find();
  558. $data['college_leader'] = $leader['nickname'];
  559. if ($user_group_id !== 3) {
  560. try {
  561. $result = $this->model->update($data);
  562. $this->model->commit();
  563. } catch (Throwable $e) {
  564. $this->model->rollback();
  565. $this->error($e->getMessage());
  566. }
  567. } else {
  568. $data['college_leader_id'] = $user_id;
  569. $data['college_leader'] = $this->auth->nickname;
  570. if ($data['status'] == '1') {
  571. $data['status'] = 7;
  572. }
  573. $result = false;
  574. $this->model->startTrans();
  575. try {
  576. $result = $this->model->update($data);
  577. $this->model->commit();
  578. } catch (Throwable $e) {
  579. $this->model->rollback();
  580. $this->error($e->getMessage());
  581. }
  582. }
  583. } else {
  584. $text['data']['const6'] = ['教学借单'];
  585. $result = $this->model->update($data);
  586. }
  587. Db::name('accessories')->update($data['accessories']);
  588. // 提交事务
  589. $this->model->commit();
  590. } catch (Throwable $e) {
  591. // 回滚事务
  592. $this->model->rollback();
  593. $this->error($e->getMessage());
  594. }
  595. if ($result !== false) {
  596. if (!empty($open_idArr) && $open_idArr !== null && $data['status'] != 8) {
  597. foreach ($open_idArr as $k => $v){
  598. $text['open_id'] = $open_idArr[$k];
  599. if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) {
  600. // halt($text);
  601. $res = self::wxAuditMessage($text);
  602. }
  603. }
  604. if ($res === true) {
  605. $this->success(__('Update successful'));
  606. } else {
  607. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  608. }
  609. } else {
  610. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  611. }
  612. } else {
  613. $this->error(__('No rows were added'));
  614. }
  615. }
  616. $row = $this->model->find($id);
  617. if (!$row) {
  618. $this->error(__('Record not found'));
  619. }
  620. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6 || $row['status'] == 7) {
  621. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  622. } else {
  623. $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();
  624. }
  625. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  626. $row['asset'] = $rows;
  627. $row['borrowImg'] = Db::name('borrow_img')->where('borrow_id', $id)->select();
  628. $this->success('', [
  629. 'row' => $row
  630. ]);
  631. }
  632. public function add(): void
  633. {
  634. if ($this->request->isPost()) {
  635. $data = $this->request->post()['data'];
  636. $this->BorrowTools->checkRules($data);
  637. $data = $this->BorrowTools->checkText($data);
  638. $asset_userArr = [];
  639. $admin_idArr = [];
  640. $open_idArr = [];
  641. //获取消息推送目标人
  642. foreach ($data['asset'] as $k => $v){
  643. $asset_user = Db::name('asset')->where('asset_name',$data['asset'][$k]['model'])->value('user');
  644. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  645. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  646. if(!in_array($asset_user,$asset_userArr)){
  647. $asset_userArr[] = $asset_user;
  648. }
  649. if(!in_array($admin_id,$admin_idArr)){
  650. $admin_idArr[] = $admin_id;
  651. }
  652. if(!in_array($open_id,$open_idArr)){
  653. $open_idArr[] = $open_id;
  654. }
  655. }
  656. $data['approval_person_id'] = implode(',',$admin_idArr);
  657. $data['approval_person'] = implode(',',$asset_userArr);
  658. //校验数据
  659. $uniID = new SnowflakeId;
  660. $data['user_id'] = $this->auth->id;
  661. $this->model->startTrans();
  662. //尝试存储
  663. try {
  664. $data['status'] = 0;
  665. $data['encoding'] = $uniID->generateParticle();
  666. if ($open_idArr !== [] && $open_idArr !== null) {
  667. $text = [
  668. "pagepath" => "pages/login/index",
  669. "data" => [
  670. 'character_string7' => $this->model->id,
  671. 'thing4' => $data['username']
  672. ]
  673. ];
  674. }
  675. if ($data['purpose'] == 0) {
  676. $text['data']['const6'] = ['科研借单'];
  677. $data['college_leader'] = Db::name('admin')->where('switch',1)->value('nickname');
  678. } else {
  679. $text['data']['const6'] = ['教学借单'];
  680. }
  681. $result = $this->model->save($data);
  682. $data['accessories']['borrow_id'] = $this->model->id;
  683. Db::name('accessories')->insert($data['accessories']);
  684. $groupAccess = [];
  685. foreach ($data['asset'] as $datum) {
  686. if ($datum['model'] !== '') {
  687. $groupAccess[] = [
  688. 'borrow_id' => $this->model->id,
  689. 'asset_name' => $datum['model'],
  690. 'origin' => $datum['origin'],
  691. 'num' => $datum['num']
  692. ];
  693. }
  694. }
  695. // halt($result);
  696. $this->BorrowNumber->saveAll($groupAccess);
  697. $this->model->commit();
  698. } catch (Throwable $e) {
  699. // 回滚事务
  700. $this->model->rollback();
  701. $this->error($e->getMessage());
  702. }
  703. if ($result !== false) {
  704. if ($open_idArr !== [] && $open_idArr !== null) {
  705. foreach ($open_idArr as $k => $v){
  706. $text['open_id'] = $open_idArr[$k];
  707. if ( $open_idArr[$k] !== [] && $open_idArr[$k] !== null) {
  708. // halt($text);
  709. $res = self::wxAuditMessage($text);
  710. }
  711. }
  712. if (isset($res)) {
  713. $this->success(__('Update successful'));
  714. } else {
  715. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  716. }
  717. }else{
  718. // halt(2);
  719. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  720. }
  721. } else {
  722. // halt(3);
  723. $this->error(__('No rows were added'));
  724. }
  725. }
  726. else {
  727. $res = $this->model->where('user_id',$this->auth->id)->find();
  728. $this->success('',$res);
  729. }
  730. }
  731. public function editPassword(): void
  732. {
  733. if ($this->request->isPost()) {
  734. $data = $this->request->param()['data'];
  735. if (!$data) {
  736. $this->error(__('Parameter %s can not be empty', ['']));
  737. }
  738. $result = $this->adminModel->resetPassword($this->auth->id, $data['_value']);
  739. if ($result !== false) {
  740. $this->success(__('Update successful'));
  741. } else {
  742. $this->error(__('No rows updated'));
  743. }
  744. }
  745. }
  746. /**
  747. * 仪器预约待审核通知
  748. * @param
  749. * @return void
  750. * @throws \Exception
  751. */
  752. public function wxAuditMessage($res = null): bool
  753. {
  754. $openid = $res["open_id"];
  755. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  756. $data = [
  757. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  758. 'thing4' => ['value' => $res["data"]["thing4"]],
  759. 'const6' => ['value' => $res["data"]["const6"]],
  760. 'time9' => ['value' => date('Y-m-d H:i')],
  761. ];
  762. $url = 'http://weixin.qq.com/download';
  763. $miniprogram = [
  764. 'appid' => 'wx58633590ab59b7a1',
  765. 'pagepath' => ''
  766. ];
  767. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  768. return $result;
  769. }
  770. public function getProtocol(){
  771. $data = Db::name('protocol')->where("switch",1)->value('editor');
  772. if($data){
  773. $this->success('', [
  774. 'data' => $data,
  775. 'data1' => htmlspecialchars_decode($data)
  776. ]);
  777. }else{
  778. $this->error("失败!");
  779. }
  780. }
  781. /**
  782. * 仪器预约审核结果通知
  783. * @param
  784. * @return void
  785. * @throws \Exception
  786. */
  787. public function wxResultMessage($res): bool
  788. {
  789. $openid = $res["openid"];
  790. // halt($res);
  791. $template_id = 'Fxd6WNSNPbxEzQ35mDCcjzJBZClv1F9L7jWCcZmAlRI';
  792. $data = [
  793. 'character_string1' => ['value' => $res["data"]["character_string1"]],
  794. 'const3' => ['value' => [$res["data"]["const3"]]],
  795. 'const4' => ['value' => $res["data"]["const4"]],
  796. 'thing5' => ['value' => $res["data"]["thing5"]],
  797. // 'time9' => ['value' => date('Y-m-d H:i')],
  798. ];
  799. $url = 'http://weixin.qq.com/download';
  800. $miniprogram = [
  801. 'appid' => 'wx58633590ab59b7a1',
  802. 'pagepath' => "pages/login/index"
  803. ];
  804. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  805. return $result;
  806. }
  807. // public function (): void
  808. // {
  809. // if ($this->request->param('select')) {
  810. // $this->select();
  811. // }
  812. // $user_id = $this->auth->id;
  813. // $user_group_id = Db::name('admin_group_access')->where('uid',$user_id)->value("group_id");
  814. // $res = [
  815. // 'damage_num' => Db::name('asset')->where("status",2)->count(),
  816. // 'damage_people' => Db::name('asset')->where("status",2)->where("damage_type",1)->count(),
  817. // 'damage_nature'=> Db::name('asset')->where("status",2)->where("damage_type",0)->count(),
  818. // 'apply'=> $this->model->where("status",0)->count(),
  819. // 'useing'=> $this->model->where("status",2)->count(),
  820. // 'overdue'=> $this->model->where("status",5)->count(),
  821. // ];
  822. // $this->success('', $res);
  823. // }
  824. }