<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Uapp\Controller;
class MemberController extends BaseController {
	 /**
     * 会员中心首页
     */
    public function index(){
    	$return=array('status'=>1);
    	$getdata = I('get.');
        $result = D('Wxsmall/Member')->get_index($getdata);
        if($result['info']){
            if(str_exists($user_info[''],'http://')==false){
                $result['info']['header_img']=thumb($result['info']['header_img'],100,100);
            }
        }
        $this->ajaxReturn($result);
    }
    /**
     * 余额明细
     */
    public function balance(){
        $getdata = I('get.');
        $result = D('Wxsmall/Member')->balance_list($getdata);
        $grade_info=getRankMoney($getdata['uid']);
        $map=array();
        $map['id']=$getdata['uid'];
        $member_info=M("Member")->where($map)->field(true)->find();
        $result['member_info']=$member_info;
        $result['grade_info']=$grade_info;
        $this->ajaxReturn($result);
    }
    /*s
    * 我的订单
    */
    public function myorder_list(){
        $getdata = I('param.');
        $result = D('Wxsmall/Member')->myorder_list($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的订单详情
     */
    public function myorder_info(){
        $getdata = I('param.');
        $result = D('Wxsmall/Member')->myorder_info($getdata);
        if($result['status'] == 1){            
            if($result['data']['s_money'] < 0)$result['data']['s_money']=1-$result['data']['s_money']-1;
        }
        $this->ajaxReturn($result);
    }
    /**
     * 我的收藏
     */
    public function my_collect(){
        $param = I('param.');
        $result = D('Wxsmall/Member')->my_collect($param);
        $this->ajaxReturn($result);
    }
    /**
     * 删除我的收藏
     */
    public function del_collect(){
        $param = I('param.');
        $result = D('Wxsmall/Member')->del_collect($param);
        $this->ajaxReturn($result);
    }
    /**
     * 修改资料
     */
    // public function get_user(){
    //     $return=array('status'=>1,'msg'=>'');
    //     $uid=I('uid');
    //     $map['id']=$uid;
    //     $user_info=M("Member")->where($map)->field('nickname,username,email,header_img,password,salt')->find();
    //     if($user_info){
    //         $return['data']=$user_info;
    //         if(str_exists($user_info[''],'http://')==false){
    //             $return['data']['header_img']=thumb($user_info['header_img'],100,100);
    //         }
    //     }
    //     $this->ajaxReturn($return);
    // }
    /**
     * 修改会员信息(登录密码、支付密码、会员信息)
     */
    // public function update_login_pwd(){
    //     $return=array('status'=>1,'msg'=>'');
    //     $postdata=I('post.');
    //     $data=json_decode(htmlspecialchars_decode($postdata['info']),true);
    //    // $return['data']=$data;
    //     $data['infos']=$postdata['infos'];
    
    //     $info= D('Common/Member')->user_infor($data);

    //     if($info['status']==1){
    //         $return['msg']=$info['msg'];
    //     }elseif($info['status']==0){
    //         $return['status']=3;
    //         $return['msg']=$info['msg'];
    //     }
    //     $this->ajaxReturn($return);
    // }
    /**
     * 修改会员资料
     */
    public function saveuser(){
        $getdata = I('post.');
        $result = D('Wxsmall/Member')->saveuser($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 支付密码
     */
    public function get_paypwd(){
        $getdata = I('param.');
        $result = D('Wxsmall/Member')->get_paypwd($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的收货地址列表
     */
    public function address_list(){
        $getdata = I('param.');
        $result = D('Wxsmall/Member')->address_list($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的资金
     */
    public function mymoney(){
        $getdata = I('get.');
        $result = D('Wxsmall/Member')->mymoney($getdata);
        $this->ajaxReturn($result);
    }
    //end
    /**
     * 我的积分
     */
    public function integral(){
        $return=array('status'=>1);
        $this->account_status=strtoarray(C('ACCOUNT_STATUS'));
        $param=I('param');
        $integral_list=D('Common/Member')->integral($param);
        //print_r($integral_list);
        if($integral_list){
            if($integral_list['points_details']['list']){
                foreach($integral_list['points_details']['list'] as $key=>$val){
                    if(intval($val['status'])==15)$val['status']=5;
                    $integral_list['points_details']['list'][$key]['account_status']=$this->account_status[$val['status']];
                    $integral_list['points_details']['list'][$key]['change_time']=time_format($val['change_time']);
                }
                $return['data']['list']=$integral_list['points_details']['list'];
            }else{
                $return['status']=2;
                $return['msg']='没有更多数据';
            }
            //$return['data']['allpoints']=$integral_list['allpoints'];
        }else{
            $return['status']=2;
            $return['msg']='没有更多数据';
        }
        $this->ajaxReturn($return);
    }
    /**
     * 变更详情
     */
    public function money_change_dec(){
        $param=array();
        $id=I('id');
        $param['id']=$id;
        $data=D("Wxsmall/Member")->money_change_desc($param);
        $this->ajaxReturn($data);
    }
    /**
     * 我的卡券
     */
    public function mycoupon_list(){
        $getdata = I('get.');
        $result = D('Wxsmall/Member')->mycoupon_list($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的卡券内容页
     */
    public function mycoupon_cont(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Wxsmall/Member')->mycoupon_cont($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 拼团订单
     */
    public function get_order_group(){
        $getdata = I('param.');
        $result = D('Wxsmall/Member')->myorder_list($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的签到
     */
    public function my_sign(){
        $return=array('status'=>1);
        $param = I('param.');
        $uid=$param['uid'];
        $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']['sign_info_day']=$sign_info_day;
        $return['data']['m_info']=$m_info; 
        $return['data']['f_qd']=$f_qd;
        $return['data']['is_mobile']=1;   
        $this->ajaxReturn($return);
    }
    /**
     * 获取推荐商品
     */
    public function get_bestgoods_list(){
        $param=I('param');
        $return=D("Wxsmall/Member")->get_bestgoods_list($param);
        $this->ajaxReturn($return);
    }
    /**
     * 会员签到
     */
    public function to_sign(){
        $param = I('param.');
        $return=array("msg"=>"您已签到");
        $return=D("Wxsmall/Member")->goto_sign($param);
        $this->ajaxReturn($return);
    }
    /**
     * 签到记录
     */
    public function sign_log(){
        $param=array();
        $return=array("status"=>2,'msg'=>'没有更多数据了');
        $uid=I('uid');
        $map=array();
        $map['user_id'] = $uid;$map['win_type'] = 5;
        $pagesize=empty($pagesize)?C('WEB_GOODS_PAGE'):$pagesize;
        $p=empty($p)?1:$p;
        $order = 'id desc';
        $gift_list=page_list('WinGift',$map,$order,true,$pagesize,'',$p);
        if(empty($gift_list['list']))$this->ajaxReturn($return);
        foreach($gift_list['list'] as $key=>$val){
            $gift_list['list'][$key]['change_type']="+";
            $gift_list['list'][$key]['change_num']=$val['gift_number'];
            $gift_list['list'][$key]['change_time']=time_format($val['add_time']);
            $gift_list['list'][$key]['change_desc']="完成签到得".$val['gift_name'];
            $gift_list['list'][$key]['account_status']="签到";
        }
        $return['status']=1;
        $return['data']=$gift_list['list'];
        $this->ajaxReturn($return);
    }
    /**
     * 签到规则
     */
    public function sign_rule(){
        $return=array('status'=>1);
        $sign_rule = C('SIGN_RULE');
        $return['data']=h($sign_rule);
        $this->ajaxReturn($return);
    }
     /**
     * 我的地址
     */
    public function address(){
        $getdata = I('param.');
        $result = D('Wxsmall/Member')->address($getdata);
        if($result['data']['region']){
            foreach ($result['data']['region'] as $key => $value) {
                $region[$key]['text']=$value['n'];
                $region[$key]['value']=$value['i'];
            }
            $result['data']['region']=$region;
        }
        // if($result['data']['area']){
        //     // $region = json_decode(htmlspecialchars_decode($result['data']['region'],true));
        //     foreach ($result['data']['area'] as $key => $value) {
        //         $area[$key]['text']=$value['n'];
        //         $area[$key]['value']=$value['i'];
        //     }
        //     $result['data']['area']=$area;
        // }
        $this->ajaxReturn($result);
    }
    /**
     * 充值
     */
    public function rechange(){
        $return=array('status'=>1);
        $uid=I('uid');
        $map=array();
        $map['id']=$uid;
        $member_info=M("Member")->where($map)->field(true)->find();
        $return['data']['member_info']=$member_info;
        $recharge_info = C('FIXED_RECHARGE');
        if($recharge_info){
            $recharge_arr = explode(',',$recharge_info);
            $return['data']['recharge_arr']=$recharge_arr;
        }
        $map=array();
        $map['consumption_type'] = array('in','1,3');
        $list = $this->lists('ManypointsRule',$map);
        $return['data']['list']=$list;
        // 支付方式
        $payment_info=getpayment();
        $return['data']['payment_info'] = $payment_info;
        $this->ajaxReturn($return);
    }
    /**
     * 提交充值
     */
    public function sub_recharge(){
        $return=array('status'=>1);
        $postdata=I('param.');
        $result=D('Common/Withdrawals')->recharge($postdata);
        if($result['status']==1){
            if($result['log_id']) {
                $where['id'] = $result['log_id'];
                $where['is_paid'] = array('neq', 1);
                $pay_info = M('PayLog')->where($where)->field(true)->find();
                $return['data']['order_sn'] = $pay_info['order_sn'];
                $return['data']['money'] = $pay_info['need_pay'];
                $return['data']['pay_code'] = $pay_info['pay_code'];
                
                $return['data']['signstring']='';
                $paycode=strtolower($pay_info['pay_code']);
                $map=array();$pay_code=$pay_info['pay_code'];
                $map['code']=$pay_code;
                $payconfig=M('Payment')->where($map)->field(true)->find();
                $pay = new \Common\Vendor\Pay($pay_code,$payconfig);
                $vo = new \Common\Vendor\Pay\PayVo();
                $vo->setBody($title_name)
                        ->setFee($pay_info['need_pay'])
                        ->setTitle('支付:'.$pay_info['order_sn'])
                        ->setOrderNo($pay_info['order_sn'])
                        ->setParam($payconfig);
                $RequestForm=$pay->buildRequestForm($vo);
                $return['data']['signstring']=$RequestForm;
            }else{
                $return['status']=3;
                $return['msg']="参数错误";
            }
        }else{
            $return['status']=3;
            $return['msg']=$result['info'];
        }
        // $result['msg']=$result['info'];
        $this->ajaxReturn($return);
    }
    /**
     *判断收货地址是否在配货区域
     */
    public function distance()
    {
        $id=I('address_id');
        $shop_id=get_wxsmall_shop_id();
        if($id){
            $map = array();
            $map['id']=$id;
            $memberaddress_info = M('MemberAddress')->where($map)->field(true)->find();
            if($memberaddress_info){
                $member_info['province'] = $memberaddress_info['province'];
                $member_info['city_id'] = $memberaddress_info['city_id'];
                $member_info['district'] = $memberaddress_info['district'];
                $member_info['temp_lng'] = $memberaddress_info['xpoint'];
                $member_info['temp_lat'] = $memberaddress_info['ypoint'];
                $member_info['temp_name'] = $memberaddress_info['address'];
                $member_info['city'] = idtoname($memberaddress_info['city'],'Area','name');
                $data['siteinfo']=$member_info;
            }
        }

        if($id && $shop_id){
            $map = array();
            $shopsxy=idtoname($shop_id,'Shops',"xpoint,ypoint");
            $map['id']=$id;
            $v=M('MemberAddress')->where($map)->field(true)->find();
            $distribution_area=D('Wxsmall/Login')->judge_shop($v['xpoint'],$v['ypoint'],$shop_id);
            if($distribution_area['status']==3){
                $data['status']=3;
            }else{
                $data['address']=$v;
                $data['status']=1;
            }
        }else{
            $data['status']=3;
        }
        $this->ajaxReturn($data);
    }
    public function invite(){
        $return=array('status'=>1,'msg'=>'');
        $uid=I('uid');
        $token=I('token');
        $page=I('page')?I('page'):1;
        $type=I('type');
        $postdata=I('param.');
        $list=array();
        if($type==1) {
            $data = D('Common/Member')->my_extension($postdata);
            foreach ($data['extension_list']['list'] as $key => $val) {
                $user_name=idtoname($val['uid'],'Member','username;nickname;mobile;real_name','id',false);
                $user_name=str_cutmobile($user_name);
                $list[$key]['title']='';
                if($val['order_sn'])$list[$key]['title']="订单号：".$val['order_sn'];
                // $list[$key]['invite_name']="邀请人：".$val['username']."";
                $list[$key]['invite_name']="邀请人：".$user_name."";
                $list[$key]['time']="注册时间：".$val['addtime']."";
                $list[$key]['status']="状态：".$val['is_separate'];
                $list[$key]['desc']="详情：".$val['desc'];
            }
            $return['comm_money']=$data['comm_money'];
        }elseif($type==2){
            $data = D('Common/Member')->extension_list($postdata);
            foreach ($data['extension_list']['list'] as $key => $val) {
                $map['id']=$val['id'];
                $user_name=idtoname($val['id'],'Member','username;nickname;mobile;real_name');
                $user_name=str_cutmobile($user_name);
                $list[$key]['user_name']=$user_name;
                $list[$key]['count_tui']=count_tui($val['id']);
                $list[$key]['title']="被推荐人: ".$user_name."(".count_tui($val['id']).")";
                $p_map['parent_id']=$val['parent_id'];
                $parent_name=idtoname($val['parent_id'],'Member','username;nickname;mobile');
                $parent_name=str_cutmobile($parent_name);
                $list[$key]['invite_name']="推荐人: ".$parent_name."(".count_tui($val['parent_id']).")";
                $list[$key]['time']="推荐时间：".time_format($val['reg_time']);
                $list[$key]['id']=$val['id'];
                $list[$key]['parent_id']=$val['parent_id'];
            }
        }
        if($list) {            
            $return['data']=$list;
        }else{
            $return['status']=2;
            $return['msg']="未获取到数据";
        }
        $this->ajaxReturn($return);
    }
    public function member_invite(){
        $return=array('status'=>1,'msg'=>'');
        $user_info=$this->checkuserauth();

        $val = unserialize(C('AFFILIATE'));
        $return['data']['config_val']=$val;

        // if($user_info['is_littleshop']==1){
        //     $lshop_id=intval(D('Wxsmall/Login')->get_lshop_id($user_info['id']));
        //     $little_info =M('LittleShop')->where('id='.$lshop_id)->find();
        //     $tui_map['parent_id']=$little_info['id'];
        //     // $tui_map['review_status']=2;
        //     $tui_map['status']=1;
        //     $tui_count = M('LittleShop')->where($tui_map)->count();
        //     $user_info['tui_count'] = $tui_count;
        // }else{
            $tui_map['parent_id']=$user_info['id'];
            $tui_map['is_littleshop']=0;
            $tui_count = M('Member')->where($tui_map)->count();
            $rank_name = idtoname($user_info['rank_id'],'MemberRank','name');
            $user_info['tui_count'] = $tui_count;
        // }
        if(str_exists($user_info['header_img'],'http://')==false){
            $user_info['header_img']=thumb($user_info['header_img'],100,100);
        }
        $integral_list=D('Common/Member')->integral();
        $user_info['allpoints']=intval($integral_list['allpoints']);
        $user_info['rank_name']=idtoname($user_info['rank_id'],'MemberRank','name');
        $return['data']['user_info']=$user_info;

        $uid=I('uid');
        $id=$uid;
        $shopewm=tgercode($id);
        if($shopewm['status'] && $shopewm['url']){
            $wximg_url=$shopewm['url'].'?'.NOW_TIME;
        }else{
            $url = U('Mobile/Login/perfect',array('eid'=>$id),true,true);
            $wximg_url =U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
        }
        $return['data']['wx_img']=$wximg_url;
        $return['data']['img_url']=$wximg_url;
        $share_url=U('Mobile/Extension/member_poster',array('eid'=>$id));
        $return['data']['share_url']=$share_url;
        $this->ajaxReturn($return);
    }
    /**
     * 提现银行信息
     */
    public function getmemberbank(){
        $map['uid']=I('uid');$map['status']=1;
        $bank_info=M('MemberBank')->where($map)->field('name,bankname,bankcode')->find();
        $return['status'] = 1;
        if($bank_info['bankcode']){
            $length=strlen($bank_info['bankcode']);
            if(str_exists('@',$bank_info['bankcode'])){
                $bankcodearr=explode('@',$bank_info['bankcode']);
                $bank_info['bankcode']=substr($bankcodearr[0],0,(strlen($bankcodearr[0])-4)).'****@'.$bankcodearr[1];
            }else{
                $bank_info['bankcode']=substr($bank_info['bankcode'],0,($length-4)).'****';
            }
        }
        if($bank_info){
            $return['data']['bank_info'] = $bank_info;
        }
        $payfree=C('TIXIAN_FREE');
        $open_paypsd = C('OPEN_PAYPSW');
        $open_sms = C('SMS_TX_VERIFY');
        $min_money=C('USER_MIN_GET_MONEY');//会员最低提现金额
        if($payfree<1)$num=$payfree*100;
        $userinfo = idtoname(I('uid'),'Member','money,mobile');
        $return['data']['payfree'] = $payfree;
        $return['data']['open_paypsd'] = $open_paypsd;
        $return['data']['open_sms'] = $open_sms;
        $return['data']['mobile'] = $userinfo['mobile'];
        $return['data']['money'] = $userinfo['money'];
        $return['data']['min_money'] = $min_money;
        $banks=array_unique(explode(',',C('MEMBER_BANK')));
        foreach($banks as $v){$banksarr[$v]=$v;}
        $return['data']['banks'] = $banksarr;
        $this->ajaxReturn($return);
    }
    /**
     * 添加提现银行
     */
    public function add_bank(){
        $postdata=I('post.');
        $result=D('Common/Withdrawals')->add_bank($postdata);
        if($result['status']!=1){
            $result['msg'] = $result['info'];
            $result['status'] = 3;
        }
        $this->ajaxReturn($result);
    }
    /**
     * 提现操作
     */
    public function go_withdrawals(){
        $postdata=I('post.');
        $result=D('Common/Withdrawals')->get_money($postdata);
        $result['msg']=$result['info'];
        if($result['status']!=1){
            $result['status']=3;
        }
        $this->ajaxReturn($result);
    }
    //提现方式
    public function wd_way(){
        $return=array('status'=>1);
        $user_info=$this->checkuserauth();
        $return['data']=$user_info;
        $payfree=C('TIXIAN_FREE');  //提现手续费
        $num=0;
        if($payfree<1)$num=$payfree*100;
        $return['data']['payfree']=$payfree;
        $return['data']['num']=$num;
        $tx_min_money=C('USER_MIN_GET_MONEY'); //最低提现金额
        $banks=array_unique(explode(',',C('MEMBER_BANK')));
        foreach($banks as $key=>$v){$banksarr[$key+1]=$v;}
        $return['data']['banks']=$banksarr;
        $return['data']['min_money']=$tx_min_money;
        $return['data']['is_code']=C('SMS_TX_VERIFY');
        $return['data']['open_sms']=C('SMS_TX_VERIFY');
        $return['data']['user_account_date']=C('USER_ACCOUNT_DATE');
        $return['data']['open_paypsd']=C('OPEN_PAYPSW');
        $this->ajaxReturn($return);
    }
    public function go_c_withdrawals(){
        $postdata=I('param.');
        $result=D('Common/Withdrawals')->get_money($postdata);
        $result['msg']=$result['info'];
        if($result['status']!=1){
            $result['status']=3;
        }
        $this->ajaxReturn($result);
    }
}