<?php

namespace App\Common\Services;

use App\Exceptions\ApiException;
use Illuminate\Support\Facades\Log;

class UserCenterService
{
    private $appCode;
    private $appKey;
    private $verKey;
    private $operationKey;
    private $url;

    public function __construct()
    {
        $config = config('api.user_center_config');
        $this->appCode = $config['app_code'];
        $this->appKey = $config['app_key'];
        $this->verKey = $config['ver_key'];
        $this->url = $config['url'];
        $this->operationKey = $config['operation_key'];
        //Log::info(json_encode($config));
    }

    /**
     * http请求
     * @param $method 请求方式 GET|POST|PUT|DELETE
     * @param $url 请求地址
     * @param array $params 请求参数
     * @param array $headers 请求带的header头
     * @return string 返回数据
     * @throws ApiException
     */
    public function request($method, $url, $params = [], $headers = [])
    {
        $headers['X-Requested-With'] = 'XMLHttpRequest';
        return httpClient($method, $url, $params, $headers);
    }

    /**
     * 登录
     * @param $username
     * @param $password
     * @param string $redirect
     * @param string $checkCode
     * @return array
     * @throws ApiException
     */
    public function pwdLogin($username, $password)
    {
        $ip = getClientIp(0, true);
        $res = $this->request('post', $this->url . 'app/user/login', [
            'user_name' => $username,
            'password' => $password,
            'app_code' => $this->appCode,
            'app_key' => $this->appKey,
            'ip' => $ip
        ]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 是否返回成功
     * @param $res
     * @return bool
     */
    private function isSuccess($res)
    {
        if (!empty($res) && is_array($res) && $res['code'] == 0) {
            return true;
        }
        return false;
    }

    /**
     * 登出
     * @param $apiToken
     * @return boolean
     * @throws ApiException
     */
    public function logout($apiToken)
    {

        $res = $this->request('post', $this->url . 'app/user/logout', ['api_token' => $apiToken], ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取主帐号详情
     * @return mixed|string
     * @throws ApiException
     */
    public function getMasterSysUserInfo($apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/master-user-info', [], ['api-token' => $apiToken]);
        $resData = json_decode($res, true);

        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取帐号详情
     * @return mixed|string
     * @throws ApiException
     */
    public function getSysUserInfo($apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/base-info', [
            'app_code' => $this->appCode
        ], ['api-token' => $apiToken]);
        $resData = json_decode($res, true);

        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 授权接口
     * @param $token
     * @return array
     * @throws ApiException
     */
    public function getAuth($apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/get-auth', [
            'app_code' => $this->appCode,
            'app_key' => $this->appKey
        ], ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 心跳接口
     * @param $apiToken
     * @return mixed|string
     * @throws ApiException
     */
    public function heartBeat($apiToken, $throwError = false)
    {
        $res = $this->request('post', $this->url . 'app/user/heart-beat', [], ['api-token' => $apiToken]);
        //file_put_contents(storage_path('logs/hearBeat.log'), $res."\r\n", FILE_APPEND);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 修改密码
     * @param integer $type 0 根据帐号 1根据手机 2根据旧邮箱
     * @param string $password 密码
     * @param string $confirm_password 确认密码
     * @param string $oldPassword 旧密码 类型（0）必填
     * @param string $validCode 验证码 类型（1、2）必填
     * @return mixed|string|array
     * @throws ApiException
     */
    public function updatePwd($type, $password, $confirm_password, $oldPassword = '', $validCode = '', $apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/update-pwd', [
            'type' => $type,
            'password' => $password,
            'confirm_password' => $confirm_password,
            'old_password' => $oldPassword,
            'valid_code' => $validCode,
        ], ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {

            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 手机号查看用户是否存在
     * @param $phone
     * @return mixed
     * @throws ApiException
     */
    public function checkSysUserPhoneUnique($phone, $throwError = true)
    {
        $res = $this->request('post', $this->url . 'app/user/check-client-phone-unique', [
            'phone' => $phone
        ]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            if ($throwError) {
                $message = empty($resData['message']) ? '' : $resData['message'];
                throw new ApiException(1004,['msg'=> $message]);
            } else {
                //10012 手机号已被注册
                return $resData;
            }
        }
    }

    /**
     * 检查注册用户名是否存在 暂时不开发用户名注册
     * @param $phone
     * @return mixed
     * @throws ApiException
     */
    public function checkSysUserUserNameUnique($userName, $throwError = true)
    {
        $res = $this->request('post', $this->url . 'app/user/check-client-user-name-unique', [
            'user_name' => $userName
        ]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            if ($throwError) {
                $message = empty($resData['message']) ? '' : $resData['message'];
                throw new ApiException(1004,['msg'=> $message]);
            } else {
                return $resData;
            }
        }
    }

    /**
     * 检查注册邮箱是否存在
     * @param $phone
     * @return mixed
     * @throws ApiException
     */
    public function checkSysUserEmailUnique($email, $throwError = true)
    {
        $res = $this->request('post', $this->url . 'app/user/check-client-email-unique', [
            'email' => $email
        ]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            if ($throwError) {
                $message = empty($resData['message']) ? '' : $resData['message'];
                throw new ApiException(1004,['msg'=> $message]);
            } else {
                //10014 邮箱已被注册
                return $resData;
            }
        }
    }

    /**
     * 检查帐号是否为主帐号
     * @param $phone
     * @return mixed
     * @throws ApiException
     */
    public function checkSysUserIsMaster($params, $throwError = true)
    {
        $checkData = [];
        if (!empty($params['user_name'])) {
            $checkData['user_name'] = $params['user_name'];
        }
        if (!empty($params['email'])) {
            $checkData['email'] = $params['email'];
        }
        if (!empty($params['phone'])) {
            $checkData['phone'] = $params['phone'];
        }
        $res = $this->request('post', $this->url . 'app/user/check-client-user-is-master', $checkData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            if ($throwError) {
                $message = empty($resData['message']) ? '' : $resData['message'];
                throw new ApiException(1004,['msg'=> $message]);
            } else {
                return $resData;
            }
        }
    }

    /**
     * 获取短信验证码
     * @param $phone
     * @param $type
     * @return mixed
     * @throws ApiException
     */
    public function getSmsCode($phone,$countryCode, $type = 0)
    {
        $res = $this->request('get', $this->url . 'common/get-sms-code', [
            'phone' => $phone,
            'type' => $type,
            'country_code' => $countryCode,
        ]);
        // $res = '{"code":0,"message":"","data":1}';
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取短信验证码
     * @param $email
     * @return mixed
     * @throws ApiException
     */
    public function getEmailCode($email,$language='zh-cn',$footer='')
    {
        $res = $this->request('get', $this->url . 'common/get-email-code', [
            'email' => $email,
            'language' => $language,
            'footer' => $footer,
        ]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            if($resData['code']==10021) {
                throw new ApiException('common.email_empty', '邮箱不能为空');
            }
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 登录
     * @param $username
     * @param $password
     * @param string $redirect
     * @param string $checkCode
     * @return array ['status'=>1,'redirect'=>'']
     * @throws ApiException
     */
    public function phoneLogin($phone, $validCode,$countryCode=86)
    {
        $ip = getClientIp(0, true);
        $res = $this->request('post', $this->url . 'app/user/phone-login', [
            'phone' => $phone,
            'valid_code' => $validCode,
            'country_code' => $countryCode,
            'app_code' => $this->appCode,
            'app_key' => $this->appKey,
            'ip' => $ip
        ]);
        // $res = '{"code":0,"message":"","data":{"api_token":"d3506997fe83325eb0cca61de31737d8","is_admin":1,"expire":604800,"nick_name":"李肖明","user_name":"lixming","real_name":"","company_id":10327,"invalid_time":"2028-04-15 23:59:59"}}';
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取子帐号
     * @param $companyId
     * @return array
     * @throws ApiException
     */
    public function getSysUserSubUser($keyword, $apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/get-client-sub-user', ['keyword' => $keyword], ['api-token' => $apiToken]);
        //file_put_contents(storage_path('logs/aaa.log'), $res."\r\n", FILE_APPEND);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 修改子帐号
     * @param array $params id nick_name password real_name status
     * @param $apiToken
     * @return array
     * @throws ApiException
     */
    public function updateSubAccount($params = [], $apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/update-sub-account', $params, ['api-token' => $apiToken]);
        //file_put_contents(storage_path('logs/aaa.log'), $res."\r\n", FILE_APPEND);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 删除
     * @param integer $sysUserId 用户中心id
     * @return array
     * @throws ApiException
     */
    public function delSubAccount($sysUserId, $apiToken)
    {
        $res = $this->request('post', $this->url . 'app/user/del-sub-account', ['id' => $sysUserId], ['api-token' => $apiToken]);
        //file_put_contents(storage_path('logs/aaa.log'), $res."\r\n", FILE_APPEND);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 添加子账号
     * @param array params 子帐号参数
     * @param array params.user_name 帐号
     * @param array params.nick_name 昵称
     * @param array params.password 密码
     * @return array
     * @throws ApiException
     */
    public function addSubAccount($params = [], $apiToken)
    {
        $subAccountData = [];
        $subAccountData['user_name'] = $params['user_name'];
        $subAccountData['nick_name'] = !empty($params['nick_name']) ? $params['nick_name'] : '';
        $subAccountData['password'] = $params['password'];
        $res = $this->request('post', $this->url . 'app/user/add-sub-account', $subAccountData, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 注册用户
     * @param array params 注册用户参数
     * @param string params.password 密码
     * @param string params.confirm_password 确认密码
     * @param string params.phone 手机
     * @param string params.email 邮箱
     * @param string params.valid_code 验证码
     * @param string params.nick_name 昵称
     * @param string params.app_code 应用code
     * @param string params.app_key 应用key
     * @param string params.ver_Key 应用版本
     * @param integer params.auto_login 自动登录
     * */
    public function registerSysUser($params)
    {
        $nowTime = nowTime();
        $registerData = [];
        if (!empty($params['password'])) {
            $registerData['password'] = $params['password'];
        }
        if (!empty($params['confirm_password'])) {
            $registerData['confirm_password'] = $params['confirm_password'];
        }
        if (!empty($params['phone'])) {
            $registerData['phone'] = $params['phone'];
        }
        if (!empty($params['email'])) {
            $registerData['email'] = $params['email'];
        }
        if (!empty($params['valid_code'])) {
            $registerData['valid_code'] = $params['valid_code'];
        }
        if (!empty($params['nick_name'])) {
            $registerData['nick_name'] = $params['nick_name'];
        }
        if (isset($params['auto_login'])) {
            $registerData['auto_login'] = empty($params['auto_login']) ? 0 : $params['auto_login'];
        }
        $registerData['app_code'] = $this->appCode;
        $registerData['app_key'] = $this->appKey;
        $registerData['ver_Key'] = $this->verKey;
        $registerData['start_time'] = $nowTime;
        $registerData['end_time'] = '2099-12-31 23:59:59';
        $res = $this->request('post', $this->url . 'app/user/register', $registerData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 忘记密码 获取重置密码的token
     * @param array params 获取重置密码的token
     * @param integer params.type 类型 1手机 2邮箱
     * @param integer params.valid_code 验证码
     * @param string params.email 邮箱
     * @param string params.phone 手机
     * */
    public function getResetPwdToken($params)
    {
        $resetParams = [];
        $resetParams['type'] = $params['type'];
        $resetParams['valid_code'] = $params['valid_code'];
        if (!empty($params['email'])) {
            $resetParams['email'] = $params['email'];
        }
        if (!empty($params['phone'])) {
            $resetParams['phone'] = $params['phone'];
        }
        $res = $this->request('post', $this->url . 'app/user/reset-pwd-validate', $resetParams);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 根据reset_token 重置密码
     * @param string password 密码
     * @param string confirm_password 确认密码
     * @param string reset_token 重置tokem
     * */
    public function resetPasswordByToken($params)
    {
        $resetParams = [];
        $resetParams['password'] = $params['password'];
        $resetParams['confirm_password'] = $params['confirm_password'];
        $resetParams['reset_token'] = $params['reset_token'];
        $res = $this->request('post', $this->url . 'app/user/reset-pwd', $resetParams);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 绑定应用
     * @param string apiToken
     * */
    public function appBind($apiToken)
    {
        $nowTime = nowTime();
        $bindData = [];
        $bindData['app_code'] = $this->appCode;
        $bindData['app_key'] = $this->appKey;
        $bindData['ver_key'] = $this->verKey;
        $bindData['start_time'] = $nowTime;
        $bindData['end_time'] = '2099-12-31 23:59:59';
        $res = $this->request('post', $this->url . 'app/user/app-bind', $bindData, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 绑定应用
     * @param string apiToken
     * */
    public function appBindById($params)
    {
        $bindData = [];
        $bindData['app_code'] = $params['app_code'];
        $bindData['app_key'] = $params['app_key'];
        $bindData['ver_key'] =  $params['ver_key'];
        $bindData['start_time'] = $params['start_time'];;
        $bindData['end_time'] = $params['end_time'];;
        $bindData['user_id'] = $params['user_id'];;
        $bindData['operation_key'] = $this->operationKey;
        $res = $this->request('post', $this->url . 'app/user/app-bind-by-id', $bindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 绑定应用
     * @param string apiToken
     * */
    public function appBindUp($params,$throw=true)
    {
        $bindData = [];
        $bindData['app_code'] = $this->appCode;
        $bindData['app_key'] = $this->appKey;
        $verKey=$params['ver_key'];
        $bindData['ver_key'] = $verKey;
        $bindData['bind_type'] =$params['bind_type'];//bind_type 绑定类型 0 新增 1续费 2升级 3降级
        $bindData['user_id'] =$params['user_id'];//bind_type 绑定类型 0 新增 1续费 2升级 3降级
        $bindData['operation_key'] = $this->operationKey;
        if(isset($params['start_time'])){
            $bindData['start_time'] = $params['start_time'];
        }
        if(isset($params['end_time'])){
            $bindData['end_time'] = $params['end_time'];
        }
        $res = $this->request('post', $this->url . 'app/user/manage-up-app-bind', $bindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            if($throw){
                $message = empty($resData['message']) ? '' : $resData['message'];
                throw new ApiException(1004,['msg'=> $message]);
            }else{
                return false;
            }

        }
    }

    /**
     * 绑定应用
     * @param string apiToken
     * */
    public function addExpandPacket($params)
    {
        $bindData = [];
        $bindData['app_code'] = $this->appCode;
        $bindData['app_key'] = $this->appKey;
        $bindData['packet_key'] = $params['packet_key'];
        $bindData['start_time'] =$params['start_time'];
        $bindData['user_id'] =$params['user_id'];
        $bindData['operation_key'] = $this->operationKey;
        $res = $this->request('post', $this->url . 'app/user/add-expand-packet', $bindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 变更绑定手机获取变更绑定的reset_token
     * @param array params 获取重置密码的token
     * @param integer params.type 类型 1当前手机 2邮箱
     * @param string params.valid_code 验证码
     * */
    public function getBindPhoneResetToken($params, $apiToken)
    {
        $resetParams = [];
        $resetParams['type'] = $params['type'];
        $resetParams['valid_code'] = $params['valid_code'];
        $res = $this->request('post', $this->url . 'app/user/get-bind-phone-reset-token', $resetParams, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 绑定手机
     * @param array params 获取重置密码的token
     * @param integer params.type 类型 1手机 2邮箱
     * @param string params.valid_code 验证码
     * */
    public function userPhoneBind($params, $apiToken)
    {
        $resetParams = [];
        $resetParams['phone'] = $params['phone'];
        $resetParams['valid_code'] = $params['valid_code'];

        if (isset($params['reset_token'])) {
            $resetParams['reset_token'] = $params['reset_token'];
        }
        $res = $this->request('post', $this->url . 'app/user/bind-phone', $resetParams, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 变更绑定邮箱获取变更绑定的reset_token
     * @param array params 获取重置密码的token
     * @param integer params.type 类型 1当前手机 2邮箱
     * @param string params.valid_code 验证码
     * */
    public function getBindEmailResetToken($params, $apiToken)
    {
        $resetParams = [];
        $resetParams['type'] = $params['type'];
        $resetParams['valid_code'] = $params['valid_code'];
        $res = $this->request('post', $this->url . 'app/user/get-bind-email-reset-token', $resetParams, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 绑定邮箱
     * @param array params 获取重置密码的token
     * @param integer params.type 类型 1当前手机 2邮箱
     * @param string params.valid_code 验证码
     * */
    public function userEmailBind($params, $apiToken)
    {
        $resetParams = [];
        $resetParams['email'] = $params['email'];
        $resetParams['valid_code'] = $params['valid_code'];
        if (isset($params['reset_token'])) {
            $resetParams['reset_token'] = $params['reset_token'];
        }
        $res = $this->request('post', $this->url . 'app/user/bind-email', $resetParams, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 子帐号修改密码
     * */
    public function subAccountUpdatePwd($params, $apiToken)
    {
        $updateParams = [];
        $updateParams['password'] = $params['password'];
        $updateParams['confirm_password'] = $params['confirm_password'];
        $updateParams['old_password'] = $params['old_password'];
        $res = $this->request('post', $this->url . 'app/user/sub-account-update-pwd', $updateParams, ['api-token' => $apiToken]);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 注册用户
     * @param array params 注册用户参数
     * @param string params.password 密码
     * @param string params.confirm_password 确认密码
     * @param string params.phone 手机
     * @param string params.email 邮箱
     * @param string params.valid_code 验证码
     * @param string params.nick_name 昵称
     * @param string params.app_code 应用code
     * @param string params.app_key 应用key
     * @param string params.ver_Key 应用版本
     * @param integer params.auto_login 自动登录
     * */
    public function preRegister($params)
    {
        $nowTime = nowTime();
        $registerData = [];
        if (!empty($params['phone'])) {
            $registerData['phone'] = $params['phone'];
            $registerData['country_code'] = empty($params['country_code'])?'86':$params['country_code'];
        }
        if (!empty($params['email'])) {
            $registerData['email'] = $params['email'];
        }
        if (!empty($params['valid_code'])) {
            $registerData['valid_code'] = $params['valid_code'];
        }
        $registerData['app_code'] = $this->appCode;
        $registerData['app_key'] = $this->appKey;
        $registerData['ver_Key'] = $this->verKey;
        $registerData['start_time'] = $nowTime;
        $registerData['end_time'] = '2099-12-31 23:59:59';
        $res = $this->request('post', $this->url . 'app/user/pre-register', $registerData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 确认注册
     * */
    public function confirmRegister($params){
        $confirmParams = [];
        $confirmParams['password'] = $params['password'];
        $confirmParams['confirm_password'] = $params['confirm_password'];
        $confirmParams['pre_register_key'] = $params['pre_register_key'];
        if (!empty($params['auto_login'])) {
            //是否自动登陆
            $confirmParams['auto_login'] = $params['auto_login'];
        }
        $res = $this->request('post', $this->url . 'app/user/confirm-register', $confirmParams);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 更新用户数值权限使用量
     * @return mixed
     * @throws ApiException
     * */
    public function updateBindUse($params){
        $updateBindData = [];
        $updateBindData['app_code'] = $this->appCode;
        $updateBindData['app_key'] = $this->appKey;
        $updateBindData['operation_key'] = $this->operationKey;
        $updateBindData['type_numerical_key'] = $params['type_numerical_key'];
        $updateBindData['user_id'] = $params['user_id'];
        $updateBindData['update_value'] = $params['update_value'];
        $res = $this->request('post', $this->url . 'app/user/update-bind-use', $updateBindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取用户数值权限使用量
     * */
    public function getBindUse($params){
        $updateBindData = [];
        $updateBindData['app_code'] = $this->appCode;
        $updateBindData['app_key'] = $this->appKey;
        $updateBindData['operation_key'] = $this->operationKey;
        $updateBindData['user_id'] = $params['user_id'];
        $updateBindData['type_numerical_key'] = empty($params['type_numerical_key'])?'':$params['type_numerical_key'];
        $res = $this->request('post', $this->url . 'app/user/get-bind-use', $updateBindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取用户已绑定(开通)应用
     * @param string apiToken
     * */
    public function getBindAppList($userId)
    {
        $bindData = [];
        $bindData['app_code'] = $this->appCode;
        $bindData['user_id'] =$userId;
        $bindData['operation_key'] = $this->operationKey;
        $res = $this->request('post', $this->url . 'app/user/get-bind-app-list', $bindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

    /**
     * 获取应用的版本列表
     * */
    public function getAppVerList()
    {
        $bindData = [];
        $bindData['app_code'] = $this->appCode;
        $bindData['operation_key'] = $this->operationKey;
        $res = $this->request('post', $this->url . 'app/user/get-app-ver-list', $bindData);
        $resData = json_decode($res, true);
        if ($this->isSuccess($resData)) {
            return $resData['data'];
        } else {
            $message = empty($resData['message']) ? '' : $resData['message'];
            throw new ApiException(1004,['msg'=> $message]);
        }
    }

}
