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.
108 lines
2.9 KiB
108 lines
2.9 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AdminSetting;
|
|
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;
|
|
|
|
/**
|
|
* 小程序登录
|
|
* Class Login
|
|
* @package App\Http\Controllers\Api
|
|
*/
|
|
class LoginController extends Controller
|
|
{
|
|
public function login()
|
|
{
|
|
$appid = request()->header('appid');
|
|
if (!$appid) {
|
|
return $this->error('appid参数缺失');
|
|
}
|
|
$agent = Agent::query()->firstWhere('appid', $appid); //代理商数据
|
|
if (!$agent) {
|
|
return $this->error('找不到该appid相关记录');
|
|
}
|
|
|
|
$code = request()->input('code');
|
|
if (!$code) {
|
|
return $this->error('code参数缺失');
|
|
}
|
|
|
|
// 如果有appsecret,使用原来的登录逻辑,否则使用第三方登录逻辑
|
|
if ($agent['appsecret']) {
|
|
$config = config('wechat.mini_program.default');
|
|
$config = array_merge($config, [
|
|
'app_id' => $agent['appid'],
|
|
'secret' => $agent['appsecret'],
|
|
]);
|
|
$app = Factory::miniProgram($config);
|
|
} else {
|
|
$setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
|
|
$config = [
|
|
'app_id' => $setting['service_appid'],
|
|
'secret' => $setting['service_appsecret'],
|
|
'token' => $setting['service_token'],
|
|
'aes_key' => $setting['service_aeskey'],
|
|
];
|
|
|
|
$app = Factory::openPlatform($config);
|
|
dd($app);
|
|
}
|
|
|
|
try {
|
|
$res = $app->auth->session($code);
|
|
if (!empty($res['errcode']) || empty($res['openid']) && empty($res['unionid'])) {
|
|
$msg = $res['errmsg'] ?? '登录失败';
|
|
return $this->error($msg);
|
|
}
|
|
if (empty($res['session_key'])) {
|
|
return $this->error('获取session_key失败');
|
|
}
|
|
} catch (InvalidConfigException $e) {
|
|
return $this->error($e->getMessage());
|
|
}
|
|
|
|
$userModel = User::query()->firstOrCreate([
|
|
'openid' => $res['openid'] ?? '',
|
|
'agent_id' => $agent->id,
|
|
], [
|
|
'unionid' => $res['unionid'] ?? '',
|
|
]);
|
|
|
|
//保存session_key
|
|
Cache::put('session_key_' . $userModel->id, $res['session_key']);
|
|
|
|
//如果是新增,插入初始数据
|
|
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);
|
|
// 测试代码结束
|
|
$userModel->token = $token_key;
|
|
return $this->success($userModel);
|
|
}
|
|
}
|