NoticeLogic.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. $res = (new SmsMessageService())->send($params);
  69. }
  70. // 公众号消息
  71. if (isset($noticeSetting['oa_notice']['status']) && $noticeSetting['oa_notice']['status'] == YesNoEnum::YES) {
  72. $res = (new WechatMessageService($params['params']['user_id'], NoticeEnum::OA))->send($params);
  73. }
  74. // 微信小程序
  75. if (isset($noticeSetting['mnp_notice']['status']) && $noticeSetting['mnp_notice']['status'] == YesNoEnum::YES) {
  76. $res = (new WechatMessageService($params['params']['user_id'], NoticeEnum::MNP))->send($params);
  77. }
  78. return $res;
  79. } catch (\Exception $e) {
  80. self::setError($e->getMessage());
  81. Log::write("场景发送通知失败:{$e->getMessage()}");
  82. Log::write($e->__toString());
  83. Log::write(json_encode($params));
  84. return false;
  85. }
  86. }
  87. /**
  88. * @notes 拼装额外参数
  89. * @param $params
  90. * @return array
  91. * @author Tab
  92. * @date 2021/8/19 9:25
  93. */
  94. public static function mergeParams($params)
  95. {
  96. // 订单相关
  97. if(!empty($params['params']['order_id'])) {
  98. if (!empty($params['params']['order_type']) && $params['params']['order_type'] == 'integral') {
  99. $order = IntegralOrder::findOrEmpty($params['params']['order_id']);
  100. $order_goods = $order['goods_snap'];
  101. $params['params']['goods_name'] = $order_goods['name'];
  102. } else {
  103. $order = Order::findOrEmpty($params['params']['order_id'])->toArray();
  104. $order_goods = OrderGoods::field('goods_name,goods_snap')
  105. ->where('order_id', $params['params']['order_id'])
  106. ->findOrEmpty()
  107. ->toArray();
  108. $params['params']['goods_name'] = $order_goods['goods_name'] ?? '商品';
  109. }
  110. if(mb_strlen($params['params']['goods_name']) > 8) {
  111. $params['params']['goods_name'] = mb_substr($params['params']['goods_name'], 0, 8) . '...';
  112. }
  113. $params['params']['order_sn'] = $order['sn'];
  114. $params['params']['create_time'] = $order['create_time'];
  115. $params['params']['pay_time'] = $order['pay_time'];
  116. $params['params']['total_num'] = $order['total_num'];
  117. $params['params']['order_amount'] = $order['order_amount'];
  118. }
  119. // 用户相关
  120. if(!empty($params['params']['user_id'])) {
  121. $user = User::findOrEmpty($params['params']['user_id'])->toArray();
  122. $params['params']['nickname'] = $user['nickname'];
  123. $params['params']['user_name'] = $user['nickname'];
  124. $params['params']['user_sn'] = $user['sn'];
  125. $params['params']['mobile'] = $params['params']['mobile'] ?? $user['mobile'];
  126. }
  127. // 粉丝
  128. if(!empty($params['params']['fans_id'])) {
  129. $user = User::findOrEmpty($params['params']['fans_id'])->toArray();
  130. $params['params']['fans_name'] = $user['nickname'];
  131. $params['params']['fans_sn'] = $user['sn'];
  132. }
  133. // 跳转路径
  134. $jumpPath = self::getPathByScene($params['scene_id'], $params['params']['order_id'] ?? 0);
  135. $params['url'] = $jumpPath['url'];
  136. $params['page'] = $jumpPath['page'];
  137. return $params;
  138. }
  139. /**
  140. * @notes 根据场景获取跳转链接
  141. * @param $sceneId
  142. * @param $extraId
  143. * @return string[]
  144. * @author Tab
  145. * @date 2021/8/19 9:26
  146. */
  147. public static function getPathByScene($sceneId, $extraId)
  148. {
  149. // 小程序主页路径
  150. $page = '/pages/index/index';
  151. // 公众号主页路径
  152. $url = '/mobile/pages/index/index';
  153. if(in_array($sceneId, NoticeEnum::ORDER_SCENE)) {
  154. $url = '/mobile/pages/order_detail/order_detail?order_id='.$extraId;
  155. $page = '/pages/order_detail/order_detail?order_id='.$extraId;
  156. }
  157. return [
  158. 'url' => $url,
  159. 'page' => $page,
  160. ];
  161. }
  162. /**
  163. * @notes 格式化消息内容(替换内容中的变量占位符)
  164. * @param $content
  165. * @param $params
  166. * @return array|mixed|string|string[]
  167. * @author Tab
  168. * @date 2021/8/19 9:39
  169. */
  170. public static function contentFormat($content, $params)
  171. {
  172. foreach($params['params'] as $k => $v) {
  173. $search = '{' . $k . '}';
  174. $content = str_replace($search, $v, $content);
  175. }
  176. return $content;
  177. }
  178. /**
  179. * @notes 添加通知记录
  180. * @param $params
  181. * @param $noticeSetting
  182. * @param $sendType
  183. * @param $content
  184. * @param string $extra
  185. * @return Notice|\think\Model
  186. * @author Tab
  187. * @date 2021/8/19 10:07
  188. */
  189. public static function addNotice($params, $noticeSetting, $sendType, $content, $extra = '')
  190. {
  191. $data = [
  192. 'user_id' => $params['params']['user_id'] ?? 0,
  193. 'title' => self::getTitleByScene($sendType, $noticeSetting),
  194. 'content' => $content,
  195. 'scene_id' => $noticeSetting['scene_id'],
  196. 'read' => YesNoEnum::NO,
  197. 'recipient' => $noticeSetting['recipient'],
  198. 'send_type' => $sendType,
  199. 'notice_type' => $noticeSetting['type'],
  200. 'extra' => $extra,
  201. ];
  202. return Notice::create($data);
  203. }
  204. /**
  205. * @notes 根据场景获取标题
  206. * @param $sendType
  207. * @param $noticeSetting
  208. * @return string
  209. * @author Tab
  210. * @date 2021/8/19 9:51
  211. */
  212. public static function getTitleByScene($sendType, $noticeSetting)
  213. {
  214. switch ($sendType)
  215. {
  216. case NoticeEnum::SYSTEM:
  217. $title = $noticeSetting['system_notice']['title'] ?? '';
  218. break;
  219. case NoticeEnum::SMS:
  220. $title = '';
  221. break;
  222. case NoticeEnum::OA:
  223. $title = $noticeSetting['oa_notice']['name'] ?? '';
  224. break;
  225. case NoticeEnum::MNP:
  226. $title = $noticeSetting['mnp_notice']['name'] ?? '';
  227. break;
  228. default:
  229. $title = '';
  230. }
  231. return $title;
  232. }
  233. }