NoticeLogic.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\notice\NoticeSetting;
  18. /**
  19. * 通知逻辑层
  20. * Class NoticeLogic
  21. * @package app\adminapi\logic\notice
  22. */
  23. class NoticeLogic extends BaseLogic
  24. {
  25. /**
  26. * @notes 查看通知设置详情
  27. * @param $params
  28. * @return array
  29. * @author 段誉
  30. * @date 2022/3/29 11:34
  31. */
  32. public static function detail($params)
  33. {
  34. $field = 'id,type,scene_id,scene_name,scene_desc,system_notice,sms_notice,oa_notice,mnp_notice,support';
  35. $noticeSetting = NoticeSetting::field($field)->findOrEmpty($params['id'])->toArray();
  36. if (empty($noticeSetting)) {
  37. return [];
  38. }
  39. if (empty($noticeSetting['system_notice'])) {
  40. $noticeSetting['system_notice'] = [
  41. 'title' => '',
  42. 'content' => '',
  43. 'status' => 0,
  44. ];
  45. }
  46. $noticeSetting['system_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::SYSTEM, $noticeSetting['scene_id']);
  47. if (empty($noticeSetting['sms_notice'])) {
  48. $noticeSetting['sms_notice'] = [
  49. 'template_id' => '',
  50. 'content' => '',
  51. 'status' => 0,
  52. ];
  53. }
  54. $noticeSetting['sms_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::SMS, $noticeSetting['scene_id']);
  55. if (empty($noticeSetting['oa_notice'])) {
  56. $noticeSetting['oa_notice'] = [
  57. 'template_id' => '',
  58. 'template_sn' => '',
  59. 'name' => '',
  60. 'first' => '',
  61. 'remark' => '',
  62. 'tpl' => [],
  63. 'status' => 0,
  64. ];
  65. }
  66. $noticeSetting['oa_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::MNP, $noticeSetting['scene_id']);
  67. if (empty($noticeSetting['mnp_notice'])) {
  68. $noticeSetting['mnp_notice'] = [
  69. 'template_id' => '',
  70. 'template_sn' => '',
  71. 'name' => '',
  72. 'tpl' => [],
  73. 'status' => 0,
  74. ];
  75. }
  76. $noticeSetting['mnp_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::MNP, $noticeSetting['scene_id']);
  77. $noticeSetting['system_notice']['is_show'] = in_array(NoticeEnum::SYSTEM, explode(',', $noticeSetting['support']));
  78. $noticeSetting['sms_notice']['is_show'] = in_array(NoticeEnum::SMS, explode(',', $noticeSetting['support']));
  79. $noticeSetting['oa_notice']['is_show'] = in_array(NoticeEnum::OA, explode(',', $noticeSetting['support']));
  80. $noticeSetting['mnp_notice']['is_show'] = in_array(NoticeEnum::MNP, explode(',', $noticeSetting['support']));
  81. $noticeSetting['default'] = '';
  82. $noticeSetting['type'] = NoticeEnum::getTypeDesc($noticeSetting['type']);
  83. return $noticeSetting;
  84. }
  85. /**
  86. * @notes 通知设置
  87. * @param $params
  88. * @return bool
  89. * @author 段誉
  90. * @date 2022/3/29 11:34
  91. */
  92. public static function set($params)
  93. {
  94. try {
  95. // 校验参数
  96. self::checkSet($params);
  97. // 拼装更新数据
  98. $updateData = [];
  99. foreach ($params['template'] as $item) {
  100. $updateData[$item['type'] . '_notice'] = json_encode($item, JSON_UNESCAPED_UNICODE);
  101. }
  102. // 更新通知设置
  103. NoticeSetting::where('id', $params['id'])->update($updateData);
  104. return true;
  105. } catch (\Exception $e) {
  106. self::setError($e->getMessage());
  107. return false;
  108. }
  109. }
  110. /**
  111. * @notes 校验参数
  112. * @param $params
  113. * @throws \Exception
  114. * @author 段誉
  115. * @date 2022/3/29 11:35
  116. */
  117. public static function checkSet($params)
  118. {
  119. $noticeSetting = NoticeSetting::findOrEmpty($params['id'] ?? 0);
  120. if ($noticeSetting->isEmpty()) {
  121. throw new \Exception('通知配置不存在');
  122. }
  123. if (!isset($params['template']) || !is_array($params['template']) || count($params['template']) == 0) {
  124. throw new \Exception('模板配置不存在或格式错误');
  125. }
  126. // 通知类型
  127. $noticeType = ['system', 'sms', 'oa', 'mnp'];
  128. foreach ($params['template'] as $item) {
  129. if (!is_array($item)) {
  130. throw new \Exception('模板项格式错误');
  131. }
  132. if (!isset($item['type']) || !in_array($item['type'], $noticeType)) {
  133. throw new \Exception('模板项缺少模板类型或模板类型有误');
  134. }
  135. switch ($item['type']) {
  136. case "system";
  137. self::checkSystem($item);
  138. break;
  139. case "sms";
  140. self::checkSms($item);
  141. break;
  142. case "oa";
  143. self::checkOa($item);
  144. break;
  145. case "mnp";
  146. self::checkMnp($item);
  147. break;
  148. }
  149. }
  150. }
  151. /**
  152. * @notes 校验系统通知参数
  153. * @param $item
  154. * @throws \Exception
  155. * @author 段誉
  156. * @date 2022/3/29 11:35
  157. */
  158. public static function checkSystem($item)
  159. {
  160. if (!isset($item['title']) || !isset($item['content']) || !isset($item['status'])) {
  161. throw new \Exception('系统通知必填参数:title、content、status');
  162. }
  163. }
  164. /**
  165. * @notes 校验短信通知必填参数
  166. * @param $item
  167. * @throws \Exception
  168. * @author 段誉
  169. * @date 2022/3/29 11:35
  170. */
  171. public static function checkSms($item)
  172. {
  173. if (!isset($item['template_id']) || !isset($item['content']) || !isset($item['status'])) {
  174. throw new \Exception('短信通知必填参数:template_id、content、status');
  175. }
  176. }
  177. /**
  178. * @notes 校验微信模板消息参数
  179. * @param $item
  180. * @throws \Exception
  181. * @author 段誉
  182. * @date 2022/3/29 11:35
  183. */
  184. public static function checkOa($item)
  185. {
  186. if (!isset($item['template_id']) || !isset($item['template_sn']) || !isset($item['name']) || !isset($item['first']) || !isset($item['remark']) || !isset($item['tpl']) || !isset($item['status'])) {
  187. throw new \Exception('微信模板消息必填参数:template_id、template_sn、name、first、remark、tpl、status');
  188. }
  189. }
  190. /**
  191. * @notes 校验微信小程序提醒必填参数
  192. * @param $item
  193. * @throws \Exception
  194. * @author 段誉
  195. * @date 2022/3/29 11:35
  196. */
  197. public static function checkMnp($item)
  198. {
  199. if (!isset($item['template_id']) || !isset($item['template_sn']) || !isset($item['name']) || !isset($item['tpl']) || !isset($item['status'])) {
  200. throw new \Exception('微信模板消息必填参数:template_id、template_sn、name、tpl、status');
  201. }
  202. }
  203. }