thinkphp接入EasyWechat 2.0版本
接入EasyWechat 2.0版本 文档 PHP >= 5.3.0
<?php
namespace app\wap\controller;
require ROOT_PATH . "/wechat-2.0/autoload.php"; // 路径请修改为你具体的实际路径
use Overtrue\Wechat\Auth;
use Overtrue\Wechat\Notice;
use Overtrue\Wechat\Server;
use think\Db;
use think\Controller;
# https://github.com/overtrue/wechat/tree/2.0
# https://github.com/overtrue/wechat/wiki
# 判断用户登陆,登陆之后没有openid就获取,然后保存到数据库
# 支付成功的时候返回模板消息
# [email protected] hongxi421
class Easywechat extends Controller
{
public $appId;
public $token;
public $secret;
public $aeskey;
public $user;
public function test()
{
$data = array(
'pay_status' => 1,
'pay_type' => $pay_type,
'pay_time' => time(),
'trade_no' => "2018100910320001"
);
$easywechat = new \app\wap\controller\Easywechat();
$easywechat->send_temp($data['pay_time'], $data['trade_no']);
}
public function _initialize()
{
$this->appId = "wx16adc4c0f50bd5f4";
$this->token = "9bb0a63d47137e95";
$this->secret = "9bb0a63d47137e95261757a0c785ad3a";
$this->aeskey = "B4RdJVEGkGA6zZcCSHyqzpXJ2GqjGD2yBnKjNpYKOTg";
}
/**
* 用户授权登陆
* @return [type] [description]
*/
public function index()
{
$appId = $this->appId;
$secret = $this->secret;
$auth = new Auth($appId, $secret);
$user = $auth->authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE');
session('wechat_user_info', $user->all());
dump($user->all());
// $this->redirect('/wap/');
}
/**
* 获取openid
* @return [type] [description]
*/
public function get_openid()
{
$appId = $this->appId;
$secret = $this->secret;
$auth = new Auth($appId, $secret);
$user = $auth->authorize($to = null, $scope = 'snsapi_userinfo', $state = 'STATE');
return $user['openid'];
}
/**
* 微信接入地址,验证echostr
* http://xxr.fenxiao421.com/index.php?s=/wap/Easywechat/response_echostr
* @return [type] [description]
*/
public function response_echostr()
{
$appId = $this->appId;
$token = $this->token;
$server = new Server($appId, $token);
die($server->serve());
}
/**
* 发送模板(仅仅通知管理员)
* 修改/data/service/UnifyPay.php
* http://xxr.fenxiao421.com/index.php?s=/wap/Easywechat/send_temp
* @return [type] [description]
*/
public function send_temp($pay_time = '时间', $ordersn = '')
{
$appId = $this->appId;
$secret = $this->secret;
$notice = new Notice($appId, $secret);
$openid = "ouVIy0rfhlAeyleGaeDvzGuc"; // 我ouVIyMmqbb6coLK-M0eWle1o // ouVIy0rfhlAeyleGaeDvz7JsPGuc
if (empty($openid)) {
# code...
} else {
$order_id = Db::table('ns_order')->where('order_no', $ordersn)->value('order_id');
$goodsname = Db::table('ns_order_goods')->where('order_id', $order_id)->find();
$templateId = 'NnooQG9YP1EB6w51LwA_YSPRbe9SU5k4VGdQb6fU';
$url = 'http://xxr.fenxi1.com/index.php?s=/admin/order/orderdetail&order_id=' . $order_id;
$color = '#FF0000';
$data = array(
"first" => "订单生成通知!",
"keyword1" => "$pay_time",
"keyword2" => "{$goodsname['goods_name']}等",
"keyword3" => "$ordersn",
);
$notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($openid)->send();
}
}
}