Browse Source

修改我的频道插入初始化数据逻辑

develop
李可松 5 years ago
parent
commit
b4801e9f01
  1. 17
      app/Http/Controllers/Api/IndexController.php
  2. 31
      app/Http/Controllers/Api/LoginController.php

17
app/Http/Controllers/Api/IndexController.php

@ -38,23 +38,6 @@ class IndexController extends Controller
# 我的频道
if ($this->user_id) {
$channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels');
//如果不存在则存入初始数据
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', $this->user_id)
->insert([
'user_id' => $this->user_id,
'channels' => json_encode($channel_ids)
]);
}
$my_channels = Channel::where('agent_id', $this->agent_id)
->whereIn('id', $channel_ids)
->get(['id', 'name', 'icon']);

31
app/Http/Controllers/Api/LoginController.php

@ -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);

Loading…
Cancel
Save