<?php
// +----------------------------------------------------------------------
// | likeshop100%开源免费商用商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | 商业版本务必购买商业授权，以免引起法律纠纷
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshopTeam
// +----------------------------------------------------------------------


namespace app\api\controller;
use app\api\logic\LoginLogic;
use app\common\server\ConfigServer;
use app\common\server\WeChatServer;
use app\api\server\UserServer;
use app\api\cache\TokenCache;
use think\facade\Config;
use think\Db;

class Account extends ApiBase
{

    public $like_not_need_login = ['register','applogin', 'login', 'mnplogin', 'codeurl', 'oalogin', 'oplogin','logout','smslogin', 'uinAppLogin', 'silentLogin', 'authLogin','stafflogin'];


    /**
     * note 注册接口
     * create_time 2020/10/23 18:42
     */
    public function register(){
        $post  = $this->request->post();
        $post['check_code'] = ConfigServer::get('register_setting', 'open', 0);
        $result = $this->validate($post,'app\api\validate\Register');
        if($result ===true){
            $data = LoginLogic::register($post);
            if($data){
                $this->_success('注册成功',$data);
            }
            $this->_error('获取失败',$result,0);
        }$this->_error($result,'',0);
    }
    /**
     * showdoc
     * @catalog 接口/账号
     * @title 手机号账号登录
     * @description 手机号账号登录
     * @method post
     * @url /account/login
     * @return {"code":1,"msg":"登录成功","data":["token":"3237676fa733d73333341",//登录令牌"nickname":"好象cms-小林",//昵称"avatar":"http://b2c.yixiangonline.com/uploads/user/avatar/3f102df244d5b40f21c4b25dc321c5ab.jpeg",//头像url"level":0,//等级],"show":0,"time":"0.775400"}
     * @param account 必填 string 账号或手机号
     * @param id 必填 int 1-密码登录-2-验证码登录
     * @param password 必填 string 密码
     * @param client 必填 int 客户端类型：2-h5，3-ios，4-android
     * @return_param token string 登录令牌
     * @return_param nickname string 昵称
     * @return_param avatar string 头像
     * @remark
     * @number 1
     */
    public function login()
    {
        $post = $this->request->post();
        $check = $this->validate($post, 'app\api\validate\Login.password');
        if (true !== $check) {
            $this->_error($check);
        }
        $data = LoginLogic::login($post);
        $this->_success('登录成功', $data);
    }

    /**
     * 验证码登录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
     public function smsLogin(){
         $post = $this->request->post();
         $post['message_key'] = 'YZMDL';
         $check = $this->validate($post, 'app\api\validate\Login.code');
         if (true !== $check) {
             $this->_error($check);
         }
         $data = LoginLogic::login($post);
         $this->_success('登录成功', $data);
     }

    /**
     * showdoc
     * @catalog 接口/账号
     * @title 小程序登录
     * @description 小程序登录
     * @method post
     * @url /account/mnpLogin
     * @return {"code":1,"msg":"登录成功","data":["token":"3237676fa733d73333341",//登录令牌"nickname":"好象cms-小林",//昵称"avatar":"http://b2c.yixiangonline.com/uploads/user/avatar/3f102df244d5b40f21c4b25dc321c5ab.jpeg",//头像url"level":0,//等级],"show":0,"time":"0.775400"}
     * @param code 必填 string code
     * @param iv 必填 string iv
     * @param encrypted_data 必填 string encrypted_data
     * @return_param token string 登录令牌
     * @return_param nickname string 昵称
     * @return_param avatar string 头像
     * @remark
     * @number 1
     */
    public function mnpLogin()
    {
        $post = $this->request->post();
        $check = $this->validate($post, 'app\api\validate\MnpLogin');
        if (true !== $check) {
            $this->_error($check);
        }
        $data = LoginLogic::mnpLogin($post);
        $this->_success($data['msg'], $data['data'], $data['code'], $data['show']);
    }


