首页 » 产品日记 » 正文

TP6写微信小程序订阅消息(5.6)

记录用TP6开发后端接口(5.3) 详细记录小程序的订阅消息功能。
首先写个配置文件,单独放到TP框架的config目录中
代码如下:


return [

    //小程序的appid和secret
    'app_id' => 'xxxxxxxxxxxxxxxxx',
    'app_secret' => 'xxxxxxxxxxxxxxxx',

    //商品保质期临期和过期提醒模版ID,在微信后台配置
    'tmpid1' =>'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'tmpid2' =>'xxxxxxxxxxxxxxxxxxxxxxxx',

    // 前端uni.login获取code换取用户openid
    'login_url' =>"https://api.weixin.qq.com/sns/jscode2session?        
    appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",

    // 微信获取access_token
    'access_token_url' => "https://api.weixin.qq.com/cgi-bin/token?" .
        "grant_type=client_credential&appid=%s&secret=%s",

    //发送模板消息
    'sendTemplateMessage_url' => "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=%s",
];

如配置所示,也是按照配置顺序,来实现业务功能。
第二步,把下面的代码丢到控制器中:

 public function getAccessToken(){
// 获取accesstoken
        $wxAppID = config('wxmini.app_id');
        $wxAppSecret = config('wxmini.app_secret');
        $wxAccessTokenUrl = sprintf(config('wxmini.access_token_url'), $wxAppID, $wxAppSecret);

        $result = json_decode(file_get_contents($wxAccessTokenUrl), true);

        return $result['access_token'];
    }

public function sendMessage($openId,$tpltype,$goodsname,$enddate)
    {

        $access_token = $this->getAccessToken();

        //要发送给微信接口的数据
        $send_data = [];
        //用户openId
        $send_data['touser'] = $openId;

        //模板id
        if ($tpltype==1){
            // 1 为临期
            $send_data['template_id'] = config('wxmini.neardate_id');
            $send_data['data'] = [
                "thing1" => [
                    "value"=> $goodsname
                ],
                "date3" => [
                    "value"=> $enddate
                ],
                "thing6" => [
                    "value"=> '请注意该商品还有3天到期哦'
                ]
            ];
        }
        if ($tpltype==0){
            // 0 为过期或到期
            $send_data['template_id'] = config('wxmini.expdate_id');
            $send_data['data'] = [
                "thing1" => [
                    "value"=> $goodsname
                ],
                "time2" => [
                    "value"=> $enddate
                ],
                "thing3"=>[
                    "value"=>"该商品于今日过期,请妥善处理"
                ]
            ];
        }

        //点击跳转到小程序的页面
        $send_data['page'] = '这里是小程序路径';


        //将路径中占位符%s替换为$access_token值
        $urls = sprintf(config('wxmini.sendTemplateMessage_url'), $access_token);

        $ret = $this->curl_post($urls, $send_data);
        return $ret;
    }

/**
 * @param string $url post请求地址
 * @param array $params
 * @return mixed
 */
function curl_post($url, array $params = array())
{
    $data_string = json_encode($params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt(
        $ch, CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json'
        )
    );
    $data = curl_exec($ch);
    curl_close($ch);
    return ($data);
}

最后一步就是实现根据指定条件来执行发送消息了,我这里的业务逻辑很简单,就是保质期与当前日期对比,小于等于当前日期就是过期,发送过期消息,距离保质期剩余3天的时候执行临期提醒。

public function sendmsg()
   {
if(保质期<=当前日期)
$this->sendMessage(要发送的人,模版ID,商品名称,到期日期)
}

保质期的最小颗粒度是天,所以我只需要让这个sendmsg每天执行一次就可以了。直接做了个计划任务,每天早上9点04分自动跑一次,符合条件的就都可以收到服务通知了。

发表评论