SystemNotificationServices.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\message\SystemNotificationDao;
  14. use app\services\BaseServices;
  15. use app\services\serve\ServeServices;
  16. use crmeb\services\template\Template;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\wechat\OfficialAccount;
  19. use think\facade\Cache;
  20. use crmeb\exceptions\AdminException;
  21. /**
  22. *
  23. * Class SystemNotificationServices
  24. * @package app\services\system
  25. * @mixin SystemNotificationDao
  26. */
  27. class SystemNotificationServices extends BaseServices
  28. {
  29. /**
  30. * SystemNotificationServices constructor.
  31. * @param SystemNotificationDao $dao
  32. */
  33. public function __construct(SystemNotificationDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 单个配置
  39. * @param int $where
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function getOneNotce(array $where)
  46. {
  47. return $this->dao->getOne($where);
  48. }
  49. /**
  50. * 后台获取列表
  51. * @param $where
  52. */
  53. public function getNotList(array $where)
  54. {
  55. $industry = CacheService::get('wechat_industry', function () {
  56. try {
  57. $cache = (new Template('wechat'))->getIndustry();
  58. if(is_object($cache)) $cache = $cache->toArray();
  59. return $cache;
  60. } catch (\Exception $e) {
  61. return $e->getMessage();
  62. }
  63. }, 0) ?: [];
  64. !is_array($industry) && $industry = [];
  65. $industry['primary_industry'] = isset($industry['primary_industry']) ? $industry['primary_industry']['first_class'] . ' | ' . $industry['primary_industry']['second_class'] : '';
  66. $industry['secondary_industry'] = isset($industry['secondary_industry']) ? $industry['secondary_industry']['first_class'] . ' | ' . $industry['secondary_industry']['second_class'] : '';
  67. $list = [
  68. 'industry' => $industry,
  69. 'list' => $this->dao->getList($where),
  70. ];
  71. return $list;
  72. }
  73. /**
  74. * 获取单条数据
  75. * @param array $where
  76. * @return array
  77. * @throws \Psr\SimpleCache\InvalidArgumentException
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public function getNotInfo(array $where)
  83. {
  84. /** @var ServeServices $ServeServices */
  85. $ServeServices = app()->make(ServeServices::class);
  86. /** @var TemplateMessageServices $TemplateMessageServices */
  87. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  88. $type = $where['type'];
  89. unset($where['type']);
  90. $info = $this->dao->getOne($where);
  91. if (!$info) return [];
  92. $info = $info->toArray();
  93. switch ($type) {
  94. case 'is_system':
  95. $info['content'] = $info['system_text'] ?? '';
  96. break;
  97. case 'is_sms':
  98. $snsCacheName = 'sms_template_list';
  99. $smsTem = [];
  100. if (Cache::has($snsCacheName)) {
  101. $smsTem = Cache::get($snsCacheName);
  102. } else {
  103. $list = $ServeServices->sms()->temps(1, 60, 0);
  104. if (isset($list['data']) && $list['data']) {
  105. foreach ($list['data'] as $item) {
  106. $smsTem[$item['temp_id']] = $item['content'];
  107. }
  108. }
  109. if ($smsTem) Cache::set($snsCacheName, $smsTem, 172800);
  110. }
  111. $info['content'] = isset($smsTem[$info['sms_id']]) ? $smsTem[$info['sms_id']] : '';
  112. break;
  113. case 'is_wechat':
  114. $wechat = $TemplateMessageServices->getTemplateOne(['notification_id' => $info['id'], 'type' => 1]);
  115. $info['tempid'] = $wechat['tempid'] ?? '';
  116. $info['content'] = $wechat['content'] ?? '';
  117. $info['wechat_id'] = $wechat['tempkey'] ?? '';
  118. break;
  119. case 'is_routine':
  120. $wechat = $TemplateMessageServices->getTemplateOne(['notification_id' => $info['id'], 'type' => 0]);
  121. $info['tempid'] = $wechat['tempid'] ?? '';
  122. $info['content'] = $wechat['content'] ?? '';
  123. $info['routine_id'] = $wechat['tempkey'] ?? '';
  124. break;
  125. }
  126. return $info;
  127. }
  128. /**
  129. * 保存数据
  130. * @param array $data
  131. */
  132. public function saveData(array $data)
  133. {
  134. $type = $data['type'];
  135. $id = $data['id'];
  136. $info = $this->dao->get($id);
  137. if (!$info) {
  138. throw new AdminException('数据不存在');
  139. }
  140. /** @var TemplateMessageServices $TemplateMessageServices */
  141. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  142. $res = true;
  143. switch ($type) {
  144. case 'is_system':
  145. $update = [];
  146. $update['name'] = $data['name'];
  147. $update['title'] = $data['title'];
  148. $update['is_system'] = $data['is_system'];
  149. $update['is_app'] = $data['is_app'];
  150. $update['system_title'] = $data['system_title'];
  151. $update['system_text'] = $data['system_text'];
  152. $res = $this->dao->update((int)$id, $update);
  153. break;
  154. case 'is_sms':
  155. $update = [];
  156. $update['name'] = $data['name'];
  157. $update['title'] = $data['title'];
  158. $update['is_sms'] = $data['is_sms'];
  159. $res = $this->dao->update((int)$id, $update);
  160. break;
  161. case 'is_wechat':
  162. $update['name'] = $data['name'];
  163. $update['title'] = $data['title'];
  164. $update['is_wechat'] = $data['is_wechat'];
  165. $res1 = $this->dao->update((int)$id, $update);
  166. $res2 = $TemplateMessageServices->update(['tempkey' => $data['wechat_id']], ['tempid' => $data['tempid']]);
  167. $res = $res1 && $res2;
  168. break;
  169. case 'is_routine':
  170. $update['name'] = $data['name'];
  171. $update['title'] = $data['title'];
  172. $update['is_routine'] = $data['is_routine'];
  173. $res1 = $this->dao->update((int)$id, $update);
  174. $res2 = $TemplateMessageServices->update(['tempkey' => $data['routine_id']], ['tempid' => $data['tempid']]);
  175. $res = $res1 && $res2;
  176. break;
  177. case 'is_ent_wechat':
  178. $update['name'] = $data['name'];
  179. $update['title'] = $data['title'];
  180. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  181. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  182. $update['url'] = $data['url'];
  183. $res = $this->dao->update((int)$id, $update);
  184. break;
  185. }
  186. return $res;
  187. }
  188. /**
  189. * 清除模版、消息缓存
  190. * @return bool
  191. */
  192. public function clearTemplateCache()
  193. {
  194. CacheService::handler('TEMPLATE')->clear();
  195. CacheService::redisHandler('NOTCEINFO')->clear();
  196. return true;
  197. }
  198. }