where($map)->find(); self::$_url = $_SERVER['SERVER_NAME']; self::$_token = self::$_set['token']; self::$_merchantid = $merchantid; $tk = $_GET['token']; /*if ($tk != self::$_token) { die('token error'); }*/ //缓存微信API模型类 $options['token'] = self::$_token; $options['appid'] = self::$_set['appid']; $options['appsecret'] = self::$_set['appsercet']; self::$_wx = new \Util\Wx\Wechat($options); //判断验证模式 if (IS_GET) { self::$_wx->valid(); } else { if (!self::$_wx->valid(true)) { 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) { 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': //优惠券的推送 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'])); $this->setCard(); 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; $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() { $appid = self::$_set['appid']; $appsercet = self::$_set['appsecret']; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsercet; //获取token $json_token = $this->curl_post('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsercet); $escape_token = json_decode($json_token);//转义 $token = $escape_token->{'access_token'};//2个小时换一次,一天2000次 return $token;//返回token值 } /* * 获取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']); $text = json_encode($item); $re = $this->curl_post($seturl, $text); } /** * 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; } }