Ver Fonte

no message

USER-20230908AJ\Administrator há 1 ano atrás
pai
commit
c53fc7f6b3

+ 83 - 0
app/Http/Controllers/Api/CommonController.php

@@ -0,0 +1,83 @@
+<?php
+
+
+namespace App\Http\Controllers\Api;
+
+
+use App\Http\Controllers\ApiController;
+use App\Servers\System\BaiduServer;
+use App\Servers\System\SmsServer;
+
+class CommonController extends ApiController
+{
+    public $request;
+    public $smsService;
+
+    public function __construct()
+    {
+
+    }
+
+
+
+
+    /**
+     * 发送短信
+     * @return \Illuminate\Http\JsonResponse
+     * @throws \End01here\EasySms\Exceptions\CodeErrorException
+     * @throws \End01here\EasySms\Exceptions\GatewayErrorException
+     * @throws \End01here\EasySms\Exceptions\MessageException
+     */
+    function sendSms()
+    {
+        $phone = request()->input('phone', '');
+        $send_type = request()->input('send_type', 'default');
+        if (empty($phone)) {
+            return $this->apiResponseError('请输入手机号码');
+        }
+        $send_server = SmsServer::creatServer();
+        $ret = $send_server->sendCode($phone, $send_type);
+        if (!$ret) {
+            return $this->apiResponseError($send_server->getErrorMsg() ?: '短信发送失败');
+        } else {
+            return $this->apiResponseSuccess('短信发送成功');
+        }
+    }
+
+
+
+
+    /**
+     * 百度图片orc识别
+     * @return \Illuminate\Http\JsonResponse
+     */
+    function getImageOrc(){
+        $image = request()->input('image','');
+        $type = request()->input('type',-1);//识别类型
+
+        if (empty($image)){
+            return $this->apiResponseError('请正确上传文件');
+        }
+        if (!in_array($type,[1,2,3,4])){
+            return $this->apiResponseError('识别类型出错');
+        }
+
+        $baiduServer = BaiduServer::creatServer();
+        if ($type == 1){//身份证含照片的一面识别
+            $result = $baiduServer->idCard($image,'front');
+        }elseif ($type == 2){//身份证带国徽的一面识别
+            $result = $baiduServer->idCard($image,'back');
+        }elseif ($type == 3){//营业执照识别
+            $result = $baiduServer->businessLicense($image);
+        }
+        if (empty($result) || $result['code'] == 0){
+            return $this->apiResponseError(empty($result['msg'])?'识别类型错误':$result['msg']);
+        }
+        return $this->apiResponseSuccess('成功',$result['data']);
+    }
+
+
+
+
+
+}

+ 10 - 0
app/Http/Controllers/ApiController.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Http\Controllers\Traits\ApiResponseTrait;
+
+class ApiController extends Controller
+{
+    use ApiResponseTrait;
+}

+ 61 - 0
app/Http/Controllers/Traits/ApiResponseTrait.php

@@ -0,0 +1,61 @@
+<?php
+
+
+namespace App\Http\Controllers\Traits;
+
+
+
+trait ApiResponseTrait
+{
+    /**
+     * @param array $data                       ## 返回数据
+     * @param string $msg                       ## 返回信息 如:auth.failed
+     * @param int $code                         ## 响应编码 1:成功 0:失败
+     * @param int $status                       ## HTTP响应状态 200:请求成功
+     * @param array $header                     ## 响应头
+     * @param int $options                      ## 其他信息
+     * @return \Illuminate\Http\JsonResponse
+     *
+     * 接口返回格式 | 成功
+     */
+    public function apiResponseSuccess( $msg = '',$data = [], $code = 1, $status = 200, $header = [], $options = 0)
+    {
+//        $this->lang();
+//        $msg=__($msg);
+        return response()->json([
+            'msg'   => $msg,
+            'data'  => empty($data) ? [] : $data,
+            'code'  => $code,
+        ], $status, $header, $options);
+    }
+
+    /**
+     * @param array $data                       ## 返回数据
+     * @param string $msg                       ## 返回信息 如:auth.failed
+     * @param int $code                         ## 响应编码 1:成功 0:失败
+     * @param int $status                       ## HTTP响应状态 200:请求成功
+     * @param array $header                     ## 响应头
+     * @param int $options                      ## 其他信息
+     * @return \Illuminate\Http\JsonResponse
+     *
+     * 接口返回格式 | 失败
+     */
+    public function apiResponseError( $msg = '',$data = [], $code = 0, $status = 200, $header = [], $options = 0)
+    {
+//        $this->lang();
+//        $msg=__($msg);
+        return response()->json([
+            'msg'   => $msg,
+            'data'  => empty($data) ? [] : $data,
+            'code'  => $code,
+        ], $status, $header, $options);
+    }
+
+    /**
+     * 双语设置
+     */
+    protected function lang()
+    {
+        app()->setLocale(request()->input('lang', 'zh'));
+    }
+}

