<?php
class MemberAction extends IniAction{
    public function index(){
        $this->isLogin();
        $this->d();
    }

    public function info(){}

    public function address(){
        $this->isLogin();
        $op = ggp('op');
        if($op=='do'){
            $d['device_token'] = ggp('address:t2').'@'.ggp('realname:t2');
            $d['mobile'] = ggp('mobile:t2');
            M('member')->where("uid='{$this->my['uid']}'")->save($d);
            $this->mySuccess('操作成功');
        }
        
        list($address,$realname) = explode('@',$this->my['device_token']);
        $this->my['device_token'] = array('address'=>$address,'realname'=>$realname);
        $this->a('user',$this->my);
        $this->d();
    }
    
    public function unFavor(){
        $this->isLogin();
        $op = ggp('op');
        if($op=='do'){
            $goods_id = ggp('id:i');
            if(MemberModel::I()->delFavor($this->my['uid'], $goods_id)){
                $this->mySuccess('取消成功');   
            }else{
                $this->mySuccess('您没有收藏该商品');   
            }            
        }
    }

    public function favor(){
        $this->isLogin();
        $op = ggp('op');
        if($op=='do'){
            $goods_id = ggp('id:i');
            if(MemberModel::I()->addFavor($this->my['uid'], $goods_id)){
                $this->mySuccess('收藏成功');   
            }else{
                $this->mySuccess('您已经收藏该商品');   
            }            
        }
        $pre = getPre();
        $goods = M()->field('g.*')
                ->table("{$pre}favor f")
                        ->join("{$pre}goods g on f.goods_id=g.goods_id")
                                ->where("f.uid='{$this->my['uid']}'")
                                        ->select();
        $this->a('goods',$goods);
        $this->d();
    }
    
    public function goods(){}//参加专题的商品

    public function register(){
        $op = ggp('op');
        if($op=='do'){
            $uname = ggp('uname:t2');
            $password = ggp('password:t2');
            $password2 = ggp('password2:t2');
            $email = ggp('email:t2');
            $mobile = ggp('mobile:t2');
            $openid = ggp('openid:t2');
            
            !$uname && $this->myError('请正确填写用户名');
            !$password && $this->myError('请正确填写密码');
            $password2!=$password && $this->myError('两次填写密码不一致');
            if(!$email || !is_email($email)){
                $this->myError('请正确填写邮箱');
            }
            
            if(!$mobile || !is_mobile($mobile)){
                $this->myError('请正确填写手机号');
            }

            $user = D('Member')->getUserByUname($uname);
            $user && $this->myError('用户已存在，请换个用户名');

            $uid = M('member')->add(array(
                'uname'=>$uname,
                'password'=>md5($password),
                'openid'=>$openid,
                'email'=>$email,
                'mobile'=>$mobile,
                'ctime'=>TIME
            ));
            $this->_login(M('member')->find($uid));
        }
        $this->a('openid',ggp('openid:t2'));
        $this->a('nickname',ggp('nickname:t2'));
        $this->display();
    }
    
    public function isSign($sign){
        return intval(date('Ymd',$sign)) == intval(date('Ymd',TIME));
    }
    
    public function sign(){
        $this->isLogin();
        $member = $this->my;
        if($this->setting['score_sign2'] && (intval(date('Ymd',$member['sign'])) - intval(date('Ymd',TIME))===1)){
            MemberModel::I()->incJf($member['uid'],$this->setting['score_sign2']);
            M('member')->where("uid='{$member['uid']}'")->save(array('sign'=>TIME));
            $this->mySuccess("连续签到成功,增加{$this->setting['score_sign2']}点积分");
        }else if($this->isSign($member['sign'])){
            $this->myError('今天已经签过到');
        }else{
            MemberModel::I()->incJf($member['uid'],$this->setting['score_sign']);
            M('member')->where("uid='{$member['uid']}'")->save(array('sign'=>TIME));
            $this->mySuccess("签到成功,增加{$this->setting['score_sign']}点积分");
        }
    }

    private function _login($member,$url='/'){
        cookie('authcookie', authcode("{$member['uid']}\t{$member['password']}\t", 'ENCODE'),3600*4);
        if($this->setting['score_login'] && date('Ymd')!=date('Ymd',$member['last_login_time'])){
            MemberModel::I()->incJf($member['uid'],$this->setting['score_login']);
        }
        M('member')->where("uid='{$member['uid']}'")->save(array('last_login_time'=>TIME,'ip'=>$_SERVER['REMOTE_ADDR']));
        $this->mySuccess('登录成功',$url);
    }

    public function login(){
        $op = ggp('op');
        if($op=='do'){
            $openid = ggp('open_id:t2');
            if($openid){
                $nickname = ggp('nickname:t2');
                $user = M('member')->where("openid='{$openid}'")->find();
                if(empty($user)){
                    header("location:http://{$_SERVER['SERVER_NAME']}/index.php?m=member&a=register&openid={$openid}&nickname={$nickname}");
                    exit;
                }                
            }else{
                $uname = ggp('uname:t2');
                $password = md5(ggp('password:t2'));
                $user = MemberModel::I()->getUserByParam(array('uname'=>$uname,'password'=>$password));
                empty($user) && $this->myError('用户名不存在或密码错误');
                $user['password']!=$password && $this->myError('密码错误');                
            }            
            $this->_login($user);
        }
        $this->display();
    }

    public function resetPassword(){
        $this->isLogin();
        $op = ggp('op');
        if($op=='do'){
            $oldPassWord = ggp('oldPassWord:t2');
            $newPassWord = ggp('newPassWord:t2');
            $newPassWord2 = ggp('newPassWord2:t2');
            md5($oldPassWord) != $this->my['password'] && $this->myError('旧密码错误');
            $newPassWord!=$newPassWord2 && $this->myError('两次密码不一致');
            
            M('member')->where("uid='{$this->my['uid']}'")->save(array('password'=>md5($newPassWord)));
            $this->mySuccess('操作成功');
        }
        $this->d();
    }

    public function logout(){
        cookie('authcookie',null);
        $this->mySuccess(L('msg_member_logout_success'));
    }

    public function forgetpwd(){
        //sendMail($mail,$msg);
        $this->mySuccess('新密码已发送至您注册邮箱，请及时修改密码！');
    }
    
    public function avatar(){
        $op = ggp('op');
        if($op=='do'){
            if ($_FILES['pic_url']['size']) {
                $dir='avatar_img/'.date('Ym');
                $info = $this->saveFile($dir);
                if ($info['state'] > 0) {
                    $p['pic_url'] = "http://{$_SERVER['SERVER_NAME']}/uploads/{$dir}/{$info['goods_img']}";
                } else {
                    $this->myError($info['msg']);
                }
                M('member')->where("uid='{$this->my['uid']}'")->save($p);
                $this->mySuccess('操作成功');    
            }
            $this->myError('请上传头像');
        }
        $this->d();
    }

    private function create_rand_str($leng){
            $rand_str = "";
            $str="abcdefghijklmnopqrstuvwxyz0123456789";
            for($i=0;$i<$leng;$i++){
                    $rand_str .= $str[mt_rand(0, strlen($str)-1)];
            }
            return $rand_str;
    }

    private function create_rand_num($leng){
            $rand_str = "";
            $str="0123456789";
            for($i=0;$i<$leng;$i++){
                    $rand_str .= $str[mt_rand(0, strlen($str)-1)];
            }
            return $rand_str;
    }
}