Преглед изворни кода

feat(aliyun-idcard): 添加阿里云身份认证扩展

runphp пре 7 месеци
комит
4bb12e07b8
9 измењених фајлова са 203 додато и 0 уклоњено
  1. 8 0
      .idea/.gitignore
  2. 7 0
      README.md
  3. 32 0
      composer.json
  4. 46 0
      config.php
  5. 16 0
      info.php
  6. 16 0
      src/Config.php
  7. 14 0
      src/Extension.php
  8. 42 0
      src/IDCardClient.php
  9. 22 0
      test/IDCardClientTest.php

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 7 - 0
README.md

@@ -0,0 +1,7 @@
+# lianzhuo_idcard
+
+扩展说明
+
+实名认证接口:
+
+https://market.aliyun.com/detail/cmapi014760?spm=5176.29867242.0.0.44e83e7edtnG2U#sku=yuncode8760000013

+ 32 - 0
composer.json

@@ -0,0 +1,32 @@
+{
+  "name": "six-shop/aliyun-idcard",
+  "description": "阿里云身份认证",
+  "type": "sixshop-extension",
+  "keywords": [
+    "sixshop",
+    "thinkphp"
+  ],
+  "require": {
+    "php": ">=8.3",
+    "six-shop/core": ">=0.5 <1.0",
+    "symfony/http-client": ">=7.3"
+  },
+  "authors": [
+    {
+      "name": "hui he",
+      "email": "runphp@qq.com"
+    }
+  ],
+  "license": "MIT",
+  "autoload": {
+    "psr-4": {
+      "SixShop\\AliyunIDCard\\": "src"
+    }
+  },
+  "extra": {
+    "sixshop": {
+      "id": "aliyun-idcard",
+      "class": "SixShop\\AliyunIDCard\\Extension"
+    }
+  }
+}

+ 46 - 0
config.php

@@ -0,0 +1,46 @@
+<?php
+declare(strict_types=1);
+
+return json_decode(<<<'JSON'
+[
+  {
+      "type": "input",
+    "title": "AppKey",
+    "field": "app_key",
+    "props": {
+      "placeholder": "请输入AppKey"
+    },
+    "_fc_id": "id_Fj8wmdlxp75qacc",
+    "name": "ref_F5uymdlxp75qadc",
+    "display": true,
+    "hidden": false,
+    "_fc_drag_tag": "input"
+  },
+  {
+      "type": "input",
+    "title": "AppSecret",
+    "field": "app_secret",
+    "props": {
+      "placeholder": "请输入AppSecret"
+    },
+    "_fc_id": "id_Fk7hmdlxp75raec",
+    "name": "ref_Fo16mdlxp75rafc",
+    "display": true,
+    "hidden": false,
+    "_fc_drag_tag": "input"
+  },
+  {
+      "type": "input",
+    "title": "AppCode",
+    "field": "app_code",
+    "props": {
+      "placeholder": "请输入AppCode"
+    },
+    "_fc_id": "id_F0wwmdlxp75ragc",
+    "name": "ref_F8mxmdlxp75rahc",
+    "display": true,
+    "hidden": false,
+    "_fc_drag_tag": "input"
+  }
+]
+JSON, true);

+ 16 - 0
info.php

@@ -0,0 +1,16 @@
+<?php
+declare(strict_types=1);
+return [
+  'id' => 'lianzhuo_idcard',
+  'name' => '身份证验证',
+  'is_core' => false,
+  'category' => 'other',
+  'description' => '验证姓名与身份证号是否一致,并且返回生日、性别,籍贯等信息',
+  'version' => '1.0.0',
+  'core_version' => '^1.0',
+  'author' => 'yourname',
+  'email' => '',
+  'website' => '',
+  'image' => '',
+  'license' => 'MIT',
+];

+ 16 - 0
src/Config.php

@@ -0,0 +1,16 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\AliyunIDCard;
+
+use SixShop\System\Trait\ConfigTrait;
+
+class Config
+{
+    use ConfigTrait;
+
+    public function getAppCode(): string
+    {
+        return $this->getConfig('app_code');
+    }
+}

+ 14 - 0
src/Extension.php

@@ -0,0 +1,14 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\AliyunIDCard;
+
+use SixShop\Core\ExtensionAbstract;
+
+class Extension extends ExtensionAbstract
+{
+    protected function getBaseDir(): string
+    {
+        return dirname(__DIR__);
+    }
+}

+ 42 - 0
src/IDCardClient.php

@@ -0,0 +1,42 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunIDCard;
+
+use SixShop\Core\Helper;
+use Symfony\Component\HttpClient\HttpClient;
+use Symfony\Contracts\HttpClient\HttpClientInterface;
+
+class IDCardClient
+{
+
+    private const string GATEWAY = 'https://idcard.market.alicloudapi.com/';
+
+    private HttpClientInterface $httpClient;
+
+    public function __construct(private Config $config)
+    {
+        $this->httpClient = HttpClient::create([
+            'base_uri' => self::GATEWAY,
+            'headers' => [
+                'Authorization' => 'APPCODE ' . $this->config->getAppCode(),
+            ]
+        ]);
+    }
+
+
+    public function idcard(string $cardno, string $name): array
+    {
+        $response = $this->httpClient->request('GET', 'lianzhuo/idcard', [
+            'query' => [
+                'cardno' => $cardno,
+                'name' => $name,
+            ]
+        ]);
+        $result = json_decode($response->getContent(), true);
+        if ($result['resp']['code'] == 0) {
+            return $result['data'];
+        } else {
+            Helper::throw_logic_exception($result['resp']['desc'], $result['resp']['code']);
+        }
+    }
+}

+ 22 - 0
test/IDCardClientTest.php

@@ -0,0 +1,22 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\AliyunIDCard;
+use PHPUnit\Framework\TestCase;
+
+class IDCardClientTest extends TestCase
+{
+    private IDCardClient $idCardClient;
+
+    protected function setUp(): void
+    {
+        $this->idCardClient = app(IDCardClient::class);
+    }
+
+    public function testIdcard()
+    {
+        $result = $this->idCardClient->idcard('370703198111300338', '郭德昌');
+        $this->assertArrayHasKey('birthday', $result);
+        $this->assertArrayHasKey('sex', $result);
+        $this->assertArrayHasKey('address', $result);
+    }
+}