<?php

namespace app\api\controller\v1\app;

use app\api\controller\Api;
use app\api\model\UserToken;
use app\common\tools\wechatUtil;
use think\Db;
use app\api\logic\app\SendLogic;
use app\api\model\User as UserModel;
use app\api\logic\app\UserLogic;

/**
 * 所有资源类接都必须继承基类控制器
 * 基类控制器提供了基础的验证，包含app_token,请求时间，请求是否合法的一系列的验证
 * 在所有子类中可以调用$this->clientInfo对象访问请求客户端信息，返回为一个数组
 * 在具体资源方法中，不需要再依赖注入，直接调用$this->request返回为请具体信息的一个对象
 * date:2017-07-25
 */
class User extends Base
{
    /**
     * 允许访问的方式列表，资源数组如果没有对应的方式列表，请不要把该方法写上，如user这个资源，客户端没有delete操作
     */
    public $restMethodList = 'get|post|put';

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 获取用户详情
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $user_info = $this->validateToken();

        $this->render(200,['user_info' => $user_info]);
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 编辑用户信息
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save()
    {
        $user_info = $this->validateToken();
        $requestData = $this->selectParam(['name', 'avatar']);
        $this->check($requestData, "User.edit");
        foreach ($requestData as $key=>$value){
            if(empty($value)){
                unset($requestData[$key]);
            }
        }
        $bool = UserModel::build()->saveInfo($requestData, $user_info['uuid']);
        if ($bool) {
            $this->render(200);
        } else {
            $this->returnmsg(403);
        }
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 更新token
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function updateToken()
    {
        $requestData = $this->selectParam(['refresh_token']);
        $result = UserToken::refreshToken($requestData);
        if($result == false){
            $this->returnmsg(412);
        }
        $this->render($result);

    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 验证码登录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $requestData = $this->selectParam(['mobile', 'code','oauth_uuid'=>""]);
        $this->check($requestData, 'Base.login');
        $requestData['sms_type'] = 'login';
        $bool = SendLogic::validateCode($requestData);
        if (!$bool) {
            self::returnmsg(403, [], [], 'service_message', 'code error.', '验证码错误');
        }
        $result = UserModel::build()->login($requestData['mobile'],$requestData['oauth_uuid']);
        if ($result) {
            $this->render($result);
        } else {
            $this->returnmsg(403, $data = [], $header = [], $type = "", "Login error", $message = "登录失败");
        }

    }

    /*
     * 退出登录
     */
    public function logout()
    {
        $this->render(200);
    }

    /*
     * 微信分享签名
     */
    public function getShareSign()
    {
        $requestData = $this->selectParam(['url'=>""]);
        if(empty($requestData['url'])){
            self::returnmsg(403, [], [], "", "param error", "请上传必填参数");
        }
        $appid = config('wechat')['PublicAppID'];
        $secret = config('wechat')['PublicAppSecret'];
        $wechat = new wechatUtil();
        $data = $wechat::getShareSign($appid,$secret,$requestData['url']);
        if(empty($data)){
            self::returnmsg(403, [], [], "", "make sign error", "签名失败");
        }
        $this->render($data,200);
    }

    /**
     * @author: Airon
     * @time:   2018年5月
     * description 公众号授权
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function publicLogin()
    {
        $requestData = $this->selectParam(['jscode' => '']);
        if(empty($requestData['jscode'])){
            self::returnmsg(403, [], [], "", "param error", "请上传必填参数");
        }
        $data = UserLogic::publicLogin($requestData);

        $data !== false ? $this->render($data) : $this->returnmsg(403,"","","","","登录失败");
    }
}
