ShopMessage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\frontend\modules\order\services\message;
  3. use app\common\models\Notice;
  4. use app\common\models\notice\MessageTemp;
  5. use app\common\models\notice\MinAppTemplateMessage;
  6. use app\common\services\notice\applet\OrderCreateMinNotice;
  7. /**
  8. * Created by PhpStorm.
  9. * User: shenyang
  10. * Date: 2017/6/7
  11. * Time: 上午10:15
  12. */
  13. class ShopMessage extends Message
  14. {
  15. protected $goods_title;
  16. public function __construct($order,$formId = '',$type = 1,$title)
  17. {
  18. parent::__construct($order,$formId = '',$type = 1,$title);
  19. $this->goods_title = $this->order->hasManyOrderGoods()->first()->title;
  20. $this->goods_title .= $this->order->hasManyOrderGoods()->first()->goods_option_title ? '['.$this->order->hasManyOrderGoods()->first()->goods_option_title.']': '';
  21. }
  22. private function sendToShops()
  23. {
  24. if (empty(\Setting::get('shop.notice.salers'))) {
  25. return;
  26. }
  27. if (empty($this->templateId)) {
  28. return;
  29. }
  30. //客服发送消息通知
  31. foreach (\Setting::get('shop.notice.salers') as $saler) {
  32. $this->notice($this->templateId, $this->msg, $saler['uid'],'',$this->news_link);
  33. }
  34. }
  35. protected function miniSendToShops($templateId,$msg)
  36. {
  37. if (empty($templateId)) {
  38. return;
  39. }
  40. \Log::debug('===============',[$templateId]);
  41. foreach (\Setting::get('mini_app.notice.salers') as $saler) {
  42. $this->MiniNotice($templateId, $msg, $saler['uid']);
  43. }
  44. }
  45. private function transfer($temp_id, $params)
  46. {
  47. $this->msg = MessageTemp::getSendMsg($temp_id, $params);
  48. if (!$this->msg) {
  49. return;
  50. }
  51. $news_link = MessageTemp::find($temp_id)->news_link;
  52. $this->news_link = $news_link ?:'';
  53. $this->templateId = MessageTemp::$template_id;
  54. $this->sendToShops();
  55. }
  56. public function created()
  57. {
  58. $temp_id = \Setting::get('shop.notice')['seller_order_create'];
  59. if ($temp_id) {
  60. $params = [
  61. ['name' => '商城名称', 'value' => \Setting::get('shop.shop')['name']],
  62. ['name' => '粉丝昵称', 'value' => $this->order->belongsToMember->nickname],
  63. ['name' => '订单号', 'value' => $this->order->order_sn],
  64. ['name' => '下单时间', 'value' => $this->order['create_time']->toDateTimeString()],
  65. ['name' => '订单金额', 'value' => $this->order['price']],
  66. ['name' => '运费', 'value' => $this->order['dispatch_price']],
  67. ['name' => '商品详情(含规格)', 'value' => $this->goods_title],
  68. ];
  69. $this->transfer($temp_id, $params);
  70. }
  71. if (!\Setting::get('mini_app.notice.notice_enable.created')) {
  72. return ;
  73. }
  74. //小程序消息通知
  75. $is_open = MinAppTemplateMessage::getTitle('订单生成通知');
  76. if (!$is_open->is_open){
  77. return;
  78. }
  79. \Log::info("小程序通知测试-4",$is_open);
  80. $address = $this->order['address'];
  81. $msg = [
  82. 'keyword1'=>['value'=> $this->order->belongsToMember->nickname],// 订单发起者
  83. 'keyword2'=>['value'=> $this->goods_title],//商品信息
  84. 'keyword3'=>['value'=> $address['province'] . ' ' . $address['city'] . ' ' . $address['area'] . ' ' . $address['address']],// 收货地址
  85. 'keyword4'=>['value'=> $this->order['price']],// 订单金额
  86. 'keyword5'=>['value'=> $this->order['create_time']->toDateTimeString()],// 生成时间
  87. 'keyword6'=>['value'=>$this->order->order_sn],//订单号
  88. ];
  89. $this->miniSendToShops($is_open->template_id, $msg);
  90. }
  91. public function paid()
  92. {
  93. $temp_id = \Setting::get('shop.notice')['seller_order_pay'];
  94. if ($temp_id) {
  95. $address = $this->order['address'];
  96. $params = [
  97. ['name' => '粉丝昵称', 'value' => $this->order->belongsToMember->nickname],
  98. ['name' => '订单号', 'value' => $this->order->order_sn],
  99. ['name' => '下单时间', 'value' => $this->order['create_time']->toDateTimeString()],
  100. ['name' => '支付时间', 'value' => $this->order['pay_time']->toDateTimeString()],
  101. ['name' => '支付方式', 'value' => $this->order->pay_type_name],
  102. ['name' => '订单金额', 'value' => $this->order['price']],
  103. ['name' => '运费', 'value' => $this->order['dispatch_price']],
  104. ['name' => '商品详情(含规格)', 'value' => $this->goods_title],
  105. ['name' => '收件人姓名', 'value' => $address['realname']],
  106. ['name' => '收件人电话', 'value' => $address['mobile']],
  107. ['name' => '收件人地址', 'value' => $address['province'] . ' ' . $address['city'] . ' ' . $address['area'] . ' ' . $address['address']],
  108. ];
  109. $this->transfer($temp_id, $params);
  110. }
  111. if (!\Setting::get('mini_app.notice.notice_enable.paid')) {
  112. return ;
  113. }
  114. //小程序消息通知
  115. $is_open = MinAppTemplateMessage::getTitle('订单支付提醒');
  116. if (!$is_open->is_open){
  117. return;
  118. }
  119. \Log::info("小程序通知测试-5",$is_open);
  120. $address = $this->order['address'];
  121. $msg = [
  122. 'keyword1'=>['value'=> $this->order->belongsToMember->nickname],// 用户
  123. 'keyword2'=>['value'=> $this->order->order_sn],//订单号
  124. 'keyword3'=>['value'=> $this->goods_title],//商品名称
  125. 'keyword4'=>['value'=> $this->order->pay_type_name],// 支付方式
  126. 'keyword5'=>['value'=> $this->order['price']],// 支付金额
  127. 'keyword6'=>['value'=> $address['realname']],//收貨人
  128. 'keyword7'=>['value'=> $address['province'] . ' ' . $address['city'] . ' ' . $address['area'] . ' ' . $address['address']],//收貨地址
  129. ];
  130. $this->miniSendToShops($is_open->template_id, $msg);
  131. }
  132. public function received()
  133. {
  134. $temp_id = \Setting::get('shop.notice')['seller_order_finish'];
  135. if ($temp_id) {
  136. $address = $this->order['address'];
  137. $params = [
  138. ['name' => '粉丝昵称', 'value' => $this->order->belongsToMember->nickname],
  139. ['name' => '订单号', 'value' => $this->order->order_sn],
  140. ['name' => '确认收货时间', 'value' => $this->order['finish_time']->toDateTimeString()],
  141. ['name' => '运费', 'value' => $this->order['dispatch_price']],
  142. ['name' => '商品详情(含规格)', 'value' => $this->goods_title],
  143. ['name' => '收件人姓名', 'value' => $address['realname']],
  144. ['name' => '收件人电话', 'value' => $address['mobile']],
  145. ['name' => '收件人地址', 'value' => $address['province'] . ' ' . $address['city'] . ' ' . $address['area'] . ' ' . $address['address']],
  146. ];
  147. $this->transfer($temp_id, $params);
  148. }
  149. if (!\Setting::get('mini_app.notice.notice_enable.received')) {
  150. return ;
  151. }
  152. //小程序消息
  153. $is_open = MinAppTemplateMessage::getTitle('确认收货通知');
  154. if (!$is_open->is_open){
  155. return;
  156. }
  157. \Log::info("小程序通知测试-6",$is_open);
  158. $msg = [
  159. 'keyword1'=>['value'=> $this->goods_title],// 商品名
  160. 'keyword2'=>['value'=> $this->order->belongsToMember->nickname],//买家昵称
  161. 'keyword3'=>['value'=> $this->order->order_sn],// 订单编号
  162. 'keyword4'=>['value'=> $this->order['create_time']->toDateTimeString()],// 订单时间
  163. 'keyword5'=>['value'=> $this->order['price']],//订单金額
  164. 'keyword6'=>['value'=> $this->order['finish_time']->toDateTimeString()],// 确认收货时间
  165. ];
  166. $this->miniSendToShops($is_open->template_id,$msg);
  167. }
  168. /**
  169. * @name 购买商品发送通知
  170. * @author
  171. * @param $status
  172. */
  173. public function goodsBuy($status)
  174. {
  175. $order_goods = $this->order->hasManyOrderGoods()->get();
  176. foreach ($order_goods as $goods) {
  177. $goods_notice = Notice::select()->where('goods_id', $goods->goods_id)->whereType($status)->first();
  178. if (!$goods_notice) {
  179. continue;
  180. }
  181. $temp_id = \Setting::get('shop.notice')['buy_goods_msg'];
  182. if ($temp_id) {
  183. $params = [
  184. ['name' => '会员昵称', 'value' => $this->order->belongsToMember->nickname],
  185. ['name' => '订单编号', 'value' => $this->order->order_sn],
  186. ['name' => '商品名称(含规格)', 'value' => $this->getGoodsTitle($goods)],
  187. ['name' => '商品金额', 'value' => $goods->price],
  188. ['name' => '商品数量', 'value' => $goods->total],
  189. ['name' => '订单状态', 'value' => $this->order->status_name],
  190. ['name' => '时间', 'value' => $this->getOrderTime($status)],
  191. ];
  192. $msg = MessageTemp::getSendMsg($temp_id, $params);
  193. if (!$msg) {
  194. continue;
  195. }
  196. $news_link = MessageTemp::find($temp_id)->news_link;
  197. $news_link = $news_link ?:'';
  198. $template_id = MessageTemp::$template_id;
  199. $this->notice($template_id, $msg, $goods_notice->uid,'',$news_link);
  200. }
  201. //小程序消息通知
  202. $is_open = MinAppTemplateMessage::getTitle('购买成功通知');
  203. if (!$is_open->is_open){
  204. return;
  205. }
  206. $miniParams = [
  207. 'keyword1'=>['value'=> $this->order->belongsToMember->nickname],// 会员姓名
  208. 'keyword2'=>['value'=> $this->order->order_sn],//订单号
  209. 'keyword3'=>['value'=> $this->getGoodsTitle($goods)],// 物品名称
  210. 'keyword4'=>['value'=> $goods->total],// 数量
  211. 'keyword5'=>['value'=> $goods->price],// 购买金额
  212. 'keyword6'=>['value'=> $this->getOrderTime($status)],//购买时间
  213. ];
  214. $this->miniSendToShops($is_open->template_id,$miniParams);
  215. }
  216. }
  217. /**
  218. * @name 获取订单操作时间
  219. * @author
  220. * @param $status
  221. * @return mixed
  222. */
  223. private function getOrderTime($status)
  224. {
  225. if ($status == 1) {
  226. $order_time = $this->order['create_time']->toDateTimeString();
  227. } else if ($status == 2) {
  228. $order_time = $this->order['pay_time']->toDateTimeString();
  229. } else if ($status == 3) {
  230. $order_time = $this->order['finish_time']->toDateTimeString();
  231. }
  232. return $order_time;
  233. }
  234. /**
  235. * @name 获取商品名
  236. * @author
  237. * @param $goods
  238. * @return string
  239. */
  240. private function getGoodsTitle($goods)
  241. {
  242. $goods_title = $goods->title;
  243. if ($goods->goods_option_title) {
  244. $goods_title .= '[' . $goods->goods_option_title . ']';
  245. }
  246. return $goods_title;
  247. }
  248. }