OrderDispatched.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace addons\shopro\notification\order;
  3. use addons\shopro\notification\Notification;
  4. use addons\shopro\notification\traits\Notification as NotificationTrait;
  5. use app\admin\model\shopro\order\Address as OrderAddressModel;
  6. /**
  7. * 订单通知
  8. */
  9. class OrderDispatched 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 = 'order_dispatched';
  19. // 额外数据
  20. public $data = [];
  21. public $template = [
  22. 'MessageDefaultContent' => '您的订单已发货,物流公司:{express_name},运单号:{express_no},请注意查收',
  23. // 'WechatOfficialAccount' => [
  24. // 'temp_no' => 'OPENTM418033200',
  25. // 'fields' => [
  26. // // 'first' => '您的订单已发货,请注意查收',
  27. // // 'keyword1' => 'order_sn', // 订单编号
  28. // // 'keyword2' => 'dispatch_date', // 发货时间
  29. // // 'keyword3' => 'express_name', // 物流公司
  30. // // 'keyword4' => 'express_no', // 快递单号
  31. // // 'remark' => '',
  32. // [
  33. // "template_field" => "first",
  34. // "value" => '您的订单已发货,请注意查收',
  35. // ],
  36. // [
  37. // "name" => "订单编号",
  38. // "field" => "order_sn",
  39. // "template_field" => "keyword1",
  40. // ],
  41. // [
  42. // "name" => "发货时间",
  43. // "field" => "dispatch_date",
  44. // "template_field" => "keyword2",
  45. // ],
  46. // [
  47. // "name" => "物流公司",
  48. // "field" => "express_name",
  49. // "template_field" => "keyword3",
  50. // ],
  51. // [
  52. // "name" => "快递单号",
  53. // "field" => "express_no",
  54. // "template_field" => "keyword4",
  55. // ],
  56. // [
  57. // "template_field" => "remark",
  58. // "value" => '您的订单已发货,请注意查收',
  59. // ]
  60. // ],
  61. // ],
  62. 'WechatOfficialAccount' => [
  63. 'temp_no' => '42984',
  64. 'keywords' => ['订单编号', '发货时间', '快递公司', '快递单号'],
  65. 'fields' => [
  66. [
  67. "name" => "订单编号",
  68. "field" => "order_sn",
  69. "template_field" => "character_string2",
  70. ],
  71. [
  72. "name" => "发货时间",
  73. "field" => "dispatch_date",
  74. "template_field" => "time12",
  75. ],
  76. [
  77. "name" => "快递公司",
  78. "field" => "express_name",
  79. "template_field" => "thing13",
  80. ],
  81. [
  82. "name" => "快递单号",
  83. "field" => "express_no",
  84. "template_field" => "character_string14",
  85. ]
  86. ],
  87. ],
  88. 'WechatMiniProgram' => [
  89. 'category_id' => 670,
  90. 'tid' => '1458',
  91. 'kid' => [7,3,1,2], // 订单编号,发货事件,快递公司,快递单号
  92. 'scene_desc' => '当订单发货时通知用户', // 申请模板场景描述
  93. 'fields' => [
  94. // 'character_string7' => 'order_sn', // 订单编号
  95. // 'time3' => 'dispatch_date', // 发货时间
  96. // 'thing1' => 'express_name', // 物流公司
  97. // 'character_string2' => 'express_no', // 快递单号
  98. [
  99. "name" => "订单编号",
  100. "field" => "order_sn",
  101. "template_field" => "character_string7",
  102. ],
  103. [
  104. "name" => "发货时间",
  105. "field" => "dispatch_date",
  106. "template_field" => "time3",
  107. ],
  108. [
  109. "name" => "物流公司",
  110. "field" => "express_name",
  111. "template_field" => "thing1",
  112. ],
  113. [
  114. "name" => "快递单号",
  115. "field" => "express_no",
  116. "template_field" => "character_string2",
  117. ],
  118. ],
  119. ]
  120. ];
  121. // 返回的字段列表
  122. public $returnField = [
  123. 'name' => '订单发货通知',
  124. 'channels' => ['Sms', 'Email', 'WechatOfficialAccount', 'WechatMiniProgram'],
  125. 'fields' => [
  126. ['name' => '消息名称', 'field' => 'template'],
  127. ['name' => '订单ID', 'field' => 'order_id'],
  128. ['name' => '订单号', 'field' => 'order_sn'],
  129. ['name' => '订单金额', 'field' => 'order_amount'],
  130. ['name' => '发货时间', 'field' => 'dispatch_date'],
  131. ['name' => '商品名称', 'field' => 'goods_title'],
  132. ['name' => '购买数量', 'field' => 'goods_num'],
  133. ['name' => '快递公司', 'field' => 'express_name'],
  134. ['name' => '快递单号', 'field' => 'express_no'],
  135. ['name' => '收件信息', 'field' => 'consignee'],
  136. ]
  137. ];
  138. /**
  139. * 组合数据参数
  140. *
  141. * @param \think\Model $notifiable
  142. * @param \think\Model $order
  143. * @param \think\Collection $items
  144. * @param \think\Model $express
  145. * @return array
  146. */
  147. protected function getData($notifiable)
  148. {
  149. $order = $this->data['order'];
  150. $items = collection($this->data['items']);
  151. $express = $this->data['express'] ?? null;
  152. $data['template'] = $this->returnField['name']; // 模板名称
  153. $data['order_id'] = $order['id'];
  154. $data['order_sn'] = $order['order_sn'];
  155. $data['order_amount'] = '¥' . $order['order_amount'];
  156. $data['dispatch_date'] = ($order['ext'] && isset($order['ext']['send_time'])) ?
  157. date('Y-m-d H:i:s', $order['ext']['send_time']) : date('Y-m-d H:i:s');
  158. $goods_titles = $items->column('goods_title');
  159. $goods_nums = $items->column('goods_num');
  160. $data['goods_title'] = join(',', $goods_titles);
  161. $data['goods_num'] = '共' . array_sum($goods_nums) . '件商品';
  162. $data['express_name'] = $express ? $express['express_name'] : '-';
  163. $data['express_no'] = $express ? $express['express_no'] : '-';
  164. $address = OrderAddressModel::where('order_id', $order['id'])->find();
  165. $data['consignee'] = $address ? ($address['consignee'] . '-' . $address['mobile']) : '';
  166. // 统一跳转地址
  167. $data['jump_url'] = "/pages/order/detail?id=" . $order['id'];
  168. return $data;
  169. }
  170. }