<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2019/12/7
 * Time: 16:12
 */

namespace app\index\controller;
use app\common\model\memodel;
use think\Controller;

//extends controller 为什么不能继承
class Me  extends Controller{
   public function add(){
      //获取参数
//       $userinfo['address']=('post.address');
//       $userinfo['avatar_url']=I('post.avatarUrl');
//       $userinfo['city']=I('post.city');
//       $userinfo['company']=I('post.company');
//       $userinfo['county']=I('post.county');
//       $userinfo['gender']=I('post.gender');
//       $userinfo['id']=I('post.id');
//       $userinfo['nick_name']=I('post.nickName');
//       $userinfo['open_id']=I('post.openid');
//       $userinfo['province']=I('post.province');
//       $userinfo['tel']=I('post.tel');
//       $userinfo['union_id']=I('post.unionId');
       //验证部分
      $userinfo = request() -> param();
       $userinfo['open_id']=$userinfo['openId'];
       $userinfo['avatar_url']=$userinfo['avatarUrl'];
       $userinfo['nick_name']=$userinfo['nickName'];
       $userinfo['create_date']=date('Y-m-d H:i:s',time());
      // $userinfo['nick_name']=$userinfo['nickName'];
       //var_dump($userinfo);
        //openid为空，返回403
       //修改一下，文档中说自动处理了大写转成下划线加小写字母
       if (!$userinfo['openId']){
           //return
           $add_res['code']='403';
           return json($add_res);
       }
       $metabel=new memodel();
       //先根据open_id判断是否存在，这个以后应当放到logic中
       $u_info=$metabel->getinfobyopenid($userinfo['open_id']);

       if (!$u_info){
           //如果为空就新增
           $id=$metabel->saveme($userinfo);
           $Resultdata['id']=$id;
           //存储之后再查询
           $new_info=$metabel->getinfobyopenid($userinfo['open_id']);
           $add_res['code']='200';
           $add_res['resultData']=$new_info;
           return json($add_res);
       }else{
           //如果有，就另一个处理逻辑,判断是否有电话，没有电话就返回
           //返回数据，前端判断
            if (!$u_info['tel']){
                //看传参中是否有手机，如果有，加上，再返回数据，如果没有，直接返回
                if (array_key_exists('tel',$userinfo)){
                    //传参中有，则更新操作
                    //tel是另外一个接口，此处后来应该修改
                }else{
                    //传参中也没有手机，直接返回
                    $add_res['code']='200';
                    $add_res['resultData']=$u_info;
                    return json($add_res);
                }
            }else{
                $add_res['code']='200';
                $add_res['resultData']=$u_info;
                return json($add_res);
            }



       }


   }
    //更新手机号用
    public function updatemetel(){
        $userinfo = request() -> param();
       // var_dump($userinfo);
        if ($userinfo['tel']){
            //有手机号，继续
            $metabel=new memodel();
            $openid='';
            if(array_key_exists('open_id',$userinfo)){
                $openid=$userinfo['open_id'];
            }else{
                $openid=$userinfo['openid'];
            }
            $metabel->updatetelbyoopenid($openid,$userinfo['tel']);
            $u_info=$metabel->getinfobyopenid($openid);
            $u_info['openid']=$u_info['open_id'];
            $u_info['openId']=$u_info['open_id'];
            $update_res['code']='200';
            $update_res['resultData']=$u_info;;
        }else{
            //无手机号，返回错误
            $update_res['code']='400';
        }
        return json($update_res);
    }

    /*
     * 20200221
     * steelxu
     * 只存入openid的
     */

    public function samplelogin(){
        //
        $userinfo = request() -> param();
        $openid=$userinfo['openId'];
        $mtable=new memodel();
        $rinfo=$mtable->getinfobyopenid($openid);
        if (!$rinfo){
            $insinfo['open_id']=$openid;
            //2010303
            $insinfo['create_date']=date('Y-m-d H:i:s');
            $insinfo['create_pin']=rand(0,1);
            //
            $rid=$mtable->insopenid($insinfo);
            if (array_key_exists('unionid',$userinfo)){
                $unionid=$userinfo['unionid'];
                $mtable->updunionidbyopenid($openid,$unionid);
            }
            $Resultdata['id']=$rid;
            $Resultdata['openId']=$openid;
            $Resultdata['openid']=$openid;
            $Resultdata['create_pin']=$insinfo['create_pin'];
        }else{
            $Resultdata=$rinfo;
            $Resultdata['openId']=$rinfo['open_id'];
            $Resultdata['openid']=$rinfo['open_id'];
            $Resultdata['create_pin']=$rinfo['create_pin'];

        }
        $res_r['code']=200;
        $res_r['resultData']=$Resultdata;
        return json($res_r);
    }

