error(__('Invalid parameters')); } $ret = $this->auth->login($account, $password); if ($ret) { $data = $this->auth->getUserinfo_simple(); $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 = [ 'plat_unique_id' => input('plat_unique_id','','trim'), ]; $ret = $this->auth->register('', '', '', $mobile, $extend); } if ($ret) { Sms::flush($mobile, 'mobilelogin'); $data = $this->auth->getUserinfo_simple(); $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } //微信登录,预先假注册 public function wechatlogin(){ $code = $this->request->param('code',''); if(!$code){ $this->error(__('Invalid parameters')); } //微信 $wechat = new Wechat(); $wxuserinfo = $wechat->getAccessToken($code); if(!$wxuserinfo){ $this->error('openid获取失败'); } if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){ $this->error('openid获取失败'); } $openid = $wxuserinfo['openid']; //检查用户 $user = Db::name('user')->where('wechat_openid',$openid)->find(); if ($user) { if ($user['status'] == -1) { $this->error('账户已注销'); } if ($user['status'] != 1) { $this->error(__('Account is locked')); } //如果已经有账号则直接登录 $ret = $this->auth->direct($user['id']); if ($ret) { $userInfo = $this->auth->getUserinfo_simple(); $userInfo['is_register'] = 0; $userInfo['code'] = $code; $this->success(__('Logged in successful'), $userInfo); } else { $this->error($this->auth->getError()); } } else { //记录code和openid,绑定手机号的时候更新openid $wechatCodeData = [ 'code' => $code, 'openid' => $openid, 'createtime' => time(), ]; $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find(); if (empty($wechatCode)) { Db::name('wechat_code')->insertGetId($wechatCodeData); } else { Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData); } //直接返回 $userInfo = []; $userInfo['is_register'] = 1; $userInfo['code'] = $code; $this->success('获取信息成功', $userInfo); } } /** * 运营商一键登录 */ 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 { $extend = [ 'plat_unique_id' => input('plat_unique_id','','trim'), ]; $ret = $this->auth->register('', '', '', $mobile, $extend); $is_register = 1; } if ($ret) { $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple()); } else { $this->error($this->auth->getError()); } // 用户登录逻辑 === 结束 } } else { $this->error("登录失败,请用验证码方式登录!"); } } //苹果登录+预注册 public function applelogin(){ $iosUserId = $this->request->param('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']); if ($ret) { $userInfo = $this->auth->getUserinfo_simple(); $userInfo['is_register'] = 0; $userInfo['ios_user_id'] = $iosUserId; $this->success(__('Logged in successful'), $userInfo); } else { $this->error($this->auth->getError()); } } else { //直接返回 $userInfo = []; $userInfo['is_register'] = 1; $userInfo['ios_user_id'] = $iosUserId; $this->success('获取信息成功', $userInfo); } } //用户详细资料 public function userInfo(){ $info = $this->auth->getUserinfo(); $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','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; } //关于我,是否需要审核 $user_bio_audit_switch = config('site.user_bio_audit_switch'); if(isset($data['bio']) && !empty($data['bio']) && $user_bio_audit_switch == 1){ $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('status',0)->find(); if(!empty($check_exist)){ $this->error('信息已提交等待审核,请勿重复提交'); } if($data['bio'] != $this->auth->bio){ $bio_data = [ 'user_id' => $this->auth->id, 'bio' => $this->auth->bio, 'new_bio' => $data['bio'], 'createtime' => time(), ]; Db::name('user_audit')->insertGetId($bio_data); $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error } unset($data['bio']); } // 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']) && !empty($data['introcode'])){ $intro_user = Db::name('user')->where('introcode',$data['introcode'])->field('id,intro_uid')->find(); if(!empty($intro_user) && empty($this->auth->intro_uid) && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id){ unset($data['introcode']);//别人的邀请码,不能改了自己的 $data['intro_uid'] = $intro_user['id']; //邀请用户注册,给邀请人奖励 if(isset($data['intro_uid']) && !empty($data['intro_uid'])){ $intro_money = config('site.intro_newuser_gift_moneynum') ?: 0; if($intro_money > 0){ $wallet_rs = model('wallet')->lockChangeAccountRemain($data['intro_uid'],'money',$intro_money,63,'邀请'.$this->auth->username); if($wallet_rs['status'] === false){ /*Db::rollback(); $this->error($wallet_rs['msg']);*/ } } } } } unset($data['introcode']); //dump($data); if(empty($data)){ $this->success(); } 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才能设置隐私权限'); } $field_array = ['yinsi','yinshen','wuhen']; $data = []; foreach($field_array as $key => $field){ if(!input('?'.$field)){ continue; } $newone = input($field); $data[$field] = $newone; } $update_rs = Db::name('user_power')->where('user_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',''); $provincename = input_post('provincename',''); //$plat_unique_id = input_post('plat_unique_id',''); /*if(empty($longitude) || empty($latitude) || empty($cityname)){ $this->error(); }*/ $data = []; $longitude && $data['longitude'] = $longitude; $latitude && $data['latitude'] = $latitude; $cityname && $data['cityname'] = $cityname; $provincename && $data['provincename'] = $provincename; //$plat_unique_id && $data['plat_unique_id'] = $plat_unique_id; //传入了城市,但是没传入省,直接省名改空 if(isset($data['cityname']) && !isset($data['provincename'])){ $data['provincename'] = ''; } //没传入城市,但是传入省,直接城市名改空 if(!isset($data['cityname']) && isset($data['provincename'])){ $data['cityname'] = ''; } if(!empty($data)){ Db::name('user')->where('id',$this->auth->id)->update($data); } $this->success(); } //修改用户设备id public function change_plat_unique_id(){ $this->success(); /*$plat_unique_id = input_post('plat_unique_id',''); $data = [ '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')); } Sms::flush($user->mobile, 'changemobile'); Sms::flush($mobile, 'changemobile'); $user->mobile = $mobile; $user->save(); $this->success(); } /** * 苹果注册来的,绑定手机号 * * @ApiMethod (POST) * @param string $mobile 手机号 * @param string $captcha 验证码 */ public function applebindmobile() { $mobile = $this->request->param('mobile'); $captcha = $this->request->param('captcha'); $iosUserId = $this->request->param('ios_user_id',''); if (!$mobile || !$captcha || !$iosUserId) { $this->error(__('Invalid parameters')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } $result = Sms::check($mobile, $captcha, 'changemobile'); if (!$result) { $this->error(__('Captcha is incorrect')); } //检查ios_user_id绑定的用户 $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']); $this->success('success',$this->auth->getUserinfo_simple()); } //新的ios用户 $where = []; $where['mobile'] = $mobile; $userData = Db::name('user')->where($where)->find();//老用户 if (!empty($userData)) { if (empty($userData['ios_user_id'])) { Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id } else { if ($userData['ios_user_id'] != $iosUserId) { $this->error('该手机号已被其他用户绑定'); } } $ret = $this->auth->direct($userData['id']); } else { $extend = [ 'ios_user_id' => $iosUserId, 'plat_unique_id' => input('plat_unique_id','','trim'), ]; $ret = $this->auth->register('', '','', $mobile, $extend); } if (!$ret) { $this->error($this->auth->getError()); } $this->success('success',$this->auth->getUserinfo_simple()); } /** * 微信注册来的,绑定手机号 * * @ApiMethod (POST) * @param string $mobile 手机号 * @param string $captcha 验证码 */ public function bindmobile() { $mobile = $this->request->param('mobile'); $captcha = $this->request->param('captcha'); $code = $this->request->param('code'); if (!$mobile || !$captcha || !$code) { $this->error(__('Invalid parameters')); } if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } $result = Sms::check($mobile, $captcha, 'changemobile'); if (!$result) { $this->error(__('Captcha is incorrect')); } $wechatCodeWhere['code'] = $code; $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find(); if (empty($wechatCode)) { $this->error('请先微信登录'); } //检查appid绑定的用户 $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find(); if ($user) { if ($user['status'] == -1) { $this->error('账户已注销'); } if ($user['status'] != 1) { $this->error(__('Account is locked')); } //如果已经有账号则直接登录 $ret = $this->auth->direct($user['id']); $this->success('success',$this->auth->getUserinfo_simple()); } //新的openid用户 $where = []; $where['mobile'] = $mobile; $userData = Db::name('user')->where($where)->find();//老用户 if (!empty($userData)) { if (empty($userData['wechat_openid'])) { Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid } else { if ($userData['wechat_openid'] != $wechatCode['openid']) { $this->error('该手机号已被其他用户绑定'); } } $ret = $this->auth->direct($userData['id']); } else { $extend = [ 'wechat_openid' => $wechatCode['openid'], 'plat_unique_id' => input('plat_unique_id','','trim'), ]; $ret = $this->auth->register('', '','', $mobile, $extend); } if (!$ret) { $this->error($this->auth->getError()); } $this->success('success',$this->auth->getUserinfo_simple()); } /** * 手机注册来的,绑定微信 * * @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('绑定成功',$this->auth->getUserinfo_simple()); } /** * 重置密码 * * @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 (!$mobile || !$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'); } //模拟一次登录 $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() { } //假注销 public function cancleUser(){ if (!$this->request->isPost()) { $this->error(__('Invalid parameters')); } //退出im // $tenIm = new Tenim(); // $tenIm->loginoutim($this->auth->id); $data = [ 'status' => -1, 'mobile' => 'close_'.$this->auth->mobile, 'wechat_openid' => 'close_'.$this->auth->wechat_openid, 'ios_user_id' => 'close_'.$this->auth->ios_user_id, ]; Db::name('user')->where('id',$this->auth->id)->update($data); $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); } }