123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Ems;
- use app\common\library\Sms;
- use app\common\library\WxPublic;
- use app\common\model\Area;
- use app\common\model\Guest;
- use app\common\model\User as U;
- use app\common\model\UserSign;
- use app\common\service\ScoreSend;
- use EasyWeChat\Kernel\Exceptions\DecryptException;
- use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
- use fast\Mini;
- use fast\Random;
- use think\Config;
- use think\Db;
- use think\Log;
- use think\Validate;
- use app\common\model\User as UserModel;
- /**
- * 会员接口
- */
- class User extends Api
- {
- protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'third','userminilogin','minilogin','wxp'];
- protected $noNeedRight='*';
- public function _initialize()
- {
- parent::_initialize();
- if (!Config::get('fastadmin.usercenter')) {
- $this->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正式会员")
- * @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=wenda_num,description=问答记录数量)
- * @ApiReturnParams (name=follow_count,description=关注数量)
- * @ApiReturnParams (name=has_answered,description=是否已答过题)
- * @ApiReturnParams (name=userinfo[custom][key],description=自定义资料key)
- * @ApiReturnParams (name=userinfo[custom][title],description=自定义资料名称)
- * @ApiReturnParams (name=userinfo[custom][value],description=自定义资料值)
- */
- public function index()
- {
- $user=$this->auth->getUser();
- if(!$user['userinfo']){
- $user->userinfo()->save([]);
- }
- $user=$user
- ->append([
- 'verification',
- 'province',
- 'city',
- 'county',
- 'userinfo',
- 'has_answered'
- ]);
- $user['wenda_num']=$user->pointUser()->count();
- $user['follow_count']=$user->follow()->count();
- $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()];
- $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=username,description="用户名")
- * @ApiParams (name=password,description="密码")
- * @ApiParams (name=email,description="邮箱")
- * @ApiParams (name=mobile,description="手机号")
- * @ApiParams (name=code,description="验证码")
- */
- 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 = ['userinfo' => $this->auth->getUserinfo()];
- $this->success(__('Sign up successful'), $data);
- } else {
- $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="custom[xxxx]",description="自定义资料放这里")
- * @ApiReturnParams (name=score,description="赠送的积分数量")
- */
- public function profile(ScoreSend $score)
- {
- $data=$this->_validate([
- 'avatar|头像'=>['require','url'],
- 'nickname|昵称'=>['require','max:12'],
- 'age|年龄'=>['require','integer','gt:0'],
- //'county_id|地区'=>['require','integer','gt:0'],
- 'county_id|地区'=>['require'],
- 'gender|性别'=>['require','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['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();
- $custom=array_column($user->userinfo->custom,'value','key');
- foreach (config('site.userApprove')?:[] as $key=>$value){
- if(!empty($data['custom'][$key])){
- $custom[$key]=$data['custom'][$key];
- }
- }
- if($custom) {
- $user->userinfo->save(['custom' => $custom]);
- }
- $scoreNum=$score->setUser($user)->setField('score')->setMemo('完善资料')->setConfig('score_editInfo')->onlyOne();
- Db::commit();
- $this->success('',[
- 'score'=>$scoreNum,
- ]);
- }
- /**
- * 退出登录
- * @ApiMethod (POST)
- */
- public function logout()
- {
- if (!$this->request->isPost()) {
- $this->error(__('Invalid parameters'));
- }
- $this->auth->logout();
- $this->success(__('Logout successful'));
- }
- /**
- * 修改邮箱
- *
- * @ApiMethod (POST)
- * @param string $email 邮箱
- * @param string $captcha 验证码
- */
- public function changeemail()
- {
- $user = $this->auth->getUser();
- $email = $this->request->post('email');
- $captcha = $this->request->post('captcha');
- if (!$email || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if (!Validate::is($email, "email")) {
- $this->error(__('Email is incorrect'));
- }
- if (UserModel::where('email', $email)->where('id', '<>', $user->id)->find()) {
- $this->error(__('Email already exists'));
- }
- $result = Ems::check($email, $captcha, 'changeemail');
- if (!$result) {
- $this->error(__('Captcha is incorrect'));
- }
- $verification = $user->verification;
- $verification->email = 1;
- $user->verification = $verification;
- $user->email = $email;
- $user->save();
- Ems::flush($email, 'changeemail');
- $this->success();
- }
- /**
- * 修改手机号
- *
- * @ApiMethod (POST)
- * @param string $mobile 手机号
- * @param string $captcha 验证码
- */
- public function changemobile()
- {
- $user = $this->auth->getUser();
- $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 (UserModel::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
- $this->error(__('Mobile already exists'));
- }
- $result = Sms::check($mobile, $captcha, 'changemobile');
- if (!$result) {
- $this->error(__('Captcha is incorrect'));
- }
- $user->mobile=$mobile;
- $user->save();
- Sms::flush($mobile, 'changemobile');
- $this->success();
- }
- /**
- * 第三方登录
- *
- * @ApiMethod (POST)
- * @param string $platform 平台名称
- * @param string $code Code码
- */
- public function third()
- {
- $this->error('功能已禁用');
- $url = url('user/index');
- $platform = $this->request->post("platform");
- $code = $this->request->post("code");
- $config = get_addon_config('third');
- if (!$config || !isset($config[$platform])) {
- $this->error(__('Invalid parameters'));
- }
- $app = new \addons\third\library\Application($config);
- //通过code换access_token和绑定会员
- $result = $app->{$platform}->getUserInfo(['code' => $code]);
- if ($result) {
- $loginret = \addons\third\library\Service::connect($platform, $result);
- if ($loginret) {
- $data = [
- 'userinfo' => $this->auth->getUserinfo(),
- 'thirdinfo' => $result
- ];
- $this->success(__('Logged in successful'), $data);
- }
- }
- $this->error(__('Operation failed'), $url);
- }
- /**
- * 重置密码
- *
- * @ApiMethod (POST)
- * @param string $mobile 手机号
- * @param string $newpassword 新密码
- * @param string $captcha 验证码
- */
- public function resetpwd()
- {
- $type = $this->request->post("type");
- $mobile = $this->request->post("mobile");
- $email = $this->request->post("email");
- $newpassword = $this->request->post("newpassword");
- $captcha = $this->request->post("captcha");
- if (!$newpassword || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if ($type == 'mobile') {
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- $user = UserModel::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 = UserModel::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)
- * @ApiParams (name=code,description="小程序code")
- * @ApiParams (name=mob_brand,description="手机品牌")
- * @ApiParams (name=mob_model,description="手机型号")
- */
- public function minilogin(){
- $data=input();
- $this->validate($data,[
- 'code'=>'require',
- 'mob_brand'=>'require',
- 'mob_model'=>'require',
- //'encryptedData'=>'require',
- //'iv'=>'require',
- ]);
- $session=Mini::mini()->auth->session($data['code']);
- //$decryptedData = Mini::mini()->encryptor->decryptData($session, $data['iv'], $data['encryptedData']);
- $user=\app\admin\model\User::where('openid',$session['openid'])->find();
- user_log('minilogin',"code:{$data['code']},session:".json_encode($session).',user:'.json_encode($user));
- //$user1=\app\admin\model\User::where('mobile',$decryptedData['phoneNumber'])->find();
- //$user=$user?:$user1;
- if(!$user){
- $ret = $this->auth->register($username=session_create_id(), '', '', $decryptedData['phoneNumber']??'', [
- 'openid'=>$session['openid'],
- 'mob_brand'=>$data['mob_brand'],
- 'nickname'=>'游客',
- 'mob_model'=>$data['mob_model'],
- 'unionid'=>$session['unionid']??'',
- ]);
- if ($ret) {
- $data = ['userinfo' => $this->auth->getUserinfo()];
- $this->success(__('Sign up successful'), $data);
- } else {
- $this->error($this->auth->getError());
- }
- }else{
- $user['unionid']=$session['unionid']??'';
- $user['mob_brand']=$data['mob_brand'];
- $user['mob_model']=$data['mob_model'];
- $user->save();
- }
- $this->auth->direct($user['id']);
- $data = ['userinfo' => $this->auth->getUserinfo()];
- $this->success(__('successful'), $data);
- }
- /**
- * 授权获取手机号
- * @ApiParams (name=code,description="小程序code")
- * @ApiParams (name=encryptedData,description="encryptedData")
- * @ApiParams (name=iv,description="iv")
- */
- public function wx_mobile(){
- $data=$this->_validate([
- 'code'=>'require',
- 'encryptedData'=>'require',
- 'iv'=>'require',
- ]);
- $user=$this->auth->getUser();
- try {
- $session=Mini::mini()->auth->session($data['code']);
- $decryptedData = Mini::mini()->encryptor->decryptData($session, $data['iv'], $data['encryptedData']);
- $user['mobile']=$decryptedData['phoneNumber'];
- $user->save();
- } catch (DecryptException | InvalidConfigException $e) {
- $this->error(sprintf("授权失败(%s)",$e->getMessage()));
- }
- $this->success('',$user);
- }
- /**
- * 关注取消关注博主
- * @ApiParams (name=id,description=博主ID)
- * @ApiParams (name=status,description=1关注2取消)
- */
- public function follow(){
- $data=$this->_validate([
- 'status'=>'require',
- 'id'=>'require',
- ]);
- $user=$this->auth->getUser();
- if($data['status']==1) {
- if(!Guest::find($data['id'])){
- $this->error('博主不存在');
- }
- if($user->follow()->guest($data['id'])->find()){
- $this->error('您已关注');
- }
- $user->follow()->attach($data['id']);
- }else{
- $user->follow()->detach($data['id']);
- }
- $this->success();
- }
- /**
- * 关注列表
- * @ApiParams (name=limit,description=每页数量)
- * @ApiParams (name=page,description=第几页)
- * @ApiReturnParams (name=id,description=博主ID)
- * @ApiReturnParams (name=name,description=名称)
- * @ApiReturnParams (name=logo,description=logo)
- */
- public function my_follow(){
- $user=$this->auth->getUser();
- $list=$user->follow()
- ->paginate(input('limit'));
- $this->success('',$list);
- }
- /**
- * 已签到日期列表
- * @ApiParams (name=date,description="月份,2020-02")
- */
- public function sign_list(){
- $data=$this->_validate([
- 'date'=>['require','date'],
- ]);
- $user=$this->auth->getUser();
- $data=$user->sign()->where('date','>=',$data['date'])->column('date');
- $this->success('',$data);
- }
- /**
- * 签到
- * @ApiReturnParams (name=score,description="赠送积分数量")
- */
- public function sign(ScoreSend $send){
- $user=$this->auth->getUser();
- $date=date('Y-m-d');
- Db::startTrans();
- if($user->sign()->where('date',$date)->find()){
- Db::rollback();
- $this->error('您已签到');
- }
- $sign=$user->sign()->save([
- 'date'=>$date,
- ]);
- $finally=['score'=>0];
- $finally['score']=$send->setUser($user)->setField('score')->setMemo("[{$date}]签到")->setObject(['user_sign',$sign['id']])->setConfig('score_sign')->onlyOne();
- Db::commit();
- $this->success('',$finally);
- }
- /**
- * 微信公众号登录获取参数
- * @ApiParams (name=redirect_uri,description=跳转链接)
- */
- public function wxp(WxPublic $wxPublic){
- $this->validate($data=input(),[
- 'redirect_uri'=>['require'],
- ]);
- $url=$wxPublic->getApp()->oauth->redirect($data['redirect_uri'])->getTargetUrl();
- $this->success('',[
- 'url'=>$url,
- ]);
- }
- /**
- * 微信公众号登录
- * @ApiParams (name=code,description=微信code)
- * @ApiReturnParams (name=userinfo.nickname,description=昵称)
- * @ApiReturnParams (name=userinfo.avatar,description=头像)
- */
- public function wxp_handle(WxPublic $wxPublic){
- $this->validate($data=input(),[
- 'code'=>['require'],
- ]);
- $user=$wxPublic->getApp()->oauth->user();
- $localUser=U::where('wxp_openid',$user->getId())->find();
- if($localUser){
- $this->auth->direct($localUser['id']);
- }else{
- $reg=$this->auth->register(session_create_id(),session_create_id(),'','',[
- 'wxp_openid'=>$user->getId(),
- 'nickname'=>$user->getNickname(),
- 'avatar'=>$user->getAvatar(),
- ]);
- if(!$reg){
- $this->error('注册失败'.$this->auth->getError());
- }
- }
- $data = [
- 'userinfo' => $this->auth->getUserinfo(),
- ];
- $this->success(__('Logged in successful'), $data);
- }
- }
|