SystemNotificationServices.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\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. */
  25. class SystemNotificationServices extends BaseServices
  26. {
  27. /**
  28. * SystemNotificationServices constructor.
  29. * @param SystemNotificationDao $dao
  30. */
  31. public function __construct(SystemNotificationDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 单个配置
  37. * @param int $where
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getOneNotce(array $where)
  44. {
  45. return $this->dao->getOne($where);
  46. }
  47. /**
  48. * 后台获取列表
  49. * @param $where
  50. */
  51. public function getNotList(array $where)
  52. {
  53. $industry = CacheService::get('wechat_industry', function () {
  54. try {
  55. $cache = (new Template('wechat'))->getIndustry();
  56. if ($cache['primary_industry']['first_class'] != 'IT科技' || $cache['primary_industry']['second_class'] != '互联网|电子商务' || $cache['secondary_industry']['first_class'] != 'IT科技' || $cache['secondary_industry']['second_class'] != 'IT软件与服务') {
  57. (new Template('wechat'))->setIndustry(1, 2);
  58. }
  59. return $cache->toArray();
  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. // return $this->dao->getList($where);
  73. }
  74. /**
  75. * 获取单条数据
  76. * @param array $where
  77. * @return array
  78. * @throws \Psr\SimpleCache\InvalidArgumentException
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function getNotInfo(array $where)
  84. {
  85. /** @var ServeServices $ServeServices */
  86. $ServeServices = app()->make(ServeServices::class);
  87. /** @var TemplateMessageServices $TemplateMessageServices */
  88. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  89. $type = $where['type'];
  90. unset($where['type']);
  91. $info = $this->dao->getOne($where);
  92. // var_dump($info);
  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. $snsCacheName = 'sms_template_list';
  101. $smsTem = [];
  102. if (Cache::has($snsCacheName)) {
  103. $smsTem = Cache::get($snsCacheName);
  104. } else {
  105. $list = $ServeServices->sms()->temps(1, 30, 0);
  106. if (isset($list['data']) && $list['data']) {
  107. foreach ($list['data'] as $item) {
  108. $smsTem[$item['temp_id']] = $item['content'];
  109. }
  110. }
  111. if ($smsTem) Cache::set($snsCacheName, $smsTem, 172800);
  112. }
  113. $info['content'] = $smsTem[$info['sms_id']] ?? '';
  114. break;
  115. case 'is_wechat':
  116. $wechat = $TemplateMessageServices->getOne(['id' => $info['wechat_id'], 'type' => 1]);
  117. $info['templage_message_id'] = $wechat['id'] ?? '';
  118. $info['tempkey'] = $wechat['tempkey'] ?? '';
  119. $info['tempid'] = $wechat['tempid'] ?? '';
  120. $info['content'] = $wechat['content'] ?? '';
  121. break;
  122. case 'is_routine':
  123. $wechat = $TemplateMessageServices->getOne(['id' => $info['routine_id'], 'type' => 0]);
  124. $info['templage_message_id'] = $wechat['id'] ?? '';
  125. $info['tempkey'] = $wechat['tempkey'] ?? '';
  126. $info['tempid'] = $wechat['tempid'] ?? '';
  127. $info['content'] = $wechat['content'] ?? '';
  128. break;
  129. }
  130. return $info;
  131. }
  132. /**
  133. * 保存数据
  134. * @param array $data
  135. */
  136. public function saveData(array $data)
  137. {
  138. $type = $data['type'];
  139. $id = $data['id'];
  140. $info = $this->dao->get($id);
  141. if (!$info) {
  142. throw new AdminException('数据不存在');
  143. }
  144. /** @var TemplateMessageServices $TemplateMessageServices */
  145. $TemplateMessageServices = app()->make(TemplateMessageServices::class);
  146. $res = null;
  147. switch ($type) {
  148. case 'is_system':
  149. $update = [];
  150. $update['name'] = $data['name'];
  151. $update['title'] = $data['title'];
  152. $update['is_system'] = $data['is_system'];
  153. $update['is_app'] = $data['is_app'];
  154. $update['system_title'] = $data['system_title'];
  155. $update['system_text'] = $data['system_text'];
  156. $res = $this->dao->update((int)$id, $update);
  157. break;
  158. case 'is_sms':
  159. $update = [];
  160. $update['name'] = $data['name'];
  161. $update['title'] = $data['title'];
  162. $update['is_sms'] = $data['is_sms'];
  163. $res = $this->dao->update((int)$id, $update);
  164. break;
  165. case 'is_wechat':
  166. $update['name'] = $data['name'];
  167. $update['title'] = $data['title'];
  168. $update['is_wechat'] = $data['is_wechat'];
  169. $res1 = $this->dao->update((int)$id, $update);
  170. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 1], ['tempid' => $data['tempid']]);
  171. $res = $res1 && $res2;
  172. break;
  173. case 'is_routine':
  174. $update['name'] = $data['name'];
  175. $update['title'] = $data['title'];
  176. $update['is_routine'] = $data['is_routine'];
  177. $res1 = $this->dao->update((int)$id, $update);
  178. $res2 = $TemplateMessageServices->update(['notification_id' => $id, 'type' => 0], ['tempid' => $data['tempid']]);
  179. $res = $res1 && $res2;
  180. break;
  181. case 'is_ent_wechat':
  182. $update['name'] = $data['name'];
  183. $update['title'] = $data['title'];
  184. $update['is_ent_wechat'] = $data['is_ent_wechat'];
  185. $update['ent_wechat_text'] = $data['ent_wechat_text'];
  186. $update['url'] = $data['url'];
  187. $res = $this->dao->update((int)$id, $update);
  188. break;
  189. }
  190. return $res;
  191. }
  192. }