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

namespace app\index\controller;
use app\index\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();
            $metabel->updatetelbyoopenid($userinfo['open_id'],$userinfo['tel']);
            $u_info=$metabel->getinfobyopenid($userinfo['open_id']);
            $update_res['code']='200';
            $update_res['resultData']=$u_info;;
        }else{
            //无手机号，返回错误
            $update_res['code']='400';
        }
        return json($update_res);
    }
} 