| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\extra\dyLife\Crypt;
- use app\extra\tools\CodeExtend;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasUserOpen;
- use DI\Attribute\Inject;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use LinFly\Annotation\Attributes\Route\Middleware;
- use LinFly\Annotation\Attributes\Route\PostMapping;
- use support\Request;
- use Shopwwi\WebmanAuth\Facade\Auth as AuthFacade;
- use support\Response;
- #[Controller("/dy/auth"),Middleware(AuthMiddleware::class)]
- class Auth extends Base
- {
- protected array $noNeedLogin = ["log2mobile","log2nickname"];
- #[Inject]
- protected SaasUserOpen $model;
- #[PostMapping("nickname")]
- public function log2nickname(Request $request): Response
- {
- try {
- $param = $request->post();
- $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
- if (empty($sessionKey)) return error("授权登录失败");
- $userInfo = [
- "openid" => $sessionKey['openid'],
- "nickname" => $param['nickname']??'',
- "avatar" => $param['avatar']??'',
- "create_ip" => $request->getRealIp()
- ];
- $map = ["openid" => $sessionKey['openid']];
- $user = $this->model->where($map)->findOrEmpty();
- if ($user->isEmpty()) {
- $user->insertGetId($userInfo);
- } else {
- $user->nickname = $param['nickname']??'';
- $user->avatar = $param['avatar']??'';
- $user->save();
- }
- $userAuth = get_object_vars(AuthFacade::guard("user")->login(['openid' => $sessionKey['openid']]));
- return success("ok",$userAuth);
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- #[PostMapping("mobile")]
- public function log2mobile(Request $request): Response
- {
- try {
- $param = $request->post();
- $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
- if (empty($sessionKey)) return error("授权登录失败");
- $mobile = [];
- if (!empty($param['code'])) {
- $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
- if (!empty($mobileStr)) {
- $mobile = $this->decrypt2code(sConf('wechat.min_private_key'), $mobileStr);
- }
- }
- $userInfo = [
- "openid" => $sessionKey['openid'],
- "nickname" => "DY-".strtoupper(CodeExtend::random(5,3)),
- "create_ip" => $request->getRealIp()
- ];
- $map = ["openid" => $sessionKey['openid']];
- if (!empty($mobile['purePhoneNumber'])) {
- $userInfo['mobile'] = $mobile['purePhoneNumber'];
- }
- $user = $this->model->where($map)->findOrEmpty();
- if ($user->isEmpty()) {
- $user->insertGetId($userInfo);
- }
- $userAuth = get_object_vars(AuthFacade::guard("user")->login(['openid' => $sessionKey['openid']]));
- return success("ok",$userAuth);
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- protected function decrypt2code($private_key, $ciphertext_str) {
- // 解码 base64 密文
- $ciphertext = base64_decode($ciphertext_str);
- // 使用私钥解密
- openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
- if ($plaintext === false) {
- return [];
- }
- return json_decode($plaintext,true);
- }
- protected function decrypt($encrypted_data, $session_key, $iv) {
- $data = base64_decode($encrypted_data);
- $key = base64_decode($session_key);
- $iv_decoded = base64_decode($iv);
- // 使用 AES-256-CBC 模式解密
- $decrypted = openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
- return $decrypted;
- }
- }
|