Pārlūkot izejas kodu

Merge branch 'develop'

xielin 5 gadi atpakaļ
vecāks
revīzija
e4cf147130

+ 83 - 0
app/Console/Commands/Generate.php

@@ -0,0 +1,83 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\Meta;
+use App\Models\Serialize;
+use BlockMatrix\EosRpc\ChainFactory;
+use BlockMatrix\EosRpc\EosRpc;
+use BlockMatrix\EosRpc\WalletFactory;
+use Carbon\Carbon;
+use GuzzleHttp\Client;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+use Org\Multilinguals\Apollo\Client\ApolloClient;
+use ZanySoft\Zip\Zip;
+
+class Generate extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'generate:randstr';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '生成序列串';
+
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        ini_set('memory_limit','128M');
+        $this->line('-----------start---------');
+        $pass=[];
+        $data=[];
+        do{
+            $str = $this->getRandomString(3);
+            //生成3位,并且最少包含一位数字
+            $preg = '/^(?=[a-z]*[0-9])(?=[0-9]*[a-z])[a-z0-9]{3,3}$/';
+            if(preg_match($preg,$str)){
+                if(!in_array($str,$pass)){
+                    $pass[] = $str;
+                    $data['serialize'] = $str;
+                    Serialize::create($data);
+                }
+            }
+        }while (count($pass)<20000);
+
+        $this->line('-----------end--------');
+    }
+
+    private function getRandomString($len, $chars=null)
+    {
+        if (is_null($chars)){
+            $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
+        }
+        mt_srand(10000000*(double)microtime());
+        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
+            $str .= $chars[mt_rand(0, $lc)];
+        }
+        return $str;
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -3,6 +3,7 @@
 namespace App\Console;
 
 use App\Console\Commands\Apollo;
+use App\Console\Commands\Generate;
 use App\Console\Commands\Qrcode;
 use App\Console\Commands\Trace;
 use Illuminate\Console\Scheduling\Schedule;
@@ -19,6 +20,7 @@ class Kernel extends ConsoleKernel
         Apollo::class,
         Trace::class,
         Qrcode::class,
+        Generate::class
     ];
 
     /**

+ 19 - 0
app/Models/Serialize.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/6/6
+ * Time: 15:32
+ */
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class Serialize extends Model
+{
+    use SoftDeletes;
+    //
+    protected $table = 'serialize_num';
+    protected $guarded = [];
+
+}

+ 16 - 3
app/Repositories/MetaRepository.php

@@ -8,6 +8,7 @@
 namespace App\Repositories;
 
 use App\Models\Meta;
+use App\Models\Serialize;
 use Dingo\Api\Http\Response;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\DB;
@@ -31,7 +32,8 @@ class MetaRepository {
     }
     //详情
     public function view($request){
-        return $this->meta->where([['id','=',$request['patch_num']],['status','=',1]])->first();
+        $info = Serialize::where('serialize',$request['patch_num'])->first();
+        return $this->meta->where([['id','=',$info['meta_id']],['status','=',1]])->first();
     }
     //创建
     public function create($request){
@@ -66,9 +68,20 @@ class MetaRepository {
         DB::beginTransaction();
         try{
             $meta = $this->meta->create($data);
-
+            $info = Serialize::where('meta_id',0)->orderBy('id','asc')->first();
+            if(empty($info)){
+                DB::rollBack();
+                return Response::create([
+                    'message'  => '新增元数据失败,请重试',
+                    'error' => '序列号已使用完成,请继续生成',
+                    'status_code'   => 500
+                ]);
+            }
+            $info->meta_id = $meta->id;
+            $info->save();
             DB::commit();
-            Storage::put('chsy/qrcodes/'.$meta['patch_num'].'.png',QrCode::size(200)->generate("http://uptoyo.com/".$meta['id']));
+
+            Storage::put('chsy/qrcodes/'.$meta['patch_num'].'.png',QrCode::size(200)->generate("http://uptoyo.com/".$info['serialize']));
             return Response::create();
         }catch (QueryException $exception){
             DB::rollBack();

+ 34 - 0
database/migrations/2019_12_05_193215_create_table_serialize_num.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateTableSerializeNum extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('serialize_num', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->char('serialize',8)->comment('序列号')->unique();
+            $table->integer('meta_id')->default(0)->comment('元数据ID');
+            $table->timestamps();
+            $table->softDeletes();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('table_serialize_num');
+    }
+}

+ 3 - 2
routes/api.php

@@ -27,9 +27,10 @@ $api->version('v1', [
     //后台
     //登录
     $api->post('login', 'AuthController@authenticate');
+    $api->get('metas', 'MetaController@index');
+    $api->post('meta', 'MetaController@create');
     $api->group(['middleware' => 'jwt.chxq_auth'], function ($api) {
-        $api->get('metas', 'MetaController@index');
-        $api->post('meta', 'MetaController@create');
+
     });
 
 });