    /**
     * showdoc
     * @catalog 接口/账号
     * @title 获取获取向微信请求code的链接
     * @description
     * @method get
     * @url /account/codeurl
     * @param url 必填 varchar 前端当前url
     * @return_param url string codeurl
     * @remark 这里是备注信息
     * @number 0
     * @return  {"code":1,"msg":"获取成功","data":{"url":'http://mp.weixin……'}}
     */
    public function codeUrl()
    {
        $url = $this->request->get('url');
        $this->_success('获取成功', ['url' => LoginLogic::codeUrl($url)], 1);
    }

    /**
     * showdoc
     * @catalog 接口/账号
     * @title 微信H5登录
     * @description 微信H5登录
     * @method post
     * @url /account/oalogin
     * @return {"code":1,"msg":"登录成功","data":["token":"3237676fa733d73333341",//登录令牌"nickname":"好象cms-小林",//昵称"avatar":"http://b2c.yixiangonline.com/uploads/user/avatar/3f102df244d5b40f21c4b25dc321c5ab.jpeg",//头像url"level":0,//等级],"show":0,"time":"0.775400"}
     * @param code 必填 string code
     * @return_param token string 登录令牌
     * @return_param nickname string 昵称
     * @return_param avatar string 头像
     * @remark
     * @number 1
     */
    public function oaLogin()
    {
        $post = $this->request->post();
        $check = $this->validate($post, 'app\api\validate\OaLogin');
        if (true !== $check) {
            $this->_error($check);
        }
        $data = LoginLogic::oaLogin($post);
        $this->_success($data['msg'], $data['data'], $data['code']);
    }

    /**
     * showdoc
     * @catalog 接口/账号
     * @title 微信第三方app登录
     * @description 微信第三方app登录
     * @method post
     * @url /account/oplogin
     * @return {"code":1,"msg":"登录成功","data":["token":"3237676fa733d73333341",//登录令牌"nickname":"好象cms-小林",//昵称"avatar":"http://b2c.yixiangonline.com/uploads/user/avatar/3f102df244d5b40f21c4b25dc321c5ab.jpeg",//头像url"level":0,//等级],"show":0,"time":"0.775400"}
     * @param code 必填 string code
     * @param client 必填 int 客户端类型：3-ios，4-android
     * @return_param token string 登录令牌
     * @return_param nickname string 昵称
     * @return_param avatar string 头像
     * @remark
     * @number 1
     */
    public function opLogin()
    {
        $post = $this->request->post();
        $check = $this->validate($post, 'app\api\validate\OpLogin');
        if (true !== $check) {
            $this->_error($check);
        }
        $data = LoginLogic::opLogin($post);
        $this->_success($data['msg'], $data['data'], $data['code']);
    }


    /**
     * 退出登录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function logout()
    {
        LoginLogic::logout($this->user_id, $this->client);
        //退出登录只有成功
        $this->_success();
    }


    /**
     * Notes: uniapp微信登录
     * @author 段誉(2021/3/16 16:00)
     */
    public function uinAppLogin()
    {
        $post = $this->request->post();
        $data = LoginLogic::uinAppLogin($post);
        $this->_success($data['msg'], $data['data'], $data['code']);
    }



    public function getrandstr($length = 10){
        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $len = strlen($str)-1;
        $randstr = '';
        for ($i=0;$i<$length;$i++) {
        $num=mt_rand(0,$len);
        $randstr .= $str[$num];
        }
        return $randstr;
    }
    //2021-0419  小程序新版登录调整

