SystemNotificationServices.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\system;
  13. use app\dao\system\SystemNotificationDao;
  14. use app\services\BaseServices;
  15. use app\services\other\TemplateMessageServices;
  16. use app\services\serve\ServeServices;
  17. use crmeb\basic\BaseManager;
  18. use crmeb\services\CacheService;
  19. use crmeb\services\template\Template;
  20. use think\facade\Cache;
  21. use crmeb\exceptions\AdminException;
  22. /**
  23. *
  24. * Class SystemNotificationServices
  25. * @package app\services\system
  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 ($cache['primary_industry']['first_class'] != 'IT科技' || $cache['primary_industry']['second_class'] != '互联网|电子商务' || $cache['secondary_industry']['first_class'] != 'IT科技' || $cache['secondary_industry']['second_class'] != 'IT软件与服务') {
  59. (new Template('wechat'))->setIndustry(1, 2);
  60. }
  61. return $cache->toArray();
  62. } catch (\Exception $e) {
  63. return $e->getMessage();
  64. }
  65. }, 0) ?: [];
  66. !is_array($industry) && $industry = [];
  67. $industry['primary_industry'] = isset($industry['primary_industry']) ? $industry['primary_industry']['first_class'] . ' | ' . $industry['primary_industry']['second_class'] : '未选择';
  68. $industry['secondary_industry'] = isset($industry['secondary_industry']) ? $industry['secondary_industry']['first_class'] . ' | ' . $industry['secondary_industry']['second_class'] : '未选择';
  69. $list = [
  70. 'industry' => $industry,
  71. 'list' => $this->dao->getList($where),
  72. ];
  73. return $list;
  74. // return $this->dao->getList($where);
  75. }
  76. /**
  77. * 获取单条数据
  78. * @param array $where
  79. * @return array
  80. * @throws \Psr\SimpleCache\InvalidArgumentException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getNotInfo(array $where)
  86. {
  87. /** @var ServeServices $ServeServices */
  88. $ServeServices = app()->make(ServeServices::class);
  89. /** @var TemplateMessageServices $TemplateMessageServices */
  90. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  91. $type = $where['type'];
  92. unset($where['type']);
  93. $info = $this->dao->getOne($where);
  94. // var_dump($info);
  95. if (!$info) return [];
  96. $info = $info->toArray();
  97. switch ($type) {
  98. case 'is_system':
  99. $info['content'] = $info['system_text'] ?? '';
  100. break;
  101. case 'is_sms':
  102. $snsCacheName = 'sms_template_list';
  103. $smsTem = [];
  104. if (Cache::has($snsCacheName)) {
  105. $smsTem = Cache::get($snsCacheName);
  106. } else {
  107. $list = $ServeServices->sms()->temps(1, 30, 0);
  108. if (isset($list['data']) && $list['data']) {
  109. foreach ($list['data'] as $item) {
  110. $smsTem[$item['temp_id']] = $item['content'];
  111. }
  112. }
  113. if ($smsTem) Cache::set($snsCacheName, $smsTem, 172800);
  114. }
  115. $info['content'] = isset($smsTem[$info['sms_id']]) ? $smsTem[$info['sms_id']] : '';
  116. break;
  117. case 'is_wechat':
  118. $wechat = $TemplateMessageServices->getOne(['notification_id' => $info['id'], 'type' => 1]);
  119. $info['templage_message_id'] = $wechat['id'] ?? '';
  120. $info['tempkey'] = $wechat['tempkey'] ?? '';
  121. $info['tempid'] = $wechat['tempid'] ?? '';
  122. $info['content'] = $wechat['content'] ?? '';
  123. break;
  124. case 'is_routine':
  125. $wechat = $TemplateMessageServices->getOne(['notification_id' => $info['id'], 'type' => 0]);
  126. $info['templage_message_id'] = $wechat['id'] ?? '';
  127. $info['tempkey'] = $wechat['tempkey'] ?? '';
  128. $info['tempid'] = $wechat['tempid'] ?? '';
  129. $info['content'] = $wechat['content'] ?? '';
  130. break;
  131. }
  132. return $info;
  133. }
  134. /**
  135. * 保存数据
  136. * @param array $data
  137. */
  138. public function saveData(array $data)
  139. {
  140. $type = $data['type'];
  141. $id = $data['id'];
  142. $info = $this->dao->get($id);
  143. if (!$info) {
  144. throw new AdminException('数据不存在');
  145. }
  146. /** @var TemplateMessageServices $TemplateMessageServices */
  147. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  148. switch ($type) {
  149. case 'is_system':
  150. $update = [];
  151. $update['name'] = $data['name'];
  152. $update['title'] = $data['title'];
  153. $update['is_system'] = $data['is_system'];
  154. $update['is_app'] = $data['is_app'];
  155. $update['system_title'] = $data['system_title'];
  156. $update['system_text'] = $data['system_text'];
  157. $res = $this->dao->update((int)$id, $update);
  158. break;
  159. case 'is_sms':
  160. $update = [];
  161. $update['name'] = $data['name'];
  162. $update['title'] = $data['title'];
  163. $update['is_sms'] = $data['is_sms'];
  164. $res = $this->dao->update((int)$id, $update);
  165. break;
  166. case 'is_wechat':
  167. $update['name'] = $data['name'];
  168. $update['title'] = $data['title'];
  169. $update['is_wechat'] = $data['is_wechat'];
  170. $res1 = $this->dao->update((int)$id, $update);
  171. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 1], ['tempid' => $data['tempid']]);
  172. $res = $res1 && $res2;
  173. break;
  174. case 'is_routine':
  175. $update['name'] = $data['name'];
  176. $update['title'] = $data['title'];
  177. $update['is_routine'] = $data['is_routine'];
  178. $res1 = $this->dao->update((int)$id, $update);
  179. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 0], ['tempid' => $data['tempid']]);
  180. $res = $res1 && $res2;
  181. break;
  182. case 'is_ent_wechat':
  183. $update['name'] = $data['name'];
  184. $update['title'] = $data['title'];
  185. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  186. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  187. $update['url'] = $data['url'];
  188. $res = $this->dao->update((int)$id, $update);
  189. break;
  190. }
  191. return $res;
  192. }
  193. }