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

namespace Wxsmall\Controller;
class MemberController extends BaseController {
    /**
     * 会员中心首页
     */
    public function index(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->get_index($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 余额明细
     */
    public function balance(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('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);
    }

    /**
     * yxn
     * 我的余额记录明细
     */
    public function getmymoney(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->balance_list($getdata);

        //109新加
        if($result['data']){
            $result['member_info'] = idtoname($getdata['uid'],'Member','*','id',false);
            $grade_info = getRankMoney($getdata['uid']);//获取等级差额
            // user_ranks($getdata['uid']);
            $result['grade_info'] = $grade_info;
        }
        //end
        $this->ajaxReturn($result);
    }

    //充值
    public function getrecharge(){
        $return=array('status'=>1);
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $return['user_info'] = $user_info;
        $getdata['uid']=$user_info['id'];
        $m_map = array();$m_map['id'] = $user_info['id'];$m_map['mobile'] = array('neq','');
        $is_m = M('Member')->where($m_map)->count();
        if(empty($is_m)){
            $return['msg'] = '请先完善资料';
            $this->ajaxReturn($return);exit();
        }
        $recharge_info = C('FIXED_RECHARGE');
        if($recharge_info){
            $recharge_arr = explode(',',$recharge_info);
            $return['recharge_arr']=$recharge_arr;
        }
        $map=array();
        $map['consumption_type'] =1;
        $list = $this->lists('ManypointsRule',$map);
        $return['data'] = $list;

        $return['payment_info'] = getpayment(0,1);

        $this->ajaxReturn($return);

    }

    //end
    /**
     * 我的积分
     */
    public function integral(){
        $return=array('status'=>1);
        $this->account_status=strtoarray(C('ACCOUNT_STATUS'));
        $param=I('param');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $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){
                    $integral_list['points_details']['list'][$key]['account_status']=$this->account_status[intval($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;
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $data=D("Member")->money_change_desc($param);
        $this->ajaxReturn($data);
    }

    /**
     * 我的卡券
     */
    public function mycoupon_list(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('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('Member')->mycoupon_cont($getdata);
        $this->ajaxReturn($result);
    }

    /**
     * 我的订单
     */
    public function myorder_list(){
        $getdata = I('param.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->myorder_list($getdata);
        $this->ajaxReturn($result);
    }

    /**
     * 我的订单详情
     */
    public function myorder_info(){
        $getdata = I('param.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->myorder_info($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的爱车
     */
    public function address(){
        $getdata = I('param.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->address($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的收货地址列表
     */
    public function address_list(){
        $getdata = I('param.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->address_list($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 我的资金
     */
    public function mymoney(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->mymoney($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 修改会员资料
     */
    public function saveuser(){
        $getdata = I('post.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->saveuser($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 支付密码
     */
    public function get_paypwd(){
        $getdata = I('get.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->get_paypwd($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 领取会员卡
     */
    public function get_card_sn(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result = D('Member')->get_card_sn($param);
        $this->ajaxReturn($result);
    }
    /**
     * 存储会员卡code
     */
    public function save_card_code(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result = D('Member')->save_card_code($param);
        $this->ajaxReturn($result);
    }
    /**
     * 会员签到
     */
    public function to_sign(){
        $param = I('param.');
        $return=array("msg"=>"您已签到");
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $return=D("Wxsmall/Member")->goto_sign($param);// 19-2-14 zjn
        $this->ajaxReturn($return);
    }

    /**
     * 签到记录
     */
    public function sign_log(){
        $param=array();
        $return=array("status"=>2,'msg'=>'没有更多数据了');
        $user_info=$this->checkuserauth();
        $uid=$user_info['id'];
        $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'];
        }
        $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 my_collect(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result = D('Member')->my_collect($param);
        $this->ajaxReturn($result);
    }

    /**
     * 删除我的收藏
     */
    public function del_collect(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result = D('Member')->del_collect($param);
        $this->ajaxReturn($result);
    }
    public function comment_list(){
        $return=array('status'=>3);
        $param = I('param.');
        if(empty($param) || empty($param['id'])){
            $return['msg']="参数错误";
            $this->ajaxReturn($return);
        }
        $result = D('Mobile/Dp')->get_goods($param,$param['id']);
        if($result['dp_list']){
            foreach($result['dp_list'] as $key=>$val){
                $result['dp_list'][$key]['addtime']=time_format($val['addtime']);
                $result['dp_list'][$key]['img']=thumb($val['img']);
                if($val['pl_img']){
                    foreach ($val['pl_img'] as $ke => $value) {
                        $result['dp_list'][$key]['pl_img'][$ke] = thumb($value);
                    }
                }
                if($val['pl_img']){
                    foreach ($val['pl_img'] as $ke => $value) {
                        $result['dp_list'][$key]['pl_img'][$ke] = thumb($value);
                    }
                }
                if(empty($val['username'])){
                    $map=array();
                    $map['id']=$val['uid'];
                    $member_info=M("Member")->where($map)->field('nickname,mobile')->find();
                    if($member_info['nickname']){
                        $result['dp_list'][$key]['username']=$member_info['nickname'];
                    }else if($member_info['mobile']){
                        $result['dp_list'][$key]['username']=$member_info['mobile'];
                    }else{
                        $result['dp_list'][$key]['username']='';
                    }
                }

            }
        }
        $return['status']=1;
        $return['data']=$result;
        $this->ajaxReturn($return);
    }
    /**
     *判断收货地址是否在配货区域
     */
    public function distance()
    {
        $id=I('post.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('xpoint,ypoint')->find();
            $distribution_area=D('Wxsmall/Login')->judge_shop($v['xpoint'],$v['ypoint'],$shop_id);
            if($distribution_area['status']==3){
                $data['status']=0;
            }else{
                $data['status']=1;
            }
        }else{
            $data['status']=0;
        }
        $this->ajaxReturn($data);
    }

    /**
     * 我的签到
     */
    public function my_sign(){
        $return=array('status'=>1);
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $uid=$user_info['id'];
        $return = D('Wxsmall/Member')->member_sign($uid);
        $this->ajaxReturn($return);
    }

    /**
     * 获取推荐商品
     */
    public function get_bestgoods_list(){
        $param=I('param');
        $user_info=$this->checkuserauth();
        $param['rank_id']=$user_info['rank_id'];
        $param['shop_id']=$user_info['HCshop']['bind_shop_id'];//追加当前会员匹配的门店
        $return=D("Wxsmall/Member")->get_bestgoods_list($param);
        $this->ajaxReturn($return);
    }

    /**
     * 拼团订单
     */
    public function get_order_group(){
        $getdata = I('param.');
        $user_info=$this->checkuserauth();
        $getdata['uid']=$user_info['id'];
        $result = D('Member')->myorder_list($getdata);
        $this->ajaxReturn($result);
    }
    /**
     * 保存会员手机号
     */
    public function save_user_mobile(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result = D('Member')->save_user_mobile($param);
        $this->ajaxReturn($result);
    }
    public function save_user_mobile_input(){
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $result = D('Member')->save_user_mobile_input($param);
        $this->ajaxReturn($result);
    }
    /**
     * 获取推广手机号
     */
    public function get_invite_mobile(){
        $return=array('status'=>1);
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $return['web_logo']=format_url(C("WEB_SMALL_LOGO"));
        $return['web_name']=format_url(C("WEB_SITE_NAME"));
        $return['open_register']=format_url(C("REGISTER_OPEN"));
        if($param['invite_key']){
            $invite_key=base64_decode($param['invite_key']);
            $map=array();
            $map['id']=$invite_key;
            $mobile=M("Member")->where($map)->getField('mobile');
            if($mobile) {
                $return['data'] = $mobile;
            }else{
                $return['status']=3;
                $return['data']=0;
                $this->ajaxReturn($return);
            }
        }else{
            $return['status']=3;
            $return['data']=0;
            $this->ajaxReturn($return);
        }
        $this->ajaxReturn($return);
    }
    /**
     * 查询快递
     */
    public function doexpress(){
        $return=array('status'=>1);
        $id=I('id','','intval');
        $code=I('code','','trim');
        $sn=I('sn','','trim');
        if(empty($id) && empty($code) && empty($sn)){$return['status']=3;$return['msg']='参数错误!';$this->ajaxReturn($return);}
        $map=array();
        if($id)$map['id']=$id;
        $map['status']=1;
        $kuaidi=M('Express')->order('id desc')->where($map)->getField('code');
        if(empty($kuaidi)){$return['status']=3;$return['msg']='快递查询接口不存在!';$this->ajaxReturn($return);}
        $express= new \Common\Vendor\Express($kuaidi);
        $params=array(
            'code'=>$code,
            'sn'=>$sn,
            'type'=>0
        );
        $res=$express->process($params);
        if($res['com']){
            $map=array();
            $map['code']=$res['com'];
            $kuaidi=M("Shipping")->where($map)->field(true)->find();
        }
        $return['data']=$res;
        $return['ship_info']=$kuaidi;
        $this->ajaxReturn($return);
    }
}