where(array('merchantid'=>$userinfo['merchantid']))->find(); if($wxconfig['type']==1){ $appid=$wxconfig['appid']; $appserect=$wxconfig['appsecret']; //获取token $json_token = http_request('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appserect); $escape_token = json_decode($json_token);//转义 $token['token'] = $escape_token->{'access_token'};//2个小时换一次,一天2000次 // var_dump($escape_token); return $token['token'];//返回token值 }else if($wxconfig['type']==2){ $tokens = get_authorizer_access_token($wxconfig['merchantid']); return $tokens; } } //微信同步代金券(已测试) public function wx_kabao($data) { $token = $this->getToKen();//获取token //需要的参数代金券 $card = array( 'card' => array( 'card_type' => 'CASH', //卡券类型 'cash' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' => array( 'type' => 'DATE_TYPE_FIX_TERM', //时间类型 // 'type' =>$data['date_info_type'], //时间类型 'fixed_term' => $data['fixed_term'],//有效期限 'fixed_begin_term' => $data['fixed_begin_term'],//从什么时候开始 ), 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 'center_title' => $data['center_title'],//中间展示按钮 'center_sub_title' => $data['center_sub_title'], 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "least_cost" => $data['least_cost'] * 100, "reduce_cost" => $data['reduce_cost'] * 100 ) ) ); //参数 token array->card //创建卡券 //返回卡券id $card_id = http_request('https://api.weixin.qq.com/card/create?access_token=' . $token, json_encode($card, JSON_UNESCAPED_UNICODE)); $json_card_id = json_decode($card_id); $card = $json_card_id->{'card_id'};//卡券id $save['card_id'] = $card; $gaibian = M('wxcard')->where(array('id' => $data['id']))->save($save); if ($gaibian) { //生成二维码 //参数 卡券id token $erwei = array('action_name' => "QR_CARD", 'action_info' => array( 'card' => array('card_id' => $card, 'is_unique_code' => false) )); $json_wxpic = json_encode($erwei, JSON_UNESCAPED_UNICODE);//转成json形式 $card_wm = http_request('https://api.weixin.qq.com/card/qrcode/create?access_token=' . $token, $json_wxpic); $wx_kabao = json_decode($card_wm); $wx_success = $wx_kabao->{'show_qrcode_url'};//获取二维码 if ($wx_success) { $sav['wx_url'] = $wx_success; $url = M('wxcard')->where(array('id' => $data['id']))->save($sav); if ($url) { return '成功'; } else { return '失败'; } } else { return '失败'; } } else { return '失败'; } } //同步优惠券信息到卡卷表 public function syncCard() { //接收优惠券id $id = $_POST['id']; //通过优惠券id查询出该优惠券的信息,用来导入到微信卡卷表,生成卡卷 $coupon_info = M('coupon')->where(array('id' => $id))->find(); // echo M('coupon')->getLastSql(); //把优惠券字段换成卡卷表所需要字段。该优惠券属于代金卡卷。卡卷类型为:CASH $info['code_type'] = 'CASH'; $info['logo_url'] = 'http://ceshi.mobzhifu.com/Public/Home/images/logo1.png'; $info['title'] = $coupon_info['coupon_name']; //卡卷名称 $info['quantity'] = $coupon_info['coupon_allnum']; //卡卷库存 $info['fixed_term'] = (int)round(($coupon_info['deadtime'] - time()) / 86400); //卡卷有效天数(按天) $info['fixed_begin_term'] = 0; //卡卷领取当天生效 $info['end_time_stamp'] = $coupon_info['deadtime']; //结束时间 $info['begin_time_stamp'] = strtotime(date('Y-m-d 00:00:00', time())); //开始时间 $info['storeid'] = $coupon_info['storeid']; //店铺id $info['least_cost'] = $coupon_info['needmoney']; //满减门槛 满XX元可以使用 $info['reduce_cost'] = $coupon_info['reducemoney']; //满减门槛 满XX元减XX元 // var_dump($data); //先判断该优惠券是否存在 if ($coupon_info) { $card = M('wxcard')->add($info); $oid = M('wxcard')->getLastInsID(); $data = M('wxcard')->where(array('id' => $oid))->find(); if ($data) { $ppp = $this->wx_kabao($data); if ($ppp) { $change = M('coupon')->where(array('id' => $id))->setField('sync', 1); $status = M('wxcard')->where(array('id' => $oid))->setField('status', 2); if ($change) { $rt['status'] = 1; $rt['msg'] = "同步成功"; } else { $rt['status'] = 1; $rt['msg'] = "同步失败"; } $this->ajaxReturn($rt); } } } } //核销 public function Writeoff() { //获取token header("Content-type: text/html; charset=utf-8"); $card_id=$_POST['code']; $code_id=$card_id; $token=$this->getToKen();//获取token $arr_code=array( "code"=>$code_id, "check_consume"=>true ); $json_arr=json_encode($arr_code); $write_judge=http_request('https://api.weixin.qq.com/card/code/get?access_token='.$token,$json_arr); //判断卡券是否核销转增 //参数token json //参数 $write=json_decode($write_judge)->{'errmsg'}; $total=json_decode($write_judge); $tard=$total->{'card'}->{'card_id'};//card_id //核查成功 if($write=='ok') { $new_code=array("code"=>$card_id); $new_json_code=json_encode($new_code,JSON_UNESCAPED_UNICODE); $Writeoff=http_request('https://api.weixin.qq.com/card/code/consume?access_token='.$token,$new_json_code);//核销接口 $writeoff_arr=json_decode($Writeoff); $card_id=$writeoff_arr->{'card'}->{'card_id'}; if($writeoff_arr->{'errmsg'}=='ok') { $da=$this->delete_card_id($card_id); if($da['status']==1) { $card_info = M('wxcard')->where(array('card_id'=>$tard))->find(); $new['title'] = $card_info['title']; $new['description'] = $card_info['description']; $new['code'] = $code_id; $new['ctime'] = time(); $new['card_type'] = $card_info['code_type']; $new['merchantid'] = $card_info['merchantid']; //插入记录表 $log = M('wxcard_log')->add($new); if($log) { $da['status']=1; $da['success']='核销成功'; } }else if($da['status']==0) { $da['status']=0; $da['success']='核销失败'; } } }else { $da['status']=0; $da['success']='卡券无效'; } $this->ajaxReturn($da); } //核销卡券数据库操作 public function delete_card_id($card_id) { $num=M('wxcard')->where(array('card_id'=>$card_id))->setInc('closure'); if($num) { $da['status']=1; $da['success']='成功'; }else { $da['status']=0; $da['success']='失败'; } return $da; } //查询card_id public function select_card($id) { $card=M('wxcard')->where(array('id'=>$id))->find(); if($card) { return $card['card_id']; }else { return false; } } //修改库存 public function save_card_kucun($id,$type,$num) { header("Content-type: text/html; charset=utf-8"); $token=$this->getToKen();//获取token $card_id=$this->select_card($id); if($card_id!=null) { if($type==1)//增加 { $kucun=array( "card_id"=>$card_id, "increase_stock_value"=> $num);//增加多少掌 }else if($type==2)//减少 { $kucun=array( "card_id"=>$card_id, "reduce_stock_value"=> $num);//减少多少掌 } $json_kucun=json_encode($kucun); $success=http_request('https://api.weixin.qq.com/card/modifystock?access_token='.$token,$json_kucun); // print_r($success); // exit; $escape_card= json_decode($success);//转义 $token=$escape_card->{'errmsg'};// if($token=='ok') { return true; }else { return false; } }else { return false; } } //删除卡券接口 public function delete($id) { header("Content-type: text/html; charset=utf-8"); $token=$this->getToKen();//获取token $card_id=$this->select_card($id); if($card_id) { $card_id=array("card_id"=> $card_id); $de_card_id=json_encode($card_id,JSON_UNESCAPED_UNICODE); $success=http_request('https://api.weixin.qq.com/card/delete?access_token='.$token,$de_card_id); $escape_card= json_decode($success);//转义 $token=$escape_card->{'errmsg'};// if($token=='ok') { return '成功'; }else { return '失败'; } }else { return false; } } //创建优惠券 public function create_coupon($data) { //固定时间和非固定时间 if($data['date_info_type']=='DATE_TYPE_FIX_TERM')//固定时间 { $term['type']='DATE_TYPE_FIX_TERM'; $term['fixed_term']=$data['fixed_term']; $term['fixed_begin_term']=$data['fixed_begin_term']; }else//非固定时间 { $term['type']='DATE_TYPE_FIX_TIME_RANGE'; $term['begin_timestamp']=$data['begin_timestamp']; $term['end_timestamp']=$data['end_timestamp']; } //有效时间 if($data['type']==null) { $yy[]['type']='HOLIDAY'; }else { $rum=$data['type']; $rumm=explode(',',$rum); foreach($rumm as $key=>&$val) { $yy[$key]['type']=$val; $yy[$key]['begin_hour']=$data['begin_hour']; $yy[$key]['end_hour']=$data['end_hour']; $yy[$key]['begin_minute']=$data['begin_minute']; $yy[$key]['end_minute']=$data['end_minute']; } } //服务 if($data['business_service']!=null) { $weekk=$data['business_service']; $week=explode(',',$weekk); }else { $week=array(); } $card = array( 'card' => array( 'card_type' => 'GENERAL_COUPON', //卡券类型 'general_coupon' => array( 'base_info' => array( 'logo_url' => 'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" =>$data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 // 'center_title' => $data['center_title'],//中间展示按钮 // 'center_sub_title' => $data['center_sub_title'], // 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, "business_service"=> $week ), "default_detail"=>$data['default_detail'] ) ) ); $success=$this->create_er($card,$data); if($success) { return true; }else { return false; } } //兑换券 public function create_voucher($data) { //固定时间和非固定时间 if($data['date_info_type']=='DATE_TYPE_FIX_TERM')//固定时间 { $term['type']='DATE_TYPE_FIX_TERM'; $term['fixed_term']=$data['fixed_term']; $term['fixed_begin_term']=$data['fixed_begin_term']; }else if($data['date_info_type']=='DATE_TYPE_FIX_TIME_RANGE')//非固定时间 { $term['type']='DATE_TYPE_FIX_TIME_RANGE'; $term['begin_timestamp']=$data['begin_timestamp']; $term['end_timestamp']=$data['end_timestamp']; } //有效时间 if($data['type']==null) { $yy[]['type']='HOLIDAY'; }else { $rum=$data['type']; $rumm=explode(',',$rum); foreach($rumm as $key=>&$val) { $yy[$key]['type']=$val; $yy[$key]['begin_hour']=$data['begin_hour']; $yy[$key]['end_hour']=$data['end_hour']; $yy[$key]['begin_minute']=$data['begin_minute']; $yy[$key]['end_minute']=$data['end_minute']; } } //服务 $weekk=$data['business_service']; $week=explode(',',$weekk); if($weekk==''){ $card = array( 'card' => array( 'card_type' => 'GIFT', //卡券类型 'gift' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 // 'center_title' => $data['center_title'],//中间展示按钮 // 'center_sub_title' => $data['center_sub_title'], // 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, ), "gift"=>$data['gift'] ) ) ); }else{ $card = array( 'card' => array( 'card_type' => 'GIFT', //卡券类型 'gift' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 // 'center_title' => $data['center_title'],//中间展示按钮 // 'center_sub_title' => $data['center_sub_title'], // 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, "business_service"=> $week ), "gift"=>$data['gift'] ) ) ); } $success=$this->create_er($card,$data); if($success) { return true; }else { return false; } } //折扣券 public function create_zcoupon($data) { //固定时间和非固定时间 if($data['date_info_type']=='DATE_TYPE_FIX_TERM')//固定时间 { $term['type']='DATE_TYPE_FIX_TERM'; $term['fixed_term']=$data['fixed_term']; $term['fixed_begin_term']=$data['fixed_begin_term']; }else if($data['date_info_type']=='DATE_TYPE_FIX_TIME_RANGE')//非固定时间 { $term['type']='DATE_TYPE_FIX_TIME_RANGE'; $term['begin_timestamp']=$data['begin_timestamp']; $term['end_timestamp']=$data['end_timestamp']; } //有效时间 if($data['type']==null) { $yy[]['type']='HOLIDAY'; }else { $rum=$data['type']; $rumm=explode(',',$rum); foreach($rumm as $key=>&$val) { $yy[$key]['type']=$val; $yy[$key]['begin_hour']=$data['begin_hour']; $yy[$key]['end_hour']=$data['end_hour']; $yy[$key]['begin_minute']=$data['begin_minute']; $yy[$key]['end_minute']=$data['end_minute']; } } //服务 $weekk=$data['business_service']; $week=explode(',',$weekk); if($weekk==''){ $card = array( 'card' => array( 'card_type' => 'DISCOUNT', //卡券类型 'discount' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 // 'center_title' => $data['center_title'],//中间展示按钮 // 'center_sub_title' => $data['center_sub_title'], // 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy ), "discount"=>$data['discount'] ) ) ); }else{ $card = array( 'card' => array( 'card_type' => 'DISCOUNT', //卡券类型 'discount' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 // 'center_title' => $data['center_title'],//中间展示按钮 // 'center_sub_title' => $data['center_sub_title'], // 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, "business_service"=> $week ), "discount"=>$data['discount'] ) ) ); } $success=$this->create_er($card,$data); if($success) { return true; }else { return false; } } //代金券 public function Establish($data) { //固定时间和非固定时间 if($data['date_info_type']=='DATE_TYPE_FIX_TERM')//固定时间 { $term['type']='DATE_TYPE_FIX_TERM'; $term['fixed_term']=$data['fixed_term']; $term['fixed_begin_term']=$data['fixed_begin_term']; }else if($data['date_info_type']=='DATE_TYPE_FIX_TIME_RANGE')//非固定时间 { $term['type']='DATE_TYPE_FIX_TIME_RANGE'; $term['begin_timestamp']=$data['begin_timestamp']; $term['end_timestamp']=$data['end_timestamp']; } //有效时间 if($data['type']==null) { $yy[]['type']='HOLIDAY'; }else { $rum=$data['type']; $rumm=explode(',',$rum); foreach($rumm as $key=>&$val) { $yy[$key]['type']=$val; $yy[$key]['begin_hour']=$data['begin_hour']; $yy[$key]['end_hour']=$data['end_hour']; $yy[$key]['begin_minute']=$data['begin_minute']; $yy[$key]['end_minute']=$data['end_minute']; } } //服务 $weekk=$data['business_service']; $week=explode(',',$weekk); if($weekk==''){ $card = array( 'card' => array( 'card_type' => 'CASH', //卡券类型 'cash' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 'center_title' => $data['center_title'],//中间展示按钮 'center_sub_title' => $data['center_sub_title'], 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, // "business_service"=> $week ), "least_cost"=> $data['least_cost']*100,//满多少钱 "reduce_cost"=> $data['reduce_cost']*100,//抵扣多少 ) ) ); }else{ $card = array( 'card' => array( 'card_type' => 'CASH', //卡券类型 'cash' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 'center_title' => $data['center_title'],//中间展示按钮 'center_sub_title' => $data['center_sub_title'], 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, "business_service"=> $week ), "least_cost"=> $data['least_cost']*100,//满多少钱 "reduce_cost"=> $data['reduce_cost']*100,//抵扣多少 ) ) ); } $success=$this->create_er($card,$data); if($success) { return true; }else { return false; } } //团购券 public function create_purchase($data) { //固定时间和非固定时间 if($data['date_info_type']=='DATE_TYPE_FIX_TERM')//固定时间 { $term['type']='DATE_TYPE_FIX_TERM'; $term['fixed_term']=$data['fixed_term']; $term['fixed_begin_term']=$data['fixed_begin_term']; }else if($data['date_info_type']=='DATE_TYPE_FIX_TIME_RANGE')//非固定时间 { $term['type']='DATE_TYPE_FIX_TIME_RANGE'; $term['begin_timestamp']=$data['begin_timestamp']; $term['end_timestamp']=$data['end_timestamp']; } //有效时间 if($data['type']==null) { $yy[]['type']='HOLIDAY'; }else { $rum=$data['type']; $rumm=explode(',',$rum); foreach($rumm as $key=>&$val) { $yy[$key]['type']=$val; $yy[$key]['begin_hour']=$data['begin_hour']; $yy[$key]['end_hour']=$data['end_hour']; $yy[$key]['begin_minute']=$data['begin_minute']; $yy[$key]['end_minute']=$data['end_minute']; } } //服务 $weekk=$data['business_service']; $week=explode(',',$weekk); if($weekk==''){ $card = array( 'card' => array( 'card_type' => 'GROUPON', //卡券类型 'groupon' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 'center_title' => $data['center_title'],//中间展示按钮 'center_sub_title' => $data['center_sub_title'], 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, ), "deal_detail"=>$data['deal_detail'] ) ) ); }else{ $card = array( 'card' => array( 'card_type' => 'GROUPON', //卡券类型 'groupon' => array( 'base_info' => array( 'logo_url' =>'http://'.$_SERVER['HTTP_HOST'].$data['logo_url'], 'brand_name' => $data['brand_name'], 'code_type' => 'CODE_TYPE_BARCODE', //生成卡券的类型 'title' => $data['title'],//卡卷名,上限9个字(建议涵盖卡卷属性 服务以及金额) 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'date_info' =>$term, 'sku' => array( 'quantity' => $data['quantity'],//库存 ), "use_limit" => $data['use_limit'], 'get_limit' => $data['get_limit'],//获取数量 'bind_openid' => false, //是不是指定发给某一个用户 'can_share' => $data['can_share'] == 2 ? true : false, //可不可以分享 'can_give_friend' => $data['can_give_friend'] == 2 ? true : false,//赠送朋友 'center_title' => $data['center_title'],//中间展示按钮 'center_sub_title' => $data['center_sub_title'], 'center_url' => $data['center_url'],//跳转链接 'custom_url_name' => $data['custom_url_name'],//跳转按钮名称 'custom_url' => $data['custom_url'], //底部点击跳转入口 'custom_url_sub_title' => $data['custom_url_sub_title'], "use_custom_code" => false ), "advanced_info" => array( "time_limit" =>$yy, "business_service"=> $week ), "deal_detail"=>$data['deal_detail'] ) ) ); } $success=$this->create_er($card,$data); if($success) { return true; }else { return false; } } //生成二维码 public function create_er($card,$data) { $token=$this->getToKen(); // var_dump(json_encode($card));exit; $card_id = http_request('https://api.weixin.qq.com/card/create?access_token=' . $token, json_encode($card, JSON_UNESCAPED_UNICODE)); $json_card_id = json_decode($card_id); $card = $json_card_id->{'card_id'};//卡券id $save['card_id'] = $card; $gaibian = M('wxcard')->where(array('id' => $data['id']))->save($save); if ($gaibian) { //生成二维码 //参数 卡券id token $erwei = array('action_name' => "QR_CARD", 'action_info' => array( 'card' => array('card_id' => $card, 'is_unique_code' => false) )); $json_wxpic = json_encode($erwei, JSON_UNESCAPED_UNICODE);//转成json形式 $card_wm = http_request('https://api.weixin.qq.com/card/qrcode/create?access_token=' . $token, $json_wxpic); $wx_kabao = json_decode($card_wm); $wx_success = $wx_kabao->{'show_qrcode_url'};//获取二维码 if ($wx_success) { //调用保存二维码的方法 $sav['imgurl'] = $this->saveImg($wx_success); $sav['wx_url'] = $wx_success; // var_dump($sav);exit; $url = M('wxcard')->where(array('id' => $data['id']))->save($sav); // echo M('wxcard')->getLastSql();exit; if ($url) { return true; } else { return false; } } else { return false; } } else { return false; } } //生成二维码 public function saveQrcode($card_id) { $token=$this->getToKen(); //生成二维码 //参数 卡券id token $erwei = array('action_name' => "QR_CARD", 'action_info' => array( 'card' => array('card_id' => $card_id, 'is_unique_code' => false) )); $json_wxpic = json_encode($erwei, JSON_UNESCAPED_UNICODE);//转成json形式 $card_wm = http_request('https://api.weixin.qq.com/card/qrcode/create?access_token=' . $token, $json_wxpic); $wx_kabao = json_decode($card_wm); $wx_success = $wx_kabao->{'show_qrcode_url'};//获取二维码 if ($wx_success) { //调用保存二维码的方法 $sav['imgurl'] = $this->saveImg($wx_success); $sav['wx_url'] = $wx_success; $url = M('wxcard')->where(array('card_id' => $card_id))->save($sav); if ($url) { return true; } else { return false; } } else { return false; } } //获取卡券信息 public function look_card($id) { $content=M('wxcard')->where(array('id'=>$id))->find(); if($content) { return $content; }else { return false; } } //修改卡券 public function modify($id) { $data=$this->look_card($id); if($data) { $token=$this->getToKen(); $ta=strtolower($data['code_type']); $arr=array( 'card_id'=>$data['card_id'], $ta=>array( 'base_info'=>array( 'color' => $data['color'],//颜色 'notice' => $data['notice'], //展示显示的卡券 'service_phone' => $data['servoce_phone'], "description" => $data['description'], 'get_limit' => $data['get_limit'],//获取数量 'can_share' =>$data['can_share']==2?true:false, //可不可以分享 'can_give_friend' =>$data['can_give_friend'] ==2?true:false,//赠送朋友 'custom_url_name' => $data['custom_url_name'], 'custom_url' => $data['custom_url'],//跳转链接 'custom_url_sub_title'=>$data['custom_url_sub_title'] ) ) ); $json_arr=json_encode($arr,JSON_UNESCAPED_UNICODE); $sucess=http_request('https://api.weixin.qq.com/card/update?access_token='.$token,$json_arr); $arr_success=json_decode($sucess); if($arr_success->{'errmsg'}=='ok') { return true; }else { return false; } }else { return false; } } //拉取商户下的卡券列表最多五十个 public function card_list() { //获取token $token=$this->getToken(); //参数 $arr=array( "offset"=>0, "count"=>50, "status_list"=>array("CARD_STATUS_DISPATCH")//类型在公众平台投放过的卡券 ); $json_arr=json_encode($arr); $success=http_request('https://api.weixin.qq.com/card/batchget?access_token='.$token,$json_arr); $arr_success=json_decode($success); if($arr_success->{'errmsg'}=='ok') { // var_dump($arr_success->{'card_id_list'}); return $arr_success->{'card_id_list'}; }else { return false; } } //获取商户下的卡券信息结束时间 public function card_num($begin_time,$end_time) { //开始有效事件 $token=$this->getToken(); $arr=array( "begin_date"=>$begin_time, "end_date"=>$end_time, "cond_source"=>1 ); $json_arr=json_encode($arr,JSON_UNESCAPED_UNICODE); $success=http_request('https://api.weixin.qq.com/datacube/getcardbizuininfo?access_token='.$token,$json_arr); $success_arr=json_decode($success,true); if($success_arr) { // dump($success_arr); return $success_arr; }else { return false; } } //获取二维码 public function get_er() { $token=$this->getToken(); $arr=array( "card_id"=>"prLyYt-yLaA-ItTnxzpVtHLY-Tts" ); $json_arr=json_encode($arr); $success=http_request('https://api.weixin.qq.com/card/get?access_token='.$token,$json_arr); $success_arr=json_decode($success,JSON_UNESCAPED_UNICODE); print_r($success_arr); } //保存卡卷二维码到本地服务器提供给用户下载 public function saveImg($url){ //获取图片路径信息 $text=file_get_contents($url); //拼接图片保存路径及名称(名称由时间戳和0-999的随机数组成) $File_name = './'.'Uploads'.'/Picture/Qrcard/'.date('YmdHis') . rand(0, 999) . '.jpg'; //把图片写入到服务器 $myfile = fopen($File_name, "w") or die("Unable to open file!"); fwrite($myfile, $text); //获取图片的保存路径 if($myfile){ //截取路径并拼接 $imgUrl = substr($File_name,1); return $imgUrl; } fclose($myfile); } //查询单个卡卷信息 $card_id:卡卷card_id $card_type:卡卷类型 public function cardInfo($card_id,$card_type){ //调用获取token的方法 $token = $this->getToKen(); //需要查询的卡卷id $arr = array( 'card_id'=>$card_id ); //转换成json格式 $json_arr = json_encode($arr); $success = http_request('https://api.weixin.qq.com/card/get?access_token='.$token,$json_arr); $success_arr=json_decode($success,true); //根据$card_type 判断卡卷类型 switch ($card_type){ case 'DISCOUNT'://折扣卷 $type = $success_arr['card']['discount']; break; case 'GIFT'://兑换券 $type = $success_arr['card']['gift']; break; case 'CASH'://代金券 $type = $success_arr['card']['cash']; break; case 'GENERAL_COUPON'://优惠券 $type = $success_arr['card']['general_coupon']; break; case 'GROUPON'://团购卷 $type = $success_arr['card']['groupon']; break; } // echo '
';
        //只返回库存信息
        return $type['base_info']['sku']['quantity'];
    }
}