WxMessageClass.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace biz\wx\classes;
  3. use biz\base\classes\BaseClass;
  4. use biz\shop\classes\ShopAdminClass;
  5. use common\components\dict;
  6. use common\components\util;
  7. use common\components\wxUtil;
  8. use Yii;
  9. class WxMessageClass extends BaseClass
  10. {
  11. public static $baseFile = '\biz\wx\models\WxMessage';
  12. public static function has()
  13. {
  14. return [
  15. //零售
  16. dict::getDict('ptStyle', 'hd') => [
  17. 'OPENTM417875155' => '新订单通知',
  18. 'OPENTM207430125' => '操作成功通知',
  19. 'OPENTM401915538' => '审核通知',
  20. 'TM00006' => '充值通知',
  21. 'OPENTM201205968' => '订单送达通知',
  22. 'OPENTM207777535' => '付款提醒',
  23. 'OPENTM207422813' => '收入提醒',
  24. 'OPENTM202297555' => '下单成功通知',
  25. 'OPENTM207327227' => '宝贝售出提醒',
  26. 'OPENTM207188526' => '付款成功',
  27. ],
  28. //供货商
  29. dict::getDict('ptStyle', 'ghs') => [
  30. 'OPENTM417875155' => '新订单通知',
  31. 'OPENTM207430125' => '操作成功通知',
  32. 'OPENTM401915538' => '审核通知',
  33. 'TM00006' => '充值通知',
  34. 'OPENTM201205968' => '订单送达通知',
  35. 'OPENTM207777535' => '付款提醒',
  36. 'OPENTM207422813' => '收入提醒',
  37. 'OPENTM202297555' => '下单成功通知',
  38. 'OPENTM207327227' => '宝贝售出提醒',
  39. 'OPENTM207188526' => '付款成功',
  40. ],
  41. //商城
  42. dict::getDict('ptStyle', 'mall') => [
  43. ],
  44. ];
  45. }
  46. //初始化消息模板
  47. // ./yii.bat wx-message/init
  48. public static function init()
  49. {
  50. $wxBase = [
  51. dict::getDict('ptStyle', 'hd') => \bizHd\wx\classes\WxOpenClass::getWxInfo(),
  52. dict::getDict('ptStyle', 'ghs') => \bizHd\wx\classes\WxOpenClass::getGhsWxInfo(),
  53. dict::getDict('ptStyle', 'mall') => \bizHd\wx\classes\WxOpenClass::getMallWxInfo(),
  54. ];
  55. $message = self::getAllByCondition([], null, '*', null, true);
  56. if (!empty($message)) {
  57. foreach ($message as $item) {
  58. $ptStyle = $item->ptStyle;
  59. $wx = $wxBase[$ptStyle] ?? [];
  60. if (empty($wx)) {
  61. util::fail('没有找到微信平台信息');
  62. }
  63. $templateId = $item->templateId;
  64. $respond = wxUtil::delTMessage($wx, $templateId, $ptStyle);
  65. if (isset($respond['errcode']) && $respond['errcode'] == 0) {
  66. $item->delete();
  67. }
  68. }
  69. }
  70. $wxMessageList = self::has();
  71. foreach ($wxMessageList as $k => $message) {
  72. $ptStyle = $k;
  73. if (empty($message)) {
  74. continue;
  75. }
  76. $wx = $wxBase[$ptStyle] ?? [];
  77. foreach ($message as $shortId => $title) {
  78. $wxReturn = wxUtil::tMessageCreate($wx, $shortId, $ptStyle);
  79. if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
  80. $templateId = $wxReturn['template_id'];
  81. $data = [
  82. 'shortTemplateId' => $shortId,
  83. 'templateId' => $templateId,
  84. 'templateName' => $title,
  85. 'ptStyle' => $ptStyle,
  86. ];
  87. self::add($data);
  88. Yii::warning("add tmessage:" . $title . " success\n");
  89. } else {
  90. Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
  91. }
  92. }
  93. }
  94. }
  95. //新订单通知 shish 20210529
  96. public static function newOrderNotice($shop, $order)
  97. {
  98. $shopId = $shop->id;
  99. $ptStyle = $shop->ptStyle;
  100. $wxBase = [
  101. dict::getDict('ptStyle', 'hd') => \bizHd\wx\classes\WxOpenClass::getWxInfo(),
  102. dict::getDict('ptStyle', 'ghs') => \bizHd\wx\classes\WxOpenClass::getGhsWxInfo(),
  103. dict::getDict('ptStyle', 'mall') => \bizHd\wx\classes\WxOpenClass::getMallWxInfo(),
  104. ];
  105. $wx = $wxBase[$ptStyle] ?? [];
  106. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  107. $shortTempId = 'OPENTM417875155';//收入提醒
  108. $tempId = isset($message[$shortTempId]) ? $message[$shortTempId] : '';
  109. if (empty($tempId)) {
  110. return false;
  111. }
  112. $orderId = $order->id;
  113. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  114. foreach ($adminList as $admin) {
  115. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  116. continue;
  117. }
  118. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  119. $data = [
  120. "touser" => $openId,
  121. "template_id" => $tempId,
  122. "url" => Yii::$app->params['ghsDomain'],
  123. "data" => [
  124. "first" => ["value" => "恭喜,您获得一张优惠劵。", "color" => "#173177"],
  125. "keyword1" => ["value" => '查看', "color" => "#173177"],
  126. "keyword2" => ["value" => '优惠劵', "color" => "#173177"],
  127. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  128. "remark" => ["value" => "点击查看详情", "color" => "#173177"]
  129. ]
  130. ];
  131. $data['miniprogram']['appid'] = $wx['miniAppId'];
  132. $data['miniprogram']['pagepath'] = 'pagesOrder/detail?id=' . $orderId;
  133. wxUtil::sendTaskInform($data, $wx);
  134. }
  135. }
  136. }