You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
3 years ago
|
<?php
|
||
|
/**
|
||
|
* Date: 2020/8/5 9:41
|
||
|
* User: chenlong <vip_chenlong@163.com>
|
||
|
*/
|
||
|
|
||
|
namespace app\common\traits\api;
|
||
|
|
||
|
use app\common\ResponseJson;
|
||
|
use app\common\SdException;
|
||
|
use sdModule\common\Sc;
|
||
|
use think\facade\Db;
|
||
|
use weChat\common\Login;
|
||
|
|
||
|
/**
|
||
|
* 微信用户登录操作
|
||
|
* Class WeChatUser
|
||
|
* @package app\common\controller\apiTrait
|
||
|
*/
|
||
|
trait WeChatUser
|
||
|
{
|
||
|
public function h5AndLetLogin($code = '')
|
||
|
{
|
||
|
if (empty($code)) throw new SdException('请求错误!');
|
||
|
|
||
|
if (!$user = Login::getUserInfo($code)) throw new SdException('授权登录失效,请重新授权。');
|
||
|
|
||
|
return Sc::redis()->lock(function () use ($user){
|
||
|
if ($user_data = Db::name('user')->where('wx_openid', $user['openid'])->field('id,nickname,avatar')->find()){
|
||
|
goto end;
|
||
|
}
|
||
|
|
||
|
$id = Db::name('user')->insertGetId([
|
||
|
'wx_openid' => $user['openid'],
|
||
|
"nickname" => $user["nickname"],
|
||
|
"avatar" => $user["headimgurl"],
|
||
|
'create_time' => datetime(),
|
||
|
'update_time' => datetime(),
|
||
|
]);
|
||
|
|
||
|
if (!$id) throw new SdException('信息错误');
|
||
|
$user_data = Db::name('user')->where('id', $id)->find();
|
||
|
|
||
|
end:
|
||
|
$user_data['token'] = Sc::jwt(['user_id' => $user_data['id']])->getRefresh(30)
|
||
|
->setExp(3600)->getToken();
|
||
|
return ResponseJson::success($user_data);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|