NoticeLogic.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\notice;
  20. use app\common\enum\NoticeEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\NoticeSetting;
  23. /**
  24. * 通知逻辑层
  25. * Class NoticeLogic
  26. * @package app\adminapi\logic\notice
  27. */
  28. class NoticeLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 查看通知设置详情
  32. * @param $params
  33. * @return array
  34. * @author Tab
  35. * @date 2021/8/18 19:01
  36. */
  37. public static function detail($params)
  38. {
  39. $field = 'id,scene_id,scene_name,scene_desc,system_notice,sms_notice,oa_notice,mnp_notice,support';
  40. $noticeSetting = NoticeSetting::field($field)->findOrEmpty($params['id'])->toArray();
  41. if(empty($noticeSetting)) {
  42. return [];
  43. }
  44. if(empty($noticeSetting['system_notice'])) {
  45. $noticeSetting['system_notice'] = [
  46. 'title' => '',
  47. 'content' => '',
  48. 'status' => "0",
  49. ];
  50. }
  51. $noticeSetting['system_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::SYSTEM, $noticeSetting['scene_id']);
  52. if(empty($noticeSetting['sms_notice'])) {
  53. $noticeSetting['sms_notice'] = [
  54. 'template_id' => '',
  55. 'content' => '',
  56. 'status' => "0",
  57. ];
  58. }
  59. $noticeSetting['sms_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::SMS, $noticeSetting['scene_id']);
  60. if(empty($noticeSetting['oa_notice'])) {
  61. $noticeSetting['oa_notice'] = [
  62. 'template_id' => '',
  63. 'template_sn' => '',
  64. 'name' => '',
  65. 'first' => '',
  66. 'remark' => '',
  67. 'tpl' => [],
  68. 'status' => "0",
  69. ];
  70. }
  71. $noticeSetting['oa_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::MNP, $noticeSetting['scene_id']);
  72. if(empty($noticeSetting['mnp_notice'])) {
  73. $noticeSetting['mnp_notice'] = [
  74. 'template_id' => '',
  75. 'template_sn' => '',
  76. 'name' => '',
  77. 'tpl' => [],
  78. 'status' => "0",
  79. ];
  80. }
  81. $noticeSetting['mnp_notice']['tips'] = NoticeEnum::getOperationTips(NoticeEnum::MNP, $noticeSetting['scene_id']);
  82. $noticeSetting['system_notice']['is_show'] = in_array(NoticeEnum::SYSTEM, explode(',', $noticeSetting['support'])) ? true : false;
  83. $noticeSetting['sms_notice']['is_show'] = in_array(NoticeEnum::SMS, explode(',', $noticeSetting['support'])) ? true : false;
  84. $noticeSetting['oa_notice']['is_show'] = in_array(NoticeEnum::OA, explode(',', $noticeSetting['support'])) ? true : false;
  85. $noticeSetting['mnp_notice']['is_show'] = in_array(NoticeEnum::MNP, explode(',', $noticeSetting['support'])) ? true : false;
  86. $noticeSetting['default'] = '';
  87. return $noticeSetting;
  88. }
  89. /**
  90. * @notes 通知设置
  91. * @param $params
  92. * @return bool
  93. * @author Tab
  94. * @date 2021/8/18 18:00
  95. */
  96. public static function set($params)
  97. {
  98. try {
  99. // 校验参数
  100. self::checkSet($params);
  101. // 拼装更新数据
  102. $updateData = [];
  103. foreach($params['template'] as $item) {
  104. $updateData[$item['type'] . '_notice'] = json_encode($item, JSON_UNESCAPED_UNICODE);
  105. }
  106. // 更新通知设置
  107. NoticeSetting::where('id', $params['id'])->update($updateData);
  108. return true;
  109. } catch(\Exception $e) {
  110. self::setError($e->getMessage());
  111. return false;
  112. }
  113. }
  114. /**
  115. * @notes 校验参数
  116. * @param $params
  117. * @throws \Exception
  118. * @author Tab
  119. * @date 2021/8/18 17:53
  120. */
  121. public static function checkSet($params)
  122. {
  123. if(!isset($params['id'])) {
  124. throw new \Exception('参数缺失');
  125. }
  126. $noticeSetting = NoticeSetting::findOrEmpty($params['id']);
  127. if($noticeSetting->isEmpty()) {
  128. throw new \Exception('通知配置不存在');
  129. }
  130. if(!isset($params['template']) || !is_array($params['template']) || count($params['template']) == 0) {
  131. throw new \Exception('模板配置不存在或格式错误');
  132. }
  133. foreach($params['template'] as $item) {
  134. if(!is_array($item)) {
  135. throw new \Exception('模板项格式错误');
  136. }
  137. if(!isset($item['type']) || !in_array($item['type'], ['system', 'sms', 'oa', 'mnp'])) {
  138. throw new \Exception('模板项缺少模板类型或模板类型有误');
  139. }
  140. switch ($item['type']) {
  141. case "system";
  142. self::checkSystem($item);
  143. break;
  144. case "sms";
  145. self::checkSms($item);
  146. break;
  147. case "oa";
  148. self::checkOa($item);
  149. break;
  150. case "mnp";
  151. self::checkMnp($item);
  152. break;
  153. }
  154. }
  155. }
  156. /**
  157. * @notes 校验系统通知参数
  158. * @param $item
  159. * @throws \Exception
  160. * @author Tab
  161. * @date 2021/8/18 17:30
  162. */
  163. public static function checkSystem($item)
  164. {
  165. if(!isset($item['title']) || !isset($item['content']) || !isset($item['status'])) {
  166. throw new \Exception('系统通知必填参数:title、content、status');
  167. }
  168. }
  169. /**
  170. * @notes 校验短信通知必填参数
  171. * @param $item
  172. * @throws \Exception
  173. * @author Tab
  174. * @date 2021/8/18 17:47
  175. */
  176. public static function checkSms($item)
  177. {
  178. if(!isset($item['template_id']) || !isset($item['content']) || !isset($item['status'])) {
  179. throw new \Exception('短信通知必填参数:template_id、content、status');
  180. }
  181. }
  182. /**
  183. * @notes 校验微信模板消息参数
  184. * @param $item
  185. * @throws \Exception
  186. * @author Tab
  187. * @date 2021/8/18 17:51
  188. */
  189. public static function checkOa($item)
  190. {
  191. if( !isset($item['template_id']) || !isset($item['template_sn']) || !isset($item['name']) || !isset($item['first']) || !isset($item['remark']) || !isset($item['tpl']) || !isset($item['status'])) {
  192. throw new \Exception('微信模板消息必填参数:template_id、template_sn、name、first、remark、tpl、status');
  193. }
  194. }
  195. /**
  196. * @notes 校验微信小程序提醒必填参数
  197. * @param $item
  198. * @throws \Exception
  199. * @author Tab
  200. * @date 2021/8/18 17:53
  201. */
  202. public static function checkMnp($item)
  203. {
  204. if( !isset($item['template_id']) || !isset($item['template_sn']) || !isset($item['name']) || !isset($item['tpl']) || !isset($item['status'])) {
  205. throw new \Exception('微信模板消息必填参数:template_id、template_sn、name、tpl、status');
  206. }
  207. }
  208. }