+ 110 - 0
app/Servers/System/BaiduServer.php

@@ -0,0 +1,110 @@
+<?php
+
+
+namespace App\Servers\System;
+
+
+
+use End01here\BaiDuOrc\BaiDuOrcService;
+
+/**
+ * Redis数据缓存类
+ */
+class BaiduServer
+{
+
+    /**
+     * 错误信息
+     * @var string
+     */
+    private $errorMsg = '';
+
+    static private $server = null;
+    private $baiduServer = null;
+
+
+    private function __construct()
+    {
+
+        $this->baiduServer = BaiDuOrcService::getOrcServer();
+    }
+
+    /**
+     * 创建对象
+     * @return BaiduServer
+     */
+    static function creatServer()
+    {
+        if (empty(self::$server)) self::$server = new BaiduServer();
+        return self::$server;
+    }
+
+
+    /**
+     * 身份证号码识别
+     * @param $imgUrl
+     * @param $idCardSide
+     */
+    function idCard($imgUrl,$idCardSide='front'){
+        // 如果有可选参数
+        $options = array();
+        $options["detect_direction"] = "true";
+        $options["detect_risk"] = "false";
+
+        // 带参数调用身份证识别
+        $data= $this->baiduServer->idcard($imgUrl, $idCardSide, $options);
+        if(!empty($data['image_status']) && $data['image_status']=='normal'){
+            if($idCardSide=='front'){
+                $userData['name']=$data['words_result']['姓名']['words'];
+                $userData['sex']=$data['words_result']['性别']['words'];
+                $userData['id_number']=$data['words_result']['公民身份号码']['words'];
+                $userData['birthday']=$data['words_result']['出生']['words'];
+            }else{
+                $userData['validity_start']=$data['words_result']['签发日期']['words'];
+                $userData['validity_end']=$data['words_result']['失效日期']['words'];
+            }
+            if(empty($userData)){
+                return ['code'=>0,'msg'=>'身份证识别失败','data'=>[]];
+            }else{
+                return ['code'=>1,'msg'=>'身份证识别成功','data'=>$userData];
+            }
+        }else{
+            return ['code'=>0,'msg'=>'身份证识别失败','data'=>[]];
+        }
+    }
+
+
+    /**
+     * 营业执照识别
+     * @param $imgUrl
+     * @return array
+     */
+    function businessLicense($imgUrl){
+        //营业执照识别
+        $data= $this->baiduServer->businessLicense($imgUrl);
+
+        if(!empty($data['words_result_num']) && $data['words_result_num']>0){
+            $cardData=$data['words_result'];
+
+            $userData['id_number']=empty($cardData['社会信用代码'])?'':$cardData['社会信用代码']['words'];
+            $userData['name']=empty($cardData['单位名称'])?'':$cardData['单位名称']['words'];
+            $userData['registered_capital']=empty($cardData['注册资本'])?'':$cardData['注册资本']['words'];
+            $userData['address']=empty($cardData['地址'])?'':$cardData['地址']['words'];
+            $userData['registration_date']=empty($cardData['成立日期'])?'':$cardData['成立日期']['words'];
+            $userData['firm_type']=empty($cardData['类型'])?'':$cardData['类型']['words'];
+            $userData['paid_capital']=empty($cardData['实收资本'])?'':$cardData['实收资本']['words'];
+
+            if(empty($userData)){
+                return ['code'=>0,'msg'=>'营业执照识别失败','data'=>[]];
+            }else{
+                return ['code'=>1,'msg'=>'营业执照识别成功','data'=>$userData];
+            }
+        }else{
+            return ['code'=>0,'msg'=>'营业执照识别失败','data'=>[]];
+        }
+
+    }
+
+
+
+}

