<?php

namespace app\api\controller;

use think\Db;
use app\common\controller\Api;

/**
 * 用户接口
 */
class User extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';


    /**
     * @ApiTitle    (用户端-授权登录)
     * @ApiSummary  (授权登录)
     * @ApiMethod (POST)
     * @ApiRoute    (/api/User/Login)
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiReturnParams   (name="code", type="integer", required=true,  sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code":"1",
    "msg": "返回成功",
    "data": {
    "token": "a32070524e6dd73c0f6a29b7993303e8",
    "type":1=已绑定,0未绑定,
    'level':类型:1=公司分管领导,2=公司机电负责人,3=维修负责人,4=维修成员,5=报修负责人,6=报修成员
    }
    })
     */
    public function Login()
    {
        $param = $this->request->param();
        //授权登录
        $ch = curl_init();
        $appid = "wx2adc803c6e8fd596";
        $secret = "925976a669063b7d26fd6527a1ed5197";
        $code = $param['code'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        if ($output === FALSE) {
            echo "CURL Error:" . curl_error($ch);
        }
        curl_close($ch);
        $curl_result = json_decode($output, true);
        $token = $this->request->token();
        $istype = Db::name('user')->where(['openid' => $curl_result['openid']])->find();
        if (!empty($istype)) {
            Db::name('user')->where(['openid' => $curl_result['openid']])->update(['token' => $token]);
            $type = 1;
            $is_token = $token;
            $level = $istype['level'];
        } else {
            $type = 0;
            $is_token = '';
            $level = '';
        }
        $return = [
            'token' => $is_token,
            'type' => $type,
            'level' => $level
        ];
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (用户端-账号登陆)
     * @ApiSummary  (账号登陆)
     * @ApiMethod (POST)
     * @ApiRoute    (/api/User/LoginSign)
     * @ApiParams   (name="username", type="string", required=true, description="账号")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiReturnParams   (name="code", type="integer", required=true,  sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code":"1",
    "msg": "返回成功",
    "data": {
    "token": "bf3e78fb461790218d62095d48270fbd",
    'level':类型:1=公司分管领导,2=公司机电负责人,3=维修负责人,4=维修成员,5=报修负责人,6=报修成员,
    "type":1=已绑定,0未绑定,
    }
    })
     */
    public function LoginSign()
    {
        $param = $this->request->param();
        if (empty($param['username']) || $param['username'] == null || $param['username'] == '' || $param['username'] == "") {
            $this->error('账号不能为空', 0);
        }
        if (empty($param['password']) || $param['password'] == null || $param['password'] == '' || $param['password'] == "") {
            $this->error('密码不能为空', 0);
        }
        $password = Db::name('user')->where(['username' => $param['username']])->find();
        if (empty($password)) {
            $this->error('帐号不存在', 0);
        }
        if (!($param['password'] == $password['password'])) {
            $this->error('密码错误', 0);
        }
        $token = $this->request->token();
        $res = Db::name('user')->where(['username' => $param['username']])->where(['password' => $param['password']])
            ->update(['token' => $token]);
        if (empty($password['openid'])) {
            $type = 0;
        } else {
            $type = 1;
        }
        $return = [
            'token' => $token,
            'level' => $password['level'],
            'type' => $type,
        ];
        if ($res) {
            $this->success('登陆成功', $return);
        } else {
            $this->error('登陆失败', 0);
        }
    }


    /**
     * @ApiTitle    (用户端-绑定微信)
     * @ApiSummary  (绑定微信)
     * @ApiMethod (POST)
     * @ApiRoute    (/api/User/SDKWechat)
     * @ApiHeaders  (name="authorization", type=string, required=true, description="请求的Token")
     * @ApiParams   (name="code", type="string", required=true, description="code")
     * @ApiReturnParams   (name="code", type="integer", required=true,  sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code":"1",
    "msg": "返回成功",
    "data": {
    }
    })
     */
    public function SDKWechat()
    {
        $user_id = $this->is_token($this->request->header());
        $ch = curl_init();
        $appid = "wx2adc803c6e8fd596";
        $secret = "925976a669063b7d26fd6527a1ed5197";
        $code = input('code');
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $output = curl_exec($ch);
        if ($output === FALSE) {
            echo "CURL Error:" . curl_error($ch);
        }
        curl_close($ch);
        $curl_result = json_decode($output, true);
        dump($curl_result);
        die;
        $res = Db::name('user')->where(['id' => $user_id])->update(['openid' => $curl_result['openid']]);
        if ($res) {
            $this->success('绑定成功', 1);
        } else {
            $this->error('绑定成功', 0);
        }
    }
}