微信小程序完整开发小程序模板消息,定时发送,附前台后台源码数据库参考

首先第一步,获取 access_token

这个access_token需要去微信公众平台去获取

由于access_tokem是有过期时间的也就是2个小时,所以咱们需要新建一个数据库来存储这些access_token判断是否过期zhe 

这是数据库结构

接下来我们需要写一下后端代码

 public function getaccess_token()
    {
        $appid = '';
        $appsecret = '';
        //这两个可以从微信公众平台获取
    }

下一步贴一下代码,基本粘贴过去就能用的,换上自己的表跟字段

 public function getaccess_token()
    {
        $appid = '';
        $appselect = '';
        //这两个可以从微信公众平台获取
        $sql    = Db::table('xdk_token')
            ->where([
                'appid' => $appid,
                'appselect'=>$appselect
            ])
            ->find();
            $now = time();
            //获取当前时间戳

         if ($now-$sql['apptime']<7000){
             $token = $sql['token'];
                return $token;

         }else{
             $token_access = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appselect";
             //执行请求file_get_contents
             $tokens =file_get_contents($token_access);
             // JSON转换
             $tokening= json_decode($tokens);
             $token =$tokening->access_token;
             //获取Token字段值
            $data = [
            'token'=> $token,
            'apptime'=>$now,
            'appid'=>$appid,
            'appselect'=>$appselect
        ];
            Db::table('xdk_token')->insert($data);
             return $token;
         }


    }

我这里是用的TP5框架写的,大家可以用在自己的写法写


版权声明:本文为weixin_44069250原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。