BorrowApply.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 Throwable;
  12. use app\common\library\xmwechat\offiaccount\OaService;
  13. use app\admin\controller\borrow\BorrowTools;
  14. class BorrowApply extends Backend
  15. {
  16. protected array|string $quickSearchField = ['username', 'mobile'];
  17. /**
  18. * 无需鉴权的方法
  19. * @var array
  20. */
  21. protected array $noNeedPermission = ['saveSign'];
  22. public function initialize(): void
  23. {
  24. parent::initialize();
  25. $this->model = new BorrowModel;
  26. $this->BorrowAccess = new BorrowAccess();
  27. $this->BorrowNumber = new BorrowNumber();
  28. $this->BorrowTools = new BorrowTools($this->app);
  29. }
  30. public function index(): void
  31. {
  32. if ($this->request->param('select')) {
  33. $this->select();
  34. }
  35. $user_id = $this->auth->id;
  36. $user_group_id = Db::name('admin_group_access')->where('uid', $user_id)->value("group_id");
  37. list($where, $alias, $limit, $order) = $this->queryBuilder();
  38. // halt($where, $alias, $limit, $order);
  39. if ($user_group_id == 1 || $user_group_id == 2) {
  40. $res = $this->model
  41. ->alias($alias)
  42. ->where($where)
  43. ->where("status", 'in', [0,1,6,7,8])
  44. ->where("user_id",$user_id)
  45. ->order($order)
  46. ->paginate($limit);
  47. } else if ($user_group_id == 3) {
  48. $res = $this->model
  49. ->alias($alias)
  50. ->where($where)
  51. ->where("status", 'in', [0,1, 6,7,8])
  52. ->where('purpose', 0)
  53. ->where("user_id",$user_id)
  54. ->order($order)
  55. ->paginate($limit);
  56. } else if ($user_group_id == 4) {
  57. $res = $this->model
  58. ->alias($alias)
  59. ->where($where)
  60. ->where('user_id', $user_id)
  61. ->order($order)
  62. ->paginate($limit);
  63. }
  64. $this->success('', [
  65. 'list' => $res->items(),
  66. 'total' => $res->total(),
  67. 'remark' => get_route_remark(),
  68. ]);
  69. }
  70. public function add(): void
  71. {
  72. if ($this->request->isPost()) {
  73. $data = $this->request->post();
  74. $this->BorrowTools->checkRules($data);
  75. $data = $this->BorrowTools->checkText($data);
  76. $asset_userArr = [];
  77. $admin_idArr = [];
  78. $open_idArr = [];
  79. //获取消息推送目标人
  80. foreach ($data['asset'] as $k => $v){
  81. $asset_user = Db::name('asset')->where('asset_name', $data['asset'][$k]['model'])->value('user');
  82. $admin_id = Db::name('admin')->where('nickname',$asset_user)->value('id');
  83. $open_id = Db::name('oauth_log')->where('user_id', $admin_id)->value('opid');
  84. if(!in_array($asset_user,$asset_userArr)){
  85. $asset_userArr[] = $asset_user;
  86. }
  87. if(!in_array($admin_id,$admin_idArr)){
  88. $admin_idArr[] = $admin_id;
  89. }
  90. if(!in_array($open_id,$open_idArr)){
  91. $open_idArr[] = $open_id;
  92. }
  93. }
  94. $data['approval_person_id'] = implode(',',$admin_idArr);
  95. $data['approval_person'] = implode(',',$asset_userArr);
  96. //获取借单编号
  97. $uniID = new SnowflakeId;
  98. $data['user_id'] = $this->auth->id;
  99. //尝试存储
  100. try {
  101. $data['status'] = 0;
  102. $data['encoding'] = $uniID->generateParticle();
  103. $result = $this->model->save($data);
  104. if (!empty($open_idArr) && $open_idArr !== null ) {
  105. $text = [
  106. "pagepath" => "pages/login/index",
  107. "data" => [
  108. 'character_string7' => $this->model->id,
  109. 'thing4' => $data['username'],
  110. ]
  111. ];
  112. }
  113. if ($data['purpose'] == 0) {
  114. $text['data']['const6'] = ['科研借单'];
  115. $leader = Db::name('admin')->where('switch', 1)->find();
  116. $data['college_leader'] = $leader['nickname'];
  117. } else {
  118. $text['data']['const6'] = ['教学借单'];
  119. }
  120. $groupAccess = [];
  121. foreach ($data['asset'] as $datum) {
  122. $groupAccess[] = [
  123. 'borrow_id' => $this->model->id,
  124. 'asset_name' => $datum['model'],
  125. 'origin' => $datum['origin'],
  126. 'num' => $datum['num']
  127. ];
  128. }
  129. $data['accessories']['borrow_id'] = $this->model->id;
  130. Db::name('accessories')->insert($data['accessories']);
  131. $this->BorrowNumber->saveAll($groupAccess);
  132. $this->model->commit();
  133. } catch (Throwable $e) {
  134. $this->model->rollback();
  135. $this->error($e->getMessage());
  136. }
  137. if ($result !== false) {
  138. if (!empty($open_idArr) && $open_idArr !== null) {
  139. foreach ($open_idArr as $k => $v){
  140. $text['openid'] = $open_idArr[$k];
  141. if($open_idArr[$k] != null){
  142. $res = self::wxAuditMessage($text);
  143. }else{
  144. $res = false;
  145. }
  146. }
  147. if ($res === true) {
  148. $this->success(__('Update successful'));
  149. } else {
  150. $this->success("更新成功,但消息发送失败,请检查网络或联系管理员");
  151. }
  152. } else {
  153. $this->success('更新成功!但对方未配置公众号,消息提示失败!');
  154. }
  155. } else {
  156. $this->error(__('No rows were added'));
  157. }
  158. }
  159. }
  160. public function del($ids = null): void
  161. {
  162. if (!$this->request->isDelete() || !$ids) {
  163. $this->error(__('Parameter error'));
  164. }
  165. $pk = $this->model->getPk();
  166. $data = $this->model->where($pk, 'in', $ids)->select();
  167. $count = 0;
  168. if ($data[0]['status'] !== 0) {
  169. $this->error("仅待实验室审核状态可以删除!!");
  170. }
  171. $this->model->startTrans();
  172. try {
  173. foreach ($data as $v) {
  174. $count += $v->delete();
  175. $this->BorrowAccess->where('borrow_id', 'in', $ids)->select();
  176. Db::name('borrow_number')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  177. Db::name('borrow_access')->where('borrow_id', $v['id'])->useSoftDelete('delete_time', date('Y-m-d H:i:s', time()))->delete();
  178. }
  179. $this->model->commit();
  180. } catch (Throwable $e) {
  181. $this->model->rollback();
  182. $this->error($e->getMessage());
  183. }
  184. if ($count) {
  185. $this->success(__('Deleted successfully'));
  186. } else {
  187. $this->error(__('No rows were deleted'));
  188. }
  189. }
  190. public function edit($id = null): void
  191. {
  192. $row = $this->model->find($id);
  193. if (!$row) {
  194. $this->error(__('Record not found'));
  195. }
  196. if ($row['status'] == 0 || $row['status'] == 1 || $row['status'] == 6 || $row['status'] == 7) {
  197. $rows = $this->BorrowNumber->field('*,asset_name as model')->where('borrow_id', $id)->select();
  198. } else {
  199. $rows = $this->BorrowAccess->field('*,asset_name as model')->where('borrow_id', $id)->select();
  200. }
  201. $row['asset'] = $rows;
  202. $row['accessories'] = Db::name('accessories')->where('borrow_id', $id)->find();
  203. // halt(123);
  204. if ($this->request->isPost()) {
  205. $data = $this->request->post();
  206. if (!isset($data['status']) && ($data['status'] !== 0 || $data['status'] !== 1 || $data['status'] == 8)) {
  207. $this->error("当前状态不可修改!!");
  208. }
  209. $data = $this->excludeFields($data);
  210. if($data['status'] != 8){
  211. $data['status'] = 2;
  212. $this->BorrowTools->checkRules($data);
  213. $data = $this->BorrowTools->checkText($data);
  214. $this->BorrowTools->checkAsset($data);
  215. }else{
  216. $data['status'] = 0;
  217. }
  218. $this->model->startTrans();
  219. $result = false;
  220. try {
  221. $result = $this->model->update($data);
  222. Db::name('accessories')->update($data['accessories']);
  223. // 提交事务
  224. $this->model->commit();
  225. } catch (Throwable $e) {
  226. // 回滚事务
  227. $this->model->rollback();
  228. $this->error($e->getMessage());
  229. }
  230. if ($result !== false) {
  231. $this->success(__('Update successful'));
  232. } else {
  233. $this->error(__('No rows updated'));
  234. }
  235. }
  236. $this->success('', [
  237. 'row' => $row
  238. ]);
  239. }
  240. //保存电子签名
  241. public function saveSign(): void
  242. {
  243. if ($this->request->isPost()) {
  244. $data = $this->request->post();
  245. if (!$data) {
  246. $this->error(__('Parameter %s can not be empty', ['']));
  247. }
  248. $id = $this->auth->id;
  249. $result = false;
  250. if (isset($data['sign']) && $data['sign']) {
  251. $result = Db::name('sign')->where('uid', $id)->find();
  252. if ($result) {
  253. $result['sign'] = $data['sign'];
  254. if (Db::name('sign')->update($result)) {
  255. $this->success(__('Avatar modified successfully!'));
  256. }
  257. } else {
  258. $result = Db::name('sign')->insert(['uid' => $id, 'sign' => $data['sign']]);
  259. if ($result) {
  260. $this->success(__('Avatar modified successfully!'));
  261. }
  262. }
  263. }
  264. if ($result == false) {
  265. $this->error('更新失败!');
  266. }
  267. }
  268. }
  269. /**
  270. * 仪器预约待审核通知
  271. * @param
  272. * @return void
  273. * @throws \Exception
  274. */
  275. public function wxAuditMessage($res = null): bool
  276. {
  277. $openid = $res["openid"];
  278. $template_id = 'xsYnur6koCvgu1nUQwHmu8DX5SoL06P_nYMyK_oThGU';
  279. $data = [
  280. 'character_string7' => ['value' => $res["data"]["character_string7"]],
  281. 'thing4' => ['value' => $res["data"]["thing4"]],
  282. 'const6' => ['value' => $res["data"]["const6"]],
  283. 'time9' => ['value' => date('Y-m-d H:i')],
  284. ];
  285. $url = 'http://weixin.qq.com/download';
  286. $miniprogram = [
  287. 'appid' => 'wx58633590ab59b7a1',
  288. 'pagepath' => ''
  289. ];
  290. $result = OaService::getInstance()->sendTemplateMessage($openid, $template_id, $data, $url, $miniprogram);
  291. return $result;
  292. }
  293. function getPlainTextFromHtml($html)
  294. {
  295. // Remove the HTML tags
  296. $html = strip_tags($html);
  297. // Convert HTML entities to single characters
  298. $html = html_entity_decode($html, ENT_QUOTES, 'UTF-8');
  299. $html_len = mb_strlen($html, 'UTF-8');
  300. // Make the string the desired number of characters
  301. // Note that substr is not good as it counts by bytes and not characters
  302. $html = mb_substr($html, 0, strlen($html), 'UTF-8');
  303. return $html;
  304. }
  305. public function getProtocol(){
  306. $data = Db::name('protocol')->where("switch",1)->value('editor');
  307. if($data){
  308. $this->success('', [
  309. 'data' => $data,
  310. 'data1' => self::getPlainTextFromHtml($data)
  311. ]);
  312. }else{
  313. $this->error("失败!");
  314. }
  315. }
  316. }