Wxcard.php 12.3 KB
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: JY
 * Date: 2015-09-23
 */

namespace app\admin\controller;

use app\admin\logic\GoodsLogic;
use app\admin\logic\ArticleCatLogic;
use think\Controller;
use think\Db;
use think\Page;
use qcloudcos\Conf;
use qcloudcos\Myqcloudcos;

class Wxcard extends Base
{

    public function index()
    {

        $config['state']=1;
        $getadmstoid=getAdmStoId();
        $config=M('store_wxcard')->where('store_id',$getadmstoid)->find();
        $wxinfo=M('wx_user')->where('store_id',$getadmstoid)->find();
        $urljson="{\"custom_cell1\":{\"name\":\"会员中心\",\"tips\":\"会员资料\",\"url\":\"".curHostURL()."/mobile/user/index/stoid/" . $getadmstoid . ".html\"},\"custom_cell2\":{\"name\":\"优惠券\",\"tips\":\"\",\"url\":\"" . curHostURL() . "/mobile/user/coupon/stoid/" . $getadmstoid . ".html\"},\"custom_cell3\":{\"name\":\"我的订单\",\"tips\":\"\",\"url\":\"" . curHostURL() . "/mobile/user/order_list/stoid/" . $getadmstoid . ".html\"}}";
        if ($wxinfo)
        {
            if ($config['urljson'])
            {
               $urljson=$config['urljson'];
            }
            $config['headerpic']=$wxinfo['headerpic'];
        }
        else
        {
            $config['brand_name']=$wxinfo['wxname'];
        }

        $this->assign('urljson', json_decode($urljson,true));

        $this->assign('config', $config);
        $erpid=getERPId();
        $this->assign('erpid', $erpid);

        return $this->fetch('', getAdmStoId());
    }

