WechatSendMessage.php 7.7 KB
<?php

namespace app\common\library;
use EasyWeChat\Factory;


/**
 * 微信公众号-发送模版消息
 */
class WechatSendMessage
{
    /**
     * 发送一次
     *
     */
    public static function sendMessage($project, $openid, $keyword3)
    {
        $config = config('wechat.official_account');
        $app    = Factory::officialAccount($config);
        $re     = [];
        switch ($project) {
            //订单驳回修改
            case 'self_order_edit':
                $template = config('project.self_order_edit');
                $rs       = $app->template_message->send([
                    'touser'      => $openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的订单已被驳回!',
                        'keyword1' => '订单不符合下单要求,请修改',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                break;
            //订单驳回退款
            case 'self_order_refund':
                $template = config('project.self_order_refund');
                $rs       = $app->template_message->send([
                    'touser'      => $openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的订单已被驳回!',
                        'keyword1' => '订单不符合下单要求,请申请退款',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                break;
            //退款审核不通过
            case 'self_order_fail':
                $template = config('project.self_order_fail');
                $rs       = $app->template_message->send([
                    'touser'      => $openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的申请退款已被驳回!',
                        'keyword1' => '请重新申请退款',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                break;
            //代客户-订单驳回修改
            case 'replace_order_edit':
                $template = config('project.replace_order_edit');
                $rs       = $app->template_message->send([
                    'touser'      => $openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的代下订单已被驳回!',
                        'keyword1' => '请修改后重新提交',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                break;
        }
        if (empty($rs) || $rs['errcode'] != 0) {
            return ['', 5001, $rs['errmsg']];
        }
        return ['', 1, $rs['errmsg']];
    }

    /**
     * 发送二次
     */
    public static function sendListMessage($project, $user_openid, $pay_openid, $keyword3)
    {
        $config = config('wechat.official_account');
        $app    = Factory::officialAccount($config);
        $user   = [];
        $pay    = [];
        switch ($project) {
            //代客户-订单驳回退款   推广员
            case 'replace_order_refund':
                $template = config('project.replace_order_refund');
                $user     = $app->template_message->send([
                    'touser'      => $user_openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的订单已被驳回!',
                        'keyword1' => '订单不符合下单要求,请申请退款',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                //代客户-订单驳回退款   用户
                $pay = $app->template_message->send([
                    'touser'      => $pay_openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的订单已被驳回!',
                        'keyword1' => '订单不符合下单要求,请申请退款',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);

                break;
            //代客户-退款审核不通过
            case 'replace_order_fail':
                $template = config('project.replace_order_fail');
                $user     = $app->template_message->send([
                    'touser'      => $user_openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的申请退款订单已被驳回!',
                        'keyword1' => '您的申请退款不通过',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                //代客户-订单驳回退款   用户
                $pay = $app->template_message->send([
                    'touser'      => $pay_openid,
                    'template_id' => $template['template_id'],
                    'url'         => $template['url'],
                    'data'        => [
                        'first'    => '您好,您的申请退款订单已被驳回!',
                        'keyword1' => '您的申请退款不通过',
                        'keyword2' => '《中保汽服》',
                        'keyword3' => $keyword3,
                        'keyword4' => date('Y-m-d H:i:s', time()),
                        'remark'   => '感谢您的使用!',
                    ],
                ]);
                break;
        }
        if (empty($user) || $user['errcode'] != 0) {
            return ['', 5001, $user['errmsg']];
        }
        if (empty($pay) || $pay['errcode'] != 0) {
            return ['', 5001, $pay['errmsg']];
        }
        return ['', 1, $user['errmsg']];
    }
}