文件
mashangyoujiu/Application/WxApi/Controller/WxController.class.php

436 行
16 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
// +----------------------------------------------------------------------
// | 第三方平台微信推送 消息
// +----------------------------------------------------------------------
//[1.1.69]
namespace WxApi\Controller;
use Think\Controller;
class WxController extends Controller{
public static $_set; //缓存全局配置
public static $appid; //缓存全局配置
public static $_wx; //缓存微信对象
public static $_token;
public static $_location; //用户地理信息
public static $_merchantid; //用户地理信息
//信息接收相关
public static $_revtype; //微信发来的信息类型
public static $_revdata; //微信发来的信息内容
//信息推送相关
public static $_url;
public static $_wecha_id;
public static $_actopen;
public static $WAP;//CMS全局静态变量
// 自动计算模型
public static $_demployee;
public function __construct($options)
{
$appid = I('get.Appid');
file_put_contents("./appid.txt",$appid);
if(!empty($appid)){
$map['authorizer_appid'] = $appid;
}else{
$this->error('参数出错,请检查');
}
if(I('get.merchantid')==1){
exit;
}
self::$_url = $_SERVER['SERVER_NAME'];
self::$_set = M('wxconfig')->where($map)->find();
$auther = M('appweixin')->find();
//缓存微信API模型类
$options['token'] = $auther['token'];
$options['appid'] = $auther['appid'];
$options['encodingaeskey'] = $auther['encodingaeskey'];
$re = self::$_wx = new \Util\Wx\Wechat($options);
//判断验证模式
if (IS_GET) {
self::$_wx->valid();
} else {
$is = self::$_wx->valid(true);
if (!$is) {
die('no access!!!');
}
//读取微信平台推送来的信息类型存全局
self::$_revtype = self::$_wx->getRev()->getRevType();
//读取微型平台推送来的信息存全局
self::$_revdata = self::$_wx->getRevData();
self::$_wecha_id = self::$_wx->getRevFrom();
//读取用户地理信息
//self::$_location=self::$_wx->getRevData();
$str = "";
foreach (self::$_revdata as $k => $v) {
$str = $str . $k . "=>" . $v . ' ';
}
}
}
/*
* appid 授权入口
*/
public function auth(){
//这里微信要做处理
$this->go();
}
public function go()
{
switch (self::$_revtype) {
case \Util\Wx\Wechat::MSGTYPE_TEXT:
$this->checkKeyword(self::$_revdata['Content']);
//self::$_wx->text(self::$_revdata['Content'])->reply();
break;
case \Util\Wx\Wechat::MSGTYPE_EVENT:
$this->checkEvent(self::$_revdata['Event']);
break;
case \Util\Wx\Wechat::MSGTYPE_IMAGE:
//$this -> checkImg();
self::$_wx->text('本系统暂不支持图片信息!')->reply();
break;
default:
self::$_wx->text("本系统暂时无法识别您的指令!")->reply();
}
}
public function toApp($type)
{
$title = "App管理员模式:\n命令:" . $type . "\n结果:\n";
switch ($type) {
case 'dkf':
$str = "人工客服接入!";
self::$_wx->dkf($str)->reply();
break;
case 'openid':
self::$_wx->text($title . self::$_revdata['FromUserName'])->reply();
break;
default:
self::$_wx->text("App:未知命令")->reply();
}
}
//微信号的关键字设置
public function checkKeyword($key)
{
// self::$_wx->text("未知的关键字信息,请联系客服")->reply();
/*if (substr($key, 0, 5) == 'App-') {
$this->toApp(substr($key, 5));
}
$mapkey['keyword'] = $key;
$mapkey['storeid'] = self::$_storeid;
//用户自定义关键词
$keyword = M('wxkeyword');
$ruser = $keyword->where($mapkey)->find();
if ($ruser) {
//进入用户自定义关键词回复
$this->toKeyUser($ruser);
}else{
//自定义推送消息
$this->checkedKey($key);
}*/
// $this->toKeyUnknow();
}
public function checkedKey($key){
$this->toKeyUnknow($key);
}
/*自定义关键词模式 by App
$ruser=关键词记录
*/
public function getPic($id)
{
$m = M('Upload_img');
$map['id'] = $id;
$list = $m->where($map)->find();
$list['imgurl'] = self::$_url . "/Upload/" . $list['savepath'] . $list['savename'];
return $list ? $list : false;
}
public function toKeyUser($ruser)
{
$type = $ruser['type'];
switch ($type) {
//文本
case "1":
self::$_wx->text($ruser['summary'])->reply();
break;
//单图文
case "2":
$news[0]['Title'] = $ruser['name'];
$news[0]['Description'] = $ruser['summary'];
$news[0]['PicUrl'] = "http://".self::$_url .$ruser['pic'];
$news[0]['Url'] = $ruser['url'];
self::$_wx->news($news)->reply();
break;
//多图文
case "3":
$pagelist = M('Wx_keyword_img')->where(array('kid' => $ruser['id']))->order('sorts desc')->select();
$news = array();
foreach ($pagelist as $k => $v) {
$news[$k]['Title'] = $v['name'];
$news[$k]['Description'] = $v['summary'];
$img = $this->getPic($v['pic']);
$news[$k]['PicUrl'] = $img['imgurl'];
$news[$k]['Url'] = $v['url'];
}
self::$_wx->news($news)->reply();
break;
default:
self::$_wx->text("未知类型的关键词,请联系客服!")->reply();
break;
}
}
//未知关键字匹配
public function toKeyUnknow()
{
// self::$_wx->text("未知的关键字信息,请联系客服")->reply();
}
public function add_vipLog($type){
$data = self::$_revdata;
$item['storeid'] = self::$_storeid;
$item['openid'] = $data['FromUserName'];
$item['addtime'] = time();
$item['type'] = $type;
D('Sublog')->addLog($item);
}
/*
*这里是 服务器配置接受微信推送事件
*/
public function checkEvent($event)
{
$wxconfig = self::$_set;
$data = self::$_revdata;
$datas = json_encode($data);
switch ($event) {
case 'subscribe':
//获取一个token
break;
case 'unsubscribe':
//关注取消事件
$this->add_vipLog(2);
case 'CLICK':
$key = self::$_revdata['EventKey'];
switch ($key) {
case '#sy':
break;
}
$this->checkKeyword($key);
break;
case 'user_get_card':
$this->setCard();
A('Card')->updateCard($data);
//根据会员卡id 获取 用户的门店信息
$cardid = $data['CardId'];
$getstore['card_id'] = $cardid;
//获取门店
$store = M('wx_membercard')->where($getstore)->getField('storeid');
//获取门店id 通过id 获取门店的微信配置
$map['storeid'] = $store;
//推送消息 推送一条用户领取会员卡的消息
$openid = $data['FromUserName'];
//$this->addcard($store,$openid);
//激活的话修改vip中的字段 同时会员卡的数量减一
$vip['openid'] = $openid;
//$vip['card_id'] = $cardid;
$vip['primaryid'] = $store;
//$vip['openid'] = "owUCP1KMgjtVXBgJbgVie8o_ff-c";
//会员入驻
$this->sendCard();
//获取会员信息
$mobile_phone = M('retail_vip')->where($vip)->getField('mobile_phone');
M('retail_vip')->where($vip)->save(array('card_id'=>$cardid,'iswxcode'=>1,'cardnum'=>$mobile_phone,'cardnum_old'=>$data['UserCardCode']));
break;
case 'user_pay_from_pay_cell':
break;
case 'user_view_card':
$cardid = $data['CardId'];
$getstore['card_id'] = $cardid;
$merchantid = M('wx_membercard')->where($getstore)->getField('merchantid');
//获取门店id 通过id 获取门店的微信配置
$map['merchantid'] = $merchantid;
//推送消息 推送一条用户领取会员卡的消息
$openid = $data['FromUserName'];
$vip['openid'] = $openid;
//$vip['card_id'] = $cardid;
$vip['merchantid'] = $merchantid;
$mobile_phone = M('retail_vip')->where($vip)->getField('mobile_phone');
M('retail_vip')->where($vip)->save(array('card_id'=>$cardid,'iswxcode'=>1,'cardnum'=>$mobile_phone,'cardnum_old'=>$data['UserCardCode']));
$this->sendCard();
break;
}
}
/*
* 实现会员卡领卡的时候 库存减一
*/
public function setCard(){
$data = self::$_revdata;
file_put_contents('./vipcard.txt',json_encode($data));
$set['card_id'] = $data['CardId'];
// 判断用户的信息
$card = M('wx_membercard')->where($set)->find();
if(!empty($card)&&$card['quantity']>0){
M('wx_membercard')->where($set)->setDec('quantity');
M('wx_membercard')->where($set)->setInc('closure');
//确定可以使用的时候的 发送请求
}
}
/*
* 推送服务器 获取token的 方法
*/
public function getToKen()
{
$tokens = get_authorizer_access_token(self::$_set["merchantid"]);
return $tokens;
}
/*
* 获取post 参数 ,发送会员变动记录 积分和余额的同步
*/
public function sendCard(){
//获取token值
$data = self::$_revdata;
$token = $this->getToKen();
$vip['openid'] = $data['FromUserName'];
$userinfo = M('retail_vip')->where($vip)->find();
$seturl = "https://api.weixin.qq.com/card/membercard/activate?access_token=".$token;
$item['membership_number'] = $userinfo['mobile_phone'];
$item['code'] = $data['UserCardCode'];
$item['card_id'] = $data['CardId'];
$item['init_bonus'] = intval($userinfo['integral']+$userinfo['cash_integral']);
$text = json_encode($item);
$re = $this->curl_post($seturl, $text);
file_put_contents("./wx_card.txt",json_encode($re)."1212");
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
private function http_post($url, $param, $post_file = false)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
function curl_post($url, array $params = array(), $timeout)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return ($data);
}
/*
* 实现会员领卡的时候
* @parem $storeid 门店的id
* @parem $openid 用户的唯一标识
*/
public function addcard($storeid,$openid){
//获取用户信息
$userinfo = $this->getUserInfo();
//获取模板信息
$w['storeid'] = $storeid;
$template = M('mark')->where($w)->find();
//$w['storeid'] = $storeid;
$store['id'] = $storeid;
$storename = M('store')->where($store)->getField('storetag');
$templateid = $template['vip_mark'];
if ($templateid && $openid && $storeid) {
//从这里发送微信模板消息 通知微信
$vipname = $userinfo['name']?$userinfo['name']:'用户';
$cardnum = self::$_revdata['UserCardCode'];
$str = '亲爱的'.$vipname.',您的微信会员卡激活成功,请从微信卡包中查看';
$data['touser'] = $openid;
$data['template_id'] = $templateid;
$data['topcolor'] = "#00FF00";
$data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/' . '/Vip/Index/index?storeid='.$storeid;
$data['data'] = array(
'first'=>array('value' => $str),
'keyword1' => array('value' => '微信会员卡'),
'keyword2' => array('value' => $cardnum),
'remark' => array('value' => $storename.':感谢您的使用。')
);
$options['appid'] = self::$_set['appid'];
$options['appsecret'] = self::$_set['appsercet'];
$wx = new \Util\Wx\Wechat($options);
$re = $wx->sendTemplateMessage($data);
$msg = json_encode($data);
}
}
/*
* 通过openid 获取用户信息
*/
public function getUserInfo(){
//先根据用户openid 查找用户信息
$openid = self::$_revdata['FromUserName'];
if(!empty($openid)){
$map['openid'] = $openid;
$cache = M('retail_vip')->where($map)->find();
if(!empty($cache)){
return $cache;
}else{
$user = $this->apiClient(self::$_revdata['FromUserName']);
return $user;
}
}else{
exit;
}
}
public function apiClient($openid)
{
$user = self::$_wx->getUserInfo($openid);
return $user ? $user : FALSE;
}
}