    public function handle()
    {

        $getadmstoid = getAdmStoId();
        $data = I('post.');

        $urljson="{\"custom_cell1\":{\"name\":\"".$data['url_name'][0]."\",\"tips\":\"".$data['url_tips'][0]."\",\"url\":\"".$data['url_http'][0]."\"},\"custom_cell2\":{\"name\":\"".$data['url_name'][1]."\",\"tips\":\"".$data['url_tips'][1]."\",\"url\":\"".$data['url_http'][1]."\"},\"custom_cell3\":{\"name\":\"".$data['url_name'][2]."\",\"tips\":\"".$data['url_tips'][2]."\",\"url\":\"".$data['url_http'][2]."\"}}";
        if ($data['url_name'][0] && $data['url_http'][0])
        {
           $custome_url1=" ,\"custom_url_name\": \"".$data['url_name'][0]."\", \"custom_url\": \"".$data['url_http'][0]."\", \"custom_url_sub_title\": \"".$data['url_tips'][0]."\"";
        }
        else
        {
            $custome_url1=" ,\"custom_url_name\": \"\", \"custom_url\": \"\", \"custom_url_sub_title\": \"\"";
        }
        if ($data['url_name'][1] && $data['url_http'][1])
        {
            $urljson1=",\"custom_cell1\":{\"name\":\"".$data['url_name'][1]."\",\"tips\":\"".$data['url_tips'][1]."\",\"url\":\"".$data['url_http'][1]."\"}";
        }
        else
        {
            $urljson1=",\"custom_cell1\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\"}";
        }
        if ($data['url_name'][2] && $data['url_http'][2])
        {
            $urljson1.=",\"custom_cell2\":{\"name\":\"".$data['url_name'][2]."\",\"tips\":\"".$data['url_tips'][2]."\",\"url\":\"".$data['url_http'][2]."\"}";
        }
        else
        {
            $urljson1.=",\"custom_cell2\":{\"name\":\"\",\"tips\":\"\",\"url\":\"\"}";
        }

       



        $getsumbittype = $data['sumbittype'];
        $getlogo_url = $data['logo_url'];


        $getbusiness_service=array();
        if ($data['business_service']) {
            if (in_array("0", $data['business_service'])) {
                $getbusiness_service[] = "BIZ_SERVICE_FREE_WIFI";
            }
            if (in_array("1", $data['business_service'])) {
                $getbusiness_service[] = "BIZ_SERVICE_WITH_PET";
            }
            if (in_array("2", $data['business_service'])) {
                $getbusiness_service[] = "BIZ_SERVICE_FREE_PARK";
            }
            if (in_array("3", $data['business_service'])) {
                $getbusiness_service[] = "BIZ_SERVICE_DELIVER";
            }
            $data['business_service']=json_encode($data['business_service']);
        }
        else
        {
            $data['business_service']='';
        }



        unset($data['sumbittype']);
        unset($data['logo_url']);
        $data['urljson'] = $urljson;
        $res = M('store_wxcard')->where('store_id', $getadmstoid)->find();
        if ($res) {

            $data['edittime'] = time();
            $data['editip'] = getIP();
            $data['editadmid'] = getAdminId();
            $row = M('store_wxcard')->where('store_id', $getadmstoid)->save($data);
        } else {
            $data['addtime'] = time();
            $data['addip'] = getIP();
            $data['store_id'] = $getadmstoid;
            $row = M('store_wxcard')->save($data);
        }




        if ($getbusiness_service) {
            $advanced_info = '"advanced_info": {
        "business_service": ' . json_encode($getbusiness_service) . '
           },';
        }else
        {
            $advanced_info = '"advanced_info": {
        "business_service":[]
           },';
        }

        $is_postwx=0;
        $errormsg="";
        //并发布
        if ($data['bgtype']==0)
        {
            unset($data['background_pic_url']);
        }
        //判断 没有ERP
            $wx_yckstr='custom_field2": {
                "name": "积分",
                 "url": "' . curHostURL() . '/mobile/user/points/stoid/' . $getadmstoid . '.html
            }, ';
        if (getERPId())
        {
            $wx_yckstr='"custom_field2": {
                "name": "预存款",
                "url": "' . curHostURL() . '/mobile/user/money_1/stoid/' . $getadmstoid . '.html"
            }, "custom_field3": {
                "name": "积分",
                 "url": "' . curHostURL() . '/mobile/user/points/stoid/' . $getadmstoid . '.html"
            }  ';
        }

       $getcode_type="CODE_TYPE_QRCODE";
        $getcode_typenum=0;
        switch ($data['code_type'])
        {
            case 1:
                $getcode_type="CODE_TYPE_TEXT";
                $getcode_typenum=0;
                break;
            case 2:
                $getcode_type="CODE_TYPE_QRCODE";
                $getcode_typenum=1;
                break;
            case 3:
                $getcode_type="CODE_TYPE_BARCODE";
                $getcode_typenum=2;
                break;
            default:
                break;
        }

        if ($getsumbittype == "2") {

            $baseconfig = tpCache('shop_info', getAdmStoId());


            if ($res && $res['card_id']) {
                $str = '{
       "card_id":"' . $res['card_id'] . '",
       "member_card": {
               "background_pic_url": "' . $data['background_pic_url'] . '",
               "base_info": {  
                   "logo_url": "' . $getlogo_url . '",
                      "code_type": "'.$getcode_type.'",
                "title":"' . $data['title'] . '",
                "color":"' . $data['color'] . '",                
                "use_all_locations": true,
                   "service_phone": "' . $data['service_phone'] . '",
                   "description":  "' . $data['description'] . '"         
                '.$custome_url1.'
               },    
              '.$advanced_info.'
               
                "prerogative": "' . $data['prerogative'] . '", 
                "auto_activate": true,               
                 "custom_field1": {
                "name": "余额",
                "url": "' . curHostURL() . '/mobile/user/money/stoid/' . $getadmstoid . '.html"
            },
            '.$wx_yckstr.'
                     
            '.$urljson1.'
       }
}';




                $acctk = m_get_access_token(null, $getadmstoid);
                $url = 'https://api.weixin.qq.com/card/update?access_token=' . $acctk;//修改
                $rs = http_post_json($url, $str);


                $rs = json_decode($rs[0], true);


                if ($rs && $rs['errcode'] == 0) {

//                    $setpost="{\"card_id\":\"".$res['card_id']."\", \"is_open\": true}";
//                    $seturl = 'https://api.weixin.qq.com/card/selfconsumecell/set?access_token=' . $acctk;
//                    $rs = http_post_json($seturl, $setpost);

                    $is_postwx = 1;

                } else {
                    if ($rs['errcode']=="48001")
                    {
                        $errormsg = $rs['errcode'] . "未开通卡券功能或未授权";
                    }
                    else {
                        $errormsg = $rs['errcode'] . $rs['errmsg'];
                    }
                }
            }


            else {

                $str = '{
    "card": {
        "card_type": "MEMBER_CARD",
        "member_card": {      
             "background_pic_url": "' . $data['background_pic_url'] . '",
            "base_info": {
                "logo_url": "' . $getlogo_url . '",
                "brand_name": "' . $data['brand_name'] . '",
                "code_type": "'.$getcode_type.'",                
                "title":"' . $data['title'] . '",
                "color":"' . $data['color'] . '",
                "notice": "使用时向服务员出示此券",
                "service_phone":"' . $data['service_phone'] . '",       
                "description": "' . $data['description'] . '",
                "pay_info": {
                    "swipe_card":{
                        "is_swipe_card":true
                    }
                },
                "date_info": {
                    "type": "DATE_TYPE_PERMANENT"
                },
                "sku": {
                    "quantity": 50000000
                },
                "get_limit":1,
                "use_custom_code": true,
                 "need_push_on_view": true,
                 "use_all_locations": true,
                "can_give_friend": true
                 '.$custome_url1.'
               
                
            },
            '.$advanced_info.'
            "supply_bonus": false,
            "supply_balance": false,
            "prerogative": "' . $data['prerogative'] . '",
            "auto_activate": true,                        
            "custom_field1": {
                "name": "余额",
                "url": "' . curHostURL() . '/mobile/user/money/stoid/' . $getadmstoid . '.html"
            },
            '.$wx_yckstr.'
              
             '.$urljson1.'
       }
    }
}';
                if (getAdmStoId()=="23")
                {
                 //   echo "add".$str;die;
                }

                $acctk = m_get_access_token(null, $getadmstoid);
                $url = 'https://api.weixin.qq.com/card/create?access_token=' . $acctk;//新增
                $rs = http_post_json($url, $str);


                $rs = json_decode($rs[0], true);
                if ($rs && $rs['errcode']==0) {
                    $is_postwx = 1;
                    $data1['card_id'] = $rs['card_id'];

//                    $setpost="{\"card_id\":\"".$rs['card_id']."\", \"is_open\": true}";
//                    $seturl = 'https://api.weixin.qq.com/card/selfconsumecell/set?access_token=' . $acctk;
//                    $rs = http_post_json($seturl, $setpost);


                    $row = M('store_wxcard')->where('store_id', $getadmstoid)->save($data1);
                } else {
                    if ($rs['errcode']=="48001")
                    {
                        $errormsg = $rs['errcode'] . "未开通卡券功能或未授权";
                    }
                    else {
                        $errormsg = $rs['errcode'] . $rs['errmsg'];
                    }
                }


            }


        }

        delFile(ROOT_PATH."/public/upload/wxcard/".getERPId()."/");//删除本地

        if ($getsumbittype == "2") {
            if ($is_postwx == 1) {
                $this->success('操作成功');
            } else {
                mlog($str,"wxcard/".getAdmStoId());
                $this->error('操作失败' . $errormsg);

            }
        }
        else
        {
            $this->success('操作成功');
        }
    }



}