海南旅游SAAS
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

4 years ago
4 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\AdminSetting;
  5. use App\Models\Agent;
  6. use App\Models\Channel;
  7. use App\Models\User;
  8. use App\Models\UserChannel;
  9. use EasyWeChat\Factory;
  10. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  11. use Illuminate\Support\Facades\Cache;
  12. /**
  13. * 小程序登录
  14. * Class Login
  15. * @package App\Http\Controllers\Api
  16. */
  17. class LoginController extends Controller
  18. {
  19. public function login()
  20. {
  21. $appid = request()->header('appid');
  22. if (!$appid) {
  23. return $this->error('appid参数缺失');
  24. }
  25. $agent = Agent::query()->firstWhere('appid', $appid); //代理商数据
  26. if (!$agent) {
  27. return $this->error('找不到该appid相关记录');
  28. }
  29. $code = request()->input('code');
  30. if (!$code) {
  31. return $this->error('code参数缺失');
  32. }
  33. // 如果有appsecret,使用原来的登录逻辑,否则使用第三方登录逻辑
  34. if ($agent['appsecret']) {
  35. $config = config('wechat.mini_program.default');
  36. $config = array_merge($config, [
  37. 'app_id' => $agent['appid'],
  38. 'secret' => $agent['appsecret'],
  39. ]);
  40. $app = Factory::miniProgram($config);
  41. } else {
  42. $setting = AdminSetting::val(['service_appid', 'service_appsecret', 'service_token', 'service_aeskey']);
  43. $config = [
  44. 'app_id' => $setting['service_appid'],
  45. 'secret' => $setting['service_appsecret'],
  46. 'token' => $setting['service_token'],
  47. 'aes_key' => $setting['service_aeskey'],
  48. ];
  49. $app = Factory::openPlatform($config);
  50. dd($app);
  51. }
  52. dd($agent->toArray());
  53. try {
  54. $res = $app->auth->session($code);
  55. if (!empty($res['errcode']) || empty($res['openid']) && empty($res['unionid'])) {
  56. $msg = $res['errmsg'] ?? '登录失败';
  57. return $this->error($msg);
  58. }
  59. if (empty($res['session_key'])) {
  60. return $this->error('获取session_key失败');
  61. }
  62. } catch (InvalidConfigException $e) {
  63. return $this->error($e->getMessage());
  64. }
  65. $userModel = User::query()->firstOrCreate([
  66. 'openid' => $res['openid'] ?? '',
  67. 'agent_id' => $agent->id,
  68. ], [
  69. 'unionid' => $res['unionid'] ?? '',
  70. ]);
  71. //保存session_key
  72. Cache::put('session_key_' . $userModel->id, $res['session_key']);
  73. //如果是新增,插入初始数据
  74. if ($userModel->wasRecentlyCreated) {
  75. $channel_ids = UserChannel::where('user_id', $userModel->id)->first();
  76. //如果不存在则存入初始数据
  77. if (!$channel_ids) {
  78. $channel_ids = Channel::where([
  79. ['agent_id', '=', $this->agent_id],
  80. ['pid', '<>', 0],
  81. ])->orderBy('id')->limit(8)->pluck('id')->toArray();
  82. //存入user_channel
  83. UserChannel::where('user_id', $userModel->id)
  84. ->insert([
  85. 'user_id' => $userModel->id,
  86. 'channels' => json_encode($channel_ids)
  87. ]);
  88. }
  89. }
  90. $token_key = md5($userModel->id . env('APP_KEY'));
  91. Cache::put($token_key, $userModel->id);
  92. // 测试代码结束
  93. $userModel->token = $token_key;
  94. return $this->success($userModel);
  95. }
  96. }