+ 232 - 0
app/Servers/System/RedisDataServer.php

@@ -0,0 +1,232 @@
+<?php
+
+
+namespace App\Servers\System;
+
+use Illuminate\Support\Facades\Redis;
+
+
+/**
+ * Redis数据缓存类
+ */
+class RedisDataServer
+{
+    /**
+     * 单列对象
+     * @var
+     */
+    private static $server;
+
+    private function __construct()
+    {
+
+    }
+
+    /**
+     * 创建对象
+     * @return RedisDataServer
+     */
+    static function creatServer()
+    {
+        if (empty(self::$server)) {
+            self::$server = new RedisDataServer();
+        }
+        return self::$server;
+    }
+
+    /**
+     * 获取缓存数据
+     * @param $key
+     * @param string $type
+     * @return mixed
+     */
+    function getData($key, $type = 'str')
+    {
+
+        $value = Redis::get($key);
+        if ($type == 'json') {
+            $value = json_decode($value, true);
+        }
+        return $value;
+    }
+
+    /**
+     * 设置缓存数据
+     * @param $key
+     * @param $value
+     * @param string $type
+     * @param int $time
+     * @param $random
+     */
+    function setData($key, $value, $type = 'str', $time = 10, $random = true)
+    {
+        if (!empty($value)) {
+            if ($type == 'json') {
+                $value = json_encode($value);
+            }
+            if ($time > 1 && $random) $time += $this->getRandom();
+            Redis::setex($key, $time, $value);
+        }
+
+    }
+
+    /**
+     * 长时间存放数据
+     * @param $key
+     * @param $value
+     * @param string $type
+     */
+    function setLongData($key, $value, $type = 'str')
+    {
+        if (!empty($value)) {
+            if ($type == 'json') {
+                $value = json_encode($value);
+            }
+            Redis::set($key, $value);
+        }
+
+    }
+
+    /**
+     * 更新数据
+     * @param $key
+     * @param $value
+     * @param string $type
+     * @param int $start_num
+     */
+    function setSetrange($key, $value, $type = 'str', $start_num = 0)
+    {
+        if (!empty($value)) {
+            if ($type == 'json') {
+                $value = json_encode($value);
+            }
+            Redis::setrange($key, $start_num, $value);
+        }
+    }
+
+    /**
+     * 删除Redis数据
+     * @param $key
+     */
+    function delData($key)
+    {
+        Redis::del($key);
+    }
+
+    /**
+     * 获取随机数
+     * @return int
+     */
+    private function getRandom()
+    {
+        return mt_rand(1, 10);
+    }
+
+    /**
+     * 创建队列序号
+     * @param $key
+     * @param $num
+     * @return mixed
+     */
+    function setQueue($key, $num)
+    {
+        $num_arr = range(1, $num);
+        $key_num = Redis::lpush($key, $num_arr);
+        return $key_num;
+    }
+
+    /**
+     * 添加序号
+     * @param $key
+     * @param $num
+     * @return mixed
+     */
+    function addQueue($key, $num)
+    {
+        $key_num = Redis::lpush($key, $num);
+        return $key_num;
+    }
+
+    /**
+     * 弹出队列数据
+     * @param $key
+     * @return false
+     */
+    function getQueue($key)
+    {
+        $key_num = Redis::rpop($key);
+        if (empty($key_num)) $key_num = false;
+        return $key_num;
+    }
+
+    /**
+     * 队列阻塞弹出
+     * @param $key
+     * @param int $wait_time
+     * @return false
+     */
+    function getWaitQueue($key, $wait_time = 5)
+    {
+        $key_num = Redis::brpop($key, $wait_time);
+        if (empty($key_num)) $key_num = false;
+        return $key_num;
+    }
+
+    /**
+     * 获取队列数据长度
+     * @param $key
+     * @return mixed
+     */
+    function getQueueList($key)
+    {
+        $key_num = Redis::llen($key);
+        return $key_num;
+    }
+
+    /**
+     * 获取过期时间
+     * @param $key
+     * @return mixed
+     */
+    function getTime($key)
+    {
+        $key_num = Redis::ttl($key);
+        return $key_num;
+    }
+
+    /**
+     * 缓存清除
+     * @return mixed
+     */
+    function flushdb()
+    {
+        return Redis::flushdb();
+    }
+
+
+    /**
+     * key加锁
+     * @param $key
+     * @param $value
+     * @param int $time
+     * @return mixed
+     */
+    function lockKey($key,$value,$time = 10){
+        return Redis::set($key, $value, 'ex', $time, 'nx');
+    }
+
+    /**
+     * 解锁
+     * @param $key
+     * @param $value
+     * @return mixed
+     */
+    function unLockKey($key, $value)
+    {
+        $res = $this->getData($key);
+
+        if ($res == $value) {
+            return Redis::del($key);
+        }
+    }
+}