    /**
     * Notes: 小程序登录(旧系统用户,返回用户信息,否则返回空)
     * @author 段誉(2021/4/19 16:50)
     */
    public function silentLogin()
    {
        $post = $this->request->post();
        
        if(!empty($post['login_type']) && $post['login_type'] == 'toutiao'){
            
            return $this->silentLogin_toutiao($post);
        }
        
        if (empty($post['code'])) {
            $this->_error('参数缺失');
        }
        $data = LoginLogic::silentLogin($post);
        $this->_success($data['msg'], $data['data'], $data['code'], $data['show']);
    }
    public function silentLogin_toutiao($data){
        $url='https://developer.toutiao.com/api/apps/v2/jscode2session';
        $data=[
             'appid'=>'tt0523739e9a12236501',
             'secret'=>'58280e4f36d88e93d7a4be9f0e590b2302a462c5',
             'code'=>$data['code'],
             //'anonymous_code'=>$anonymousCode,
         ];
         $res=$this->json_post($url,$data);
            //  array(3) {
            //   ["err_no"]=>
            //   int(0)
            //   ["err_tips"]=>
            //   string(7) "success"
            //   ["data"]=>
            //   array(5) {
            //     ["session_key"]=>
            //     string(24) "xDIPf7whhzHFz+5ppTanFA=="
            //     ["openid"]=>
            //     string(36) "_000dypj-umBDuy6oz-nKqesccbK29flyIws"
            //     ["anonymous_openid"]=>
            //     string(0) ""
            //     ["unionid"]=>
            //     string(36) "eb8fbd8b-4124-595d-abf4-9fbca7ef3611"
            //     ["dopenid"]=>
            //     string(0) ""
            //   }
            // } 
         $res=json_decode($res,true); 
         if(empty($res['data']['openid'])){
              $this->_error('登录失败：'.json_encode($res));
         }
         $openid = $res['data']['openid'];
         $unionid =  $res['data']['unionid'];
 
         $user_id =Db::name('user_auth')->where('openid',$openid)->find();
         
         $response['headimgurl'] =  $this->getrandstr();
         $response['headimgurl'] = 'https://web.dulmao.com/uploads/images/202404240124289ecef8304.png';
         $response['openid'] = $openid;
         $response['unionid'] = $unionid;
             
        if (empty($user_id)) {
            $user_info = UserServer::createUser($response,8);
        } else {
            $user_info = UserServer::updateUser($response,8, $user_id);
        }

        //验证用户信息
        $check_res = self::checkUserInfo($user_info);
        if (true !== $check_res) {
            return $this->_error($check_res);
        }

        //创建会话
        $user_info['token'] = self::createSession($user_info['id'], 8);

        unset($user_info['id'], $user_info['disable']);
        return $this->_success('登录成功', $user_info);
    }
    public static function createSession($user_id, $client)
    {

        //清除之前缓存
        $token = Db::name('session')
            ->where(['user_id' => $user_id, 'client' => $client])
            ->value('token');
        if($token) {
            $token_cache = new TokenCache($token);
            $token_cache->del();
        }

        $result = Db::name('session')
            ->where(['user_id' => $user_id, 'client' => $client])
            ->find();

        $time = time();
        $expire_time = $time + Config::get('project.token_expire_time');
        $token = md5($user_id . $client . $time);
        $data = [
            'user_id' => $user_id,
            'token' => $token,
            'client' => $client,
            'update_time' => $time,
            'expire_time' => $expire_time,
        ];

        if (empty($result)) {
            Db::name('session')->insert($data);
        } else {
            Db::name('session')
                ->where(['user_id' => $user_id, 'client' => $client])
                ->update($data);
        }

        //更新登录信息
        $login_ip = $ip = request()->ip();
        Db::name('user')
            ->where(['id' => $user_id])
            ->update(['login_time' => $time, 'login_ip' => $login_ip]);

        //创建新的缓存
        (new TokenCache($token, ['token' => $token]))->set(300);
        return $token;
    }
    public static function checkUserInfo($user_info)
    {
        if (empty($user_info)) {
            return '登录失败:user';
        }

        if ($user_info['disable']) {
            return '该用户被禁用';
        }

        return true;
    }
    public function json_post($url, $data = [])
    {
        //$data=http_build_query($data);
        //$data=json_encode($data);
        $curl = curl_init();
    
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if(!$data){
            return 'data is null';
        }
        if(is_array($data))
        {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER,array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length:' . strlen($data),
                'Cache-Control: no-cache',
                'Pragma: no-cache'
        ));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return $errorno;
        }
        curl_close($curl);
        return $res;
    
    }


    /**
     * Notes: 小程序登录(新用户登录->需要提交昵称和头像参数)
     * @author 段誉(2021/4/19 16:49)
     */
    public function authLogin()
    {
        $post = $this->request->post();
        $post['nickname'] =  $this->getrandstr();
        $post['headimgurl'] = 'https://web.dulmao.com/uploads/images/202404240124289ecef8304.png';
        
        //|
        if (empty($post['code']) || empty($post['nickname']) ||  empty($post['phone_code'])) {
            $this->_error('参数缺失');
        }
        
        $phone = $this->code_phone($post['phone_code']);
  
        
        $data = LoginLogic::authLogin($post,$phone);
        $this->_success($data['msg'], $data['data'], $data['code'], $data['show']);
    }
    
    //获取手机号
    public function  code_phone($code){
         $data['code'] = $code;
         $config = WeChatServer::getMnpConfig();
         $accessToken = '';
         
         $cache = cache('wx_mini_token');
         if(!empty($cache) && !empty($cache['token']) && $cache['exp'] > time()){
             $accessToken = $cache['token']; 
         }else{
             $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$config['app_id']."&secret=".$config['secret']."";
             $_accessToken = file_get_contents($token_url);
             $_accessToken = json_decode($_accessToken);
             if(!empty($_accessToken)){
                 $accessToken = $_accessToken->access_token;
                 cache('wx_mini_token',['token'=>$accessToken,'exp'=>time()+6000]);
             }else{
                 $this->_error('获取token失败');
             }
         }
         
        
         
        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$accessToken";
        
        $info = $this->http_request($url,json_encode($data),'json');
        // 一定要注意转json，否则汇报47001错误
        $tmpinfo = json_decode($info,true);
        if(empty($tmpinfo)){
            $this->_error('获取手机号失败');
        }
        
        if(!empty($tmpinfo['errcode'])){
            $this->_error($tmpinfo['errmsg']);
        }
        
        return $tmpinfo['phone_info']['phoneNumber']; 
    }
     public  function getAccessToken()
    {
        $appid = '填写自己的appID';
        $secret = '填写自己的秘钥';

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
        exit();
    }
    //图片合法性验证
    public function http_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, TRUE);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json'
            ));
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);

        return $output;
    }
    //  获取手机号
    public function getPhoneNumber(){
        $tmp = $this->getAccessToken();
        $tmptoken = json_decode($tmp);
        $token = $tmptoken->access_token;
        $data['code'] = $_GET['code'];//前端获取code

        $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=$token";
        $info = $this->http_request($url,json_encode($data),'json');
        // 一定要注意转json，否则汇报47001错误
        $tmpinfo = json_decode($info);

        $code = $tmpinfo->errcode;
        $phone_info = $tmpinfo->phone_info;
        //手机号
        $phoneNumber = $phone_info->phoneNumber;
        if($code == '0'){
            echo json_encode(['code'=>1,'msg'=>'请求成功','phoneNumber'=>$phoneNumber]);
            die();
        }else{
            echo json_encode(['code'=>2,'msg'=>'请求失败']);
            die();
        }

    }
    
    //员工端登入接口
    public function stafflogin(){
         $get = $this->request->get();
         //判断是不是员工
        $pass=md5(md5($get['password'])); 
         $phone=Db::name('staff')->where('mobile',$get['phone'])->where('password','like',$pass)->find();
         
      
       
         if($phone){
              $this->_success('登入成功',$phone);
         }else{
              $this->_error('登入失败,请检查账号和密码'); 
         }
        
    }


}