    public function updateme(){

        //验证部分
        $userinfo = request() -> param();
        if (array_key_exists('unionid',$userinfo)){
            $userinfo['union_id']=$userinfo['unionId'];
        }
        $userinfo['open_id']=$userinfo['openId'];
        $userinfo['avatar_url']=$userinfo['avatarUrl'];
        $userinfo['nick_name']=$userinfo['nickName'];
        $userinfo['create_date']=date('Y-m-d H:i:s',time());
        // $userinfo['nick_name']=$userinfo['nickName'];
        //var_dump($userinfo);
        //openid为空，返回403
        //修改一下，文档中说自动处理了大写转成下划线加小写字母
        if (!$userinfo['openId']){
            //return
            $add_res['code']='403';
            return json($add_res);
        }
        $metabel=new memodel();
        //先根据open_id判断是否存在，这个以后应当放到logic中
        $u_info=$metabel->getinfobyopenid($userinfo['open_id']);

        if (!$u_info){
            //如果为空就新增
            $id=$metabel->saveme($userinfo);
            $Resultdata['id']=$id;
            //存储之后再查询
            $new_info=$metabel->getinfobyopenid($userinfo['open_id']);
            $new_info['openid']=$new_info['open_id'];
            $new_info['openId']=$new_info['open_id'];
            $add_res['code']='200';
            $add_res['resultData']=$new_info;

            return json($add_res);
        }else{
            //如果已经有open_id，则判断是否授权了union_id，进行二级登录
            if (!$u_info['nick_name']){
                //数据表中无姓名，则未授权姓名,传参中有姓名，则更新后返回
                if (array_key_exists('unionId',$userinfo)){
                    $update_arr['union_id']=$userinfo['unionId'];
                    $update_arr['avatar_url']=$userinfo['avatarUrl'];
                    $update_arr['nick_name']=$userinfo['nickName'];
                    $update_arr['gender']=$userinfo['gender'];
                    $update_arr['province']=$userinfo['province'];
                    $update_arr['city']=$userinfo['city'];
                    $update_arr['country']=$userinfo['country'];

                    $update_arr['create_date']=date('Y-m-d H:i:s',time());
                    $metabel->updateinfobyopenid($userinfo['open_id'],$update_arr);
                    //重新获取，返回
                    $u_info=$metabel->getinfobyopenid($userinfo['open_id']);
                    $u_info['openid']=$u_info['open_id'];
                    $u_info['openId']=$u_info['open_id'];
                    $add_res['code']='200';
                    $add_res['resultData']=$u_info;
                    return json($add_res);

                }else{
                    //无姓名，则更新直接返回，不影响使用，但下个功能不能使用
                    $update_arr['avatar_url']=$userinfo['avatarUrl'];
                    $update_arr['nick_name']=$userinfo['nickName'];
                    $update_arr['gender']=$userinfo['gender'];
                    $update_arr['province']=$userinfo['province'];
                    $update_arr['city']=$userinfo['city'];
                    $update_arr['country']=$userinfo['country'];

                    $update_arr['create_date']=date('Y-m-d H:i:s',time());
                    $metabel->updateinfobyopenid($userinfo['open_id'],$update_arr);
                    //重新获取，返回
                    $u_info=$metabel->getinfobyopenid($userinfo['open_id']);
                    $u_info['openid']=$u_info['open_id'];
                    $u_info['openId']=$u_info['open_id'];
                    $add_res['code']='200';
                    $add_res['resultData']=$u_info;
                    return json($add_res);
                }

            }else{
                //数据表中有姓名，则判断有无电话，进行三级登录
                //如果有，就另一个处理逻辑,判断是否有电话，没有电话就返回
                //返回数据，前端判断

                if (!$u_info['tel']){
                    //看传参中是否有手机，如果有，加上，再返回数据，如果没有，直接返回，目前不强制登录手机，
                    if (array_key_exists('tel',$userinfo)){
                        //传参中有，则更新操作
                        //tel是另外一个接口，此处后来应该修改
                    }else{
                        //传参中也没有手机，直接返回
                        $u_info['openid']=$u_info['open_id'];
                        $u_info['openId']=$u_info['open_id'];
                        $add_res['code']='200';
                        $add_res['resultData']=$u_info;
                        return json($add_res);
                    }
                }else{
                    //数据表中有手机，说明手机号已授权，直接返回
                    $u_info['openid']=$u_info['open_id'];
                    $u_info['openId']=$u_info['open_id'];
                    $add_res['code']='200';
                    $add_res['resultData']=$u_info;
                    return json($add_res);
                }
            }




        }


    }

} 