微信开放平台(第三方平台)代替微信公众号发送模板消息(基于lavarel框架开发,EasyWeChat)

1、公众号必须得把模板消息授权到第三方平台。
在这里插入图片描述
2、我用的是 EasyWeChat

3、通过接口修改账号所属行业 (实质上就是开通模板消息)

//修改账号所属行业
    public function set_industry()
    {
        $store_id = request('store_id', get_store_id());//小店id
        $template = $this->base_model($store_id);
        $result = $template->setIndustry(1, 31);
        Log::channel('wx_third')->info('修改公众号所属行业 store_id:' . $store_id . ' -- ' . json_encode($result));
        if ($result) {
            return ["message" => "设置成功!", 'data' => $result];
        }
        return ["message" => '失败', "code" => 201];
    }

4、选择模板,进入公众号后台(新的功能 已开通 模板库)选择对应的消息模板
在这里插入图片描述
5、添加模板 (因为是多个公众号所以要把每个公众号对应的模板id存下,发消息的时候需要用到)
可以添加一个也可以批量添加。

//添加一个模板
    public function add_a_template()
    {
        $store_id = get_store_id();//小店id
        $short_id = request('short_id');//模板编号(模板库的模板编号)
        $list = $this->get_wx_template($store_id);
        if (count($list) > 23) {
            return ["message" => '公众号模板库已满,请先删除其他模板', "code" => 201];
        }
        if (empty($short_id)) {
            return ["message" => "模板编号不能为空!", "code" => 201];
        }
        $library = $this->smallTemplateMessage->template_library();//模板库
        if (!isset($library[$short_id])) {
            return ["message" => "模板编号不存在!", "code" => 201];
        }
        $info = $library[$short_id];
        $result = $this->add_template($store_id, $short_id);
        $time = date('Y-m-d H:i:s');
        if ($result['errcode'] == 0) {
            $template_array = [
                'store_id' => $store_id,
                'template_id' => $result['template_id'],
                'template_title' => $info['title'],
                'template_type' => $info['template_type'],
                'short_id' => $short_id,
                'created_at' => $time,
                'status' => 1,
            ];
            $where = ['store_id' => $store_id, 'template_type' => $info['template_type']];
            if ($this->smallTemplateMessage->updateOrCreate($where, $template_array)) {
                return ["message" => "添加成功!"];
            }
        }

        return ["message" => "添加失败!", "code" => 201];
    }

    //添加模板
    public function add_template($store_id, $short_id)
    {
        $template = $this->base_model($store_id);
        return $template->addTemplate($short_id);
    }

6、模板消息对应的参数
在这里插入图片描述
模板消息案例

//模版库
    public function template_library()
    {
        return [
            //商家端
            'OPENTM409521500' => ['template_type' => 11, 'title' => '订单发货提醒'],
            'OPENTM407277862' => ['template_type' => 12, 'title' => '退款申请通知'],
            'OPENTM410181374' => ['template_type' => 13, 'title' => '提现申请通知'],
            'OPENTM417244450' => ['template_type' => 14, 'title' => '注册成功提醒'],
            //用户端
            'OPENTM418245409' => ['template_type' => 21, 'title' => '订单发货提醒'],
            'OPENTM418336055' => ['template_type' => 22, 'title' => '订单超时提醒'],
            'OPENTM416598606' => ['template_type' => 23, 'title' => '退款成功通知'],
            'OPENTM401105166' => ['template_type' => 24, 'title' => '退款申请驳回通知'],
        ];
    }

    //消息类型
    public function message_type($type, $data)
    {
        switch ($type) {
            case 11:
                return [
                    'first' => '您有一个新的待发货订单!',
                    'keyword1' => '3705121411',
                    'keyword2' => '666.00元',
                    'keyword3' => '爱你的宝宝',
                    'keyword4' => '2017-02-20 14:11:26',
                    'remark' => '用户已付款,尽快发货吧!'
                ];
            case 12:
                return [
                    'first' => '您有一个新的退货订单!',
                    'keyword1' => '201609300012344',
                    'keyword2' => '20.00元',
                    'keyword3' => '感觉不太好,不想要了',
                    'remark' => '请及时登录PC端后台协商处理,避免引起不必要的纠纷!'
                ];
            case 13:
                return [
                    'first' => '推广员申请提现申请!',
                    'keyword1' => '149.48 元',
                    'keyword2' => '2.00元',
                    'keyword3' => '2017-02-20 14:11:26',
                    'keyword4' => '银行卡',
                    'remark' => '请及时登录PC端后台审核处理!'
                ];
            case 14:
                return [
                    'first' => '您有一位新用户注册成功啦!',
                    'keyword1' => '爱你的宝宝',
                    'keyword2' => '13866666666',
                    'keyword3' => '2022-02-26 14:03:59',
                ];
            case 21:
                return [
                    'first' => '尊敬的xx用户,您的货物已发出!',
                    'keyword1' => '牛皮男士钱包',
                    'keyword2' => '共2件',
                    'keyword3' => '黄宏东,广东省 深圳市 宝安区',
                    'keyword4' => '2022-02-26 14:03:59',
                ];
            case 22:
                return [
                    'first' => '您有一个订单支付超时!',
                    'keyword1' => '哇哈哈4瓶',
                    'keyword2' => '0004529847',
                    'keyword3' => '2022-02-26 14:03:59',
                    'keyword4' => '订单已关闭',
                ];
            case 23:
                return [
                    'first' => '您的退款申请已受理!',
                    'keyword1' => '哇哈哈4瓶',
                    'keyword2' => '0004529847',
                    'keyword3' => '用户申请退款',
                    'keyword4' => '55.02元',
                    'remark' => '具体到账时间以收到时间为准',
                ];
            case 24:
                return [
                    'first' => '您的退款申请被商家驳回!',
                    'keyword1' => '22.00 元',
                    'keyword2' => '惠氏金装1段奶粉',
                    'keyword3' => '0004529847',
                    'remark' => '可拨打800客服电话进一步沟通',
                ];
            default:
                return [];
        }
    }

