NoticeLogic.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\logic;
  15. use app\common\enum\notice\NoticeEnum;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\model\channel\OfficialAuth;
  18. use app\common\model\notice\NoticeRecord;
  19. use app\common\model\notice\NoticeSetting;
  20. use app\common\model\user\User;
  21. use app\common\model\user\UserAuth;
  22. use app\common\model\master_worker\MasterWorker;
  23. use app\common\model\master_worker\MasterWorkerAuth;
  24. use app\common\service\sms\SmsMessageService;
  25. use app\common\service\wechat\WeChatOaService;
  26. use think\facade\Log;
  27. /**
  28. * 通知逻辑层
  29. * Class NoticeLogic
  30. * @package app\common\logic
  31. */
  32. class NoticeLogic extends BaseLogic
  33. {
  34. /**
  35. * @notes 根据场景发送短信
  36. * @param $params
  37. * @return bool
  38. * @author 段誉
  39. * @date 2022/9/15 15:28
  40. */
  41. public static function noticeByScene($params)
  42. {
  43. try {
  44. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  45. if (empty($noticeSetting)) {
  46. throw new \Exception('找不到对应场景的配置');
  47. }
  48. $noticeSetting['designated_user'] && $params['params']['user_id'] = $noticeSetting['designated_user'];
  49. /*if(empty($params['params']['user_id']) && (!isset($params['params']['mobile']) && !isset($params['params']['openid']))){
  50. throw new \Exception('发送对象不能为空');
  51. }*/
  52. // 合并额外参数
  53. $params = self::mergeParams($params,$noticeSetting['recipient']);
  54. $res = false;
  55. self::setError('发送通知失败');
  56. Log::info('NoticeLogic-noticeByScene:'.json_encode($params));
  57. // 短信验证码通知(具有时效性)
  58. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::VERIFICATION_CODE) {
  59. $res = (new SmsMessageService())->send($params);
  60. Log::info('NoticeLogic-noticeByScene-send:'.json_encode([$res]));
  61. }
  62. // 短信业务通知
  63. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION) {
  64. $res = (new SmsMessageService())->sendBusiness($params);
  65. Log::info('NoticeLogic-noticeByScene-sendBusiness:'.json_encode([$res]));
  66. }
  67. // 微信公众号消息模板业务通知
  68. if (isset($noticeSetting['oa_notice']['status']) && $noticeSetting['oa_notice']['status'] == YesNoEnum::YES
  69. && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION
  70. && isset($params['params']['openid']) && !empty($params['params']['openid'])
  71. ) {
  72. $res = (new WeChatOaService())->sendTemplateMessage($params);
  73. Log::info('NoticeLogic-noticeByScene-sendTemplateMessage:'.json_encode([$res]));
  74. }
  75. return $res;
  76. } catch (\Exception $e) {
  77. Log::info('NoticeLogic-noticeByScene:'.$e->getMessage());
  78. self::setError($e->getMessage());
  79. return false;
  80. }
  81. }
  82. /**
  83. * @notes 整理参数
  84. * @param $params
  85. * @return array
  86. * @author 段誉
  87. * @date 2022/9/15 15:28
  88. */
  89. public static function mergeParams($params,$recipient)
  90. {
  91. if($recipient == 1) {
  92. // 用户相关
  93. if (!empty($params['params']['user_id'])) {
  94. $user = User::findOrEmpty($params['params']['user_id'])->toArray();
  95. $params['params']['nickname'] = $user['nickname'];
  96. $params['params']['user_name'] = $user['nickname'];
  97. $params['params']['user_sn'] = $user['sn'];
  98. $params['params']['mobile'] = $user['mobile'];
  99. $wx_user = UserAuth::where('user_id',$params['params']['user_id'])->findOrEmpty()->toArray();
  100. $params['params']['openid'] = '';
  101. $params['params']['unionid'] = '';
  102. if(isset($wx_user['unionid']) && !empty($wx_user['unionid'])) {
  103. $openid = OfficialAuth::where('unionid',$wx_user['unionid'])->where('subscribe',1)->value('openid');
  104. $params['params']['openid'] = $openid?:'';
  105. $params['params']['unionid'] = $wx_user['unionid']?:'';
  106. }
  107. }
  108. }elseif($recipient == 3) {
  109. // 工程师相关
  110. if (!empty($params['params']['user_id'])) {
  111. $user = MasterWorker::findOrEmpty($params['params']['user_id'])->toArray();
  112. $params['params']['nickname'] = $user['nickname'];
  113. $params['params']['user_name'] = $user['nickname'];
  114. $params['params']['user_sn'] = $user['sn'];
  115. $params['params']['mobile'] = $user['mobile'];
  116. $wx_user = MasterWorkerAuth::where('worker_id',$params['params']['user_id'])->findOrEmpty()->toArray();
  117. $params['params']['openid'] = '';
  118. $params['params']['unionid'] = '';
  119. if(isset($wx_user['unionid']) && !empty($wx_user['unionid'])) {
  120. $openid = OfficialAuth::where('unionid',$wx_user['unionid'])->where('subscribe',1)->value('openid');
  121. $params['params']['openid'] = $openid?:'';
  122. $params['params']['unionid'] = $wx_user['unionid']?:'';
  123. }
  124. }
  125. }else{
  126. // 后台账号
  127. if (!empty($params['params']['user_id'])) {
  128. }
  129. }
  130. if (isset($params['params']['order_id']) && !empty($params['params']['order_id'])) {
  131. // 跳转路径
  132. $jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
  133. $params['url'] = $jumpPath['url'];
  134. $params['page'] = $jumpPath['page'];
  135. }
  136. return $params;
  137. }
  138. /**
  139. * @notes 根据场景获取跳转链接
  140. * @param $sceneId
  141. * @param $extraId
  142. * @return string[]
  143. * @author 段誉
  144. * @date 2022/9/15 15:29
  145. */
  146. public static function getPathByScene($sceneId, $extraId)
  147. {
  148. // 小程序主页路径
  149. $page = '';
  150. switch (intval($sceneId)) {
  151. case 113:
  152. case 118:
  153. //case 100:
  154. $page = "/subPages/detail/detail?id={$extraId}";
  155. break;
  156. case 116:
  157. $page = '/pages/tabView/workbench';
  158. break;
  159. /*case 118:
  160. $page = '/pages/tabView/user';
  161. break;*/
  162. case 124: //返修工程师预约上门通知
  163. $page = "/subPages/return_work_detail/return_work_detail?id={$extraId}";
  164. break;
  165. }
  166. // 公众号主页路径
  167. $url = '';
  168. return [
  169. 'url' => $url,
  170. 'page' => $page,
  171. ];
  172. }
  173. /**
  174. * @notes 替换消息内容中的变量占位符
  175. * @param $content
  176. * @param $params
  177. * @return array|mixed|string|string[]
  178. * @author 段誉
  179. * @date 2022/9/15 15:29
  180. */
  181. public static function contentFormat($content, $params)
  182. {
  183. foreach ($params['params'] as $k => $v) {
  184. $search = '{' . $k . '}';
  185. $content = str_replace($search, $v, $content);
  186. }
  187. return $content;
  188. }
  189. /**
  190. * @notes 添加通知记录
  191. * @param $params
  192. * @param $noticeSetting
  193. * @param $sendType
  194. * @param $content
  195. * @param string $extra
  196. * @return NoticeRecord|\think\Model
  197. * @author 段誉
  198. * @date 2022/9/15 15:29
  199. */
  200. public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
  201. {
  202. return NoticeRecord::create([
  203. 'user_id' => $params['params']['user_id'] ?? 0,
  204. 'title' => self::getTitleByScene($sendType, $noticeSetting),
  205. 'content' => $content,
  206. 'scene_id' => $noticeSetting['scene_id'],
  207. 'read' => YesNoEnum::NO,
  208. 'recipient' => $noticeSetting['recipient'],
  209. 'send_type' => $sendType,
  210. 'notice_type' => $noticeSetting['type'],
  211. 'extra' => $extra,
  212. ]);
  213. }
  214. /**
  215. * @notes 通知记录标题
  216. * @param $sendType
  217. * @param $noticeSetting
  218. * @return string
  219. * @author 段誉
  220. * @date 2022/9/15 15:30
  221. */
  222. public static function getTitleByScene($sendType, $noticeSetting)
  223. {
  224. switch ($sendType) {
  225. case NoticeEnum::SMS:
  226. $title = '';
  227. break;
  228. case NoticeEnum::OA:
  229. $title = $noticeSetting['oa_notice']['name'] ?? '';
  230. break;
  231. case NoticeEnum::MNP:
  232. $title = $noticeSetting['mnp_notice']['name'] ?? '';
  233. break;
  234. default:
  235. $title = '';
  236. }
  237. return $title;
  238. }
  239. }