//////////////////////////////// 主入口 ////////////////////////////////
/**
* 站点统一入口
*/
public function actionIndex()
{
//验证token
$access_token = $this->checkToken();
///////验证token结束/////////////////////////////////////////////////////
//var_dump($access_token);
//上传logo
//echo "</hr>"."上传logo"."<br/>";
$storeLogo = $this->upLogo($access_token);
//var_dump($storeLogo);
/////////上传logo结束/////////////////////////////////////////////
//上传门店列表
//echo "<br/>"."门店信息jason"."<br/>";
$jsonStore = $this->upStoreList($access_token);
$storeList = $jsonStore["location_id_list"];//门店列表
//var_dump($storeList);
//获取到门店id,拼装成字符串
$storeSub = implode(‘,‘, $storeList);
//var_dump($storeSub);
//////上传门店列表结束//////////////////////////////////////////////////////////////
//拉取门店信息
//echo "<br/>"."拉取门店"."<br/>";
$getStoreList = $this->getStoreList($access_token);
//var_dump($getStoreList);
//var_dump($jsonGetList);
/////////拉取门店信息结束////////////////////////////////////////////////////////////////////////
//获取颜色列表
// echo "<br/>"."获取颜色列表"."<br/>";
$colorList = $this->getColorList($access_token);
var_dump($colorList);
//var_dump($jsonColor);
/////////获取颜色列表结束////////////////////////////////////////////////////////////////////////
//创建卡券信息
//echo "<br/>"."创建卡券信息"."<br/>";
$cardMeg = $this->createCardMeg($access_token, $storeLogo,$storeSub);
//var_dump($jsonCard);
$cardCode = $cardMeg["card_id"];//拿到cardCode
//echo $cardCode;
///////创建卡券信息结束//////////////////////////////////////////////////////////////////
////创建二维码ticket
//echo "<br/>"."创建二维码ticket"."<br/>";
$QRMeg = $this->getQRTicket($access_token, $cardCode);
//var_dump($QRMeg);
$ticket = $QRMeg["ticket"];
var_dump($ticket);
////创建二维码ticket结束///////////////////////////////////////////////
//创建二维码接口开始///////////////////////////////////////////////
//echo "<br/>"."创建卡卷二维码"."<br/>";
$urlTicket = urlencode($ticket);
//二维码URL
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$urlTicket;
//$img = $this->curlGet($url);
//header( "Content-Type: image/jpeg" );
//输出二维码
//echo "<img src=‘$url‘/>";
////创建二维码接口结束/////////////////////////////////////////////////////////
//api_ticket缓存与更新开始/////////////////////////////////////////////////
//调用次数有限 使用yii缓存机制
$apiTicket = $this->getApiTicket($access_token);
//api_ticket缓存与更新结束/////////////////////////////////////////////////
//////添加到卡包(bathAddCard)JSAPI开始////文档25页////////////////////////////////////////////////
//////添加到卡包(bathAddCard)JSAPI结束////////////////////////////////////////////////////
//////卡券核销开始////////////////////////////////////////////////////
$cardConsume = $this->cardConsume($access_token,$code,$cardId);
////卡券核销结束////////////////////////////////////////////////////
//////拉起卡券列表(chooseCard)JSAPI开始///文档30页///////////////////////////////////////////////////////
///////////拉起卡券列表(chooseCard)JSAPI结束/////////////////////////////////////////////////////
//////////code 解码接口 开始//34////////////////////////////////////////////////////
$cardDecode = $this->cardDecode($access_token,$encrypt_code);
////////////code 解码接口end////////////////////////////////////////////////////
//////删除卡券开始//////////////////////////////////////////////////////////
$cardDel = $this->ardDelete($access_token,$cardId);
///////删除卡券结束/////////////////////////////////////////////////////////
/////////查询code开始///36////////////////////////////////////////////////////
$codeQuery = $this->codeQuery($access_token,$code,$cardId);
/////////查询code结束///////////////////////////////////////////////////////
/////////批量查询卡列表开始///////////////////////////////////////////////////////
$cardBatchQuery = $this->cardBatchQuery($access_token);
/////////批量查询卡列表结束///////////////////////////////////////////////////
/////////查询卡券详情开始///////////////////////////////////////////////////////
$cardDetialQuery = $this->cardDetialQuery($access_token,$cardId);
/////////查询卡券详情结束///////////////////////////////////////////////////
/////////更改code开始///47////////////////////////////////////////////////////
$codeChange = $this->codeChange($access_token);
/////////更改code结束///////////////////////////////////////////////////
/////////设置卡券失效接口开始//48///不可逆//////////////////////////////////////////////////
$cardFail = $this->cardFail($access_token);
/////////设置卡券失效接口结束///////////////////////////////////////////////////
/////////更改卡券信息接口开始///////////////////////////////////////////////////////
$cardUpdate = $this->cardUpdate($access_token);
/////////更改卡券信息接口结束///////////////////////////////////////////////////
/////////批量查询卡列表开始///////////////////////////////////////////////////////
$cardUpdate = $this->cardStockUp($access_token);
/////////批量查询卡列表结束///////////////////////////////////////////////////
/////////激活/绑定会员卡开始///////////////////////////////////////////////////////
$vipActBind = $this->vipActBind($access_token);
/////////激活/绑定会员卡结束////////////////////////////////////////////////
/////////会员卡交易开始///////////////////////////////////////////////////////
$vipCardExchange = $this->vipCardExchange($access_token);
/////////会员卡交易结束////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
$data = array();
$this->render(‘card‘, $data);
}
////封装函数开始/////////////////////////////////////////////////////////
/**
* 会员卡交易
*/
public function vipCardExchange($access_token){
$url = "https://api.weixin.qq.com/card/membercard/updateuser?access_token=$access_token";
$data = ‘{
"code": "12312313",
"card_id":"p1Pj9jr90_SQRaVqYI239Ka1erkI",
"record_bonus": "消费30元,获得3积分",
"add_bonus": 3,
"add_balance": -3000
"record_balance": "购买焦糖玛琪朵一杯,扣除金额30元。"
}‘;
$res = $this->curlPost($url, $data);
$vipCardExchange = json_decode($res, true);
return $vipCardExchange;
}
/**
* 激活/绑定会员卡
*/
public function vipActBind($access_token){
$url = "https://api.weixin.qq.com/card/membercard/activate?access_token=$access_token";
$data = ‘{
"init_bonus": 100,
"init_balance": 200,
"membership_number": "AAA00000001",
"code": "12312313",
"card_id": "xxxx_card_id"
}‘;
$res = $this->curlPost($url, $data);
$vipActBind = json_decode($res, true);
return $vipActBind;
}
/**
* 库存修改接口
*/
public function cardStockUp($access_token){
$url = "https://api.weixin.qq.com/card/modifystock?access_token=$access_token";
$data = ‘{
"card_id": "xxxx_card_id",
"increase_stock_value": 1231231,
"reduce_stock_value": 1231231
}‘;
$res = $this->curlPost($url, $data);
$cardStockUp = json_decode($res, true);
return $cardStockUp;
}
/**
* 更改卡券信息接口
*/
public function cardUpdate($access_token){
$url = "https://api.weixin.qq.com/card/update?access_token=$access_token";
$data = ‘{
"card_id": "xxxxxxxxxxxxx",
"member_card": {
"base_info": {
"logo_url":"http:\/\/www.supadmin.cn\/uploads\/allimg\/120216\/1_120216214725_1.jpg",
"color": "Color010",
"notice": "使用时向服务员出示此券",
"service_phone": "020-88888888",
"description": "不可与其他优惠同享\n 如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食\n 餐前不可打包,餐后未吃完,可打包\n 本团购券不限人数,建议2 人使用,超过建议人数须另收酱料费5 元/位\n 本单谢绝自带酒水饮料"
"location_id_list" : [123, 12321, 345345]
},
"bonus_cleared": "aaaaaaaaaaaaaa",
"bonus_rules": "aaaaaaaaaaaaaa",
"prerogative": ""
}
}‘;
$res = $this->curlPost($url, $data);
$cardUpdate = json_decode($res, true);
return $cardUpdate;
}
/**
* 设置卡券失效接口
*/
public function cardFail($access_token){
$url = "https://api.weixin.qq.com/card/code/unavailable?access_token=$access_token";
$data = ‘{
"code": "12312313",
"card_id": "xxxx_card_id"
}‘;
$res = $this->curlPost($url, $data);
$cardFail = json_decode($res, true);
return $cardFail;
}
/**
* 更改code
*/
public function codeChange($access_token){
$url = "https://api.weixin.qq.com/card/code/update?access_token=$access_token";
$data = ‘{
"code": "12345678",
"card_id": "p1Pj9jr90_SQRaxxxxxxxx",
"new_code": "3495739475"
}‘;
$res = $this->curlPost($url, $data);
$codeChange = json_decode($res, true);
return $codeChange;
}
/**
* 查询卡券详情
*/
public function cardDetialQuery($access_token,$cardId){
$url = "https://api.weixin.qq.com/card/get?access_token=$access_token";
$data = ‘{
"card_id":"pFS7Fjg8kV1IdDz01r4SQwMkuCKc"
}‘;
$res = $this->curlPost($url, $data);
$cardDetialQuery = json_decode($res, true);
return $cardDetialQuery;
}
/**
* 批量查询卡列表
*/
public function cardBatchQuery($access_token){
$url = "https://api.weixin.qq.com/card/batchget?access_token=$access_token";
$data = ‘{
"offset": 0, //偏移量,从0 开始,即offset: 5 是指从列表里的第六个开始读取。
"count": 10 //需要查询的卡片的数量(数量最大50)
}‘;
$res = $this->curlPost($url, $data);
$cardBatchQuery = json_decode($res, true);
return $cardBatchQuery;
}
/**
* 查询code
*/
public function codeQuery($access_token,$code,$cardId){
$url = "https://api.weixin.qq.com/card/code/get?access_token=$access_token";
$data = ‘{ "code" : "110201201245", "card_id" : ""}‘;
$res = $this->curlPost($url, $data);
$codeQuery = json_decode($res, true);
return $codeQuery;
}
/**
* 删除卡券
*/
public function cardDelete($access_token,$cardId){
$url = "https://api.weixin.qq.com/card/delete?access_token=$access_token";
$data = ‘{ "card_id" : "p1Pj9jr90_SQRaVqYI239Ka1erkI" }‘;
$res = $this->curlPost($url, $data);
$cardDelete = json_decode($res, true);
return $cardDelete;
}
/**
* code解码
*/
public function cardDecode($access_token,$encrypt_code){
$url = "https://api.weixin.qq.com/card/code/decrypt?access_token=$access_token";
$data = ‘{"encrypt_code" : "XXIzTtMqCxwOaawoE91+VJdsFmv7b8g0VZIZkqf4GWA60Fzpc8ksZ/5ZZ0DVkXdE"}‘;
$res = $this->curlPost($url, $data);
$cardDecode = json_decode($res, true);
return $cardDecode;
}
/**
* 卡券核销 post
*/
public function cardConsume($access_token,$code,$cardId){
$url = "https://api.weixin.qq.com/card/code/consume?access_token=$access_token";
$data = ‘{"cade" : "$code", "card_id" : "$cardId"}‘;
$res = $this->curlPost($url, $data);
$cardConsume = json_decode($res, true);
return $cardConsume;
}
/**
* 从缓存中get apiTicket
*/
public function getApiTicket($access_token){
$key = ‘apiTicket‘;
$cache = Yii::app()->cache;
$apiTicket = Yii::app()->cache->get($key);
if(!$apiTicket){
$apiTicket = $this->getNewApiTicket($access_token);
$cache->set($key,$apiTicket,$expire=2);
echo $apiTicket;die;
}
return $apiTicket;
}
/**
* 得到新的api_ticket
*/
public function getNewApiTicket($access_token){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=wx_card";
$resJson = $this->curlGet($url);
$res = json_decode($resJson, true);
return $res[‘ticket‘];
}
/**
* 微信token验证
* @return string $access_token 微信token
*/
public function checkToken(){
//失效
//$appid = "";
//$appsecret = "";
//微商公众平台获取
$appid = ‘‘;
$appsecret =‘‘;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
$res = $this->curlGet($url);
$jsonToken = json_decode($res, true);
$access_token = $jsonToken["access_token"];
return $access_token;
}
/**
* 上传logo
* @param string $access_token 微信token验证码
* @return string $storeLogo
*/
public function upLogo($access_token){
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=$access_token";
$data = array(
‘buffer‘ => ‘@/home/sqliuxinliang/app.shiqutech.com/static/zldt/images/card.jpg‘,
);
$res = $this->curlPost($url, $data);
$storeLogo = json_decode($res, true);
return $storeLogo;
}
/**
* 上传门店列表
* @param unknown $access_token
*/
public function upStoreList($access_token){
$data = ‘{"location_list":[
{
"business_name":"麦当劳",
"branch_name":"赤岗店",
"province":"广东省",
"city":"广州市",
"district":"海珠区",
"address":"中国广东省广州市海珠区艺苑路11 号",
"telephone":"020-89772059",
"category":"房产小区",
"longitude":"115.32375",
"latitude":"25.097486"
},
{
"business_name":"麦当劳",
"branch_name":"珠江店",
"province":"广东省",
"city":"广州市",
"district":"海珠区",
"address":"中国广东省广州市海珠区艺苑路12 号",
"telephone":"020-89772059",
"category":"房产小区",
"longitude":"113.32375",
"latitude":"23.097486"
}
]}‘;
$url = "https://api.weixin.qq.com/card/location/batchadd?access_token=$access_token";
$res = $this->curlPost($url, $data);
$jsonStore = json_decode($res, true);
return $jsonStore;
}
/**
* 获取门店
*/
public function getStoreList($access_token){
$data = ‘{"offset": 0, "count": 1}‘;
$url = "https://api.weixin.qq.com/card/location/batchget?access_token=$access_token";
$res = $this->curlPost($url, $data);
$storeList = json_decode($res, true);
return $storeList;
}
/**
* 获取颜色列表
*/
public function getColorList($access_token){
$url = "https://api.weixin.qq.com/card/getcolors?access_token=$access_token";
$data = ‘{"access_token" : "" }‘;
$res = $this->curlPost($url, $data);
$colorList = json_decode($res, true);
return $colorList;
}
/**
* 创建卡券信息
*/
public function createCardMeg($access_token, $storeLogo,$storeSub){
$url = "https://api.weixin.qq.com/card/create?access_token=$access_token";
$data = ‘{ "card": {
"card_type": "GROUPON",
"groupon": {
"base_info":
{
"logo_url":"‘.$storeLogo.‘",
"brand_name":"test",
"code_type":"CODE_TYPE_TEXT",
"title": "test",
"sub_title": "",
"color": "Color010",
"notice": "test",
"service_phone": "020-88888888",
"description": "test",
"date_info": {
"type": 1,
"begin_timestamp": 1397577600 ,
"end_timestamp": 1522724261
},
"sku": {
"quantity": 50000000
},
"get_limit": 3,
"use_custom_code": false,
"bind_openid": false,
"can_share": true,
"can_give_friend": true,
"location_id_list" : [‘.$storeSub.‘],
"url_name_type": "URL_NAME_TYPE_RESERVATION",
"custom_url": "http://www.qq.com",
"source": "test"
},
"deal_detail": "testtesttesttesttesttesttesttesttest"
}
}
}‘;
$res = $this->curlPost($url, $data);
$cardMeg = json_decode($res, true);
return $cardMeg;
}
/**
* 创建获取二维码ticket
*/
public function getQRTicket($access_token, $cardCode){
$data = ‘{
"action_name": "QR_CARD",
"action_info": {
"card": {
"card_id": "‘.$cardCode.‘",
"code": "",
"openid": "",
"expire_seconds": "",
"is_unique_code": false,
"outer_id" : 0
}
}
}‘;
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=$access_token";
$res = $this->curlPost($url, $data);
$QRMeg = json_decode($res, true);
return $QRMeg;
}
//curl_get
public function curlGet($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($ch);
if($res == false){
error_log(curl_error($ch));
Yii::app()->end();
}
curl_close($ch);
return $res;
}
//curl_post
public function curlPost($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
if($res == false){
var_dump(curl_error($ch));
Yii::app()->end();
}
return $res;
}
////封装函数结束/////////////////////////////////////////////////////////
//////////////////////////////// wapIndex 测试卡券结束 ////////////////////////////////
public function actionWapIndex(){
$userId = Yii::app()->user->id;//session授权user
//////////原中粮答题头部开始///////////////////////////////////////////////////
$this->layout = ‘//layouts/wap-card‘;
$title = $storeUrl[‘title‘];
$this->pageTitle= $title; //设置网页标题
$url = $storeUrl[‘url‘];
$caseList = $signPakage =array();
//拿到case列表
$caseList = CaseList::model()->getCaseList();
// 移动端/PC端页面分离
//if ( JYii::isMobileClient() ) { Yii::app()->getRequest()->redirect(Yii::app()->createUrl(‘home/wapIndex‘, array (‘source‘ => ‘local‘))); }
//实例微信接口认证类
$weixin = new WeixinJSSDK("wx7838ede94514cbc7", "578b2e9dc7edf625ac8eeeabab6a0241");
$signPackage = $weixin->GetSignPackage();
//拼接数组,抛到页面
$data = array(
‘caseList‘ => $caseList,
‘signPackage‘ => $signPackage,
‘url‘ => $url,
);
//////原中粮答题结束/////////////////////////////////////////////////////////
$appid = "";
$appsecret = "";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
//拿到卡券商店信息
//$card = CaseList::model()->getCardList();
//$this->render(‘card‘, $card);
}
//////////////////////////////// AJAX ////////////////////////////////
原文:http://www.cnblogs.com/building/p/4445171.html