NoticeLogic.php 9.3 KB

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