NoticeLogic.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\logic;
  20. use app\common\enum\NoticeEnum;
  21. use app\common\enum\YesNoEnum;
  22. use app\common\model\IntegralOrder;
  23. use app\common\model\Notice;
  24. use app\common\model\NoticeSetting;
  25. use app\common\model\Order;
  26. use app\common\model\OrderGoods;
  27. use app\common\model\User;
  28. use app\common\service\sms\SmsMessageService;
  29. use app\common\service\WechatMessageService;
  30. use think\facade\Log;
  31. /**
  32. * 通知逻辑层
  33. * Class NoticeLogic
  34. * @package app\common\logic
  35. */
  36. class NoticeLogic extends BaseLogic
  37. {
  38. /**
  39. * @notes 根据不同的场景发送通知
  40. * @param $params
  41. * @return bool
  42. * @author Tab
  43. * @date 2021/8/19 9:26
  44. */
  45. public static function noticeByScene($params)
  46. {
  47. try {
  48. outFileLog($params,'noticeLogic','params');
  49. $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray();
  50. if(empty($noticeSetting)) {
  51. throw new \Exception('找不到对应场景的配置');
  52. }
  53. // 合并额外参数
  54. $params = self::mergeParams($params);
  55. $res = false;
  56. self::setError('通知功能未开启');
  57. // 系统通知
  58. outFileLog($noticeSetting,'noticeLogic','$noticeSetting');
  59. if(isset($noticeSetting['system_notice']['status']) && $noticeSetting['system_notice']['status'] == YesNoEnum::YES) {
  60. $content = self::contentFormat($noticeSetting['system_notice']['content'], $params);
  61. $notice = self::addNotice($params, $noticeSetting, NoticeEnum::SYSTEM, $content);
  62. if($notice) {
  63. $res = true;
  64. }
  65. }
  66. // 短信通知
  67. if (isset($noticeSetting['sms_notice']['status']) && $noticeSetting['sms_notice']['status'] == YesNoEnum::YES) {
  68. outFileLog($params,'noticeLogic','短信通知');
  69. $res = (new SmsMessageService())->send($params);
  70. }
  71. // 公众号消息
  72. if (isset($noticeSetting['oa_notice']['status']) && $noticeSetting['oa_notice']['status'] == YesNoEnum::YES) {
  73. $res = (new WechatMessageService($params['params']['user_id'], NoticeEnum::OA))->send($params);
  74. }
  75. // 微信小程序
  76. if (isset($noticeSetting['mnp_notice']['status']) && $noticeSetting['mnp_notice']['status'] == YesNoEnum::YES) {
  77. $res = (new WechatMessageService($params['params']['user_id'], NoticeEnum::MNP))->send($params);
  78. }
  79. return $res;
  80. } catch (\Exception $e) {
  81. self::setError($e->getMessage());
  82. Log::write("场景发送通知失败:{$e->getMessage()}");
  83. Log::write($e->__toString());
  84. Log::write(json_encode($params));
  85. return false;
  86. }
  87. }
  88. /**
  89. * @notes 拼装额外参数
  90. * @param $params
  91. * @return array
  92. * @author Tab
  93. * @date 2021/8/19 9:25
  94. */
  95. public static function mergeParams($params)
  96. {
  97. // 订单相关
  98. if(!empty($params['params']['order_id'])) {
  99. if (!empty($params['params']['order_type']) && $params['params']['order_type'] == 'integral') {
  100. $order = IntegralOrder::findOrEmpty($params['params']['order_id']);
  101. $order_goods = $order['goods_snap'];
  102. $params['params']['goods_name'] = $order_goods['name'];
  103. } else {
  104. $order = Order::findOrEmpty($params['params']['order_id'])->toArray();
  105. $order_goods = OrderGoods::field('goods_name,goods_snap')
  106. ->where('order_id', $params['params']['order_id'])
  107. ->findOrEmpty()
  108. ->toArray();
  109. $params['params']['goods_name'] = $order_goods['goods_name'] ?? '商品';
  110. }
  111. if(mb_strlen($params['params']['goods_name']) > 8) {
  112. $params['params']['goods_name'] = mb_substr($params['params']['goods_name'], 0, 8) . '...';
  113. }
  114. $params['params']['order_sn'] = $order['sn'];
  115. $params['params']['create_time'] = $order['create_time'];
  116. $params['params']['pay_time'] = $order['pay_time'];
  117. $params['params']['total_num'] = $order['total_num'];
  118. $params['params']['order_amount'] = $order['order_amount'];
  119. }
  120. // 用户相关
  121. if(!empty($params['params']['user_id'])) {
  122. $user = User::findOrEmpty($params['params']['user_id'])->toArray();
  123. $params['params']['nickname'] = $user['nickname'];
  124. $params['params']['user_name'] = $user['nickname'];
  125. $params['params']['user_sn'] = $user['sn'];
  126. $params['params']['mobile'] = $params['params']['mobile'] ?? $user['mobile'];
  127. }
  128. // 粉丝
  129. if(!empty($params['params']['fans_id'])) {
  130. $user = User::findOrEmpty($params['params']['fans_id'])->toArray();
  131. $params['params']['fans_name'] = $user['nickname'];
  132. $params['params']['fans_sn'] = $user['sn'];
  133. }
  134. // 跳转路径
  135. $jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
  136. $params['url'] = $jumpPath['url'];
  137. $params['page'] = $jumpPath['page'];
  138. return $params;
  139. }
  140. /**
  141. * @notes 根据场景获取跳转链接
  142. * @param $sceneId
  143. * @param $extraId
  144. * @return string[]
  145. * @author Tab
  146. * @date 2021/8/19 9:26
  147. */
  148. public static function getPathByScene($sceneId, $extraId)
  149. {
  150. // 小程序主页路径
  151. $page = '/pages/index/index';
  152. // 公众号主页路径
  153. $url = '/mobile/pages/index/index';
  154. if(in_array($sceneId, NoticeEnum::ORDER_SCENE)) {
  155. $url = '/mobile/pages/order_detail/order_detail?order_id='.$extraId;
  156. $page = '/pages/order_detail/order_detail?order_id='.$extraId;
  157. }
  158. return [
  159. 'url' => $url,
  160. 'page' => $page,
  161. ];
  162. }
  163. /**
  164. * @notes 格式化消息内容(替换内容中的变量占位符)
  165. * @param $content
  166. * @param $params
  167. * @return array|mixed|string|string[]
  168. * @author Tab
  169. * @date 2021/8/19 9:39
  170. */
  171. public static function contentFormat($content, $params)
  172. {
  173. foreach($params['params'] as $k => $v) {
  174. $search = '{' . $k . '}';
  175. $content = str_replace($search, $v, $content);
  176. }
  177. return $content;
  178. }
  179. /**
  180. * @notes 添加通知记录
  181. * @param $params
  182. * @param $noticeSetting
  183. * @param $sendType
  184. * @param $content
  185. * @param string $extra
  186. * @return Notice|\think\Model
  187. * @author Tab
  188. * @date 2021/8/19 10:07
  189. */
  190. public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
  191. {
  192. $data = [
  193. 'user_id' => $params['params']['user_id'] ?? 0,
  194. 'title' => self::getTitleByScene($sendType, $noticeSetting),
  195. 'content' => $content,
  196. 'scene_id' => $noticeSetting['scene_id'],
  197. 'read' => YesNoEnum::NO,
  198. 'recipient' => $noticeSetting['recipient'],
  199. 'send_type' => $sendType,
  200. 'notice_type' => $noticeSetting['type'],
  201. 'extra' => $extra,
  202. ];
  203. return Notice::create($data);
  204. }
  205. /**
  206. * @notes 根据场景获取标题
  207. * @param $sendType
  208. * @param $noticeSetting
  209. * @return string
  210. * @author Tab
  211. * @date 2021/8/19 9:51
  212. */
  213. public static function getTitleByScene($sendType, $noticeSetting)
  214. {
  215. switch ($sendType)
  216. {
  217. case NoticeEnum::SYSTEM:
  218. $title = $noticeSetting['system_notice']['title'] ?? '';
  219. break;
  220. case NoticeEnum::SMS:
  221. $title = '';
  222. break;
  223. case NoticeEnum::OA:
  224. $title = $noticeSetting['oa_notice']['name'] ?? '';
  225. break;
  226. case NoticeEnum::MNP:
  227. $title = $noticeSetting['mnp_notice']['name'] ?? '';
  228. break;
  229. default:
  230. $title = '';
  231. }
  232. return $title;
  233. }
  234. }