|
|
|
@ -4,7 +4,9 @@ namespace App\Http\Controllers\Api; |
|
|
|
|
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
use App\Models\Agent; |
|
|
|
use App\Models\Channel; |
|
|
|
use App\Models\User; |
|
|
|
use App\Models\UserChannel; |
|
|
|
use EasyWeChat\Factory; |
|
|
|
use EasyWeChat\Kernel\Exceptions\InvalidConfigException; |
|
|
|
use Illuminate\Support\Facades\Cache; |
|
|
|
@ -53,19 +55,38 @@ class LoginController extends Controller |
|
|
|
} |
|
|
|
|
|
|
|
// TODO 登录部分待优化
|
|
|
|
$userModel = User::firstOrCreate([ |
|
|
|
$userModel = User::query()->firstOrCreate([ |
|
|
|
'openid' => $res['openid'] ?? '', |
|
|
|
'unionid' => $res['unionid'] ?? '', |
|
|
|
'agent_id' => $agent->id, |
|
|
|
], [ |
|
|
|
//'nickname' => uniqid(), 根据前端判断,不设置默认值
|
|
|
|
//'avatar' => '/static/images/avatar.png'
|
|
|
|
'unionid' => $res['unionid'] ?? '', |
|
|
|
]); |
|
|
|
|
|
|
|
//保存session_key
|
|
|
|
Cache::put('session_key_' . $userModel->id, $res['session_key']); |
|
|
|
|
|
|
|
//TODO 存入初始化数据 user_channel
|
|
|
|
//如果是新增,插入初始数据
|
|
|
|
if ($userModel->wasRecentlyCreated) { |
|
|
|
$channel_ids = UserChannel::where('user_id', $userModel->id)->first(); |
|
|
|
//如果不存在则存入初始数据
|
|
|
|
if (!$channel_ids) { |
|
|
|
$channel_ids = Channel::where([ |
|
|
|
['agent_id', '=', $this->agent_id], |
|
|
|
['pid', '<>', 0], |
|
|
|
]) |
|
|
|
->orderBy('id') |
|
|
|
->limit(8) |
|
|
|
->pluck('id') |
|
|
|
->toArray(); |
|
|
|
//存入user_channel
|
|
|
|
UserChannel::where('user_id', $userModel->id) |
|
|
|
->insert([ |
|
|
|
'user_id' => $userModel->id, |
|
|
|
'channels' => json_encode($channel_ids) |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$token_key = md5($userModel->id . env('APP_KEY')); |
|
|
|
Cache::put($token_key, $userModel->id); |
|
|
|
|