<?php
/**
* Created by PhpStorm.
* User: 老辉辉
* Date: 2018/1/15
* Time: 15:13
*/
namespace app\common\sendmessage;
use app\common\model\User;
use app\common\model\UserFormId;
use JiaweiXS\WeApp\WeApp;
use think\Exception;
class NewTopRemind
{
public function send($title = ''){
//创建一个小程序对象
$weapp = new WeApp(APPID, APPSECRET, RUNTIME_PATH);
//从‘小程序’获取一个‘模板消息’单例对象
$templateMsg = $weapp->getTemplateMsg();
$userList = User::all();
foreach ($userList as $item) {
$obj = new UserFormId();
$userFormId = $obj->where('user_id','eq',$item['id'])->order('create_time desc')->limit(1)->find();
$formId = $userFormId['form_id'];
$userFormId->delete();
$data = array();
$data['keyword1'] = ['value'=>$title,'color'=>'#173177'];
$data['keyword2'] = ['value'=>'红人榜中榜发起了新话题,快来参与投票,发表你的看法吧!\n进入小程序看看,点击进入投票页面','color'=>'#173177'];
try{
//发送模板消息
$res_array = $templateMsg->send($item['openid'],NEW_TOP_TEMPLATE_ID,$formId,$data);
} catch(Exception $e){
throw new Exception('send error');
}
}
}
}