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\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. $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(100026);
  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. $update['sms_id'] = $data['sms_id'];
  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. }