100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信开放平台(第三方平台)代替微信公众号发送模板消息(基于lavarel框架开发 EasyWeChat)

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

时间:2024-02-18 11:37:16

相关推荐

微信开放平台(第三方平台)代替微信公众号发送模板消息(基于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' => '-02-20 14:11:26','remark' => '用户已付款,尽快发货吧!'];case 12:return ['first' => '您有一个新的退货订单!','keyword1' => '09300012344','keyword2' => '20.00元','keyword3' => '感觉不太好,不想要了','remark' => '请及时登录PC端后台协商处理,避免引起不必要的纠纷!'];case 13:return ['first' => '推广员申请提现申请!','keyword1' => '149.48 元','keyword2' => '2.00元','keyword3' => '-02-20 14:11:26','keyword4' => '银行卡','remark' => '请及时登录PC端后台审核处理!'];case 14:return ['first' => '您有一位新用户注册成功啦!','keyword1' => '爱你的宝宝','keyword2' => '13866666666','keyword3' => '-02-26 14:03:59',];case 21:return ['first' => '尊敬的xx用户,您的货物已发出!','keyword1' => '牛皮男士钱包','keyword2' => '共2件','keyword3' => '黄宏东,广东省 深圳市 宝安区','keyword4' => '-02-26 14:03:59',];case 22:return ['first' => '您有一个订单支付超时!','keyword1' => '哇哈哈4瓶','keyword2' => '0004529847','keyword3' => '-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);}

<?phpnamespace 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、结果

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。