GrouponFinish.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace addons\shopro\notification\activity;
  3. use addons\shopro\notification\Notification;
  4. use addons\shopro\notification\traits\Notification as NotificationTrait;
  5. use app\admin\model\shopro\order\Order as OrderModel;
  6. /**
  7. * 拼团完成
  8. */
  9. class GrouponFinish extends Notification
  10. {
  11. use NotificationTrait;
  12. // 队列延迟时间,必须继承 ShouldQueue 接口
  13. public $delay = 0;
  14. public $receiver_type = 'user'; // 接收人:user=用户
  15. // 消息类型 Notification::$notificationType
  16. public $notification_type = 'shop';
  17. // 发送类型
  18. public $event = 'groupon_finish';
  19. // 额外数据
  20. public $data = [];
  21. public $template = [
  22. 'MessageDefaultContent' => '您的拼团已成功,商品:{goods_title},成团人数:{groupon_num},请注意查收',
  23. // 'WechatOfficialAccount' => [
  24. // 'temp_no' => 'OPENTM400932513',
  25. // 'fields' => [
  26. // // 'first' => '您的拼团已成功',
  27. // // 'keyword1' => 'goods_title', // 商品名称
  28. // // 'keyword2' => 'groupon_leader_nickname', // 团长
  29. // // 'keyword3' => 'groupon_num', // 成团人数
  30. // // 'remark' => '您的拼团已成功,点击查看详情',
  31. // [
  32. // "template_field" => "first",
  33. // "value" => '您的拼团已成功',
  34. // ],
  35. // [
  36. // "name" => "商品名称",
  37. // "field" => "goods_title",
  38. // "template_field" => "keyword1",
  39. // ],
  40. // [
  41. // "name" => "团长",
  42. // "field" => "groupon_leader_nickname",
  43. // "template_field" => "keyword2",
  44. // ],
  45. // [
  46. // "name" => "成团人数",
  47. // "field" => "groupon_num",
  48. // "template_field" => "keyword3",
  49. // ],
  50. // [
  51. // "template_field" => "remark",
  52. // "value" => '您的拼团已成功,点击查看详情',
  53. // ]
  54. // ],
  55. // ],
  56. 'WechatOfficialAccount' => [
  57. 'temp_no' => false, // 目前公众号类目模板库,找不到符合条件模板
  58. 'keywords' => [],
  59. 'fields' => [],
  60. // 'temp_no' => '44579',
  61. // 'keywords' => ['商品名称', '拼单时间'],
  62. // 'fields' => [
  63. // [
  64. // "name" => "商品名称",
  65. // "field" => "goods_title",
  66. // "template_field" => "thing2",
  67. // ],
  68. // [
  69. // "name" => "拼单时间",
  70. // "field" => "groupon_start_date",
  71. // "template_field" => "time4",
  72. // ]
  73. // ],
  74. ],
  75. 'WechatMiniProgram' => [
  76. 'category_id' => 670,
  77. 'tid' => '3098',
  78. 'kid' => [7,3,2,5], // 商品名称,团长,成团人数,开团时间
  79. 'scene_desc' => '当拼团成功时通知用户', // 申请模板场景描述
  80. 'fields' => [
  81. // 'thing7' => 'goods_title', // 商品名称
  82. // 'name3' => 'groupon_leader_nickname', // 团长
  83. // 'number2' => 'groupon_num', // 成团人数
  84. // 'date5' => 'groupon_start_date', // 开团时间
  85. [
  86. "name" => "商品名称",
  87. "field" => "goods_title",
  88. "template_field" => "thing7",
  89. ],
  90. [
  91. "name" => "团长",
  92. "field" => "groupon_leader_nickname",
  93. "template_field" => "name3",
  94. ],
  95. [
  96. "name" => "成团人数",
  97. "field" => "groupon_num",
  98. "template_field" => "number2",
  99. ],
  100. [
  101. "name" => "开团时间",
  102. "field" => "groupon_start_date",
  103. "template_field" => "date5",
  104. ],
  105. ],
  106. ]
  107. ];
  108. // 返回的字段列表
  109. public $returnField = [
  110. 'name' => '拼团成功通知',
  111. 'channels' => ['Sms', 'Email', 'WechatOfficialAccount', 'WechatMiniProgram'],
  112. 'fields' => [
  113. ['name' => '消息名称', 'field' => 'template'],
  114. ['name' => '团ID', 'field' => 'groupon_id'],
  115. ['name' => '商品名称', 'field' => 'goods_title'],
  116. ['name' => '拼团用户', 'field' => 'groupon_user'],
  117. ['name' => '用户手机', 'field' => 'groupon_mobile'],
  118. ['name' => '团长', 'field' => 'groupon_leader_nickname'],
  119. ['name' => '团长手机', 'field' => 'groupon_leader_mobile'],
  120. ['name' => '商品金额', 'field' => 'groupon_price'],
  121. ['name' => '开团时间', 'field' => 'groupon_start_date'],
  122. ['name' => '成团时间', 'field' => 'groupon_finish_date'],
  123. ['name' => '成团人数', 'field' => 'groupon_num'],
  124. ['name' => '订单ID', 'field' => 'order_id'],
  125. ['name' => '订单号', 'field' => 'order_sn'],
  126. ['name' => '订单金额', 'field' => 'order_amount'],
  127. ['name' => '支付金额', 'field' => 'pay_fee'],
  128. ]
  129. ];
  130. /**
  131. * 组合数据参数
  132. *
  133. * @param \think\Model $notifiable
  134. * @return array
  135. */
  136. protected function getData($notifiable)
  137. {
  138. $groupon = $this->data['groupon'];
  139. $grouponLogs = $this->data['groupon_logs'];
  140. $grouponLogs = $grouponLogs instanceof \think\Collection ? $grouponLogs : collection($grouponLogs); // 转为 collection
  141. $grouponLeader = $this->data['groupon_leader'];
  142. $goods = $this->data['goods'];
  143. // 当前订单
  144. $order = $this->getCurrentOrder($notifiable, $groupon, $grouponLogs);
  145. $data['template'] = $this->returnField['name']; // 模板名称
  146. $data['groupon_id'] = $groupon['id'];
  147. $data['goods_title'] = $goods['title'];
  148. $data['groupon_user'] = $notifiable['nickname'];
  149. $data['groupon_mobile'] = $notifiable['mobile'];
  150. $data['groupon_leader_nickname'] = $grouponLeader['nickname'] ?? '';
  151. $data['groupon_leader_mobile'] = $grouponLeader['mobile'] ?? '';
  152. $data['groupon_price'] = $order ? '¥' . $order['goods_amount'] : '';
  153. $data['groupon_start_date'] = $groupon['createtime'];
  154. $data['groupon_finish_date'] = $groupon['finish_time'];
  155. $data['groupon_num'] = $groupon['num'];
  156. $data['order_id'] = $order['id'] ?? '';
  157. $data['order_sn'] = $order['order_sn'] ?? '';
  158. $data['order_amount'] = '¥' . $order['order_amount'] ?? '';
  159. $data['pay_fee'] = '¥' . $order['pay_fee'] ?? '';
  160. // 统一跳转地址
  161. $data['jump_url'] = "/pages/activity/groupon/detail?id=" . $groupon['id'];
  162. return $data;
  163. }
  164. // 获取当前订单
  165. private function getCurrentOrder($notifiable, $groupon, $grouponLogs)
  166. {
  167. $grouponLogs = $grouponLogs->column(null, 'user_id');
  168. $currentLog = $grouponLogs[$notifiable['id']] ?? null;
  169. if ($currentLog) {
  170. $order = OrderModel::where('id', $currentLog['order_id'])->find();
  171. }
  172. return $order ?? null;
  173. }
  174. }