PHP code example of mouyong / laravel-doc

1. Go to this page and download the library: Download mouyong/laravel-doc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mouyong / laravel-doc example snippets


// yapi.php
base_url 是 yapi 的 url.
project_id 是 yapi 的项目 id
token 是项目的 token

openapi 可以配置与 openapi 的路由信息。以及是否生成 openapi 文档。

openapi 文档的访问路由,默认是 /openapi



namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use MouYong\LaravelDoc\Traits\YapiTrait; // here

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use YapiTrait; // here
}




namespace Tests\Feature\Tenant;

use App\Models\Tenant;
use Cblink\YApiDoc\YapiDTO;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\Feature\Tenant\Traits\AdminUserTrait;
use Tests\TestCase;

class OemTest extends TestCase
{
    use AdminUserTrait;

    /**
     * Get tenant oem info.
     *
     * @return void
     */
    public function test_get_tenant_oem_info()
    {
        $response = $this->getJson($this->tenantApi('/api/oem-info', false));

         // 断言接口响应数据格式
        $this->assertSuccess($response, [
            'api',
            'tenant_id',
            'doctor_name',
            'hospital_name',
            'department_name',
            'doctor_username',
            'domains',
        ]);

        // 生成 yapi 文档 与 openapi 2.0 文档
        $this->yapi($response, new YapiDTO([
            // 可以同时生成到多个 yapi 项目
            'project' => ['default'],
            // api 名称
            'name' => '获取租户 oem 信息',
            // api 分类
            'category' => '系统',
            'params' => [],
            'desc' => '',
            'request' =>[
                // 这里是字段含义
                'trans' => [
                ],
                // 这里是非必填字段
                'except' => [],
            ],
            'response' =>[
                // 这里是字段含义
                'trans' => [
                    'api' => '租户 api 地址',
                    'tenant_id' => '租户 id',
                    'doctor_name' => '医生名',
                    'hospital_name' => '医院名',
                    'department_name' => '科室名',
                    'doctor_username' => '医生用户名',
                    'domains' => '租户域名',
                ],
                // 这里是非必填字段
                'except' => [],
            ],
        ]));
    }
}




namespace Tests\Feature\Tenant;

use Tests\TestCase;
use App\Models\Patient;
use Cblink\YApiDoc\YapiDTO;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\Feature\Tenant\Traits\AdminUserTrait;
use Illuminate\Foundation\Testing\RefreshDatabase;

class PatientTest extends TestCase
{
    use AdminUserTrait;
    
    public function test_admin_add_patient()
    {
        $this->adminLogin();
        
        $response = $this->postJson($this->tenantApi('/api/patients'), [
            'name' => '张三',
            'gender' => Patient::GENDER_MAN,
            'mobile' => '13333333333',
            'address' => '广东省深圳市宝安区 xx 花园',
            'highest_education' => '',
            'nation' => '',
            'birthday' => '',
            'native_place' => '',
        ]);

        // $this->assertSuccess($response, [
        //     'patient_id',
        //     'user_id',
        // ]);

        $this->yapi($response, new YapiDTO([
            'project' => ['default'],
            'name' => '新增患者',
            'category' => '患者',
            'params' => [],
            'desc' => '',
            'request' =>[
                'trans' => [
                    'name' => '患者姓名',
                    'gender' => $this->mapDesc('患者性别', Patient::GENDER_MAP),
                    'mobile' => '患者手机号',
                    'address' => '患者居住地址,完整的省市区及详细地址。如:广东省深圳市宝安区',
                    'highest_education' => '学历枚举值',
                    'nation' => '民族',
                    'birthday' => '患者生日,格式:2022-02-02 02:02:02',
                    'native_place' => '患者籍贯,身份证的所属地,如广东、北京',
                ],
                'except' => [],
            ],
            'response' =>[
                'trans' => [
                    'patient_id' => 'patients.id 患者 ID',
                    'user_id' => 'users.id 用户 ID',
                ],
                'except' => [],
            ],
        ]));
    }

    public function test_admin_get_patient_list()
    {
        $this->adminLogin();
        
        $response = $this->getJson($this->tenantApi('/api/patients'), [
            'keyword' => '',
        ]);

        // $this->assertSuccess($response, [
        //     'patient_id',
        //     'user_id',
        // ]);

        $this->yapi($response, new YapiDTO([
            'project' => ['default'],
            'name' => '获取患者列表',
            'category' => '患者',
            'params' => [],
            'desc' => '',
            'request' =>[
                'trans' => [
                    'keyword' => '搜索关键词,搜索字段:patients.name',
                ],
                'except' => [],
            ],
            'response' =>[
                'trans' => [
                    'data.*.patient_id' => 'patients.id 患者 ID',
                    'data.*.user_id' => 'users.id 用户 ID',
                    'data.*.user_name' => 'users.name',
                    'data.*.user_mobile' => 'users.mobile 脱敏手机号,如:133****3333',
                    'data.*.user_gender' => $this->mapDesc('users.gender 用户性别', Patient::GENDER_MAP),
                    'data.*.user_gender_desc' => '性别文字描述,如:男 具体描述值见 users.gender',
                ],
                'except' => [],
            ],
        ]));
    }

    public function test_admin_delete_patient()
    {
        $this->markAsRisky();
    }

    public function test_admin_add_patient_family_info()
    {
        $this->markAsRisky();
    }

    public function test_admin_get_patient_family_list()
    {
        $this->markAsRisky();
    }

    public function test_admin_get_patient_family_detail()
    {
        $this->markAsRisky();
    }
}