| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace biz\wx\classes;
- use biz\base\classes\BaseClass;
- use biz\shop\classes\ShopAdminClass;
- use common\components\dict;
- use common\components\util;
- use common\components\wxUtil;
- use Yii;
- class WxMessageClass extends BaseClass
- {
- public static $baseFile = '\biz\wx\models\WxMessage';
- public static function has()
- {
- return [
- //零售
- dict::getDict('ptStyle', 'hd') => [
- 'OPENTM417875155' => '新订单通知',
- 'OPENTM207430125' => '操作成功通知',
- 'OPENTM401915538' => '审核通知',
- 'TM00006' => '充值通知',
- 'OPENTM201205968' => '订单送达通知',
- 'OPENTM207777535' => '付款提醒',
- 'OPENTM207422813' => '收入提醒',
- 'OPENTM202297555' => '下单成功通知',
- 'OPENTM207327227' => '宝贝售出提醒',
- 'OPENTM207188526' => '付款成功',
- ],
- //供货商
- dict::getDict('ptStyle', 'ghs') => [
- 'OPENTM417875155' => '新订单通知',
- 'OPENTM207430125' => '操作成功通知',
- 'OPENTM401915538' => '审核通知',
- 'TM00006' => '充值通知',
- 'OPENTM201205968' => '订单送达通知',
- 'OPENTM207777535' => '付款提醒',
- 'OPENTM207422813' => '收入提醒',
- 'OPENTM202297555' => '下单成功通知',
- 'OPENTM207327227' => '宝贝售出提醒',
- 'OPENTM207188526' => '付款成功',
- ],
- //商城
- dict::getDict('ptStyle', 'mall') => [
- ],
- ];
- }
- //初始化消息模板
- // ./yii.bat wx-message/init
- public static function init()
- {
- $wxBase = [
- dict::getDict('ptStyle', 'hd') => \bizHd\wx\classes\WxOpenClass::getWxInfo(),
- dict::getDict('ptStyle', 'ghs') => \bizHd\wx\classes\WxOpenClass::getGhsWxInfo(),
- dict::getDict('ptStyle', 'mall') => \bizHd\wx\classes\WxOpenClass::getMallWxInfo(),
- ];
- $message = self::getAllByCondition([], null, '*', null, true);
- if (!empty($message)) {
- foreach ($message as $item) {
- $ptStyle = $item->ptStyle;
- $wx = $wxBase[$ptStyle] ?? [];
- if (empty($wx)) {
- util::fail('没有找到微信平台信息');
- }
- $templateId = $item->templateId;
- $respond = wxUtil::delTMessage($wx, $templateId, $ptStyle);
- if (isset($respond['errcode']) && $respond['errcode'] == 0) {
- $item->delete();
- }
- }
- }
- $wxMessageList = self::has();
- foreach ($wxMessageList as $k => $message) {
- $ptStyle = $k;
- if (empty($message)) {
- continue;
- }
- $wx = $wxBase[$ptStyle] ?? [];
- foreach ($message as $shortId => $title) {
- $wxReturn = wxUtil::tMessageCreate($wx, $shortId, $ptStyle);
- if (isset($wxReturn['errcode']) && $wxReturn['errcode'] == 0) {
- $templateId = $wxReturn['template_id'];
- $data = [
- 'shortTemplateId' => $shortId,
- 'templateId' => $templateId,
- 'templateName' => $title,
- 'ptStyle' => $ptStyle,
- ];
- self::add($data);
- Yii::warning("add tmessage:" . $title . " success\n");
- } else {
- Yii::warning("add tmessage error:" . json_encode($wxReturn) . " \n");
- }
- }
- }
- }
- //新订单通知 shish 20210529
- public static function newOrderNotice($shop, $order)
- {
- $shopId = $shop->id;
- $ptStyle = $shop->ptStyle;
- $wxBase = [
- dict::getDict('ptStyle', 'hd') => \bizHd\wx\classes\WxOpenClass::getWxInfo(),
- dict::getDict('ptStyle', 'ghs') => \bizHd\wx\classes\WxOpenClass::getGhsWxInfo(),
- dict::getDict('ptStyle', 'mall') => \bizHd\wx\classes\WxOpenClass::getMallWxInfo(),
- ];
- $wx = $wxBase[$ptStyle] ?? [];
- $message = self::getAllByCondition(['ptStyle' => $ptStyle], null, '*', 'shortTemplateId');
- $shortTempId = 'OPENTM417875155';//收入提醒
- $tempId = isset($message[$shortTempId]) ? $message[$shortTempId] : '';
- if (empty($tempId)) {
- return false;
- }
- $orderId = $order->id;
- $adminList = ShopAdminClass::getRemainAdmin($shopId);
- foreach ($adminList as $admin) {
- if (isset($admin['subscribe']) == false || $admin['subscribe'] == 0) {
- continue;
- }
- $openId = isset($admin['openId']) ? $admin['openId'] : '';
- $data = [
- "touser" => $openId,
- "template_id" => $tempId,
- "url" => Yii::$app->params['ghsDomain'],
- "data" => [
- "first" => ["value" => "恭喜,您获得一张优惠劵。", "color" => "#173177"],
- "keyword1" => ["value" => '查看', "color" => "#173177"],
- "keyword2" => ["value" => '优惠劵', "color" => "#173177"],
- "keyword3" => ["value" => date("Y-m-d H:i"), "color" => "#173177"],
- "remark" => ["value" => "点击查看详情", "color" => "#173177"]
- ]
- ];
- $data['miniprogram']['appid'] = $wx['miniAppId'];
- $data['miniprogram']['pagepath'] = 'pagesOrder/detail?id=' . $orderId;
- wxUtil::sendTaskInform($data, $wx);
- }
- }
- }
|