WxMessageClass.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. 'OPENTM411207151' => '提现成功通知',
  28. 'OPENTM406262304' => '还款通知',
  29. 'OPENTM416881153' => '发货通知',
  30. ],
  31. //供货商
  32. dict::getDict('ptStyle', 'ghs') => [
  33. 'OPENTM417875155' => '新订单通知',
  34. 'OPENTM207430125' => '操作成功通知',
  35. 'OPENTM401915538' => '审核通知',
  36. 'TM00006' => '充值通知',
  37. 'OPENTM201205968' => '订单送达通知',
  38. 'OPENTM207777535' => '付款提醒',
  39. 'OPENTM207422813' => '收入提醒',
  40. 'OPENTM202297555' => '下单成功通知',
  41. 'OPENTM207327227' => '宝贝售出提醒',
  42. 'OPENTM207188526' => '付款成功',
  43. 'OPENTM411207151' => '提现成功通知',
  44. 'OPENTM406262304' => '还款通知',
  45. 'OPENTM416881153' => '发货通知',
  46. ],
  47. //商城
  48. dict::getDict('ptStyle', 'mall') => [
  49. ],
  50. ];
  51. }
  52. //初始化消息模板
  53. // ./yii.bat wx-message/init
  54. public static function init()
  55. {
  56. $wxBase = [
  57. dict::getDict('ptStyle', 'hd') => \bizHd\wx\classes\WxOpenClass::getWxInfo(),
  58. dict::getDict('ptStyle', 'ghs') => \bizHd\wx\classes\WxOpenClass::getGhsWxInfo(),
  59. dict::getDict('ptStyle', 'mall') => \bizHd\wx\classes\WxOpenClass::getMallWxInfo(),
  60. ];
  61. $message = self::getAllByCondition([], null, '*', null, true);
  62. if (!empty($message)) {
  63. foreach ($message as $item) {
  64. $ptStyle = $item->ptStyle;
  65. $wx = $wxBase[$ptStyle] ?? [];
  66. if (empty($wx)) {
  67. util::fail('没有找到微信平台信息');
  68. }
  69. $templateId = $item->templateId;
  70. $respond = wxUtil::delTMessage($wx, $templateId, $ptStyle);
  71. if (isset($respond['errcode']) && $respond['errcode'] == 0) {
  72. $item->delete();
  73. }
  74. }
  75. }
  76. $wxMessageList = self::has();
  77. foreach ($wxMessageList as $k => $message) {
  78. $ptStyle = $k;
  79. if (empty($message)) {
  80. continue;
  81. }
  82. $wx = $wxBase[$ptStyle] ?? [];
  83. foreach ($message as $shortId => $title) {
  84. $wxReturn = wxUtil::tMessageCreate($wx, $shortId, $ptStyle);
  85. if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
  86. $templateId = $wxReturn['template_id'];
  87. $data = [
  88. 'shortTemplateId' => $shortId,
  89. 'templateId' => $templateId,
  90. 'templateName' => $title,
  91. 'ptStyle' => $ptStyle,
  92. ];
  93. self::add($data);
  94. Yii::warning("add tmessage:" . $title . " success\n");
  95. } else {
  96. Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
  97. }
  98. }
  99. }
  100. }
  101. //新订单通知 shish 20210529
  102. public static function newOrderInform($shop, $order)
  103. {
  104. $shopId = $shop->id;
  105. $ptStyle = $shop->ptStyle;
  106. $wxBase = WxOpenClass::getWxBase();
  107. $wx = $wxBase[$ptStyle] ?? [];
  108. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  109. $shortTempId = 'OPENTM417875155';//新订单通知
  110. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  111. if (empty($tempId)) {
  112. return false;
  113. }
  114. $orderId = $order->id;
  115. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  116. foreach ($adminList as $admin) {
  117. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  118. continue;
  119. }
  120. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  121. $data = [
  122. "touser" => $openId,
  123. "template_id" => $tempId,
  124. "url" => Yii::$app->params['ghsDomain'],
  125. "data" => [
  126. "first" => ["value" => "您有新的订单,请注意查收", "color" => "#173177"],
  127. "keyword1" => ["value" => $order->orderSn, "color" => "#173177"],
  128. "keyword2" => ["value" => $order->actPrice, "color" => "#173177"],
  129. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  130. "remark" => ["value" => "点击查看详情", "color" => "#173177"]
  131. ]
  132. ];
  133. $data['miniprogram']['appid'] = $wx['miniAppId'];
  134. $data['miniprogram']['pagepath'] = 'pagesOrder/detail?id=' . $orderId;
  135. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  136. }
  137. }
  138. //审核通过通知 shish 20210530
  139. public static function applyPass($shop)
  140. {
  141. $shopId = $shop->id;
  142. $ptStyle = $shop->ptStyle;
  143. $wxBase = WxOpenClass::getWxBase();
  144. $wx = $wxBase[$ptStyle] ?? [];
  145. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  146. $shortTempId = 'OPENTM401915538';//审核通过通知
  147. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  148. if (empty($tempId)) {
  149. return false;
  150. }
  151. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  152. foreach ($adminList as $admin) {
  153. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  154. continue;
  155. }
  156. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  157. $data = array(
  158. "touser" => $openId,
  159. "template_id" => $tempId,
  160. "url" => Yii::$app->params['ghsDomain'],
  161. "data" => array(
  162. "first" => array(
  163. "value" => "恭喜,您的开店申请审核通过。",
  164. "color" => "#173177"
  165. ),
  166. "keyword1" => array(
  167. "value" => '审核通过',
  168. "color" => "#173177"
  169. ),
  170. "keyword2" => array(
  171. "value" => date("Y-m-d H:i:s"),
  172. "color" => "#173177"
  173. ),
  174. "remark" => array(
  175. "value" => '点击登陆后台',
  176. "color" => "#173177"
  177. )
  178. ),
  179. );
  180. $data['miniprogram']['appid'] = $wx['miniAppId'];
  181. $data['miniprogram']['pagepath'] = 'admin/home/workbench';
  182. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  183. }
  184. }
  185. //采购单下单成功 shish 20210531
  186. public static function generateCgOrder($shop, $order)
  187. {
  188. $shopId = $shop->id;
  189. $ptStyle = $shop->ptStyle;
  190. $wxBase = WxOpenClass::getWxBase();
  191. $wx = $wxBase[$ptStyle] ?? [];
  192. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  193. $shortTempId = 'OPENTM202297555';//下单成功通知
  194. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  195. if (empty($tempId)) {
  196. return false;
  197. }
  198. $orderId = $order->id;
  199. $orderSn = (string)$order->orderSn;
  200. $totalFee = $order->actPrice;
  201. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  202. foreach ($adminList as $admin) {
  203. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  204. continue;
  205. }
  206. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  207. $data = [
  208. "touser" => $openId,
  209. "template_id" => $tempId,
  210. "url" => Yii::$app->params['ghsDomain'],
  211. "data" => [
  212. "first" => ["value" => '亲,您的采购单已生成', "color" => "#173177"],
  213. "keyword1" => ["value" => $orderSn, "color" => "#173177"],
  214. "keyword2" => ["value" => '花材', "color" => "#173177"],
  215. "keyword3" => ["value" => $order->bigNum . '/' . $order->smallNum, "color" => "#173177"],
  216. "keyword4" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
  217. "keyword5" => ["value" => '-', "color" => "#173177"],
  218. "remark" => ["value" => "", "color" => "#173177"]
  219. ]
  220. ];
  221. $data['miniprogram']['appid'] = $wx['miniAppId'];
  222. $data['miniprogram']['pagepath'] = 'pagesPurchase/purDetails?id=' . $orderId;
  223. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  224. }
  225. }
  226. //采购单已发货 shish 20210531
  227. public static function cgOrderHasSend($shop, $order)
  228. {
  229. $shopId = $shop->id;
  230. $ptStyle = $shop->ptStyle;
  231. $wxBase = WxOpenClass::getWxBase();
  232. $wx = $wxBase[$ptStyle] ?? [];
  233. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  234. $shortTempId = 'OPENTM416881153';//下单成功通知
  235. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  236. if (empty($tempId)) {
  237. return false;
  238. }
  239. $orderId = $order->id;
  240. $orderSn = (string)$order->orderSn;
  241. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  242. $ghsShopAdminName = $order->ghsShopAdminName ?? '';
  243. foreach ($adminList as $admin) {
  244. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  245. continue;
  246. }
  247. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  248. $data = [
  249. "touser" => $openId,
  250. "template_id" => $tempId,
  251. "url" => Yii::$app->params['ghsDomain'],
  252. "data" => [
  253. "first" => ["value" => '亲,您的采购单已发货,请注意签收', "color" => "#173177"],
  254. "keyword1" => ["value" => $orderSn, "color" => "#173177"],
  255. "keyword2" => ["value" => date("Y-m-d H:i:s"), "color" => "#173177"],
  256. "keyword3" => ["value" => $ghsShopAdminName, "color" => "#173177"],
  257. "remark" => ["value" => "点击查看详情", "color" => "#173177"]
  258. ]
  259. ];
  260. $data['miniprogram']['appid'] = $wx['miniAppId'];
  261. $data['miniprogram']['pagepath'] = 'pagesPurchase / purDetails ? id = ' . $orderId;
  262. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  263. }
  264. }
  265. }