+ 130 - 0
app/Servers/System/SmsServer.php

@@ -0,0 +1,130 @@
+<?php
+
+
+namespace App\Servers\System;
+
+use End01here\EasySms\EasySmsService;
+
+
+/**
+ * Redis数据缓存类
+ */
+class SmsServer
+{
+
+
+    /**
+     * 错误信息
+     * @var string
+     */
+    private $errorMsg = '';
+
+
+    private function __construct()
+    {
+    }
+
+    /**
+     * 创建对象
+     * @return SmsServer
+     */
+    static function creatServer()
+    {
+        return new SmsServer();
+    }
+
+    /**
+     * @return string
+     */
+    public function getErrorMsg(): string
+    {
+        return $this->errorMsg;
+    }
+
+    /**
+     * 发送短信
+     * @param $phone_num
+     * @param $send_type
+     * @return bool
+     * @throws \End01here\EasySms\Exceptions\CodeErrorException
+     * @throws \End01here\EasySms\Exceptions\GatewayErrorException
+     * @throws \End01here\EasySms\Exceptions\MessageException
+     */
+    function sendCode($phone_num,$send_type='default')
+    {
+        $wxCode = CommonServer::creatServer()->filtrationStr('wxCode');
+        if(empty($wxCode)){
+            $this->errorMsg = '缺少验证信息';
+            return false;
+        }
+//        $openInfo = WeixinServer::creatServer()->getAppletsOpenId($wxCode);
+        $openInfo = [];
+        if(!isset($openInfo['openid'])){
+            $this->errorMsg = '授权验证失败';
+            return false;
+        }
+        $send_type='default';
+        try {
+            $phone = EasySmsService::getPhoneSever($phone_num);
+            $code_server = EasySmsService::getCodeSever($phone)->setCode($send_type);
+            $message_server = EasySmsService::getMessageServer();
+            $content = '尊敬的客户,验证码为'.$code_server->getCode().',请妥善保管,10分钟内有效!' ;
+            $message_server->setContent($content);
+            $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
+        }catch (\Exception $e){
+            $this->errorMsg=$e->getMessage();
+            return false;
+        }
+
+        if (!empty($send_info['code']) && $send_info['code'] != 1) {
+            $this->errorMsg = '短信发送失败';
+            return false;
+        }
+        return true;
+    }
+
+
+    /**
+     * 发送短信模板内容
+     * @param $phone_num
+     * @param $content
+     * @return bool
+     */
+    function sendContent($phone_num,$content)
+    {
+        try {
+            $phone = EasySmsService::getPhoneSever($phone_num);
+            $message_server = EasySmsService::getMessageServer();
+            $message_server->setContent($content);
+            $send_info = EasySmsService::getSmsServer()->creatQybSms()->send($phone, $message_server);
+        }catch (\Exception $e){
+            $this->errorMsg=$e->getMessage();
+            return false;
+        }
+
+        if (!empty($send_info['code']) && $send_info['code'] != 1) {
+            $this->errorMsg = '短信发送失败';
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * 验证码验证
+     * @param $phone_num
+     * @param $code
+     * @param $send_type
+     * @return bool
+     */
+    public function verifyCode($phone_num, $code,$send_type='default')
+    {
+        $send_type='default';
+        $phone = EasySmsService::getPhoneSever($phone_num);
+        $code_sever= EasySmsService::getCodeSever($phone);
+        $ret_code = $code_sever->verifyCode($code,$send_type);
+        $this->errorMsg=$code_sever->getErrorMsg();
+        return $ret_code;
+    }
+
+
+}

+ 2 - 0
composer.json

@@ -6,6 +6,8 @@
     "license": "MIT",
     "require": {
         "php": "^7.3|^8.0",
+        "end01here/baidu-orc": "^1.0",
+        "end01here/easy-sms": "^1.0",
         "fruitcake/laravel-cors": "^2.0",
         "guzzlehttp/guzzle": "^7.0.1",
         "laravel/framework": "^8.75",

+ 78 - 2
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "c61ff82cbf0142a401a48a8161e1595a",
+    "content-hash": "2189b6124a2a797d3c86711df525104e",
     "packages": [
         {
             "name": "asm89/stack-cors",
@@ -614,6 +614,82 @@
             ],
             "time": "2020-12-29T14:50:06+00:00"
         },
+        {
+            "name": "end01here/baidu-orc",
+            "version": "v1.0.3",
+            "source": {
+                "type": "git",
+                "url": "https://gitee.com/lcpcp/baidu-orc.git",
+                "reference": "85a6ef6860f5557ea9f98003f987f3de1d781356"
+            },
+            "require": {
+                "illuminate/redis": "*"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "End01here\\BaiDuOrc\\BaiDuOrcServiceProvider"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "End01here\\BaiDuOrc\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "authors": [
+                {
+                    "name": "lihang",
+                    "email": "448212125@qq.com"
+                }
+            ],
+            "description": "baidu-orc",
+            "keywords": [
+                "baidu-orc",
+                "laravel"
+            ],
+            "time": "2024-03-27T07:29:03+00:00"
+        },
+        {
+            "name": "end01here/easy-sms",
+            "version": "v1.0.21",
+            "source": {
+                "type": "git",
+                "url": "https://gitee.com/lcpcp/easy-sms.git",
+                "reference": "931c2d43c71ded5636a0e5ae98ba479816e475b5"
+            },
+            "require": {
+                "illuminate/redis": "*"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "End01here\\EasySms\\EasySmsServiceProvider"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "End01here\\EasySms\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "authors": [
+                {
+                    "name": "lihang",
+                    "email": "448212125@qq.com"
+                }
+            ],
+            "description": "easy-sms",
+            "keywords": [
+                "easy-sms",
+                "laravel"
+            ],
+            "time": "2024-12-17T07:09:26+00:00"
+        },
         {
             "name": "fruitcake/laravel-cors",
             "version": "v2.2.0",
@@ -8411,5 +8487,5 @@
         "php": "^7.3|^8.0"
     },
     "platform-dev": [],
-    "plugin-api-version": "2.3.0"
+    "plugin-api-version": "2.6.0"
 }

+ 9 - 0
config/baidu_orc.php

@@ -0,0 +1,9 @@
+<?php
+
+return [
+    //code码验证存储方式
+    'app_id' => env('BAI_APP_ID',''),
+    //短信有效时长(分钟)
+    'api_key' => env('BAI_API_KEY',''),
+    'secret_key' => env('BAI_SECRET_KEY',''),
+];

+ 49 - 0
config/easy_sms.php

@@ -0,0 +1,49 @@
+<?php
+
+return [
+    //code码验证存储方式
+    'cache_type' => env('CACHE_SMS_TYPE','file'),
+    //短信有效时长(分钟)
+    'cache_time' => env('CACHE_SMS_TIME','15'),
+    //短信最短重发时间(秒)
+    'mix_time' => env('CACHE_MIX_TIME','60'),
+    //签名信息
+    'sign_text'=>env('SIGN_TEXT',''),
+    //请求超时时间
+    'timeout'=>env('SMS_TIMEOUT',''),
+    //是否开启通用验证码
+    'is_current'=>env('IS_CURRENT',false),
+    //通用验证码
+    'current_code'=>env('CURRENT_CODE','7453'),
+    //kewail平台短信配置信息
+    'kewail' => [
+        'accesskey' => env('KEWAIL_ACCESSKEY',''),
+        'secretkey' => env('KEWAIL_SECRETKEY',''),
+    ],
+    //阿里云短信配置
+    'ali' => [
+        'access_key_id' => env('ALI_ACCESS_KEY_ID',''),
+        'secretkey' => env('ALI_ACCESS_KEY',''),
+    ],
+    //企业宝短信配置
+    'qyb' => [
+        'username' => env('QYB_USERNAME',''),
+        'password' => env('QYB_PASSWORD',''),
+    ],
+    //蒲公英短信
+    'px' => [
+        'username' => env('PX_USERNAME',''),
+        'password' => env('PX_PASSWORD',''),
+        'userid' => env('PX_USERID',''),
+    ],
+    //蔚岛云短信
+    'wddz' => [
+        'username' => env('WDDZ_USERNAME',''),
+        'password' => env('WDDZ_PASSWORD',''),
+    ],
+    //短信短信
+    'dx' => [
+        'username' => env('DX_USERNAME',''),
+        'password' => env('DX_PASSWORD',''),
+    ]
+];

+ 62 - 0
config/flare.php

@@ -0,0 +1,62 @@
+<?php
+
+return [
+    /*
+    |
+    |--------------------------------------------------------------------------
+    | Flare API key
+    |--------------------------------------------------------------------------
+    |
+    | Specify Flare's API key below to enable error reporting to the service.
+    |
+    | More info: https://flareapp.io/docs/general/projects
+    |
+    */
+
+    'key' => env('FLARE_KEY'),
+
+    /*
+    |--------------------------------------------------------------------------
+    | Reporting Options
+    |--------------------------------------------------------------------------
+    |
+    | These options determine which information will be transmitted to Flare.
+    |
+    */
+
+    'reporting' => [
+        'anonymize_ips' => true,
+        'collect_git_information' => false,
+        'report_queries' => true,
+        'maximum_number_of_collected_queries' => 200,
+        'report_query_bindings' => true,
+        'report_view_data' => true,
+        'grouping_type' => null,
+        'report_logs' => true,
+        'maximum_number_of_collected_logs' => 200,
+        'censor_request_body_fields' => ['password'],
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | Reporting Log statements
+    |--------------------------------------------------------------------------
+    |
+    | If this setting is `false` log statements won't be sent as events to Flare,
+    | no matter which error level you specified in the Flare log channel.
+    |
+    */
+
+    'send_logs_as_events' => true,
+
+    /*
+    |--------------------------------------------------------------------------
+    | Censor request body fields
+    |--------------------------------------------------------------------------
+    |
+    | These fields will be censored from your request when sent to Flare.
+    |
+    */
+
+    'censor_request_body_fields' => ['password'],
+];

+ 18 - 0
routes/api/common.php

@@ -0,0 +1,18 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+
+
+
+/** 会员 **/
+Route::group(['domain' => env('APP_HOST_API'), 'prefix' => 'common'],function (){
+    Route::match(['get','post'],'send', 'CommonController@sendSms')->name('common.send');//发送短信
+    Route::match(['get','post'],'image_orc', 'CommonController@getImageOrc')->name('common.image_orc');//百度orc识别
+
+});
+
+
+
+
+
+