| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737 | <?phpnamespace app\api\controller;use app\common\controller\Api;use app\common\library\Ems;use app\common\library\Sms;use fast\Random;use think\Config;use think\Validate;use app\common\library\Token;use think\Db;use app\common\model\UserDeviceInfo;use onlogin\onlogin;use addons\epay\library\Service;//use addons\epay\library\Wechat;use app\common\library\Wechat;/** * 会员接口,登录,注册,修改资料等 */class User extends Api{    protected $noNeedLogin = ['login', 'mobilelogin','wechatlogin','applelogin', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];    protected $noNeedRight = '*';    public function _initialize()    {        parent::_initialize();    }    /**     * 会员中心     */    public function index()    {        $this->success('', ['welcome' => $this->auth->nickname]);    }    /**     * 会员登录     *     * @ApiMethod (POST)     * @param string $account  账号     * @param string $password 密码     */    public function login()    {        $account = input('account');        $password = input('password');        if (!$account || !$password) {            $this->error(__('Invalid parameters'));        }        $ret = $this->auth->login($account, $password);        if ($ret) {            $data = $this->userInfo('return');            $this->success(__('Logged in successful'), $data);        } else {            $this->error($this->auth->getError());        }    }    /**     * 手机验证码登录     *     * @ApiMethod (POST)     * @param string $mobile  手机号     * @param string $captcha 验证码     */    public function mobilelogin()    {        $mobile = input('mobile');        $captcha = input('captcha');        if (!$mobile || !$captcha) {            $this->error(__('Invalid parameters'));        }        if (!Validate::regex($mobile, "^1\d{10}$")) {            $this->error(__('Mobile is incorrect'));        }        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {            $this->error(__('Captcha is incorrect'));        }        $user = \app\common\model\User::getByMobile($mobile);        if ($user) {            if ($user->status == -1) {                $this->error('账户已注销');            }            if ($user->status != 1) {                $this->error(__('Account is locked'));            }            //如果已经有账号则直接登录            $ret = $this->auth->direct($user->id);        } else {            $extend = [//                'register_from' => input('register_from',''),            ];            $ret = $this->auth->register('', '', '', $mobile, $extend);        }        if ($ret) {            Sms::flush($mobile, 'mobilelogin');            $data = $this->userInfo('return');            $this->success(__('Logged in successful'), $data);        } else {            $this->error($this->auth->getError());        }    }    /**     * 注册会员     *     * @ApiMethod (POST)     * @param string $username 用户名     * @param string $password 密码     * @param string $email    邮箱     * @param string $mobile   手机号     * @param string $code     验证码     */    /*public function register()    {        $username = $this->request->post('username');        $password = $this->request->post('password');        $email = $this->request->post('email');        $mobile = $this->request->post('mobile');        $code = $this->request->post('code');        if (!$username || !$password) {            $this->error(__('Invalid parameters'));        }        if ($email && !Validate::is($email, "email")) {            $this->error(__('Email is incorrect'));        }        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {            $this->error(__('Mobile is incorrect'));        }        $ret = Sms::check($mobile, $code, 'register');        if (!$ret) {            $this->error(__('Captcha is incorrect'));        }        $ret = $this->auth->register($username, $password, $email, $mobile, []);        if ($ret) {            $data = $this->userInfo('return');            $this->success(__('Sign up successful'), $data);        } else {            $this->error($this->auth->getError());        }    }*/    //微信登录+注册    public function wechatlogin(){        $nickname = input('nickname','');        $avatar = input('avatar','');        $gender = input('gender',1);        $wechat_openid = input('wechat_openid','');        if (!$wechat_openid) {            $this->error(__('Invalid parameters'));        }        if($gender != 1){            $gender = 0;        }        $user = \app\common\model\User::getByOpenid($wechat_openid);        if ($user) {            if ($user->status == -1) {                $this->error('账户已注销');            }            if ($user->status != 1) {                $this->error(__('Account is locked'));            }            //如果已经有账号则直接登录            $ret = $this->auth->direct($user->id);        } else {            if (!$nickname || !$avatar) {                $this->error(__('Invalid parameters'));            }            $reg_data = [                'nickname'=>$nickname,                'avatar'=>$avatar,                'gender'=>$gender,                'register_from' => input('register_from',''),            ];            $ret = $this->auth->openid_register($wechat_openid,$reg_data);        }        if ($ret) {            $data = $this->userInfo('return');            $this->success(__('Logged in successful'), $data);        } else {            $this->error($this->auth->getError());        }    }    /**     * 运营商一键登录     */    public function onLogin()    {        $accessToken = input('accessToken');// 运营商预取号获取到的token        $token = input('tokenT');// 易盾返回的token        if (!$accessToken || !$token) {            $this->error("参数获取失败!");        }        $params = array(            // 运营商预取号获取到的token            "accessToken" => $accessToken,            // 易盾返回的token            "token"       => $token        );        // 获取密钥配置        $configInfo = config("onLogin");        $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);        $onret = $onlogin->check($params);//        $ret = [];//        $ret["code"] = 200;//        $ret["msg"] = "ok";//        $ret["data"] = [//            "phone" => "17574504021",//            "resultCode" => 0//        ];        if ($onret["code"] == 200) {            $mobile = $onret["data"]["phone"];            if (empty($mobile)) {                // 取号失败,建议进行二次验证,例如短信验证码                $this->error("取号登录失败,请用验证码方式登录!");            } else {                // 取号成功, 执行登录等流程                // 用户登录逻辑 === 开始                $user = \app\common\model\User::getByMobile($mobile);                if ($user) {                    if ($user->status == -1) {                        $this->error('账户已注销');                    }                    if ($user->status != 1) {                        $this->error(__('Account is locked'));                    }                    //如果已经有账号则直接登录                    $ret = $this->auth->direct($user->id);                    $is_register = 0;                } else {                    $ret = $this->auth->register('', '', '', $mobile, []);                    $is_register = 1;                }                if ($ret) {                    $this->success(__('Logged in successful'), $this->auth->getUserinfo());                } else {                    $this->error($this->auth->getError());                }                // 用户登录逻辑 === 结束            }        } else {            $this->error("登录失败,请用验证码方式登录!");        }    }    //苹果登录+注册    public function applelogin(){        $iosUserId = input_post('ios_user_id','');        if(!$iosUserId){            $this->error(__('Invalid parameters'));        }        $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();        if ($user) {            if ($user['status'] == -1) {                $this->error('账户已经注销');            }            if ($user['status'] != 1) {                $this->error(__('Account is locked'));            }            //如果已经有账号则直接登录            $ret = $this->auth->direct($user['id']);        } else {            $ret = $this->auth->ios_register($iosUserId);        }        if ($ret) {            $this->success(__('Logged in successful'), $this->auth->getUserinfo());        } else {            $this->error($this->auth->getError());        }    }    //用户详细资料    public function userInfo($type = 1){        $info = $this->auth->getUserinfo();        if($type == 'return'){            return $info;        }        $this->success(__('success'),$info);    }    /**     * 退出登录     * @ApiMethod (POST)     */    public function logout()    {        if (!$this->request->isPost()) {            $this->error(__('Invalid parameters'));        }        //退出im//        $tenIm = new Tenim();//        $tenIm->loginoutim($this->auth->id);        $this->auth->logout();        $this->success(__('Logout successful'));    }    /**     * 修改会员个人信息     *     * @ApiMethod (POST)     * @param string $avatar   头像地址     * @param string $username 用户名     * @param string $nickname 昵称     * @param string $bio      个人简介     */    public function profile()    {        $field_array = ['nickname','introcode','gender','birthday','attribute','shoesize','height','weight','bio','avatar','photo_images','tag_ids','hide_is_finishinfo'];        $data = [];        foreach($field_array as $key => $field){            //前端传不了post,改了            /*if(!request()->has($field,'post')){                continue;            }*/            if(!input('?'.$field)){                continue;            }            $newone = input($field);            if($field == 'avatar'){                $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');            }            if($field == 'photo_images'){                $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');            }            $data[$field] = $newone;        }        if(isset($data['birthday'])){            $data['birthday'] = strtotime($data['birthday']);        }        if(isset($data['tag_ids'])){            $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));        }        if(isset($data['introcode'])){            $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');            if(!$intro_user){                $this->error('不存在的邀请人');            }            unset($data['introcode']);//别人的邀请码,不能改了自己的            $data['intro_uid'] = $intro_user;        }        //dump($data);        if(empty($data)){            $this->error('没有任何改变');        }        Db::startTrans();        $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);        if($update_rs === false){            Db::rollback();            $this->error('修改资料失败');        }        //task任务        //上传本人头像        if(isset($data['avatar'])&& $data['avatar'] != config('site.domain_cdnurl').'/avatar.png'){            $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);            if($task_rs === false){                Db::rollback();                $this->error('完成任务失败');            }        }        Db::commit();        $this->success();    }    /**     * 修改会员权限     */    public function setpower()    {        $is_vip = $this->is_vip($this->auth->id);        if(!$is_vip){            $this->error('VIP才能设置隐私权限');        }        $data = [            'yinsi'   => input('yinsi',0),            'yinshen' => input('yinshen',0),            'wuhen'   => input('wuhen',0),        ];        $update_rs = Db::name('user_power')->where('id',$this->auth->id)->update($data);        $this->success();    }    /*     * 修改用户的坐标     * */    public function change_longlat(){        $longitude = input_post('longitude',0);        $latitude  = input_post('latitude',0);        $cityname  = input_post('cityname','');        $plat_unique_id  = input_post('plat_unique_id','');        if(empty($longitude) || empty($latitude) || empty($cityname)){            $this->error();        }        $data = [            'longitude' => $longitude,            'latitude'  => $latitude,            'cityname'  => $cityname,            'plat_unique_id'  => $plat_unique_id,        ];        Db::name('user')->where('id',$this->auth->id)->update($data);        $this->success();    }    /**     * 修改手机号     *     * @ApiMethod (POST)     * @param string $mobile   手机号     * @param string $captcha 验证码     */    public function changemobile()    {        $user = $this->auth->getUser();        $oldcaptcha = $this->request->request('oldcaptcha');        $mobile = $this->request->request('mobile');        $captcha = $this->request->request('captcha');        if (!$oldcaptcha || !$mobile || !$captcha) {            $this->error(__('Invalid parameters'));        }        if (!Validate::regex($mobile, "^1\d{10}$")) {            $this->error(__('Mobile is incorrect'));        }        if($user->mobile == $mobile){            $this->error('新手机号不能与旧手机号相同');        }        if (\app\common\model\User::where('mobile', $mobile)->find()) {            $this->error(__('Mobile already exist'));        }        $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');        if (!$result) {            $this->error(__('Captcha is incorrect'));        }        $result = Sms::check($mobile, $captcha, 'changemobile');        if (!$result) {            $this->error(__('Captcha is incorrect'));        }        $verification = $user->verification;        $verification->mobile = 1;        $user->verification = $verification;        $user->mobile = $mobile;        $user->save();        Sms::flush($user->mobile, 'changemobile');        Sms::flush($mobile, 'changemobile');        $this->success();    }    /**     * 微信注册来的,绑定手机号     *     * @ApiMethod (POST)     * @param string $mobile   手机号     * @param string $captcha 验证码     */    public function bindmobile()    {        $user = $this->auth->getUser();        $mobile = $this->request->request('mobile');        $captcha = $this->request->request('captcha');        if(!empty($this->auth->mobile)){            $this->error('已经绑定了手机号');        }        if (!$mobile || !$captcha) {            $this->error(__('Invalid parameters'));        }        if (!Validate::regex($mobile, "^1\d{10}$")) {            $this->error(__('Mobile is incorrect'));        }        if (\app\common\model\User::where('mobile', $mobile)->find()) {            $this->error('该手机号已被其他用户绑定');        }        $result = Sms::check($mobile, $captcha, 'changemobile');        if (!$result) {            $this->error(__('Captcha is incorrect'));        }        $verification = $user->verification;        $verification->mobile = 1;        $user->verification = $verification;        $user->mobile = $mobile;        $user->save();        Sms::flush($mobile, 'changemobile');        $this->success('success',$this->userInfo('return'));    }    /**     * 手机注册来的,绑定微信     *     * @ApiMethod (POST)     * @param string $wechat_openid     */    public function bindopenid()    {        $wechat_openid = $this->request->request('wechat_openid');        if (!$wechat_openid) {            $this->error(__('Invalid parameters'));        }        if(!empty($this->auth->wechat_openid)){            $this->error('已经绑定了微信号');        }        $otherUserWhere['wechat_openid'] = $wechat_openid;        $otherUserWhere['id'] = ['neq',$this->auth->id];        if (\app\common\model\User::where($otherUserWhere)->find()) {            $this->error('该微信号已被其他用户绑定');        }        $user = $this->auth->getUser();        $user->wechat_openid = $wechat_openid;        $user->save();        $this->success('success',$this->userInfo('return'));    }    /**     * 重置密码     *     * @ApiMethod (POST)     * @param string $mobile      手机号     * @param string $newpassword 新密码     * @param string $captcha     验证码     */    public function resetpwd()    {        //$type = input("type");        $type = 'mobile';        $mobile = input("mobile");        $email = input("email");        $newpassword = input("newpassword");        $captcha = input("captcha");        if (!$newpassword || !$captcha) {            $this->error(__('Invalid parameters'));        }        if ($type == 'mobile') {            if (!Validate::regex($mobile, "^1\d{10}$")) {                $this->error(__('Mobile is incorrect'));            }            $user = \app\common\model\User::getByMobile($mobile);            if (!$user) {                $this->error(__('User not found'));            }            $ret = Sms::check($mobile, $captcha, 'resetpwd');            if (!$ret) {                $this->error(__('Captcha is incorrect'));            }            Sms::flush($mobile, 'resetpwd');        } else {            if (!Validate::is($email, "email")) {                $this->error(__('Email is incorrect'));            }            $user = \app\common\model\User::getByEmail($email);            if (!$user) {                $this->error(__('User not found'));            }            $ret = Ems::check($email, $captcha, 'resetpwd');            if (!$ret) {                $this->error(__('Captcha is incorrect'));            }            Ems::flush($email, 'resetpwd');        }        //模拟一次登录        $this->auth->direct($user->id);        $ret = $this->auth->changepwd($newpassword, '', true);        if ($ret) {            $this->success(__('Reset password successful'));        } else {            $this->error($this->auth->getError());        }    }    /**     * 修改密码     *     * @ApiMethod (POST)     * @param string $newpassword 新密码     * @param string $oldpassword 旧密码     */    public function changepwd(){        $newpassword = input('newpassword');        $oldpassword = input('oldpassword','');        if (!$newpassword) {            $this->error(__('Invalid parameters'));        }        if($this->auth->password && empty($oldpassword)){            $this->error('原密码必填');        }        if(empty($this->auth->password)){            $ret = $this->auth->changepwd($newpassword, '', true);        }else{            $ret = $this->auth->changepwd($newpassword,$oldpassword,false);        }        if ($ret) {            $this->success('设置密码成功');        } else {            $this->error($this->auth->getError());        }    }    /**     * 记录当前登陆的设备ID,设备信息,IP等     */    public function changeDeviceIp()    {        // 接口防并发        if (!$this->apiLimit(1, 5000)) {            return ;        }        $user = $this->auth->getUser();        $ip = request()->ip();        $deviceId = $this->request->request('device_id','');        $phoneModel = $this->request->request('phone_model','');        $brand = $this->request->request('brand','');        $apiVersion = $this->request->request('api_version','');        $deviceOs = $this->request->request('device_os','');        if ($ip !== $user->loginip){            $update = [];            $update['id'] = $user->id;            $update['loginip'] = $ip;            \app\common\model\User::update($update);        }        $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);        if (empty($userDeviceInfo)){            $userDeviceInfo = new UserDeviceInfo();            $userDeviceInfo->user_id = $user->u_id;        }        $userDeviceInfo->device_os = $deviceOs;        $userDeviceInfo->device_id = $deviceId;        $userDeviceInfo->phone_model = $phoneModel;        $userDeviceInfo->brand = $brand;        $userDeviceInfo->api_version = $apiVersion;        $userDeviceInfo->save();        //首页接口调用,这里不反回信息//        $this->success("更新成功!");    }    //假注销    public function cancleUser(){        if (!$this->request->isPost()) {            $this->error(__('Invalid parameters'));        }        //退出im//        $tenIm = new Tenim();//        $tenIm->loginoutim($this->auth->id);        Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);        $this->auth->logout();        $this->success('注销成功');    }    //公众号获取openid    public function getUserOpenid_gzh(){        $configValue = Service::getConfig('wechat');        $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);        $rs = $wechat->getOpenid();        $this->success('success',$rs);    }    /**     * 微信内H5-JSAPI支付     */    public function jssdkBuildConfig() {        $url = $this->request->request("url");        $configValue = Service::getConfig('wechat');        $wechat = new Wechat($configValue['app_id'],$configValue['app_secret']);        $sign = $wechat->getSignPackage(urldecode($url));        $this->success("获取成功!",$sign);    }}
 |