微信公众号登录和获得access_token和发送模板消息

<?php
declare (strict_types = 1);

namespace app\index\controller;

use think\facade\Db;
use think\Request;
use think\facade\View;
use \app\common\Facades;
use think\facade\Cache;
use app\index\model\User;
/**
 * 首页和个人中心
 * */
class Index
{
    public static function wxapp(){
        $date["appid"] = "appid";
        $date["appsecret"] = "appsecret";
        //$date["url"] = "http://bdzjm.369showwq.com";
        $date["url"] = "成功跳回自己域名链接";
        return $date;
    }
    /**
     * index首页
     */
    public function index(Request $request)
    {
        header("Content-type:text/html;charset=utf-8");
        $appids = $this->wxapp();
        $appid = $appids['appid'];
        $appsecret = $appids['appsecret'];
        $url = $appids['url'];
        $code = $request->get("code");
        $openid =  Cache::get("openid");
        if($code){
            $urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
            $result = $this->curl_post($urls);
            $openid = $result["openid"];
            Cache::set("openid",$openid);
        }else{
            header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_re");
        }
        $list = Db::query("select * from ims_ewei_shop_merch_user where status=1 and uniacid=1");
        $userlist = Db::query("select * from ims_make_speed_rider");
        View::assign([
            'list'  => $list,
            'userlist'=>$userlist,
            "openid" => $openid,
        ]);
        return View::fetch('index');
    }
    public function indexpost(Request $request){
        $date['openid'] = $request->post("openid");
        $date['merchid']  = $request->post("merchid");
        $date['riderid'] = $request->post("riderid");
        $date['date'] = time();
        $user = User::where("openid",$date['openid'])->select();
        if(empty($user[0]['id'])){
            User::create($date);
            return $date;
        }else{
            return 2;
        }
    }

    //发送post请求
    static function curl_post($url,$data=array()){
        $ch = curl_init();//创建curl请求
        curl_setopt($ch, CURLOPT_URL,$url); //设置发送数据的网址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值,0,直接显示
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
        curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post请求发送的数据包
        $data = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($data,true); //将json数据转成数组
        return $data;
    }
    //发送get请求
    static function curl_get($url){
        $s = file_get_contents($url);
        $s = json_decode($s, true);
        return $s;
    }
    
