<?php
/**
 * 管理员管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class MemberModel extends Model{
    public function get_index($getdata){
        $return=array('status'=>3,'msg'=>'参数错误');
        if(empty($getdata['uid'])){return $return;}
        $map=array('id'=>$getdata['uid']);
        $info=M('Member')->where($map)->field(true)->find();
        if(empty($info['rank_id'])) {$info['rank_id']=1;}
        $info['rank_name'] = idtoname($info['rank_id'], 'MemberRank','name','id',false);
        if($info['rank_name']==false) {$info['rank_name']='普通会员';}
        if($info['header_img']!=''){
            $info['header_img']=thumb($info['header_img']);/*'https://'.$_SERVER['SERVER_NAME'].$info['header_img'];*/
        }
        if($info['card_sn']){
            $info['card_img']=geturl('Home/qrcode/index',array('data'=>$info['card_sn'],'appid'=>C("appid")),true,true);

        }
        $info['six_array']=array(
            0=>'保密',
            1=>'男',
            2=>'女'
        );
        $info['constellationData'] = array(0=>'白羊座',1=>'金牛座',2=>'双子座',3=>'巨蟹座',4=>'狮子座',5=>'处女座',6=>'天枰座',7=>'射手座',8=>'摩羯座',9=>'水瓶座',10=>'双鱼座',11=>'天蝎座');
        $info['six_name']=$info['six_array'][$info['sex']];
        $info['constellation_name']=$info['constellation'];
        $map = array('uid'=>$getdata['uid']);
        $no_pay=D('Common/OrderInfo')->all_order($map,2,1);
        $takeout=D('Common/OrderInfo')->all_order($map,4,1,5,true,14);
        // var_dump($takeout);exit;
        $no_group=D('Common/OrderInfo')->all_order($map,6,1);//待成团
        $no_ship=D('Common/OrderInfo')->all_order($map,3,1);
        $yes_ship=D('Common/OrderInfo')->all_order($map,4,1);
        $no_comment=D('Common/OrderInfo')->all_order($map,5,1);
        $service_order=D('Common/OrderInfo')->all_order($map,7,1,'','',7);//服务订单
        $tui_kuan=D('Common/OrderInfo')->all_order($map,9,1);//服务订单
        // print_r($service_order);exit;
        $goods_count=M('GoodsCollect')->where($map)->field('id')->count();
        //$shops_count=M('ShopsCollect')->where($map)->field('id')->count();
        $xs_map['user_id'] =$getdata['uid'];
        $xs_bonu=M('MemberBonus')->where($xs_map)->field('id')->count();
        $c_map = $map;
        $c_map['coupons_type'] = 1;
        // $c_map['buy_type'] = 0;
        $get_coupons=M('CouponsGet')->where($c_map)->field('id')->count();
        //会员卡信息
        $map = array();
        $map['name'] = 'WX_CARD';
        $config = M('Config')->where($map)->getField('value');
        $config = unserialize($config);
        if($info['is_tmp']!=1&&$info['mobile'] && $info['card_sn']){//会员状态
            $card_status = 1;
        }elseif($info['code']&&empty($info['card_sn'])){//会员卡待激活状态
            $card_status = 2;
            $card_code = array(
                'cardId'=>$config['card_id'],
                'code'=>$info['code']
            );
        }else{//会员卡待领卡状态
            $card_status = 3;
        }
        //会员卡信息end
        $return=array(
            'status'=>1,
            'data'=>array(
                'no_ship'=>$no_ship['num'],//待发货
                'yes_ship'=>$yes_ship['num'],//待收货
                'no_comment'=>$no_comment['num'],//待评价
                'service_order'=>$service_order['num'],//服务预约
                'no_pay'=>$no_pay['num'],//待付款
                'tui_kuan'=>$tui_kuan['num'],//退款
                //'shops_count'=>$shops_count,
                'get_coupons'=>$get_coupons,
            ),
            'info'=>$info,
            'card_color'=>$config['color']?$config['color']:'',
            'card_code'=>$card_code?$card_code:array(),
            'card_status'=>$card_status,
            'web_logo'=>format_url(C("WEB_SMALL_LOGO")),
            'web_name'=>C("WEB_SITE_NAME"),
            'open_register'=>C("REGISTER_OPEN"),
        );

        //1031主播信息
        
        //end
        return $return;
    }

    /**
     * 余额明细
     * @param $getdata
     * @return array
     */
    public function balance_list($getdata){
        $return=array('status'=>3,'msg'=>'参数错误');
        if(empty($getdata['uid'])){return $return;}
        $page=I('get.page')?I('get.page'):1;
        $map=array(
            'uid'=>$getdata['uid'],
            'type'=>array('in',array(1,2)),
            'role'=>0
        );
        $list=page_list('AccountLog',$map,'change_time desc,id desc',true,'','',$page);
        if(empty($list['list'])){
            $return = array(
                'status' => 2,
                'msg'=>'没有更多的数据了'
            );
        }else {
            $account_status=strtoarray(C('ACCOUNT_STATUS'));
            foreach($list['list'] as $k=>$v){
                $list['list'][$k]['account_status']=$account_status[$v['status']];
                $list['list'][$k]['change_num']=$v['change_num'];
                $list['list'][$k]['change_time']=time_format($v['change_time'],'Y-m-d');
                $list['list'][$k]['change_type']=$v['change_type']==1?'-':'+';
            }
            $return = array(
                'status' => 1,
                'data' => $list['list']
            );
        }
        return $return;
    }

    /**
     * 变更详情
     */
    public function money_change_desc($param){
        $return=array('status'=>1);
        if(empty($param['id'])){
            $return['status']=3;
            $return['msg']='参数错误';
            return $return;
        }
        $map = array();$map['id'] = $param['id'];
        $info = M('AccountLog')->where($map)->find();
        $account_status=strtoarray(C('ACCOUNT_STATUS'));
        if($info){
            $info['status']=$account_status[$info['status']];
            $info['change_type']=$info['change_type']==1?'-':'+';
            $info['change_time']=time_format($info['change_time']);
            $return['data']=$info;
        }
        return $return;
    }
    public function mycoupon_list($getdata){
        $return=array('status'=>2,'msg'=>'没有更多的数据了');
        $status=intval($getdata['type']);
        $map = array();
        $map['uid'] = $getdata['uid'];
        $map['coupons_type'] = 1;
        // $map['buy_type'] = 0;
        if($status==2){
            $map['status'] = 0;
            $map['end_timestamp'] = array(array('lt', NOW_TIME));
        }else {
            $map['status'] = $status;
            // $map['end_timestamp'] = array(array('egt', NOW_TIME));
        }
        $order = 'add_time desc,end_timestamp asc';
        $pagesize = '5';
        $list = page_list('CouponsGet', $map, $order, true, $pagesize,'', $getdata['page']);
        if(!empty($list['list'])){
            $rank_name=array(
                'GENERAL_COUPON'=>'优惠券',
                'DISCOUNT'=>'折扣券',
                'GIFT'=>'礼品券',
                'CASH'=>'代金券',
                'GROUPON'=>'团购券'
            );
            $status_cont=$status==0?'立即使用':($status==1?'已使用':'已过期');
            $sn_hid=array();
            foreach($list['list'] as $key=>$val){
                $sn_hid[$val['id']]=true;
                $list['list'][$key]=$this->format_mycoupon($val);
            }
            $return=array('status'=>1,'data'=>$list['list'],'sn_hid'=>$sn_hid,'totalpage'=>$list['totalPages']);
        }
        return $return;
    }

    /**我的卡券详情
     * @param $getdata
     * @return array
     */
    public function mycoupon_cont($getdata){
        $return=array('status'=>1,'msg'=>'');
        if(empty($getdata['id'])){
            $return['msg']='参数错误';
            $return['status']=3;
            return $return;
        }
        $map=array();
        $map['id']=$getdata['id'];
        $info=M("CouponsGet")->where($map)->field(true)->find();
        $return['data']=$this->format_mycoupon($info);
        return $return;
    }

    /**格式化我的卡券
     * @param $data
     */
    private function format_mycoupon($data){
        $return=array();
        if(empty($data)){
            $return['status']=3;
            $return['msg']='参数错误';
            return $return;
        }
        $rank_name=array(
            'GENERAL_COUPON'=>'优惠券',
            'DISCOUNT'=>'折扣券',
            'GIFT'=>'礼品券',
            'CASH'=>'代金券',
            'GROUPON'=>'团购券'
        );
        $status=array(
            0=>'立即使用',
            1=>'已使用',
            2=>'已过期',
            3=>'已过期',
        );
        $data['rank_type']=$rank_name[$data['rank_id']];
        $data['start_time']=time_format($data['begin_timestamp']);
        $data['end_time']='至'.time_format($data['end_timestamp']);
        if(empty($data['code'])){
            $data['code'] = $data['id'];
        }
        $data['img_code']=geturl('Home/Qrcode/index',array('data'=>$data['code'],'appid'=>C("appid")),true,true);
        if($data['rank_id']=='CASH'){
            $data['money']=intval($data['reduce_cost']);
        }elseif($data['rank_id']=='DISCOUNT'){
            $money=(100-$data['discount'])/10;
            $data['money']=$money.'折';
        }
        if($data['shop_id']==0){
            $data['shop_name']='全部门店可用';
        }else{
            $map=array();
            $map=array('in',$data['shop_id']);
            $shop_name=M("Shops")->where($map)->getField('name');
            $data['shop_name']=implode(',',$shop_name);
        }
        $map=array();
        $map['id']=$data['coupons_id'];
        $c_color=M("Coupons")->where($map)->getField('color');
        $color=D("Wxsmall/Coupons")->get_cpupon_color();
        if($c_color){
            $data['color']=$color[$c_color];
            $data['class']='flexbox voucher_huang '.$color[$c_color];
        }else{
            $data['color']='';
            $data['class']='flexbox voucher_huang';
        }
        $data['least_cost']=intval($data['least_cost']);
        $data['status_cont']=$status[$data['status']];
        $data['status_type']=$data['status'];
        return $data;
    }
    public function myorder_list($getdata){
        $return=array('status'=>2,'msg'=>'没有更多的数据了');
        $data=array('uid'=>$getdata['uid']);
        if($getdata['type']=='no_ship'){// 退货/退款订单
            $getdata['type']=9; 
        }
        if($getdata['type']=='group' || $getdata['type']==6){// 未/已成团
            $order_list=D('Common/OrderInfo')->all_order($data,$getdata['type'],'',10,'',6);
        }else if($getdata['type']==0){//全部订单
            $order_list=D('Common/OrderInfo')->all_order($data,$getdata['type'],'',10,'',15);
        }else{
            $order_list=D('Common/OrderInfo')->all_order($data,$getdata['type'],'',10);
        }
        if(!empty($order_list['list'])){
            foreach($order_list['list'] as $k=>$v){
                $order_list['list'][$k]['shop_name']=idtoname($v['shop_id'],'Shops');
                $order_operation=$this->order_operation($v);
                $order_list['list'][$k]['button_list']=$order_operation['button_list'];
                $order_list['list'][$k]['order_status']=$order_operation['order_status'];
                if($v['send_type']==1){
                    if($v['best_time']){
                        $order_list['list'][$k]['send_time']=time_format($v['best_time']);
                    }else{
                        $order_list['list'][$k]['send_time']='';
                    }
                    $order_list['list'][$k]['send_order_sn']=$v['order_sn'];
                }
                $goods_list=array();
                foreach($order_list['list'][$k]['goods']['list'] as $ke=>$va){
                    $goods_list[$ke]=$va;
                    $goods_list[$ke]['price_desc']=format_price($va['goods_price']);
                    if($va['type']==3 && $va['goods_integral']>0){$goods_list[$ke]['price_desc'].=" + ".$va['goods_integral']."积分";}
                    $goods_status='';
                    if($va['status']==0 && $va['pay_status']!=3){$goods_status='未确认';}
                    elseif($va['status']==2){$goods_status='已完成';}
                    elseif($va['status']==5){$goods_status='拒绝退货';}
                    $goods_list[$ke]['goods_status']=$goods_status;
                    $goods_list[$ke]['img']=thumb($va['img'],430,430,'noimg_430x430.png');
                }
                $order_list['list'][$k]['goods_list']=$goods_list;
                $order_list['list'][$k]['all_num_desc']='';
                if($v['goods']['all_num']>0){$order_list['list'][$k]['all_num_desc']="共".$v['goods']['all_num']."件商品  ";}
                if($v['type']==3){$order_list['list'][$k]['account_desc']=$v['used_score']."积分 + ".format_price($v['goods_amount']);}
                else{$order_list['list'][$k]['account_desc']=format_price($v['order_money']);}
                if($v['delivery_fee']>0) {
                    $order_list['list'][$k]['ship_desc'] = "（含运费:" . format_price($v['delivery_fee']) . "）";
                }
            }
            $return=array('status'=>1,'data'=>$order_list['list']);
        }
        return $return;
    }
    public function myorder_info($getdata){
        $return=array('status'=>3,'msg'=>'参数错误');
        $id=intval($getdata['id']);$uid=intval($getdata['uid']);
        if($id<=0 || $uid<=0){return $return;}
        $map=array('id'=>$id);
        $order_info=M('OrderInfo')->where($map)->field(true)->find();
        if($order_info['is_delete']==1){$return['msg']='订单已被删除';return $return;}
        if($order_info['shop_id']>0){
            $shop_info=idtoname($order_info['shop_id'],'Shops','*','id','name,phone,province,city_id,district,address,thumb');
            $order_info['shop_name']=$shop_info['name'];
            $order_info['shop_phone']=$shop_info['phone'];
            $order_info['img']=thumb($shop_info['thumb']);
            $order_info['address_desc']=idtoname($shop_info['province'],'Area').idtoname($shop_info['city_id'],'Area').idtoname($shop_info['district'],'Area').$shop_info['address'];
        }
        if($order_info['best_time']){
            $order_info['best_time']=time_format($order_info['best_time']);
        }else{
            $order_info['best_time']='';
        }

        $order_info['goods_allmoney']=format_price($order_info['goods_allmoney']);
        if($order_info['bonus_money']>0){$order_info['bonus_money_f']=format_price($order_info['bonus_money']);}
        if($order_info['pay_time']){
            $order_info['pay_time']=time_format($order_info['pay_time']);
        }
        if($order_info['accept_time']){
            $order_info['accept_time']=time_format($order_info['accept_time']);
        }
        if($order_info['finish_time']){
            $order_info['finish_time']=time_format($order_info['finish_time']);
        }
        if($order_info['comment_time']){
            $order_info['comment_time']=time_format($order_info['comment_time']);
        }
        if($order_info['addtime']){
            $order_info['addtime']=time_format($order_info['addtime']);
        }
        $order_info['order_money']=format_price($order_info['order_money']);
        $map=array('order_id'=>array('like','%,'.$id.',%'));
        $order_info['used_score'] = M('PayLog')->where($map)->getField('use_score_money');
        $order_operation=$this->order_operation($order_info);
        $order_info['button_list']=$order_operation['button_list'];
        $order_info['order_status']=$order_operation['order_status'];
        if($order_info['payment_id']){
            $order_info['payment_code']=idtoname($order_info['payment_id'],"Payment",'code');
            if($order_info['payment_code']=='Hanchao'){
                $order_info['payment_code']="余额支付";
            }else{
                $order_info['payment_code']=idtoname($order_info['payment_id'],"Payment",'name');
            }
        }else{
            $order_info['payment_code']="余额支付";
        }
        if($order_info['send_type']==1){
            $order_info['code_img']=geturl('Home/qrcode/index',array('data'=>$order_info['order_sn'],'appid'=>C("appid")),true,true);
        }
        $order_info['member_address']=idtoname($order_info['province'],'Area').idtoname($order_info['city'],'Area').idtoname($order_info['district'],'Area').$order_info['address'];//$shop_info['address'];
        $map=array('order_id'=>$id);
        $order_goods=M('OrderGoods')->where($map)->field(true)->select();
        foreach($order_goods as $k=>$v){
            $attr=unserialize($v['goods_attr']);
             $order_goods[$k]['img']=thumb(idtoname($v['goods_id'],'Goods','img'));
            $order_goods[$k]['attr_name']=$attr['name'];
            $order_info['ordergoods_id']=$v['id'].',';//获取该订单所有的order_goods的id
            $order_goods[$k]['goods_price']=format_price($v['goods_price']);
            if($attr['name']){
                $order_goods[$k]['goods_name']=$v['goods_name']."(".$attr['name'].")";
            }
        }
        $s_money = $order_info['discount']-$order_info['bonus_money']-$order_info['member_discount']-$order_info['custom_discount']-$order_info['goods_custom_discount']-$order_info['used_score_money']-$order_info['first_money'];

        $map=array('id'=>$order_info['uid']);
        $user_info=M("Member")->where($map)->field('paypassword')->find();
        //判断是否有支付密码
        if(empty($user_info['paypassword'])){
            $data['userpaypass']=1;
        }
        $DIS_TRIBUTE=C("DIS_TRIBUTE");
        if($DIS_TRIBUTE == 1 && $order_info['distribute']){
            $reqStatus = D('Common/Distribute')->details_order($order_info);
            $data['reqstatus']=$reqStatus;
        }
        if($order_info['distribute']){
            $order_info['distribut_name']=idtoname($order_info['distribute'],'Distribute','name','code');
        }
        $data['distribute']=$DIS_TRIBUTE;
        //快递100
        $map=array();
        $map['order_id']=$id;
        $ordershipping=M('ShipOrder')->where($map)->field(true)->find();
        if($ordershipping){
            $map=array();
            $map['code']=$ordershipping['send_type'];
            $ordershipping['kuaidi_name']=M("Shipping")->where($map)->getField("name");
            $order_info['distribut_name']=$ordershipping['kuaidi_name'];
        }
        $data['shipping_info']=$ordershipping;
        $map=array('uid'=>$uid,'order_id'=>$id);
        $service_sn=M('MemberPackage')->where($map)->getField('service_sn');
        $data['order_info']=$order_info;
        $data['s_money']=$s_money;
        $data['service_sn']=!empty($service_sn)?$service_sn:'';
        $data['goods_info']=!empty($order_goods)?$order_goods:'';
        $return=array(
            'status'=>1,
            'data'=>$data
        );
        return $return;
    }
    public function order_operation($v){
        $order_status='';$button_list=array();
        switch($v['status']){
            case 1:
                if($v['pay_status']<3){$button_list=array('continue_pay'=>'继续付款','cancel_order'=>'取消订单');}
                $order_status='未确认';
                break;
            case 2:
                if($v['pay_status']<3){$button_list=array('continue_pay'=>'继续付款','cancel_order'=>'取消订单');$order_status='未支付';}
                elseif($v['pay_status']==3 && $v['shipping_status']==1 && $v['type']!=3){if($v['type']==1){$button_list=array('cancel_order'=>'取消订单');}elseif($v['type']==14){$button_list=array('cancel_order'=>'取消订单');}$order_status='待发货';}
                elseif($v['pay_status']==3 && $v['shipping_status']==1 && $v['type']==3){$order_status='待兑换';}
                elseif($v['pay_status']==3 && $v['shipping_status']==3 && $v['type']!=11 && $v['type']!=12){if($v['type']!=14){$button_list=array('cancel_order'=>'取消订单');}$button_list=array('confirm'=>'确认收货');if($v['send_type']==1){$button_list=array('confirm'=>'确认收货','cancel_order'=>'取消订单');$order_status='待自提';}else{
                    $order_status='待收货';
                }}
                elseif($v['pay_status']==3 && $v['shipping_status']==3 && $v['type']==11 || $v['type']==12){$order_status='待使用';}
                elseif($v['pay_status']==3 && $v['shipping_status']==4){$order_status='已收货';}
                elseif($v['pay_status']==3 && $v['shipping_status']==5){$order_status='配货中';}
                elseif($v['pay_status']==3 && $v['shipping_status']==6){$order_status='待成团';}
                else{$button_list=array('continue_pay'=>'继续付款');$order_status='部分支付';}
                break;
            case 3:
                if($v['is_comment']==0 && $v['type']!=12 && $v['type']!=13){$order_status='未评论';  $button_list=array('go_dp'=>'去评论');}
                elseif($v['is_comment']==0 && $v['type']==12){$order_status='已完成';}
                elseif($v['is_comment']==0 && $v['type']==13){$order_status='已完成';}
                else{$order_status='已评论';}
                break;
            case 4:
                $order_status='已取消';
                break;
            case 5:
                $order_status='已做废';
                break;
            case 6:
                $order_status='退款中';
                break;
            case 7:
                $order_status='已退款';
                break;
            case 8:
                $order_status='退货中';
                break;
            case 9:
                $order_status='已退货';
                break;
            case 10:
                $order_status='已过期';
                break;
        }
        $return=array('order_status'=>$order_status,'button_list'=>$button_list);
        return $return;
    }
    public function get_brand_car_id($name){
        $add = array();
        $add['name'] = $name;
        $thisid = M('BrandCar')->where($add)->getField('id');
        if($thisid){
            return $thisid;
        }else{
            return M('BrandCar')->add($add);
        }
    }
    public function address($getdata){
        $return=array('status'=>2,'msg'=>'缺少必要参数');
        if(!$getdata['types'])return $return;
        $result = array();
        switch($getdata['types']){
            case 1://获取所有地区列表
                $regiondata = $this->linkagearea();
                if($regiondata['status']==1){
                    $result['region'] = $regiondata['data']['region'];
                    $result['area'] = $regiondata['data']['area'];
                }
                if($getdata['id']){//编辑信息
                    $result['init_list'] = idtoname($getdata['id'],'MemberAddress','*','id',false);
                    $regions['province']=idtoname($result['init_list']['province'],"Area",'name');
                    $regions['city']=idtoname($result['init_list']['city'],"Area",'name');
                    $regions['district']=idtoname($result['init_list']['district'],"Area",'name');
                    $result['init_list']['region'] = $regions['province'].$regions['city'].$regions['district'];
                    $result['init_list']['city_name']=$regions['city'];
                }
            break;
            case 4://保存收货地址信息
            $model = M('MemberAddress');
            $save=$getdata;
            $regular= new \Common\Vendor\Regular();
            if(!$regular->isEmpty($save['consignee'])){
                    $return['msg']='收件人不能为空';
                    return $return;
            }
                if(!$regular->isEmpty($save['province']) || !$regular->isEmpty($save['city'])){
                    $return['msg'] = '请填写完整的所在地区';
                    return $return;
                }
                if(!$regular->isEmpty($save['address'])){
                    $return['msg'] = '请填写完整的收货地址';
                    return $return;
                }
                if(!$regular->isEmpty($save['mobile'])){
                    $return['msg'] = '手机号不能为空';
                    return $return;
                }
                if(!$regular->isPhone($save['mobile'])){
                    $return['msg']='手机号格式不正确';
                    return $return;
                }

            $save['updatetime'] = NOW_TIME;
            $save['type'] = 1;
            $save['uid'] = $getdata['uid'];
            if($getdata['status']==1){
                $map = array();
                $map['uid'] = $getdata['uid'];
                $del['status'] = 0;
                $model->where($map)->save($del);
                $save['status'] = 1;
            }
            if($getdata['id']>0){
                $map = array();
                $map['id'] = $getdata['id'];
                $model->where($map)->save($save);
            }else{
                $model->add($save);
            }
            $return['code'] = 'success';
            $result = true;
            break;
        }
        if($result){
            $return['status'] = 1;
            $return['data'] = $result;
        }
        return $return;
    }
    public function address_list($getdata){
        $return=array('status'=>2,'msg'=>'未获取到数据');
        if(empty($getdata['uid']))return $return;
        $model = M('MemberAddress');
        if($getdata['id']){
            if($getdata['code']=="del"){
                $map = array();
                $map['id'] = $getdata['id'];
                $model->where($map)->delete();
            }else if($getdata['code']=="change"){
                $map = array();
                $map['uid'] = $getdata['uid'];
                $del['status'] = 0;
                $model->where($map)->save($del);
                $map = array();
                $map['id'] = $getdata['id'];
                $save['status'] = 1;
                $model->where($map)->save($save);
            }
        }
        $map = array();
        $map['uid'] = $getdata['uid'];
        $list =D('Common/Member')->get_address_list($map);
        if($list){
            $return['status'] = 1;
            $return['data'] = $list['list'];
        }
        return $return;
    }
    public function mymoney($getdata){
        $map=array('id'=>$getdata['uid']);
        $user_info=M('Member')->where($map)->field('money,frozen_money')->find();
        $integral_list=D('Common/Member')->integral();
        $map = array('uid'=>$getdata['uid'],'coupons_type'=>1);
        $map['status'] = 0;
        $map['end_timestamp'] = array(array('egt', NOW_TIME));
        $get_coupons=M('CouponsGet')->where($map)->field('id')->count();
        $return=array(
            'status'=>1,
            'data'=>array(
                'money'=>$user_info['money'].'元',
                'frozen_money'=>$user_info['frozen_money'].'元',
                'get_coupons'=>$get_coupons.'张',
                'allpoints'=>$integral_list['allpoints'],
            )
        );
        return $return;
    }
    public function saveuser($param){
        $return = array('status'=>3);
        $savedata=$param;
        if($param['username']){
            $where = array();
            $where['id']=array('neq',$param['uid']);
            $where['username']=$param['username'];
            $count=M('Member')->where($where)->count('id');
            if($count>0){
                $return['msg']='会员名已存在，请更换会员名';
                return $return;
            }
            $savedata['username'] = $param['username'];
        }
        $regular= new \Common\Vendor\Regular();
        if(!empty($param['email']) && !$regular->isEmail($param['email'])){
            $return['msg']='邮箱格式不正确';
            return $return;
            $savedata['email'] = $param['email'];
        }
        if(!empty($param['idcard']) && !$regular->idCard($param['idcard'])){
            $return['msg']='身份证格式不正确';
            return $return;
        }
        $savedata['updatetime']=NOW_TIME;
        $map = array();
        $map['id'] = $param['uid'];
        $result=M('Member')->where($map)->save($savedata);
        $return['status'] = 1;
        $return['code'] = 'save';
        $return['data'] = $result;
        return $return;
    }
    /**
     * 支付密码
     */
    public function get_paypwd($getdata){
        $return=array('status'=>3,'msg'=>'参数错误');
        if(empty($getdata['uid'])){return $return;}
        $map=array('id'=>$getdata['uid']);
        $info=M('Member')->where($map)->field(true)->find();
        if(empty($info)){$return['msg']='会员不存在！'; return $return;}
        if(!$getdata['types'])return $return;
        $result = array();
        switch($getdata['types']){
            case 1://检查是否设置了支付密码
                if($info['paypassword']){$result['is_set']=1;}
                else{ $result['is_set']=0;}
                break;
            case 2://保存支付密码
                $model = M('Member');
                $save['updatetime'] = NOW_TIME;
                $regular= new \Common\Vendor\Regular();
                if(!$regular->isEmpty($getdata['paypassword']) || !$regular->isEmpty($getdata['newpaypassword'])){
                    $return['msg']="必填项不能为空";return $return;
                }
                if($info['paypassword']){
                    if($info['salt']){$password=creatpwd($getdata['paypassword'],'HANCHAO'.$info['salt']);}
                    if ($password != $info['paypassword']){
                        $return['msg']='旧密码输入错误';return $return;
                    }
                }
                if($getdata['newpaypassword'] == $getdata['paypassword']){
                    $return['msg']='新密码和旧密码一样，请重新输入';return $return;
                }
                if(!$regular->passWord(6,16,$getdata['newpaypassword'])){
                    $return['msg']='密码长度应为6-16位，可包含数字、字母、特殊符号';return $return;
                }
                if ($getdata['newpaypassword'] != $getdata['repaypassword']) {
                    $return['msg']='两次输入密码不一致';return $return;
                }
                if(empty($info['salt'])){$info['salt']=randStr();}
                $save['paypassword'] = creatpwd($getdata['newpaypassword'],'HANCHAO'.$info['salt']);
                $save['salt'] = $info['salt'];
                $map = array();
                $map['id'] = $getdata['uid'];
                $res=$model->where($map)->save($save);
                if($res){
                    $return['code'] = 'success';
                    $result = true;
                }else{
                    $return['msg']='操作失败'; return $return;
                }
                break;
        }
        if($result){
            $return['status'] = 1;
            $return['data'] = $result;
        }
        return $return;
    }
    /**
     * 领取会员卡
     */
    public function get_card_sn($param){
        $return = array('status'=>3,'msg'=>'缺少必要参数');
        $Weixin = new \Common\Vendor\WeixinCard();
        $wxcard = $Weixin->get_wxsmall_param();
        $result = D('Wxsmall/Login')->datacrypt($param);
        if($result['status']==1){
            //判断该会员是否有手机号
            if(empty($result['data']['phoneNumber']))return false;
            $map = array();
            $map['mobile'] =  $result['data']['phoneNumber'];
            $map['wx_unionid'] = '';
            $map['status'] = 1;
            $old_info = M('Member')->where($map)->field(true)->find();
           if($old_info){
               $return['status']=1;
                $map=$ol_data=array();
                $map['id']=$param['uid'];
                $member_info=M("Member")->where($map)->field(true)->find();
                $ol_data['username']=$member_info['username'];
                $ol_data['nickname']=$member_info['nickname'];
                $ol_data['wxid']=$member_info['wxid'];
                $ol_data['wxsmall_openid']=$member_info['wxsmall_openid'];
                $ol_data['wxsmall_session_key']=$member_info['wxsmall_session_key'];
                $ol_data['wx_unionid']=$member_info['wx_unionid'];
                $ol_data['header_img']=$member_info['header_img'];
                $ol_data['code']=$member_info['code'];
                $ol_data['mobile']=$old_info;
                $ol_data['usepoints'] = $old_info['usepoints']+$member_info['usepoints'];
                $map=array();
               if(empty($result['data']['phoneNumber']))return false;
                $map['mobile'] =  $result['data']['phoneNumber'];
                $map['wx_unionid'] = '';
                $map['status'] = 1;
                $map['id']=array('neq',$param['uid']);
                M("Member")->where($map)->save($ol_data);//修改旧数据
                //作废新数据
                $map=$ndata=array();
                $map['id']=$param['uid'];
                $ndata['status']=0;
               $ndata['status'] = 0;
               $ndata['usepoints'] = 0;
               $ndata['wxid'] =$ndata['mobile'] = $ndata['wxsmall_openid'] =$ndata['wxsmall_session_key'] = $ndata['wx_unionid'] =$ndata['code'] ='';
               M("Member")->where($map)->save($ndata);
               //修改oauth_member
               $map = array();$map['uid'] =  $param['uid'];
               $save = array();$save['uid'] = $old_info['id'];
               M('AccountLog')->where($map)->save($save);//修改日志表
               $map = array();$map['lock_to_id'] =  $param['uid'];
               $save = array();$save['lock_to_id'] = $old_info['id'];
               M('OauthMember')->where($map)->save($save);//修改第三方登录会员
            }else {
                $map = $savedata = array();
                $map['id'] = $param['uid'];
                $savedata['mobile'] = $result['data']['phoneNumber'];
                $savedata['is_tmp'] = 0;
                M('Member')->where($map)->save($savedata);
         }
        }else{
            return $return;
        }
        // if($wxcard['status']==1){
        //     $return['status'] = 1;
        //     $result = explode('&&',$wxcard['data']);
        //     $result = explode('&',$result[1]);
        //     $arr = $param = array();
        //     foreach($result as $val){
        //         $arr = explode('=',$val);
        //         $param[$arr[0]] = urldecode(strtr($arr[1],array("#wechat_redirect"=>"")));
        //     }
        //     $return['data'] = $param;
        //     unset($return['msg']);
        // }
        return $wxcard;
    }
    /**
     * 存储会员卡code（便于下次激活使用）
     */
    public function save_card_code($param){
        $return = array('status'=>3,'msg'=>'缺少必要参数');
        if(empty($param['code'])){
            return $return;
        }
        $Weixin = new \Common\Vendor\WeixinCard();
        $result = $Weixin->card_code_decrypt($param['code']);
        if($result['status']==1){
            $map = $save = array();
            $map['id'] = $param['uid'];
            $save['code'] = $result['code'];
            $code = M('Member')->where($map)->save($save);
            if($code){
                $return['status'] = 1;
                $return['msg'] = '更新成功';
            }
        }
        return $return;
    }
    /**
	 * 省市县三级联动数据
	 */
    public function linkagearea(){
        $return['status'] = 3;
        $region = get_arearegion_cache(2);
        $region = array_values($region[1]);
        $area = get_arearegion_cache(0);
        if(!$region||!$area){
            $this->ajaxReturn($return);
        }
        $return['status'] = 1;
        $return['data']['region'] = $region;
        $return['data']['area'] = $area;
        return $return;
    }
    /**
	 * 签到
     * @param 会员信息
	 */
    public function to_sign($param){
        $return = array('status' => 3,'msg'=>'今天已经签到过了！');
        if(empty($param['uid'])){
            return $return;
        }
        $result = give_login_point($param['uid']);
        if($result['status']==1){
            save_sign_member(1,$param['uid']);
            $return['status'] = 1;
            $return['data'] = $result['usepoints'];
            unset($return['msg']);
        }
        return $return;
    }

    /**我的收藏
     * @param $param
     * @return array
     */
    public function my_collect($param){
        $return = array('status' =>2,'msg'=>'暂无数据！');
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $p=empty($param['page'])?1:$param['page'];
        $map=array();
        $map['a.status']=1;
        $map['a.uid']=$param['uid'];
        $field='g.*,a.*';
        $order="a.id desc";
        $join=" LEFT JOIN __GOODS__ g ON a.goods_id=g.id ";
        $collectgoods=page_list('GoodsCollect',$map,$order,$field,$pagesize,$join,$p);
        if($collectgoods['list']){
            foreach($collectgoods['list'] as $key=>$val){
                $collectgoods['list'][$key]['img']=thumb($val['img']);
                $collectgoods['list'][$key]['shop_price']=format_price($val['shop_price']);
            }
            $return['status']=1;
            $return['data']=$collectgoods['list'];
            return $return;
        }
        return $return;
    }

    /**删除收藏
     * @param $param
     * @return array
     */
    public function del_collect($param){
        $return=array('status'=>3);
        if(empty($param) || empty($param['id']) || empty($param['uid'])){
            $return['msg']='参数错误';
            return $return;
        }
        $map=array();
        $map['id']=$param['id'];
        $map['uid']=$param['uid'];
        $info=M("GoodsCollect")->where($map)->delete();
        if($info){
            $return['status']=1;
            $return['msg']='删除成功';
            return $return;
        }else{
            $return['msg']='删除失败';
            return $return;
        }
        return $return;
    }

    /**
     * 推荐商品
     */
    public function get_bestgoods_list($param){
        $return = array('status' =>2,'msg'=>'暂无数据！');
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $p=empty($param['page'])?1:$param['page'];
        $map=array();
        $map['is_internet'] = 1;
        $map['status'] = 1;
        $map['is_delete'] = 0;
        $map['is_offline'] = 1;
        $order = 'is_best desc,sort desc,id desc';
        $filed='id,goods_name,img,shop_price,is_promote,promote_price,goods_max_number,goods_number,promote_id,promote_end_time,promote_start_time,pt_id,cat_id,market_price';
        $collectgoods=page_list('Goods',$map,$order,$filed,$pagesize,'',$p);
        if($collectgoods['list']){
           $list=D('Wxsmall/Takeout')->format_data($collectgoods['list'],2,'',$param,$param['rank_id']);
            $return['status']=1;
            $return['data']=$list;
            return $return;
        }
        return $return;
    }
    /*
    * 签到
    */
    public function member_sign($uid){
        $return = array('status' =>1,'msg'=>'缺少参数！');
        if(!$uid){$return['status']=2;return $return;}
        $sign_type = C('SIGN_TYPE');
        // 连续签到天数;今天是否签到过;显示签到天数
        $sum_day = 0;$f_qd = 0;$xs_day = 0;
        $map = array();$map['id']=$uid;
        $map['sign_time']=array('gt',strtotime(date('Y-m-d',NOW_TIME)));
        $is_count=M('Member')->where($map)->count();

        $u_map = array();$u_map['id'] = $uid;
        $m_info = M('Member')->where($u_map)->field('sign_day_num,sign_time,usepoints')->find();
        $sign_info = json_decode(C('SIGN_INFO'),true);
        $today = strtotime(date('Y-m-d',NOW_TIME));
        if($is_count){
            $f_qd = 1;
            $sum_day = $m_info['sign_day_num'];
        }else{
            $f_qd = 0;
            $ys_day_start = strtotime(date('Y-m-d',strtotime('-1 day')));
            $ys_day_end = $ys_day_start+86400-1;
            if($m_info['sign_time']>$ys_day_start && $m_info['sign_time']<$ys_day_end){
                $sum_day = $m_info['sign_day_num'];
            }else{
                $sum_day = 0;
                $m_info['sign_day_num'] = 0;
            }
        }
        $now = empty($sum_day)?0:intval($sum_day);
        $sign_info_day = getmiddlenum($now);
        $return['data']['m_info']=$m_info;
        $return['data']['sign_info_day']=$sign_info_day;
        $return['data']['f_qd']=$f_qd;
        return $return;
    }
    /*
    * 去签到
    */
    public function goto_sign($param){
        $return = array('status' =>1,'msg'=>'缺少参数！');
        $map = array();$map['id']=$param['uid'];
        $map['sign_time']=array('gt',strtotime(date('Y-m-d',NOW_TIME)));
        $count=M('Member')->where($map)->count();

        $u_map = array();$u_map['id'] = $param['uid'];
        $m_info = M('Member')->where($u_map)->field('sign_day_num,sign_time,usepoints')->find();
        if($count){
            $return['status'] = 2;
            $return['is_day'] = $m_info['sign_day_num'];
            $return['all_points'] = $m_info['usepoints'];
            return $return;
        }else{
            if($m_info['sign_day_num'] > 0){
                $ys_day_start = strtotime(date('Y-m-d',strtotime('-1 day')));
                $ys_day_end = $ys_day_start+86400-1;
                if($m_info['sign_time']>$ys_day_start && $m_info['sign_time']<$ys_day_end){
                    $sum_day = $m_info['sign_day_num']+1;
                }else{
                    $sum_day = 1;
                }
            }else{
                $sum_day = 1;
            }
            $return = save_sign($sum_day,$param['uid']);
            return $return;
        }
        return $return;
    }
    public function save_user_mobile($param){
        $return = array('status'=>3,'msg'=>'缺少必要参数');
        /*if(empty($param['tiaokuan'])){
            $return['msg']="请查看用户服务协议并选择";
            return $return;
        }*/
        $res=D("Common/Login")->check_member_info($param);
        if($res['status']==3){
            return $res;
        }
        $result = D('Wxsmall/Login')->datacrypt($param);
        if($result['status']==1){
            //判断该会员是否有手机号
            if(empty($result['data']['phoneNumber']))return false;
            $map = array();
            $map['mobile'] =  $result['data']['phoneNumber'];
            $map['wx_unionid'] = '';
            $map['status'] = 1;
            $old_info = M('Member')->where($map)->field(true)->find();
            if($old_info){
                $return['status']=1;
                $map=$ol_data=array();
                $map['id']=$param['uid'];
                $member_info=M("Member")->where($map)->field(true)->find();
                $ol_data['username']=$member_info['username'];
                $ol_data['nickname']=$member_info['nickname'];
                if($member_info['wxid']) $ol_data['wxid']=$member_info['wxid'];
                if($member_info['wxsmall_openid']) $ol_data['wxsmall_openid']=$member_info['wxsmall_openid'];
                if($member_info['wxsmall_session_key']) $ol_data['wxsmall_session_key']=$member_info['wxsmall_session_key'];
                if($member_info['wx_unionid']) $ol_data['wx_unionid']=$member_info['wx_unionid'];
                $ol_data['header_img']=$member_info['header_img'];
                $ol_data['code']=$member_info['code'];
                $ol_data['mobile']=$old_info;
                $ol_data['usepoints'] = $old_info['usepoints']+$member_info['usepoints'];
                $map=array();
                if(empty($result['data']['phoneNumber']))return false;
                $map['mobile'] =  $result['data']['phoneNumber'];
                $map['wx_unionid'] = '';
                $map['status'] = 1;
                $map['id']=array('neq',$param['uid']);
                M("Member")->where($map)->save($ol_data);//修改旧数据
                //作废新数据
                $map=$ndata=array();
                $map['id']=$param['uid'];
                $ndata['status']=0;
                $ndata['status'] = 0;
                $ndata['usepoints'] = 0;
                $ndata['wxid'] =$ndata['mobile'] = $ndata['wxsmall_openid'] =$ndata['wxsmall_session_key'] = $ndata['wx_unionid'] =$ndata['code'] ='';
                M("Member")->where($map)->save($ndata);
                //修改oauth_member
                $map = array();$map['uid'] =  $param['uid'];
                $save = array();$save['uid'] = $old_info['id'];
                M('AccountLog')->where($map)->save($save);//修改日志表
                $map = array();$map['lock_to_id'] =  $param['uid'];
                $save = array();$save['lock_to_id'] = $old_info['id'];
                $res=M('OauthMember')->where($map)->save($save);//修改第三方登录会员
            }else {
                $map = $savedata = array();
                $map['id'] = $param['uid'];
                $savedata['mobile'] = $result['data']['phoneNumber'];
                $savedata['is_tmp'] = 0;
                $res= M('Member')->where($map)->save($savedata);
                $parent_id = idtoname($param['uid'],'Member','parent_id');
                if($parent_id)TooKeenAward('',1,'register',$parent_id);
            }
            if($res){
                $return['status']=1;
                $return['data']['mobile']=$result['data']['phoneNumber'];
                return $return;
            }
        }else{
            return $return;
        }
    }
    public function save_user_mobile_input($param){
        $return = array('status'=>3,'msg'=>'缺少必要参数');
        if(empty($param['id'])){return $return;}
        if(empty($param['mobilecode'])){
            $return['msg']="请输入短信验证码";
            return $return;
        }
        if(empty($param['tiaokuan'])){
            $return['msg']="请查看用户服务协议并选择";
            return $return;
        }
        if(empty($param['mobile'])){
            $return['msg']="请输入手机号";
            return $return;
        }
        $reg = '';
        $m_num = strlen($param['mobile']);
        if($m_num == 11) $reg = '/^0?1[3|4|5|6|7|8|9]\d{9}$/';
        elseif($m_num == 10) $reg = '/^(\+?61|0)4\d{8}$/';
        else{
            $return['msg']='手机号格式不正确';return $return;
        }
        $chaeck_m = preg_match($reg,$param['mobile']);
        if(!$chaeck_m){
            $return['msg']='手机号格式不正确';return $return;
        }
        $is_true = check_verify_code($param['mobile'],$param['mobilecode'],0);
        if($is_true == false){$return['msg']='短信验证码错误，请重新获取!';return $return;}
        $Weixin = new \Common\Vendor\WeixinCard();
        $wxcard = $Weixin->get_wxsmall_param();
        $map = array();
        $map['mobile'] =  $param['mobile'];
        $map['wx_unionid'] = '';
        $map['status'] = 1;
        $old_info = M('Member')->where($map)->field(true)->find();
        if($old_info){
            $return['status']=1;
            $map=$ol_data=array();
            $map['id']=$param['uid'];
            $member_info=M("Member")->where($map)->field(true)->find();
            $ol_data['username']=$member_info['username'];
            $ol_data['nickname']=$member_info['nickname'];
            $ol_data['wxid']=$member_info['wxid'];
            $ol_data['wxsmall_openid']=$member_info['wxsmall_openid'];
            $ol_data['wxsmall_session_key']=$member_info['wxsmall_session_key'];
            $ol_data['wx_unionid']=$member_info['wx_unionid'];
            $ol_data['header_img']=$member_info['header_img'];
            $ol_data['code']=$member_info['code'];
            $ol_data['mobile']=$old_info;
            $ol_data['usepoints'] = $old_info['usepoints']+$member_info['usepoints'];
            $map=array();
            if(empty($param['mobile']))return false;
            $map['mobile'] =  $param['mobile'];
            $map['wx_unionid'] = '';
            $map['status'] = 1;
            $map['id']=array('neq',$param['uid']);
            M("Member")->where($map)->save($ol_data);//修改旧数据
            //作废新数据
            $map=$ndata=array();
            $map['id']=$param['uid'];
            $ndata['status']=0;
            $ndata['status'] = 0;
            $ndata['usepoints'] = 0;
            $ndata['wxid'] =$ndata['mobile'] = $ndata['wxsmall_openid'] =$ndata['wxsmall_session_key'] = $ndata['wx_unionid'] =$ndata['code'] ='';
            M("Member")->where($map)->save($ndata);
            //修改oauth_member
            $map = array();$map['uid'] =  $param['uid'];
            $save = array();$save['uid'] = $old_info['id'];
            M('AccountLog')->where($map)->save($save);//修改日志表
            $map = array();$map['lock_to_id'] =  $param['uid'];
            $save = array();$save['lock_to_id'] = $old_info['id'];
            $res=M('OauthMember')->where($map)->save($save);//修改第三方登录会员
        }else {
            $map = $savedata = array();
            $map['id'] = $param['uid'];
            $savedata['mobile'] = $param['mobile'];
            $savedata['is_tmp'] = 0;
            //$savedata['parent_id'] = intval(base64_decode($param['parent_id']));
            $res= M('Member')->where($map)->save($savedata);
            $parent_id = idtoname($param['uid'],'Member','parent_id');
            if($parent_id)TooKeenAward('',1,'register',$parent_id);
        }
        if($res){
            $return['status']=1;
            $return['data']['mobile']=$param['mobile'];
            $return['data']['wxcard']=$wxcard;
            return $return;
        }
    }
    /**
     * 推广海报
     * @param $id 会员 $is_check 是否检测 1不检测 2检测
     * @return mixed
     */
    public function tgercode($id,$poster_id='',$is_type=0){
        $id=intval($id);
        $return =array('status'=>0,'msg'=>'');
        $return['data']=1;
        if(empty($id)){
            $return['msg']="参数错误!";
            return $return;
        }
        $map=array();
        $map['id']=$id;
        $map['status']=1;
        $member_info = M('Member')->where($map)->field('id,username,real_name,mobile,header_img,nickname,wxid')->find();
        if (empty($member_info)) {
            $return['msg']="会员不存在";
            return $return;
        }
        $save_member_header_img=save_member_header_img($id);
        if($save_member_header_img['status']==1){
            $member_info['header_img']=$save_member_header_img['url'];
        }
        $appid=C('APPID');
        if(empty($poster_id))$poster_id=C('POSTER_LIST_ID');
        $poster_conf=array();
        if($poster_id){
            $map_share=array();$map_share['name']='SHARE_POSTER';
            $share_poster = M('Config')->where($map_share)->getField('value');
            $attr_data=unserialize($share_poster);
            if(!empty($attr_data[$poster_id])){
                $poster_conf=$attr_data[$poster_id];
            }else{
                $attr_data = require_once ROOT_PATH."/Public/data/poster_data.php";
                $poster_conf=$attr_data[$poster_id];
            }
        }
        //头像、昵称和二维码的坐标
        // $ewm_xy = '215,59|222,250|230,296';
        $ewm_xy =!empty($poster_conf['param'])?$poster_conf['param']:C('COORDINATE');
        $size=!empty($poster_conf['font_size'])?$poster_conf['font_size']:20;
        $color=!empty($poster_conf['font_color'])?$poster_conf['font_color']:'orange';
        $xyy = array();
        $xy = explode('|',$ewm_xy);
        foreach($xy as $it){$xyy[] = explode(',',$it);}
        unset($xy,$ewm_xy);//原图背景、
        $yuan_img = !empty($poster_conf['background'])?$poster_conf['background']:C('BOTTOM_IMG');if(empty($yuan_img))return false;

        $old_cache_f='';
        //生成微信推广二维码
        $sp =!empty($poster_conf['permanent'])?$poster_conf['permanent']: intval(C('PERMANENT'));if(empty($sp))$sp=0;
        $erwei =!empty($poster_conf['erweima'])?$poster_conf['erweima']: intval(C('ERWEIMA'));if(empty($erwei))$erwei=150;
        if(C('WX_TYPE')==4){
            if($is_type==1){
                $invite_key = base64_encode($id);
                $param=array();$param['width']=120;
                $param['type']=1;
                $param['path'] = "pages/index/index?invite_key=".$invite_key."&uid=".$id."&eid=".$id;//小程序参数
                $wxsmall = new \Common\Vendor\Wxsmallopen;
                $result=$wxsmall->get_location_small_code($param);
                preg_match('/(.*)base64,(.*)/', $result, $matches);
                $base64 = $matches['2'];
                $img = base64_decode($base64);
                $old_cache_f= ROOT_PATH."/uploads/".date("YmdHis")."wx_img.jpg";
                if(!empty($img)){@file_put_contents($old_cache_f,$img);}
            }else{
                $weixin = new \Common\Vendor\WeixinChat;
                if(empty($access_token))$access_token=$weixin->getdd();
                if($sp==0){
                    $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';
                }else{
                    $data = '{"expire_seconds": 604800,"action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$id.'}}}';
                }
                $rt=curlget('https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token,$data,"POST",array(),true);
                $json=json_decode($rt);
                $ticket = $json->ticket;
                if($ticket){
                    $str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
                    $img = curlget($str);
                    $old_cache_f= ROOT_PATH."/uploads/image/qrcode/cache/qrcode_".$appid.'_'.$id.".jpg";//微信二维码
                    if(!empty($img)){@file_put_contents($old_cache_f,$img);}
                }
            }

        }else{
            $url = U('Mobile/Login/register',array('eid'=>$id),true,true);
            $old_cache_f = phpgrcode(urlencode($url),'',10,'wximg_'.$id);//二维码 缓存生成之后的图
        }

        $haibao= new \Common\Vendor\Haibao;
        $params=array();//二维码写入到背景图片中
        $params[]=array('path_logo'=>format_url($old_cache_f),'dst_x'=>$xyy[2][0],'dst_y'=>$xyy[2][1],'imgWidth'=>$erwei,'imgHeight'=>$erwei);
        $touxiang =!empty($poster_conf['touxiang'])?$poster_conf['touxiang']:intval(C('TOUXIANG'));if(empty($touxiang))$touxiang=150;
        if($member_info['header_img']){
            $w_img = $member_info['header_img'];//头像写入到背景图片中
            $params[]=array('path_logo'=>format_url($w_img),'dst_x'=>$xyy[0][0],'dst_y'=>$xyy[0][1],'imgWidth'=>$touxiang,'imgHeight'=>$touxiang);
        }
        $nickname=!empty($member_info['nickname'])?$member_info['nickname']:$member_info['username'];
        $nickname=trim($nickname);
        if(empty($nickname))$nickname=trim($member_info['real_name']);
        if(empty($nickname))$nickname=trim($member_info['mobile']);
        $encode = mb_detect_encoding($nickname,array("ASCII","UTF-8","GB2312","GBK","BIG5"));
        if ($encode != "UTF-8"){$nickname = iconv($encode,"UTF-8",$nickname);}
        if(empty($nickname))$nickname=trim($member_info['mobile']);
        if($xyy[1][0]=='center'){
            $length= $haibao->checkStrlength($nickname,$size);
            $imgwidth=$xyy[1][2]?$xyy[1][2]:827;
            $xyy[1][0]= floor(($imgwidth - $length)/2);
        }
        $result=false;
        if(empty($poster_conf['ready_str']) || $poster_conf['ready_str']['ready_status']==0){
            $params[]=array('str'=>$nickname,'posX'=>$xyy[1][0],'poxY'=>$xyy[1][1],'size'=>$size,'color'=>$color);
        }else{
            $params[]=array('str'=>$nickname,'posX'=>$xyy[1][0],'poxY'=>$xyy[1][1],'size'=>$size,'color'=>$color);

            $ready_str=$poster_conf['ready_str'];
            $xyy[3]=explode(',',$ready_str['ready_xy']);
            $nickname=get_toker_ready_name($ready_str['ready_id'],$id,1);
            $size=$ready_str['ready_size']?$ready_str['ready_size']:20;
            if($xyy[3][0]=='center'){
                $length= $haibao->checkStrlength($nickname,$size);
                $imgwidth=$xyy[1][2]?$xyy[1][2]:827;
                $xyy[3][0]= floor(($imgwidth - $length)/2);
            }
            $params[]=array('str'=>$nickname,'posX'=>$xyy[3][0],'poxY'=>$xyy[3][1],'size'=>$size,'color'=>$ready_str['ready_colors']?$ready_str['ready_colors']:'orange');
        }
        $poster=array (
            'background' => $yuan_img,
        );
        $return['status']=1;
        $return['data']=$haibao->format_mini($params,$poster);
        return $return;
    }
}
