NoticeLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\notice;
  15. use app\common\enum\notice\NoticeEnum;
  16. use app\common\logic\BaseLogic;
  17. use app\common\model\master_worker_register\MasterWorkerRegister;
  18. use app\common\model\notice\NoticeSetting;
  19. use app\common\model\works\ServiceWork;
  20. /**
  21. * 通知逻辑层
  22. * Class NoticeLogic
  23. * @package app\adminapi\logic\notice
  24. */
  25. class NoticeLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 查看通知设置详情
  29. * @param $params
  30. * @return array
  31. * @author 段誉
  32. * @date 2022/3/29 11:34
  33. */
  34. public static function detail($params)
  35. {
  36. $noticeSetting = NoticeSetting::field('*')->findOrEmpty($params['id'])->toArray();
  37. if (empty($noticeSetting)) {
  38. return [];
  39. }
  40. if (empty($noticeSetting['system_notice'])) {
  41. $noticeSetting['system_notice'] = [
  42. 'title' => '',
  43. 'content' => '',
  44. 'status' => 0,
  45. ];
  46. }
  47. $noticeSetting['system_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::SYSTEM, $noticeSetting['scene_id']);
  48. if (empty($noticeSetting['sms_notice'])) {
  49. $noticeSetting['sms_notice'] = [
  50. 'template_id' => '',
  51. 'content' => '',
  52. 'status' => 0,
  53. ];
  54. }
  55. $noticeSetting['sms_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::SMS, $noticeSetting['scene_id']);
  56. if (empty($noticeSetting['oa_notice'])) {
  57. $noticeSetting['oa_notice'] = [
  58. 'template_id' => '',
  59. 'template_sn' => '',
  60. 'name' => '',
  61. 'first' => '',
  62. 'remark' => '',
  63. 'tpl' => [],
  64. 'status' => 0,
  65. ];
  66. }
  67. $noticeSetting['oa_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::OA, $noticeSetting['scene_id']);
  68. if (empty($noticeSetting['mnp_notice'])) {
  69. $noticeSetting['mnp_notice'] = [
  70. 'template_id' => '',
  71. 'template_sn' => '',
  72. 'name' => '',
  73. 'tpl' => [],
  74. 'status' => 0,
  75. ];
  76. }
  77. $noticeSetting['mnp_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::MNP, $noticeSetting['scene_id']);
  78. $noticeSetting['system_notice']['is_show'] = in_array(NoticeEnum::SYSTEM, explode(',', $noticeSetting['support']));
  79. $noticeSetting['sms_notice']['is_show'] = in_array(NoticeEnum::SMS, explode(',', $noticeSetting['support']));
  80. $noticeSetting['oa_notice']['is_show'] = in_array(NoticeEnum::OA, explode(',', $noticeSetting['support']));
  81. $noticeSetting['mnp_notice']['is_show'] = in_array(NoticeEnum::MNP, explode(',', $noticeSetting['support']));
  82. $noticeSetting['default'] = '';
  83. //$noticeSetting['type'] = NoticeEnum::getTypeDesc($noticeSetting['type']);
  84. $noticeSetting['type'] = $noticeSetting['type'].'';
  85. $noticeSetting['recipient'] = $noticeSetting['recipient'].'';
  86. return $noticeSetting;
  87. }
  88. /**
  89. * @notes 通知设置
  90. * @param $params
  91. * @return bool
  92. * @author 段誉
  93. * @date 2022/3/29 11:34
  94. */
  95. public static function set($params)
  96. {
  97. try {
  98. // 校验参数
  99. //self::checkSet($params);
  100. // 拼装更新数据
  101. $updateData = [];
  102. $updateData['scene_id'] = $params['scene_id'];
  103. $updateData['scene_name'] = $params['scene_name'];
  104. $updateData['scene_desc'] = $params['scene_desc'];
  105. $updateData['recipient'] = $params['recipient'];
  106. $updateData['designated_user'] = $params['designated_user'];
  107. $updateData['type'] = $params['type'];
  108. $updateData['support'] = $params['support']??2;
  109. foreach ($params['template'] as $item) {
  110. if($item['type']??0) $updateData[$item['type'] . '_notice'] = json_encode($item, JSON_UNESCAPED_UNICODE);
  111. }
  112. $noticeSetting = NoticeSetting::findOrEmpty($params['id'] ?? 0);
  113. if ($noticeSetting->isEmpty()) {
  114. $noticeSetting->save($updateData);
  115. return true;
  116. }
  117. // 更新通知设置
  118. NoticeSetting::where('id', $params['id'])->update($updateData);
  119. return true;
  120. } catch (\Exception $e) {
  121. self::setError($e->getMessage());
  122. return false;
  123. }
  124. }
  125. /**
  126. * @notes 校验参数
  127. * @param $params
  128. * @throws \Exception
  129. * @author 段誉
  130. * @date 2022/3/29 11:35
  131. */
  132. public static function checkSet($params)
  133. {
  134. $noticeSetting = NoticeSetting::findOrEmpty($params['id'] ?? 0);
  135. if ($noticeSetting->isEmpty()) {
  136. throw new \Exception('通知配置不存在');
  137. }
  138. if (!isset($params['template']) || !is_array($params['template']) || count($params['template']) == 0) {
  139. throw new \Exception('模板配置不存在或格式错误');
  140. }
  141. // 通知类型
  142. $noticeType = ['system', 'sms', 'oa', 'mnp'];
  143. foreach ($params['template'] as $item) {
  144. if (!is_array($item)) {
  145. throw new \Exception('模板项格式错误');
  146. }
  147. if (!isset($item['type']) || !in_array($item['type'], $noticeType)) {
  148. throw new \Exception('模板项缺少模板类型或模板类型有误');
  149. }
  150. switch ($item['type']) {
  151. case "system";
  152. self::checkSystem($item);
  153. break;
  154. case "sms";
  155. self::checkSms($item);
  156. break;
  157. case "oa";
  158. self::checkOa($item);
  159. break;
  160. case "mnp";
  161. self::checkMnp($item);
  162. break;
  163. }
  164. }
  165. }
  166. /**
  167. * @notes 校验系统通知参数
  168. * @param $item
  169. * @throws \Exception
  170. * @author 段誉
  171. * @date 2022/3/29 11:35
  172. */
  173. public static function checkSystem($item)
  174. {
  175. if (!isset($item['title']) || !isset($item['content']) || !isset($item['status'])) {
  176. throw new \Exception('系统通知必填参数:title、content、status');
  177. }
  178. }
  179. /**
  180. * @notes 校验短信通知必填参数
  181. * @param $item
  182. * @throws \Exception
  183. * @author 段誉
  184. * @date 2022/3/29 11:35
  185. */
  186. public static function checkSms($item)
  187. {
  188. if (!isset($item['template_id']) || !isset($item['content']) || !isset($item['status'])) {
  189. throw new \Exception('短信通知必填参数:template_id、content、status');
  190. }
  191. }
  192. /**
  193. * @notes 校验微信模板消息参数
  194. * @param $item
  195. * @throws \Exception
  196. * @author 段誉
  197. * @date 2022/3/29 11:35
  198. */
  199. public static function checkOa($item)
  200. {
  201. if (!isset($item['template_id']) || !isset($item['template_sn']) || !isset($item['name']) || !isset($item['first']) || !isset($item['remark']) || !isset($item['tpl']) || !isset($item['status'])) {
  202. throw new \Exception('微信模板消息必填参数:template_id、template_sn、name、first、remark、tpl、status');
  203. }
  204. }
  205. /**
  206. * @notes 校验微信小程序提醒必填参数
  207. * @param $item
  208. * @throws \Exception
  209. * @author 段誉
  210. * @date 2022/3/29 11:35
  211. */
  212. public static function checkMnp($item)
  213. {
  214. if (!isset($item['template_id']) || !isset($item['template_sn']) || !isset($item['name']) || !isset($item['tpl']) || !isset($item['status'])) {
  215. throw new \Exception('微信模板消息必填参数:template_id、template_sn、name、tpl、status');
  216. }
  217. }
  218. public static function getInformation($params)
  219. {
  220. try {
  221. $noticeInfo = [
  222. 'confirm_code' => 0,
  223. 'massage' => '',
  224. 'to_router' => '',
  225. ];
  226. /*if(!in_array($adminId,[1,2])){
  227. return $noticeInfo;
  228. }*/
  229. //$params['business_code']
  230. switch ($params['business_code']){
  231. case 'service_work':
  232. // 用户下单待派单
  233. $serviceWorkCount = ServiceWork::where('work_status', 0)->where('work_pay_status','>', 0)->count('id');
  234. if($serviceWorkCount){
  235. $noticeInfo['confirm_code'] = 101;
  236. $noticeInfo['count'] = $serviceWorkCount;
  237. $noticeInfo['massage'] .= '您有'.$serviceWorkCount.'个工单待派单';
  238. $noticeInfo['to_router'] = '/works/service_work';
  239. }
  240. break;
  241. case 'worker_register':
  242. // 工程师入驻待审核
  243. $workerRegisterCount = MasterWorkerRegister::where('status', 0)->count('id');
  244. if($workerRegisterCount){
  245. $noticeInfo['confirm_code'] = 101;
  246. $noticeInfo['count'] = $workerRegisterCount;
  247. $noticeInfo['massage'] .= '您有'.$workerRegisterCount.'个工程师入驻待审核';
  248. $noticeInfo['to_router'] = '/worker/master_worker_register?master_worker_register.master_worker_register/lists';
  249. }
  250. break;
  251. case 'appoint_approval':
  252. // 用户更新工单上门时间超过15分钟工程师未确认
  253. $serviceWorkCount = ServiceWork::where('appoint_approval', 1)->where('refund_approval','=',0)->where('work_pay_status','>',0)->where('update_time','<', time()-15*60)->count('id');
  254. if($serviceWorkCount){
  255. $noticeInfo['confirm_code'] = 101;
  256. $noticeInfo['count'] = $serviceWorkCount;
  257. $noticeInfo['massage'] .= '您有'.$serviceWorkCount.'个工单超过15分钟工程师未确认新上门时间';
  258. $noticeInfo['to_router'] = '/works/service_work';
  259. }
  260. break;
  261. }
  262. return $noticeInfo;
  263. } catch (\Exception $e) {
  264. self::setError($e->getMessage());
  265. return false;
  266. }
  267. }
  268. }