WxMessageClass.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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\noticeUtil;
  7. use common\components\util;
  8. use common\components\wxUtil;
  9. use Yii;
  10. class WxMessageClass extends BaseClass
  11. {
  12. public static $baseFile = '\biz\wx\models\WxMessage';
  13. public static function has()
  14. {
  15. return [
  16. //零售
  17. dict::getDict('ptStyle', 'hd') => [
  18. 'OPENTM417875155' => '新订单通知',
  19. 'OPENTM207430125' => '操作成功通知',
  20. 'OPENTM401915538' => '审核通知',
  21. 'OPENTM201205968' => '订单送达通知',
  22. 'OPENTM207777535' => '付款提醒',
  23. 'OPENTM207422813' => '收入提醒',
  24. 'OPENTM202297555' => '下单成功通知',
  25. 'OPENTM207327227' => '宝贝售出提醒',
  26. 'OPENTM207188526' => '付款成功',
  27. 'OPENTM411207151' => '提现成功通知',
  28. 'OPENTM416881153' => '发货通知',
  29. ],
  30. //供应商
  31. dict::getDict('ptStyle', 'ghs') => [
  32. 'OPENTM417875155' => '新订单通知',
  33. 'OPENTM207430125' => '操作成功通知',
  34. 'OPENTM401915538' => '审核通知',
  35. 'OPENTM201205968' => '订单送达通知',
  36. 'OPENTM207777535' => '付款提醒',
  37. 'OPENTM207422813' => '收入提醒',
  38. 'OPENTM202297555' => '下单成功通知',
  39. 'OPENTM207327227' => '宝贝售出提醒',
  40. 'OPENTM207188526' => '付款成功',
  41. 'OPENTM411207151' => '提现成功通知',
  42. 'OPENTM416881153' => '发货通知',
  43. ],
  44. //商城
  45. dict::getDict('ptStyle', 'mall') => [
  46. ],
  47. ];
  48. }
  49. //初始化消息模板
  50. // ./yii.bat wx-message/init
  51. public static function init()
  52. {
  53. $wxBase = [
  54. dict::getDict('ptStyle', 'hd') => \bizHd\wx\classes\WxOpenClass::getWxInfo(),
  55. dict::getDict('ptStyle', 'ghs') => \bizHd\wx\classes\WxOpenClass::getGhsWxInfo(),
  56. dict::getDict('ptStyle', 'mall') => \bizHd\wx\classes\WxOpenClass::getMallWxInfo(),
  57. ];
  58. $message = self::getAllByCondition([], null, '*', null, true);
  59. if (!empty($message)) {
  60. foreach ($message as $item) {
  61. $ptStyle = $item->ptStyle;
  62. $wx = $wxBase[$ptStyle] ?? [];
  63. if (empty($wx)) {
  64. util::fail('没有找到微信平台信息');
  65. }
  66. $templateId = $item->templateId;
  67. $respond = wxUtil::delTMessage($wx, $templateId, $ptStyle);
  68. if (isset($respond['errcode']) && $respond['errcode'] == 0) {
  69. $item->delete();
  70. }
  71. }
  72. }
  73. $wxMessageList = self::has();
  74. foreach ($wxMessageList as $k => $message) {
  75. $ptStyle = $k;
  76. if (empty($message)) {
  77. continue;
  78. }
  79. $wx = $wxBase[$ptStyle] ?? [];
  80. foreach ($message as $shortId => $title) {
  81. $wxReturn = wxUtil::tMessageCreate($wx, $shortId, $ptStyle);
  82. echo json_encode($wxReturn) . "\n";
  83. if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
  84. $templateId = $wxReturn['template_id'];
  85. $data = [
  86. 'shortTemplateId' => $shortId,
  87. 'templateId' => $templateId,
  88. 'templateName' => $title,
  89. 'ptStyle' => $ptStyle,
  90. ];
  91. self::add($data);
  92. //Yii::warning("add tmessage:" . $title . " success\n");
  93. } else {
  94. Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
  95. noticeUtil::push("add wx message {$shortId} error:" . json_encode($wxReturn), '15280215347');
  96. }
  97. }
  98. }
  99. }
  100. //新订单通知 shish 20210529
  101. public static function newOrderInform($shop, $order)
  102. {
  103. $shopId = $shop->id;
  104. $ptStyle = $shop->ptStyle;
  105. $wxBase = WxOpenClass::getWxBase();
  106. $wx = $wxBase[$ptStyle] ?? [];
  107. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  108. $shortTempId = 'OPENTM417875155';//新订单通知
  109. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  110. if (empty($tempId)) {
  111. return false;
  112. }
  113. $orderId = $order->id;
  114. $actPrice = $order->actPrice;
  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" => $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. if (empty($adminList)) {
  153. return false;
  154. }
  155. foreach ($adminList as $admin) {
  156. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  157. continue;
  158. }
  159. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  160. $data = array(
  161. "touser" => $openId,
  162. "template_id" => $tempId,
  163. "url" => Yii::$app->params['ghsDomain'],
  164. "data" => array(
  165. "first" => array(
  166. "value" => "恭喜,您的开店申请审核通过。",
  167. "color" => "#173177"
  168. ),
  169. "keyword1" => array(
  170. "value" => '审核通过',
  171. "color" => "#173177"
  172. ),
  173. "keyword2" => array(
  174. "value" => date("Y-m-d H:i:s"),
  175. "color" => "#173177"
  176. ),
  177. "remark" => array(
  178. "value" => '点击登陆后台',
  179. "color" => "#173177"
  180. )
  181. ),
  182. );
  183. $data['miniprogram']['appid'] = $wx['miniAppId'];
  184. $data['miniprogram']['pagepath'] = 'admin/home/workbench';
  185. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  186. }
  187. }
  188. //采购单下单成功 shish 20210531
  189. public static function generateCgOrder($shop, $order)
  190. {
  191. $shopId = $shop->id;
  192. $ptStyle = $shop->ptStyle;
  193. $wxBase = WxOpenClass::getWxBase();
  194. $wx = $wxBase[$ptStyle] ?? [];
  195. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  196. $shortTempId = 'OPENTM202297555';//下单成功通知
  197. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  198. if (empty($tempId)) {
  199. return false;
  200. }
  201. $orderId = $order->id;
  202. $orderSn = (string)$order->orderSn;
  203. $totalFee = $order->actPrice;
  204. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  205. foreach ($adminList as $admin) {
  206. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  207. continue;
  208. }
  209. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  210. $data = [
  211. "touser" => $openId,
  212. "template_id" => $tempId,
  213. "url" => Yii::$app->params['ghsDomain'],
  214. "data" => [
  215. "first" => ["value" => '亲,您的采购单已生成', "color" => "#173177"],
  216. "keyword1" => ["value" => $orderSn, "color" => "#173177"],
  217. "keyword2" => ["value" => '花材', "color" => "#173177"],
  218. "keyword3" => ["value" => $order->bigNum . '/' . $order->smallNum, "color" => "#173177"],
  219. "keyword4" => ["value" => number_format($totalFee, 2) . '元', "color" => "#173177"],
  220. "keyword5" => ["value" => '-', "color" => "#173177"],
  221. "remark" => ["value" => "", "color" => "#173177"]
  222. ]
  223. ];
  224. $data['miniprogram']['appid'] = $wx['miniAppId'];
  225. $data['miniprogram']['pagepath'] = 'pagesPurchase/purDetails?id=' . $orderId;
  226. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  227. }
  228. }
  229. //采购单已发货 shish 20210531
  230. public static function cgOrderHasSend($shop, $order)
  231. {
  232. $shopId = $shop->id;
  233. $ptStyle = $shop->ptStyle;
  234. $wxBase = WxOpenClass::getWxBase();
  235. $wx = $wxBase[$ptStyle] ?? [];
  236. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  237. $shortTempId = 'OPENTM416881153';//下单成功通知
  238. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  239. if (empty($tempId)) {
  240. return false;
  241. }
  242. $orderId = $order->id;
  243. $orderSn = (string)$order->orderSn;
  244. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  245. $ghsShopAdminName = $order->ghsShopAdminName ?? '';
  246. foreach ($adminList as $admin) {
  247. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  248. continue;
  249. }
  250. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  251. $data = [
  252. "touser" => $openId,
  253. "template_id" => $tempId,
  254. "url" => Yii::$app->params['ghsDomain'],
  255. "data" => [
  256. "first" => ["value" => '亲,您的采购单已发货,请注意签收', "color" => "#173177"],
  257. "keyword1" => ["value" => $orderSn, "color" => "#173177"],
  258. "keyword2" => ["value" => date("Y-m-d H:i:s"), "color" => "#173177"],
  259. "keyword3" => ["value" => $ghsShopAdminName, "color" => "#173177"],
  260. "remark" => ["value" => "点击查看详情", "color" => "#173177"]
  261. ]
  262. ];
  263. $data['miniprogram']['appid'] = $wx['miniAppId'];
  264. $data['miniprogram']['pagepath'] = 'pagesPurchase/purDetails?id=' . $orderId;
  265. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  266. }
  267. }
  268. //零售清算欠款通知供应商 shish 20210531
  269. public static function clearInform($shop, $order, $custom)
  270. {
  271. $shopId = $shop->id;
  272. $ptStyle = $shop->ptStyle;
  273. $wxBase = WxOpenClass::getWxBase();
  274. $wx = $wxBase[$ptStyle] ?? [];
  275. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  276. $shortTempId = 'OPENTM207422813';//收入提醒
  277. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  278. if (empty($tempId)) {
  279. return false;
  280. }
  281. $actPrice = $order->actPrice;
  282. $name = $custom->name ?? '';
  283. $customId = $custom->id;
  284. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  285. foreach ($adminList as $admin) {
  286. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  287. continue;
  288. }
  289. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  290. $data = [
  291. "touser" => $openId,
  292. "template_id" => $tempId,
  293. "url" => Yii::$app->params['ghsDomain'],
  294. "data" => [
  295. "first" => ["value" => "客户{$name}结款{$actPrice}元", "color" => "#173177"],
  296. "keyword1" => ["value" => $actPrice, "color" => "#173177"],
  297. "keyword2" => ["value" => '客户结款', "color" => "#173177"],
  298. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  299. "remark" => ["value" => "点击查看详情", "color" => "#173177"]
  300. ]
  301. ];
  302. $data['miniprogram']['appid'] = $wx['miniAppId'];
  303. $data['miniprogram']['pagepath'] = 'pagesArrears/details?id=' . $customId;
  304. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  305. }
  306. }
  307. //收款码收入通知 shish 20210531
  308. public static function gatheringIncomeInform($shop, $order)
  309. {
  310. $shopId = $shop->id;
  311. $ptStyle = $shop->ptStyle;
  312. $wxBase = WxOpenClass::getWxBase();
  313. $wx = $wxBase[$ptStyle] ?? [];
  314. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  315. $shortTempId = 'OPENTM207422813';//收入提醒
  316. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  317. if (empty($tempId)) {
  318. return false;
  319. }
  320. $actPrice = $order->actPrice;
  321. $orderId = $order->id;
  322. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  323. foreach ($adminList as $admin) {
  324. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  325. continue;
  326. }
  327. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  328. $data = [
  329. "touser" => $openId,
  330. "template_id" => $tempId,
  331. "url" => Yii::$app->params['ghsDomain'],
  332. "data" => [
  333. "first" => ["value" => "您有新的收入。", "color" => "#173177"],
  334. "keyword1" => ["value" => $actPrice, "color" => "#173177"],
  335. "keyword2" => ["value" => '二维码收款', "color" => "#173177"],
  336. "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  337. "remark" => ["value" => "点击查看详情", "color" => "#173177"]
  338. ]
  339. ];
  340. $data['miniprogram']['appid'] = $wx['miniAppId'];
  341. $data['miniprogram']['pagepath'] = 'admin/order/detail?id=' . $orderId;
  342. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  343. }
  344. }
  345. //提现成功通知 shish 20210531
  346. public static function cashInform($shop, $cash)
  347. {
  348. $shopId = $shop->id;
  349. $ptStyle = $shop->ptStyle;
  350. $wxBase = WxOpenClass::getWxBase();
  351. $wx = $wxBase[$ptStyle] ?? [];
  352. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  353. $shortTempId = 'OPENTM411207151';
  354. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  355. if (empty($tempId)) {
  356. return false;
  357. }
  358. $amount = $cash->amount;
  359. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  360. foreach ($adminList as $admin) {
  361. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  362. continue;
  363. }
  364. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  365. $data = [
  366. "touser" => $openId,
  367. "template_id" => $tempId,
  368. "url" => Yii::$app->params['ghsDomain'],
  369. "data" => [
  370. "first" => ["value" => "提现成功。", "color" => "#173177"],
  371. "keyword1" => ["value" => $amount, "color" => "#173177"],
  372. "keyword2" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
  373. "remark" => ["value" => "感谢您的使用", "color" => "#173177"]
  374. ]
  375. ];
  376. $data['miniprogram']['appid'] = $wx['miniAppId'];
  377. $data['miniprogram']['pagepath'] = 'pagesStore/me/withdraw';
  378. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  379. }
  380. }
  381. //订单送达通知 shish 20210607
  382. public static function reachInform($shop, $order)
  383. {
  384. $shopId = $shop->id;
  385. $ptStyle = $shop->ptStyle;
  386. $wxBase = WxOpenClass::getWxBase();
  387. $wx = $wxBase[$ptStyle] ?? [];
  388. $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
  389. $shortTempId = 'OPENTM201205968';
  390. $tempId = isset($message[$shortTempId]['templateId']) ? $message[$shortTempId]['templateId'] : '';
  391. if (empty($tempId)) {
  392. return false;
  393. }
  394. $orderId = $order->id ?? '';
  395. $orderSn = (string)$order->orderSn ?? '';
  396. $customName = $order->customName ?? '';
  397. $adminList = ShopAdminClass::getRemainAdmin($shopId);
  398. foreach ($adminList as $admin) {
  399. if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
  400. continue;
  401. }
  402. $openId = isset($admin['openId']) ? $admin['openId'] : '';
  403. $data = [
  404. "touser" => $openId,
  405. "template_id" => $tempId,
  406. "url" => Yii::$app->params['ghsDomain'],
  407. "data" => [
  408. "first" => ["value" => '客户订单已送达', "color" => "#173177"],
  409. "keyword1" => ["value" => $orderSn, "color" => "#173177"],
  410. "keyword2" => ["value" => $customName, "color" => "#173177"],
  411. "keyword3" => ["value" => '已送达', "color" => "#173177"],
  412. "remark" => ["value" => '点击查看详情', "color" => "#173177"]
  413. ]
  414. ];
  415. $data['miniprogram']['appid'] = $wx['miniAppId'];
  416. $data['miniprogram']['pagepath'] = 'pagesOrder/detail?id=' . $orderId;
  417. wxUtil::sendTaskInform($data, $wx, $ptStyle);
  418. }
  419. }
  420. }