<?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;
}
}