|
@@ -10,6 +10,8 @@ use app\common\model\Guest;
|
|
|
use app\common\model\User as U;
|
|
|
use app\common\model\UserSign;
|
|
|
use app\common\service\ScoreSend;
|
|
|
+use app\service\byte_dance\ByteDanceCode2Session;
|
|
|
+use app\service\byte_dance\ByteDanceDecrypt;
|
|
|
use EasyWeChat\Kernel\Exceptions\DecryptException;
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
|
|
|
use fast\Mini;
|
|
@@ -252,351 +254,33 @@ class User extends Api
|
|
|
]);
|
|
|
}
|
|
|
/**
|
|
|
- * 退出登录
|
|
|
- * @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")
|
|
|
+ * 抖音小程序登陆
|
|
|
+ * @ApiParams (name=code,description=code)
|
|
|
+ * @ApiParams (name=encryptedData,description=encryptedData)
|
|
|
+ * @ApiParams (name=iv,description=iv)
|
|
|
*/
|
|
|
- public function wx_mobile(){
|
|
|
+ public function dy_login(ByteDanceCode2Session $code2Session,ByteDanceDecrypt $byteDanceDecrypt){
|
|
|
$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'],
|
|
|
+ 'encryptedData'=>['require'],
|
|
|
+ 'iv'=>['require'],
|
|
|
]);
|
|
|
- $user=$wxPublic->getApp()->oauth->user();
|
|
|
- $localUser=U::where('wxp_openid',$user->getId())->find();
|
|
|
- if($localUser){
|
|
|
- $this->auth->direct($localUser['id']);
|
|
|
+ $info=$code2Session->setCode($data['code'])->get();
|
|
|
+ $byteDanceDecrypt->setEncryptedData($data['encryptedData']);
|
|
|
+ $byteDanceDecrypt->setIv($data['iv']);
|
|
|
+ $byteDanceDecrypt->setSessionKey($info['session_key']);
|
|
|
+ $mobileInfo=$byteDanceDecrypt->get();
|
|
|
+ Db::startTrans();
|
|
|
+ $user= UserModel::where('openid',$info['openid'])->find();
|
|
|
+ if($user){
|
|
|
+ $this->auth->direct($user['id']);
|
|
|
}else{
|
|
|
- $reg=$this->auth->register(session_create_id(),session_create_id(),'','',[
|
|
|
- 'wxp_openid'=>$user->getId(),
|
|
|
- 'nickname'=>$user->getNickname(),
|
|
|
- 'avatar'=>$user->getAvatar(),
|
|
|
+ $this->auth->register(session_create_id(),'','',$mobileInfo['phoneNumber'],[
|
|
|
+ 'openid'=>$info['openid'],
|
|
|
+ 'unionid'=>$info['unionid'],
|
|
|
]);
|
|
|
- if(!$reg){
|
|
|
- $this->error('注册失败'.$this->auth->getError());
|
|
|
- }
|
|
|
}
|
|
|
- $data = [
|
|
|
- 'userinfo' => $this->auth->getUserinfo(),
|
|
|
- ];
|
|
|
- $data['userinfo']['token']=$this->auth->getToken();
|
|
|
+ $data = ['userinfo' => $this->auth->getUserinfo()];
|
|
|
$this->success(__('Logged in successful'), $data);
|
|
|
}
|
|
|
}
|