GrouponFail.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 GrouponFail 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_fail';
  19. // 额外数据
  20. public $data = [];
  21. public $template = [
  22. 'MessageDefaultContent' => '您的拼团未成功,商品:{goods_title},将全额退款,请注意查收',
  23. // 'WechatOfficialAccount' => [
  24. // 'temp_no' => 'OPENTM401113750',
  25. // 'fields' => [
  26. // // 'first' => '您的拼团未成功',
  27. // // 'keyword1' => 'goods_title', // 拼团商品
  28. // // 'keyword2' => 'groupon_price', // 商品金额
  29. // // 'keyword3' => 'pay_fee', // 退款金额
  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_price",
  43. // "template_field" => "keyword2",
  44. // ],
  45. // [
  46. // "name" => "退款金额",
  47. // "field" => "pay_fee",
  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. ],
  61. 'WechatMiniProgram' => [
  62. 'category_id' => 670,
  63. 'tid' => '4534',
  64. 'kid' => [1,2,8,5], // 商品名称,参团人数,商品金额,退款金额
  65. 'scene_desc' => '当拼团失败时通知用户', // 申请模板场景描述
  66. 'fields' => [
  67. // 'thing1' => 'goods_title', // 商品名称
  68. // 'number2' => 'groupon_current_num', // 参团人数
  69. // 'amount8' => 'groupon_price', // 商品金额
  70. // 'amount5' => 'pay_fee', // 退款金额
  71. [
  72. "name" => "商品名称",
  73. "field" => "goods_title",
  74. "template_field" => "thing1",
  75. ],
  76. [
  77. "name" => "参团人数",
  78. "field" => "groupon_current_num",
  79. "template_field" => "number2",
  80. ],
  81. [
  82. "name" => "商品金额",
  83. "field" => "groupon_price",
  84. "template_field" => "amount8",
  85. ],
  86. [
  87. "name" => "退款金额",
  88. "field" => "groupon_start_date",
  89. "template_field" => "pay_fee",
  90. ],
  91. ],
  92. ]
  93. ];
  94. // 返回的字段列表
  95. public $returnField = [
  96. 'name' => '拼团失败通知',
  97. 'channels' => ['Sms', 'Email', 'WechatOfficialAccount', 'WechatMiniProgram'],
  98. 'fields' => [
  99. ['name' => '消息名称', 'field' => 'template'],
  100. ['name' => '团ID', 'field' => 'groupon_id'],
  101. ['name' => '商品名称', 'field' => 'goods_title'],
  102. ['name' => '拼团用户', 'field' => 'groupon_user'],
  103. ['name' => '用户手机', 'field' => 'groupon_mobile'],
  104. ['name' => '团长', 'field' => 'groupon_leader_nickname'],
  105. ['name' => '团长手机', 'field' => 'groupon_leader_mobile'],
  106. ['name' => '商品金额', 'field' => 'groupon_price'],
  107. ['name' => '开团时间', 'field' => 'groupon_start_date'],
  108. ['name' => '参团人数', 'field' => 'groupon_current_num'],
  109. ['name' => '成团人数', 'field' => 'groupon_num'],
  110. ['name' => '订单ID', 'field' => 'order_id'],
  111. ['name' => '订单号', 'field' => 'order_sn'],
  112. ['name' => '订单金额', 'field' => 'order_amount'],
  113. ['name' => '支付金额', 'field' => 'pay_fee'],
  114. ]
  115. ];
  116. /**
  117. * 组合数据参数
  118. *
  119. * @param \think\Model $notifiable
  120. * @return array
  121. */
  122. protected function getData($notifiable)
  123. {
  124. $groupon = $this->data['groupon'];
  125. $grouponLogs = $this->data['groupon_logs'];
  126. $grouponLogs = $grouponLogs instanceof \think\Collection ? $grouponLogs : collection($grouponLogs); // 转为 collection
  127. $grouponLeader = $this->data['groupon_leader'];
  128. $goods = $this->data['goods'];
  129. // 当前订单
  130. $order = $this->getCurrentOrder($notifiable, $groupon, $grouponLogs);
  131. $data['template'] = $this->returnField['name']; // 模板名称
  132. $data['groupon_id'] = $groupon['id'];
  133. $data['goods_title'] = $goods['title'];
  134. $data['groupon_user'] = $notifiable['nickname'];
  135. $data['groupon_mobile'] = $notifiable['mobile'];
  136. $data['groupon_leader_nickname'] = $grouponLeader['nickname'] ?? '';
  137. $data['groupon_leader_mobile'] = $grouponLeader['mobile'] ?? '';
  138. $data['groupon_price'] = $order ? '¥' . $order['goods_amount'] : '';
  139. $data['groupon_start_date'] = $groupon['createtime'];
  140. $data['groupon_current_num'] = $groupon['current_num'];
  141. $data['groupon_num'] = $groupon['num'];
  142. $data['order_id'] = $order['id'] ?? '';
  143. $data['order_sn'] = $order['order_sn'] ?? '';
  144. $data['order_amount'] = '¥' . $order['order_amount'] ?? '';
  145. $data['pay_fee'] = '¥' . $order['pay_fee'] ?? '';
  146. // 统一跳转地址
  147. $data['jump_url'] = "/pages/activity/groupon/detail?id=" . $groupon['id'];
  148. return $data;
  149. }
  150. // 获取当前订单
  151. private function getCurrentOrder($notifiable, $groupon, $grouponLogs)
  152. {
  153. $grouponLogs = $grouponLogs->column(null, 'user_id');
  154. $currentLog = $grouponLogs[$notifiable['id']] ?? null;
  155. if ($currentLog) {
  156. $order = OrderModel::where('id', $currentLog['order_id'])->find();
  157. }
  158. return $order ?? null;
  159. }
  160. }