NoticeLogic.php 8.9 KB

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