7 、添加成功了就可发送消息了

    //发送模板消息
    public function send_message()
    {
        $store_id = 52; //小店id
        $type = (int)request('type', 14);//消息类型
        $to_user = 'ooX4o60aZRoUvsb8-oesImf';//收件人(用户用户微信授权后的 openid)
        $data1 = [];//模板消息数据 - 需要到对应的模板赋值
        $url = request('url', ''); //模板消息查看详情 跳转地址(可以为空)

        $data = $this->message_type_data($type, $data1);
        return $this->sending($store_id, $type, $to_user, $data, $url);
    }
<?php

namespace App\Http\Repositories\SmallShop\Traits;

use Illuminate\Support\Facades\Log;

trait WeChatTrait
{
    /**
     * 实例化微信第三方类
     * @return \EasyWeChat\OpenPlatform\Application
     */
    public function third_open_platform()
    {
        return \EasyWeChat\Factory::openPlatform(config('wechat.open_platform.default'));
    }

    /**
     * 实例化指定公众号类
     * @param $store_id
     * @return \EasyWeChat\OpenPlatform\Authorizer\OfficialAccount\Application|false
     */
    public function platform_model($store_id)
    {
        $account = (new \App\Model\SmallShop\SmallPlatformAccount())->where(['store_id' => $store_id, 'status' => 1])
            ->orderBy('id', 'desc')->first(['id', 'store_id', 'appid', 'refresh_token']);
        if ($account) {
            return $this->third_open_platform()->officialAccount($account->appid, $account->refresh_token);
        }

        return false;
    }

    //获取公众号授权信息
    public function get_authorizer_info($appid)
    {
        return $this->third_open_platform()->getAuthorizer($appid);
    }
    
    //根据消息类型给赋值
    public function message_type_data(int $type, array $data)
    {
        return (new \App\Model\SmallShop\SmallTemplateMessage())->message_type($type, $data);
    }

    //发送公众号模板消息
    public function sending(int $store_id, int $type, string $to_user, array $data, $url = '')
    {
        $where = ['store_id' => $store_id, 'template_type' => $type, 'status' => 1];
        $template = (new \App\Model\SmallShop\SmallTemplateMessage())->where($where)->first('template_id');
        if (empty($template)) {
            return ["message" => '模板不存在或已被禁用', "code" => 201];
        }

        //发给商家特殊处理
        if ($type < 19) {
            $to = (new \App\Model\SmallShop\SmallPlatformAccount())->where(['store_id' => $store_id, 'status' => 1])->first('wx_openid');
            if ($to && empty($to->wx_openid)) {
                return ["message" => '商家未绑定接收消息微信账户', "code" => 201];
            }
            $to_user = $to->wx_openid;
        }

        $officialAccount = $this->platform_model($store_id);
        if (!$officialAccount) {
            return ["message" => '公众号授权有误', "code" => 201];
        }

        $message = ['touser' => $to_user, 'template_id' => $template->template_id, 'url' => $url, 'data' => $data];
        $result = $officialAccount->template_message->send(array_filter($message));

        if ($result['errcode'] == 0) {
            return ["message" => "消息发送成功!", "data" => $result, "code" => 200];
        } elseif ($result['errcode'] == 43004) {
            return ["message" => "接收者未关注公众号!", "data" => $result, "code" => 201];
        } else {
            $log = ['store_id' => $store_id, 'type' => $type, 'to_user' => $to_user, 'result' => $result];
            Log::channel('wx_third_message')->info('消息发送失败:' . json_encode($log));
            return ["message" => '消息发送失败!', "data" => $result, "code" => 201];
        }
    }
}

8、结果
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值