    //通知定时任务
    public function  notice(Request $request){
        $appids = $this->wxapp();
        $appid = $appids['appid'];
        $secret = $appids['appsecret'];
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $weix = file_get_contents($url);//获得网页输出
        // print_r($weix);die;
        $obj=json_decode($weix,true );//解码
        $access_token= $obj['access_token'];//网页授权接口调用凭证
        // echo $access_token;die;
        //发送模板消息url
        $fasuerl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
        
        
        $list = Db::query("select * from ims_make_speed_order where status=2 and (notice=1 or notice=2) ");//查询订单
        $ids = 0;
        $idsa = 0;
        foreach($list as $it){
            if($it['notice'] == 1){
                //调用模板消息
                 $title = '"'.$it['goodsname'].'"';
                 $goods = Db::query("select * from ims_ewei_shop_goods where title=$title");//查询门店
                 $orderid = $it['id'];
                 $listaddress = Db::query("select * from ims_make_speed_order_address where order_id=$orderid");//查询用户信息
                 if(!empty($goods[0]['id'])){
                     $merchid = '"'.$goods[0]['merchid'].'"';
                     $notice = Db::query("select * from ims_wxapp_merch_rider where merchid=$merchid");//查询骑手
                     $goodsname = $it['goodsname'];//商品名称
                     $name = $listaddress[0]['end_username'];//用户姓名
                     $phone = $listaddress[0]['end_phone'];//用户电话
                     $end_address = $listaddress[0]['end_address'].$listaddress[0]['end_detail'];//用户地址
                     $dates = date("Y-m-d H:i:s",$it['add_time']);
                     $text = "请骑手尽快处理。";
                     foreach($notice as $it1){
                         $openid = $it1['openid'];//openid
                         $params = $this->mb1($openid,$goodsname,$name,$phone,$end_address,$dates,$text);
                          //发送post(模板消息通知)
                         $res=$this->curl_post($fasuerl,$params);
                         $ids = $ids + 1;
                     }
                     $id = $it['id'];
                     Db::execute("update ims_make_speed_order set notice=2 where id=$id"); 
                 }
            }
            if($it['notice'] == 2){
                $tiem = date("YmdHi",$it['add_time']);
                $tiems = date("YmdHi");
                $im = $tiems-$tiem;
                if($im>3){
                     //调用模板消息
                     $title = '"'.$it['goodsname'].'"';
                    //  $goods = Db::query("select * from ims_ewei_shop_goods where title=$title");//查询门店
                     $orderid = $it['id'];
                     $listaddress = Db::query("select * from ims_make_speed_order_address where order_id=$orderid");//查询用户信息
                     
                     $notice = Db::query("select * from ims_wxapp_merch_rider");//查询骑手
                     $goodsname = $it['goodsname'];//商品名称
                     $name = $listaddress[0]['end_username'];//用户姓名
                     $phone = $listaddress[0]['end_phone'];//用户电话
                     $end_address = $listaddress[0]['end_address'].$listaddress[0]['end_detail'];//用户地址
                     $dates = date("Y-m-d H:i:s",$it['add_time']);
                     $text = "请骑手尽快处理,该订单超过三分钟。";
                     foreach($notice as $it1){
                         $openid = $it1['openid'];//openid
                         $params = $this->mb1($openid,$goodsname,$name,$phone,$end_address,$dates,$text);
                          //发送post(模板消息通知)
                         $res=$this->curl_post($fasuerl,$params);
                         $idsa = $idsa + 1;
                     }
                     $id = $it['id'];
                     Db::execute("update ims_make_speed_order set notice=3 where id=$id"); 
                }
            }
        }
        echo "新订单通知了".$ids."条,三分钟通知了".$idsa."条";
    }
    // 模板
    public static function mb(){
         $date = date('Y-m-d H:i:s');
         $data = array(
            "touser"=>"ovH-s6quZbzRBswtK4D_TkX9Vls0",
            "template_id"=>"3MJHBDiD38gcvGHVTPTX2R0A8l_e2VYSZLiKWD98Yro",
            'url'=>"bdzjm.510show.com",
             "data" => array(
                "first" => array(
                    "value"=>"接到一个新订单:详情",
                    "color"=>"#173177"
                ),
                "keyword1" => array(
                    "value"=>"$date",
                    "color"=>"#000000"
                ),
                "keyword2" => array(
                    "value"=>"张三",
                    "color"=>"#000000"
                ),
                "keyword3" => array(
                    "value"=>"14798767853",
                    "color"=>"#000000"
                ),
                "keyword4" => array(
                    "value"=>"商品名称",
                    "color"=>"#000000"
                ),
                "keyword5" => array(
                    "value"=>"1",
                    "color"=>"#000000"
                ),
                "remark" => array(
                    "value"=>"请骑手尽快处理。",
                    "color"=>"#173177"
                ),
            )
        );
        $params = json_encode($data);
        return $params;
    }
    //模板,跳转小程序
    public static function mb1($openid,$goodsname,$name,$phone,$end_address,$dates,$text){
         $date = date('Y-m-d H:i:s');
         $data = array(
            "touser"=>"$openid",
            "template_id"=>"eOXeRCqpN97lQEgTFiX4_oUsdUwb8mZQtguka0v6Xm4",
            "miniprogram"=> array(
               "appid"=>"wx9dcb8c5a2b7b6a55",
               "pagepath"=>"make_rider/index/index"
            ),  
            "data" => array(
                "first" => array(
                    "value"=>"接到一个新订单:详情",
                    "color"=>"#173177"
                ),
                "keyword1" => array(
                    "value"=>"$dates",
                    "color"=>"#000000"
                ),
                "keyword2" => array(
                    "value"=>"$name",
                    "color"=>"#000000"
                ),
                "keyword3" => array(
                    "value"=>"$phone",
                    "color"=>"#000000"
                ),
                "keyword4" => array(
                    "value"=>"$goodsname",
                    "color"=>"#000000"
                ),
                "keyword5" => array(
                    "value"=>"1",
                    "color"=>"#000000"
                ),
                "remark" => array(
                    "value"=>"$text",
                    "color"=>"#173177"
                ),
            )
        );
        $params = json_encode($data);
        return $params;
    }


}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Java微信公众号定时发送消息模板通常分为两部分: 第一部分是实现Wechat API与Java后端的通信。Wechat API是微信提供的接口,可以让我们通过Java代码与微信公众号进行交互。具体来说,我们需要在Java中通过HTTP或HTTPS协议发送请求,获取微信公众号access_token,再通过access_token来进行后续的操作,例如:获取用户的消息发送消息、创建菜单等等。Java中可以使用Apache HttpClient或OkHttp等库来发送HTTP请求,拿到接口返回的JSON数据。 第二部分是实现定时发送消息。Java中可以使用Quartz框架用于实现定时任务。Quartz是一个开源的作业调度框架,可以用于在指定时间执行任务。我们可以利用Quartz提供的API,创建一个定时任务,定期调用我们的后端Java代码发送微信公众号消息。在定时任务的实现中,我们需要考虑任务的频率、执行时间、失败策略等问题。 当以上两部分都实现以后,我们就可以在Java后端实现微信公众号定时发送消息的功能了。具体来说,我们需要将发送消息的代码放到定时任务中,指定要发送消息内容,以及要发送给的用户。然后启动定时任务即可,Java后端会自动按照设定的规则发送微信公众号消息。 值得注意的是,微信官方文档要求所有公众号对接的应用都必须是官方认证的,所以在实现这个功能之前,我们需要先将我们的应用向微信官方申请认证,获得相应的API调用权限。 ### 回答2: Java微信公众号定时发送消息模板是一种利用Java语言实现的微信公众平台的定时发送消息模板。该模板可以帮助微信公众号上的管理员在指定时间点自动发送指定的消息,从而提高工作效率和用户体验。 实现Java微信公众号定时发送消息模板的关键是使用了微信公众平台提供的接口。这些接口可以通过Java语言进行调用,从而实现向微信公众号发送消息、设置菜单、自动回复、素材管理、用户管理等功能。在定时发送消息模板中,我们需要利用这些接口设置定时任务,并在指定时间点调用接口发送消息。 具体实现过程如下: 1. 首先需要在微信公众平台上申请开发者账号,并获取相应的开发者ID、开发者密码和Token。 2. 在Java中使用微信公众平台提供的SDK调用相关接口,实现发送消息、设置菜单、自动回复等功能。 3. 利用Java中的定时任务框架,如Quartz、Spring定时任务等,在指定的时间点调用相应的微信接口实现定时发送消息。 4. 利用Java中的模板功能,以标准化的格式组织需要发送消息内容,使消息具有可读性和易管理性。 需要注意的是,为了避免被微信公众平台封禁账号,我们需要遵循微信公众平台的相关规定,比如定时发送消息不能含有敏感词汇、不能频繁发送等。另外,定时发送的频次也需要控制在一定范围内,避免对用户造成困扰。 总之,Java微信公众号定时发送消息模板是一种非常实用的工具,可以帮助管理员提高工作效率和用户体验。但实现过程需要注意一些细节和规范,确保操作的合法性和安全性。 ### 回答3: Java微信公众号定时发送消息模板是指利用Java编程语言实现微信公众号定时发送消息功能,并且提供了消息模板方便开发者进行二次开发。这个模板主要包含以下几个方面: 一、微信公众号接入 首先,需要在微信公众号后台注册并获取开发者ID和开发者密钥,然后使用Java框架接入微信公众号的开放平台。接入之后,就可以使用微信公众号提供的API实现发送消息功能。 二、定时任务 接下来,需要编写Java代码实现定时任务功能。Java提供了Timer和TimerTask类可以很方便地实现定时任务功能。开发者可以选择设定每日,周,月等不同的定时任务。 三、发送消息 定时任务设置好后,需要编写Java代码实现发送消息的功能。这个功能可以通过调用微信公众号提供的API来实现。消息发送可以以文本消息,图文消息,视频消息等不同的形式呈现。 四、编写消息模板 最后,为了让开发者方便使用,我们需要编写一个消息模板来进行二次开发。消息模板可以包含以下几个方面:微信公众号接入的配置信息、定时任务的配置信息、发送消息的配置信息,以及发送消息的实现方法等。这个模板可以让开发者快速地实现微信公众号定时发送消息的功能。 通过这个模板,开发者可以很方便地搭建起微信公众号定时发送消息的系统,从而可以提高服务的质量和用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值