SystemNotificationServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\services\message;
  13. use app\dao\system\SystemNotificationDao;
  14. use app\services\BaseServices;
  15. use app\services\serve\ServeServices;
  16. use crmeb\services\CacheService;
  17. use crmeb\services\template\Template;
  18. use think\facade\Cache;
  19. use crmeb\exceptions\AdminException;
  20. /**
  21. * 消息管理类
  22. * Class SystemNotificationServices
  23. * @package app\services\system
  24. * @method value($where, $value) 条件获取某个字段的值
  25. */
  26. class SystemNotificationServices extends BaseServices
  27. {
  28. /**
  29. * SystemNotificationServices constructor.
  30. * @param SystemNotificationDao $dao
  31. */
  32. public function __construct(SystemNotificationDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 单个配置
  38. * @param int $where
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getOneNotce(array $where)
  45. {
  46. return $this->dao->getOne($where);
  47. }
  48. /**
  49. * 后台获取列表
  50. * @param $where
  51. */
  52. public function getNotList(array $where)
  53. {
  54. $industry = CacheService::get('wechat_industry', function () {
  55. try {
  56. $cache = (new Template('wechat'))->getIndustry();
  57. if ($cache['primary_industry']['first_class'] != 'IT科技' || $cache['primary_industry']['second_class'] != '互联网|电子商务' || $cache['secondary_industry']['first_class'] != 'IT科技' || $cache['secondary_industry']['second_class'] != 'IT软件与服务') {
  58. (new Template('wechat'))->setIndustry(1, 2);
  59. }
  60. return $cache->toArray();
  61. } catch (\Exception $e) {
  62. return $e->getMessage();
  63. }
  64. }, 0) ?: [];
  65. !is_array($industry) && $industry = [];
  66. $industry['primary_industry'] = isset($industry['primary_industry']) ? $industry['primary_industry']['first_class'] . ' | ' . $industry['primary_industry']['second_class'] : '未选择';
  67. $industry['secondary_industry'] = isset($industry['secondary_industry']) ? $industry['secondary_industry']['first_class'] . ' | ' . $industry['secondary_industry']['second_class'] : '未选择';
  68. $list = [
  69. 'industry' => $industry,
  70. 'list' => $this->dao->getList($where),
  71. ];
  72. return $list;
  73. // return $this->dao->getList($where);
  74. }
  75. /**
  76. * 获取单条数据
  77. * @param array $where
  78. * @return array
  79. * @throws \Psr\SimpleCache\InvalidArgumentException
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function getNotInfo(array $where)
  85. {
  86. /** @var ServeServices $ServeServices */
  87. $ServeServices = app()->make(ServeServices::class);
  88. /** @var TemplateMessageServices $TemplateMessageServices */
  89. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  90. $type = $where['type'];
  91. unset($where['type']);
  92. $info = $this->dao->getOne($where);
  93. if (!$info) return [];
  94. $info = $info->toArray();
  95. switch ($type) {
  96. case 'is_system':
  97. $info['content'] = $info['system_text'] ?? '';
  98. break;
  99. case 'is_sms':
  100. $info['content'] = $info['sms_text'];
  101. break;
  102. case 'is_wechat':
  103. $wechat = $TemplateMessageServices->getOne(['id' => $info['wechat_id'], 'type' => 1]);
  104. $info['templage_message_id'] = $wechat['id'] ?? '';
  105. $info['tempkey'] = $wechat['tempkey'] ?? '';
  106. $info['tempid'] = $wechat['tempid'] ?? '';
  107. $info['content'] = $wechat['content'] ?? '';
  108. break;
  109. case 'is_routine':
  110. $wechat = $TemplateMessageServices->getOne(['id' => $info['routine_id'], 'type' => 0]);
  111. $info['templage_message_id'] = $wechat['id'] ?? '';
  112. $info['tempkey'] = $wechat['tempkey'] ?? '';
  113. $info['tempid'] = $wechat['tempid'] ?? '';
  114. $info['content'] = $wechat['content'] ?? '';
  115. break;
  116. }
  117. return $info;
  118. }
  119. /**
  120. * 保存数据
  121. * @param array $data
  122. */
  123. public function saveData(array $data)
  124. {
  125. $type = $data['type'];
  126. $id = $data['id'];
  127. $info = $this->dao->get($id);
  128. if (!$info) {
  129. throw new AdminException(100026);
  130. }
  131. /** @var TemplateMessageServices $TemplateMessageServices */
  132. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  133. $res = null;
  134. switch ($type) {
  135. case 'is_system':
  136. $update = [];
  137. $update['name'] = $data['name'];
  138. $update['title'] = $data['title'];
  139. $update['is_system'] = $data['is_system'];
  140. $update['is_app'] = $data['is_app'];
  141. $update['system_title'] = $data['system_title'];
  142. $update['system_text'] = $data['system_text'];
  143. $res = $this->dao->update((int)$id, $update);
  144. break;
  145. case 'is_sms':
  146. $update = [];
  147. $update['name'] = $data['name'];
  148. $update['title'] = $data['title'];
  149. $update['is_sms'] = $data['is_sms'];
  150. $update['sms_id'] = $data['sms_id'];
  151. $res = $this->dao->update((int)$id, $update);
  152. break;
  153. case 'is_wechat':
  154. $update['name'] = $data['name'];
  155. $update['title'] = $data['title'];
  156. $update['is_wechat'] = $data['is_wechat'];
  157. $res1 = $this->dao->update((int)$id, $update);
  158. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 1], ['tempid' => $data['tempid']]);
  159. $res = $res1 && $res2;
  160. break;
  161. case 'is_routine':
  162. $update['name'] = $data['name'];
  163. $update['title'] = $data['title'];
  164. $update['is_routine'] = $data['is_routine'];
  165. $res1 = $this->dao->update((int)$id, $update);
  166. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 0], ['tempid' => $data['tempid']]);
  167. $res = $res1 && $res2;
  168. break;
  169. case 'is_ent_wechat':
  170. $update['name'] = $data['name'];
  171. $update['title'] = $data['title'];
  172. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  173. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  174. $update['url'] = $data['url'];
  175. $res = $this->dao->update((int)$id, $update);
  176. break;
  177. }
  178. return $res;
  179. }
  180. }