NoticeLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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($noticeSetting['designated_user']){
  50. $designated_users = explode(',', $noticeSetting['designated_user']);
  51. foreach ($designated_users as $user_id) {
  52. $params['params']['user_id'] = $user_id;
  53. self::noticeBySceneOne($params,$noticeSetting);
  54. }
  55. }else{
  56. self::noticeBySceneOne($params,$noticeSetting);
  57. }
  58. return true;
  59. } catch (\Exception $e) {
  60. Log::info('NoticeLogic-noticeByScene:'.$e->getMessage());
  61. self::setError($e->getMessage());
  62. return false;
  63. }
  64. }
  65. public static function noticeBySceneOne($params,$noticeSetting)
  66. {
  67. try {
  68. // 合并额外参数
  69. $params = self::mergeParams($params,$noticeSetting['recipient']);
  70. $res = false;
  71. Log::info('NoticeLogic-noticeByScene:'.json_encode($params));
  72. // 短信验证码通知(具有时效性)
  73. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::VERIFICATION_CODE) {
  74. $res = (new SmsMessageService())->send($params);
  75. Log::info('NoticeLogic-noticeByScene-send:'.json_encode([$res]));
  76. }
  77. // 短信业务通知
  78. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION) {
  79. $res = (new SmsMessageService())->sendBusiness($params);
  80. Log::info('NoticeLogic-noticeByScene-sendBusiness:'.json_encode([$res]));
  81. }
  82. // 微信公众号消息模板业务通知
  83. if (isset($noticeSetting['oa_notice']['status']) && $noticeSetting['oa_notice']['status'] == YesNoEnum::YES
  84. && $noticeSetting['type'] == NoticeEnum::BUSINESS_NOTIFICATION
  85. && isset($params['params']['openid']) && !empty($params['params']['openid'])
  86. ) {
  87. $res = (new WeChatOaService())->sendTemplateMessage($params);
  88. Log::info('NoticeLogic-noticeByScene-sendTemplateMessage:'.json_encode([$res]));
  89. }
  90. return $res;
  91. } catch (\Exception $e) {
  92. Log::info('NoticeLogic-noticeByScene:'.$e->getMessage());
  93. self::setError($e->getMessage());
  94. return false;
  95. }
  96. }
  97. /**
  98. * @notes 整理参数
  99. * @param $params
  100. * @return array
  101. * @author 段誉
  102. * @date 2022/9/15 15:28
  103. */
  104. public static function mergeParams($params,$recipient)
  105. {
  106. if($recipient == 1) {
  107. // 用户相关
  108. if (!empty($params['params']['user_id'])) {
  109. $user = User::findOrEmpty($params['params']['user_id'])->toArray();
  110. $params['params']['nickname'] = $user['nickname'];
  111. $params['params']['user_name'] = $user['nickname'];
  112. $params['params']['user_sn'] = $user['sn'];
  113. $params['params']['mobile'] = $user['mobile'];
  114. $wx_user = UserAuth::where('user_id',$params['params']['user_id'])->findOrEmpty()->toArray();
  115. $params['params']['openid'] = '';
  116. $params['params']['unionid'] = '';
  117. if(isset($wx_user['unionid']) && !empty($wx_user['unionid'])) {
  118. $openid = OfficialAuth::where('unionid',$wx_user['unionid'])->where('subscribe',1)->value('openid');
  119. $params['params']['openid'] = $openid?:'';
  120. $params['params']['unionid'] = $wx_user['unionid']?:'';
  121. }
  122. }
  123. }elseif($recipient == 3) {
  124. // 工程师相关
  125. if (!empty($params['params']['user_id'])) {
  126. $user = MasterWorker::findOrEmpty($params['params']['user_id'])->toArray();
  127. $params['params']['nickname'] = $user['nickname'];
  128. $params['params']['user_name'] = $user['nickname'];
  129. $params['params']['user_sn'] = $user['sn'];
  130. $params['params']['mobile'] = $user['mobile'];
  131. $wx_user = MasterWorkerAuth::where('worker_id',$params['params']['user_id'])->findOrEmpty()->toArray();
  132. $params['params']['openid'] = '';
  133. $params['params']['unionid'] = '';
  134. if(isset($wx_user['unionid']) && !empty($wx_user['unionid'])) {
  135. $openid = OfficialAuth::where('unionid',$wx_user['unionid'])->where('subscribe',1)->value('openid');
  136. $params['params']['openid'] = $openid?:'';
  137. $params['params']['unionid'] = $wx_user['unionid']?:'';
  138. }
  139. }
  140. }else{
  141. // 后台账号
  142. if (!empty($params['params']['user_id'])) {
  143. }
  144. }
  145. if (isset($params['params']['order_id']) && !empty($params['params']['order_id'])) {
  146. // 跳转路径
  147. $jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
  148. $params['url'] = $jumpPath['url'];
  149. $params['page'] = $jumpPath['page'];
  150. }
  151. return $params;
  152. }
  153. /**
  154. * @notes 根据场景获取跳转链接
  155. * @param $sceneId
  156. * @param $extraId
  157. * @return string[]
  158. * @author 段誉
  159. * @date 2022/9/15 15:29
  160. */
  161. public static function getPathByScene($sceneId, $extraId)
  162. {
  163. // 小程序主页路径
  164. $page = '';
  165. switch (intval($sceneId)) {
  166. case 113:
  167. case 118:
  168. //case 100:
  169. $page = "/subPages/detail/detail?id={$extraId}";
  170. break;
  171. case 116:
  172. $page = '/pages/tabView/workbench';
  173. break;
  174. /*case 118:
  175. $page = '/pages/tabView/user';
  176. break;*/
  177. case 124: //返修工程师预约上门通知
  178. $page = "/subPages/return_work_detail/return_work_detail?id={$extraId}";
  179. break;
  180. case 129: //用户券页面
  181. $page = "/pages/center/discounts?type=0";
  182. break;
  183. }
  184. // 公众号主页路径
  185. $url = '';
  186. return [
  187. 'url' => $url,
  188. 'page' => $page,
  189. ];
  190. }
  191. /**
  192. * @notes 替换消息内容中的变量占位符
  193. * @param $content
  194. * @param $params
  195. * @return array|mixed|string|string[]
  196. * @author 段誉
  197. * @date 2022/9/15 15:29
  198. */
  199. public static function contentFormat($content, $params)
  200. {
  201. foreach ($params['params'] as $k => $v) {
  202. $search = '{' . $k . '}';
  203. $content = str_replace($search, $v, $content);
  204. }
  205. return $content;
  206. }
  207. /**
  208. * @notes 添加通知记录
  209. * @param $params
  210. * @param $noticeSetting
  211. * @param $sendType
  212. * @param $content
  213. * @param string $extra
  214. * @return NoticeRecord|\think\Model
  215. * @author 段誉
  216. * @date 2022/9/15 15:29
  217. */
  218. public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
  219. {
  220. return NoticeRecord::create([
  221. 'user_id' => $params['params']['user_id'] ?? 0,
  222. 'title' => self::getTitleByScene($sendType, $noticeSetting),
  223. 'content' => $content,
  224. 'scene_id' => $noticeSetting['scene_id'],
  225. 'read' => YesNoEnum::NO,
  226. 'recipient' => $noticeSetting['recipient'],
  227. 'send_type' => $sendType,
  228. 'notice_type' => $noticeSetting['type'],
  229. 'extra' => $extra,
  230. ]);
  231. }
  232. /**
  233. * @notes 通知记录标题
  234. * @param $sendType
  235. * @param $noticeSetting
  236. * @return string
  237. * @author 段誉
  238. * @date 2022/9/15 15:30
  239. */
  240. public static function getTitleByScene($sendType, $noticeSetting)
  241. {
  242. switch ($sendType) {
  243. case NoticeEnum::SMS:
  244. $title = '';
  245. break;
  246. case NoticeEnum::OA:
  247. $title = $noticeSetting['oa_notice']['name'] ?? '';
  248. break;
  249. case NoticeEnum::MNP:
  250. $title = $noticeSetting['mnp_notice']['name'] ?? '';
  251. break;
  252. default:
  253. $title = '';
  254. }
  255. return $title;
  256. }
  257. }