error(__('User center already closed')); } } /** * 会员信息 * @ApiReturnParams (name=id,description=用户ID) * @ApiReturnParams (name=username,description=用户名) * @ApiReturnParams (name=nickname,description=昵称) * @ApiReturnParams (name=mobile,description=手机号) * @ApiReturnParams (name=avatar,description=头像) * @ApiReturnParams (name=age,description=年龄) * @ApiReturnParams (name=gender,description="性别1男2女") * @ApiReturnParams (name=level_text,description=会员级别标题) * @ApiReturnParams (name=level,description="会员级别,0普通用户10钻石20白金30金卡") * @ApiReturnParams (name=level_expire,description="会员级别到期时间") * @ApiReturnParams (name=money,description=余额) * @ApiReturnParams (name=has_follow,description=是否关注) * @ApiReturnParams (name=verification,description=认证信息) * @ApiReturnParams (name=province,description=省对象) * @ApiReturnParams (name=city,description=市对象) * @ApiReturnParams (name=county,description=县对象) * @ApiReturnParams (name=live_addr,description=居住地) * @ApiReturnParams (name=com_name,description=公司名称) * @ApiReturnParams (name=wx_account,description=微信号) * @ApiReturnParams (name=level_expire,description=会员到期时间) */ public function index() { $user=$this->auth->getUser(); if(!$user['userinfo']){ $user->userinfo()->save([]); } $user=$user ->append([ 'verification', 'userinfo', ]); $this->success('', $user); } /** * 会员登录 * * @ApiMethod (POST) * @param string $account 账号 * @param string $password 密码 */ public function login() { $account = $this->request->post('account'); $password = $this->request->post('password'); if (!$account || !$password) { $this->error(__('Invalid parameters')); } $ret = $this->auth->login($account, $password); if ($ret) { $data = ['userinfo' => $this->auth->getUserinfo()]; UserLoginRange::addRange($this->auth->getUser()); $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 手机验证码登录 * * @ApiMethod (POST) * @param string $mobile 手机号 * @param string $captcha 验证码 */ public function mobilelogin() { $mobile = $this->request->post('mobile'); $captcha = $this->request->post('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 = UserModel::getByMobile($mobile); if ($user) { if ($user->status != 'normal') { $this->error(__('Account is locked')); } //如果已经有账号则直接登录 $ret = $this->auth->direct($user->id); } else { $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []); } if ($ret) { Sms::flush($mobile, 'mobilelogin'); $data = ['userinfo' => $this->auth->getUserinfo()]; $this->success(__('Logged in successful'), $data); } else { $this->error($this->auth->getError()); } } /** * 注册会员 * * @ApiMethod (POST) * @ApiParams (name=mobile,description="手机号") * @ApiParams (name=username,description="用户名") * @ApiParams (name=password,description="密码") * @ApiParams (name=password_confirm,description="确认密码") * @ApiParams (name=live_addr,description="居住地") * @ApiParams (name=com_name,description="公司名称") * @ApiParams (name=email,description="邮箱") * @ApiParams (name=code,description="验证码") * @ApiParams (name=openid,description="绑定的微信openid") * @ApiParams (name=frommanager,description="绑定的销售经理ID") */ public function register() { $input=$this->_validate([ 'mobile'=>['require','mobile'], 'username'=>['require'], 'password|密码'=>['require','min:6'], 'password_confirm|确认密码'=>['require','confirm:password'], 'live_addr'=>['max:200'], 'com_name'=>['max:100'], 'email'=>['email'], 'code'=>['require','integer'], 'frommanager|来源'=>['integer','gt:0'], ]); $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')); } if(!empty($input['frommanager']) && is_numeric($input['frommanager'])){ $admin=Admin::find($input['frommanager']); if(!$admin && !$admin->is_seller){ $this->error('无效的销售经理'); } }else{ $input['frommanager']=null; } Db::startTrans(); $ret = $this->auth->register($username, $password, $email?:null, $mobile?:null,[ 'live_addr'=>$input['live_addr']??null, 'com_name'=>$input['com_name']??null, 'openid' =>$input['openid']??null, 'admin_id'=>$input['frommanager'], ]); if ($ret) { Db::commit(); $data = ['userinfo' => $this->auth->getUserinfo()]; $this->success(__('Sign up successful'), $data); } else { Db::rollback(); $this->error($this->auth->getError()); } } /** * 修改会员个人信息 * * @ApiMethod (POST) * @ApiParams (name=avatar,description=头像地址) * @ApiParams (name=nickname,description=昵称) * @ApiParams (name=bio,description=个人简介) * @ApiParams (name=age,description=年龄) * @ApiParams (name=gender,description="性别1男2女") * @ApiParams (name=county_id,description=区县ID) * @ApiParams (name=wx_account,description=微信号) * @ApiParams (name=live_addr,description=居住地) * @ApiParams (name=com_name,description=公司名称) */ public function profile() { $data=$this->_validate([ 'avatar|头像'=>['url'], 'nickname|昵称'=>['max:12'], 'age|年龄'=>['integer','gt:0'], //'county_id|地区'=>['require','integer','gt:0'], 'county_id|地区'=>['integer'], 'gender|性别'=>['integer','in:1,2'], 'bio|性别'=>['max:100'], ]); $user = $this->auth->getUser(); Db::startTrans(); $user= UserModel::lock(true)->find($user->id); $nickname = $data['nickname']??''; $bio = $data['bio']; $avatar = $data['avatar']??''; if ($nickname) { /*$exists = UserModel::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find(); if ($exists) { $this->error(__('Nickname already exists')); }*/ $user->nickname = $nickname; } if($bio) { $user->bio = $bio; } if($avatar) { $user->avatar = $avatar; } if(!empty($data['age'])){ $user->age=$data['age']; } if(isset($data['gender'])){ $user->gender=$data['gender']; } if(!empty($data['wx_account'])){ $user['wx_account']=$data['wx_account']; } if(!empty($data['live_addr'])){ $user['live_addr']=$data['live_addr']; } if(!empty($data['com_name'])){ $user['com_name']=$data['com_name']; } if(!empty($data['county_id'])){ $county=Area::area()->where('name|shortname',$data['county_id'])->find(); if(!$county) { $this->error('地区不存在'); } $user->county_id=$county['id']; $user->city_id=$county['pid']; $user->province_id=Area::where('id',$county['pid'])->value('pid'); } $user->save(); Db::commit(); $this->success('',$user); } /** * 生成绑定码 * @ApiParams (name=phone,description="手机号,event等于1时需要") * @ApiParams (name=sms_code,description="手机验证码,event等于1时需要") * @ApiParams (name=event,description="1注册2个人中心绑定3扫码登录") * @ApiParams (name=callback_url,description="扫完码跳转url") * @ApiReturnParams (name=url,description=二维码链接) * @ApiReturnParams (name=key,description="扫码登录key") */ public function wx_bind_qr(){ $this->_validate([ 'callback_url'=>['require','url'], ]); $data=$this->checkBindParam(); if($data['event']==3){ $data['phone']=session_create_id(); Session::set('wx_scan_login',$data['phone']); } $info=[]; $info['url']=WxOpenService::getBindUrl($data['phone'],$data['event'],$data['callback_url']); $this->success('',$info); } protected function checkBindParam(){ $data=$this->_validate([ 'phone|手机号'=>['requireIf:event,1','mobile'], 'sms_code|手机验证码'=>['requireIf:event,1','integer'], 'event'=>['require','in:1,2,3'] ]); if($data['event']==2){ $user=$this->auth->getUser(); if(!$user){ $this->error('请登录'); } $data['phone']=$user['mobile']; } if($data['event']==1){ $checkSms=Sms::check($data['phone'],$data['sms_code'],'register'); if(!$checkSms){ $this->error('验证码错误'); } } return $data; } /** * 检查是否已绑定 * @ApiParams (name=phone,description="手机号,event等于1时需要") * @ApiParams (name=sms_code,description="手机验证码,event等于1时需要") * @ApiParams (name=event,description="1注册2个人中心绑定3扫码登录") * @ApiParams (name=code,description="微信回传的code") * @ApiReturnParams (name=bind,description="是否绑定成功") * @ApiReturnParams (name=openid,description="openid") * @ApiReturnParams (name=user,description="用户信息,仅扫码登录有") * @ApiReturnParams (name="user.token",description="token") */ public function wx_bind_qr_check(){ $this->_validate([ 'code'=>['require'], ]); $data=$this->checkBindParam(); if($data['event']==1){ $key=$data['phone']; }elseif ($data['event']==2){ $key=$this->auth->getUser()['mobile']; }elseif($data['event']==3){ $key=Session::get('wx_scan_login')?:session_create_id(); }else{ $key=''; } if(empty($key)){ $this->error('马苏德'); } list($res,$openid)=WxOpenService::bind($key,$data['event']); if(!$res){ $this->error($openid); } $info=[ 'bind'=>false, 'openid'=>'', 'user'=>null ]; if(in_array($data['event'],[1,2])) { $info['openid'] = $openid; if ($info['openid']) { $info['bind'] = true; } }elseif ($data['event']==3){ $user= UserModel::where('openid',$openid)->find(); if(!$user){ $this->error('用户未注册',['unregister'=>1]); } if($user['status']=='hidden'){ $this->error('用户被禁用'); } $this->auth->direct($user['id']); UserLoginRange::addRange($this->auth->getUser()); $info['user']=$this->auth->getUserinfo(); } $this->success('',$info); } /** * 密码方式修改密码 * @ApiParams (name=old_pwd,description=旧密码) * @ApiParams (name=new_pwd,description=新密码) * @ApiParams (name=new_pwd_confirm,description=新密码确认密码) */ public function changepwd(){ $user=$this->auth->getUser(); $data=$this->_validate([ 'old_pwd|旧密码'=>['require','min:6'], 'new_pwd|新密码'=>['require','min:6'], 'new_pwd_confirm|确认新密码'=>['require','min:6','confirm:new_pwd'], ]); if ($user->password != $this->auth->getEncryptPassword($data['old_pwd'], $user->salt)) { $this->error('密码错误'); } $this->auth->changepwd($data['new_pwd'],'',true); $this->success(); } /** * 重置密码 * * @ApiMethod (POST) * @param string mobile 手机号重置 * @param string email 邮箱重置 * @param string newpassword 新密码 * @param string newpassword_confirm 新密码 * @param string captcha 验证码 */ public function resetpwd() { $this->_validate([ 'newpassword'=>['require','min:6'], 'newpassword_confirm'=>['require','min:6'], ]); $mobile = $this->request->post("mobile"); $email = $this->request->post("email"); $newpassword = $this->request->post("newpassword"); $newpassword_confirm = $this->request->post("newpassword_confirm"); $captcha = $this->request->post("captcha"); if (!$newpassword || !$captcha || !$newpassword_confirm) { $this->error(__('Invalid parameters')); } if($newpassword!==$newpassword_confirm){ $this->error('两次密码不一致'); } $type = $mobile?'mobile':'email'; //验证Token if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) { $this->error(__('Password must be 6 to 30 characters')); } if ($type == 'mobile') { if (!Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } $user = UserModel::getByMobile($mobile); if (!$user) { $this->error(__('用户不存在')); } $ret = Sms::check($mobile, $captcha, 'resetpwd'); if (!$ret) { $this->error(__('Captcha is incorrect')); } Sms::flush($mobile, 'resetpwd'); } elseif($type=='email') { if (!Validate::is($email, "email")) { $this->error(__('Email is incorrect')); } $user = UserModel::getByEmail($email); if (!$user) { $this->error(__('用户不存在')); } $ret = Ems::check($email, $captcha, 'resetpwd'); if (!$ret) { $this->error(__('Captcha is incorrect')); } Ems::flush($email, 'resetpwd'); }else{ $this->error('无法完成重置'); } //模拟一次登录 $this->auth->direct($user->id); $ret = $this->auth->changepwd($newpassword, '', true); if ($ret) { $this->success(__('Reset password successful')); } else { $this->error($this->auth->getError()); } } /** * 记录登录时长 */ public function ls(){ $user=$this->auth->getUser(); $user->setInc('login_seconds',5); $this->success(); } /** * 增加页面访问次数 */ public function lsp(){ $user=$this->auth->getUser(); $user->setInc('page_times'); #访问支付转化率 $nowNum=PayTransPerSvc::add($user); $this->success('',compact('nowNum')); } }