Sfoglia il codice sorgente

Merge branch 'develop'

caihongxingqiu 6 anni fa
parent
commit
dbac8fa57c

+ 6 - 1
.env.example

@@ -20,4 +20,9 @@ QUEUE_CONNECTION=sync
 
 JWT_SECRET=chxqyhnjuikm67wshaed8ij3hyf2ndh3
 
-PER_PAGE=20
+PER_PAGE=20
+
+APP_ID=chxq-platform-manage
+CLUSTER=default
+APOLLO_NAMESPACES="application,shop-manage,admin-manage"
+APOLLO_CONFIG_SERVER=http://127.0.0.1:18080

+ 1 - 0
.gitignore

@@ -6,3 +6,4 @@ Homestead.yaml
 composer.lock
 /storage/*
 .DS_Store
+/config/customer.php

+ 86 - 0
app/Console/Commands/apollo.php

@@ -0,0 +1,86 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Org\Multilinguals\Apollo\Client\ApolloClient;
+
+class Apollo extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'apollo';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Apollo配置服务';
+
+
+    private $apollo;
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+
+        $this->save_dir = storage_path('apollo');
+        $this->config_tpl = config_path() . '/customer.tpl';
+        $this->config_file = config_path() . '/customer.php';
+
+        $this->apollo = new ApolloClient(
+            config('apollo.config_server'),
+            config('apollo.app_id'),
+            config('apollo.namespaces')
+        );
+        $this->apollo->save_dir = $this->save_dir;
+        if(!is_dir($this->save_dir)){
+            mkdir($this->save_dir);
+        }
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line('apollo配置服务开启');
+        $restart = true; //失败自动重启
+        do {
+            $error = $this->apollo->start(function () {
+                $list = glob($this->save_dir . DIRECTORY_SEPARATOR . 'apolloConfig.*');
+                $apollo = [];
+                foreach ($list as $l) {
+                    $config = require $l;
+                    if (is_array($config) && isset($config['configurations'])) {
+                        $apollo = array_merge($apollo, $config['configurations']);
+                    }
+                }
+                if (!$apollo) {
+                    Log::error('Load Apollo Config Failed, no config available');
+                }
+                $tpl = file_get_contents($this->config_tpl);
+                foreach ($apollo as $key =>$value){
+                    $tpl = str_replace('{'.$key.'}',$value,$tpl);
+                }
+                file_put_contents($this->config_file,$tpl);
+            }); //此处传入回调
+            if ($error) {
+                Log::info("Apollo Hand error :" . $error);
+            }
+            sleep(60);
+        } while ($error && $restart);
+    }
+}

+ 2 - 1
app/Console/Kernel.php

@@ -2,6 +2,7 @@
 
 namespace App\Console;
 
+use App\Console\Commands\Apollo;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
 
@@ -13,7 +14,7 @@ class Kernel extends ConsoleKernel
      * @var array
      */
     protected $commands = [
-        //
+        Apollo::class
     ];
 
     /**

+ 70 - 0
app/Helper/helper.php

@@ -0,0 +1,70 @@
+<?php
+/**
+ * 添加自定义辅助函数
+ */
+
+if ( ! function_exists('config_path'))
+{
+    /**
+     * Get the configuration path.
+     *
+     * @param string $path
+     * @return string
+     */
+    function config_path($path = '')
+    {
+        return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
+    }
+}
+
+/**
+ * @param $url 地址
+ * @param $param 参数
+ * @param bool $isCheck 是否检查返回结果
+ * @param string $method 请求方式
+ * @return array|mixed
+ * @throws \GuzzleHttp\Exception\GuzzleException
+ */
+function http($url, $param, $isCheck = true, $method = 'post')
+{
+    try {
+        $client = new \GuzzleHttp\Client();
+        $response = $client->request($method, $url, $param);
+        $result = json_decode($response->getBody()->getContents(), true);
+        if ($isCheck == true) {
+            if ($result['code'] == 0) {
+                return $result['data'];
+            } else {
+                return [];
+            }
+        } else {
+            return $result;
+        }
+
+    } catch (\Exception $exception) {
+        return [];
+    }
+
+}
+
+function generateSign(array $params, $secret_key)
+{
+    unset($params['sign']);
+    // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
+    $params = array_filter($params);
+
+    // 按照键名对参数数组进行升序排序
+    ksort($params);
+
+    // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
+    $params['chxq_key'] = $secret_key;
+    \Illuminate\Support\Facades\Log::debug($params);
+    // 生成 URL-encode 之后的请求字符串
+    $str = http_build_query($params);
+    $str = urldecode($str);
+    \Illuminate\Support\Facades\Log::debug($str);
+    //$str = "address=计算机啊手机壳阿看见手机卡&address_type=1&area_id=2&area_name=西安市&city_id=2&city_name=西安市&contact_mobile
+    //=18458881890&contact_name=刘德华&province_id=1&province_name=陕西省&uid=0&zipcode=1000000";
+    // 将请求字符串使用MD5加密后,再转换成大写,并返回
+    return strtoupper(MD5($str));
+}

+ 8 - 3
app/Http/Controllers/V1/AuthController.php

@@ -8,6 +8,7 @@
 
 namespace App\Http\Controllers\V1;
 
+use App\Shop;
 use App\ShopAccount;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
@@ -55,18 +56,22 @@ class AuthController extends Controller {
         if ($validator->fails()) {
             return $this->response->error($validator->errors()->first(), 500);
         }
+
         $account = ShopAccount::where(['account'=>$data['account'],'status'=>1])->first();
         if(!$account){
             return $this->response->error('登录失败,请重试', 500);
         }
-        $token = Auth::attempt(['mobile'=>$request->get('mobile'),'password'=>$request->get('password')]);
+        $shop = Shop::where(['shop_id'=>$account->shop_id,'status'=>1])->first();
+        if(!$shop){
+            return $this->response->error('登录失败,该商家不存在或已禁用', 500);
+        }
+        $token = Auth::attempt(['account'=>$request->get('account'),'password'=>$request->get('password')]);
         if(!$token){
             return $this->response->error('登陆失败', 500);
         }else{
             $shopAccount = Auth::user();
-            $factory = JWTFactory::customClaims(['shop'=>['uid'=>$shopAccount->id,'username'=>$shopAccount->account,'shop_id'=>$shopAccount->shop_id,'sign'=>md5($shopAccount->id).env('JWT_SECRET')],'type'=>2]);
+            $factory = JWTFactory::customClaims(['shop'=>['uid'=>$shopAccount->id,'username'=>$shopAccount->account,'shop_id'=>$shopAccount->shop_id,'sign'=>md5($shopAccount->id).config('customer.jwt_secret')],'type'=>2]);
             $payload = $factory->make();
-
             $token = JWTAuth::encode($payload);
             $shopAccount->token = $token;
             $shopAccount->token_ttl = config('jwt.ttl');

+ 52 - 0
app/Http/Controllers/V1/ShopController.php

@@ -9,6 +9,7 @@
 namespace App\Http\Controllers\V1;
 
 
+use App\Repositories\ProductRepository;
 use App\Repositories\ShopRepository;
 use App\Shop;
 use App\ShopAccount;
@@ -149,6 +150,35 @@ class ShopController extends Controller {
         $data['extra']['columns'] = ['shop_id','shop_name','mobile','province_name','city_name','product_count','proportion','status'];
         return $data;
     }
+
+    /**
+     * @param Request $request
+     * @return array
+     */
+    public function isOpen(Request $request){
+        $data = $request->only('shop_id','status');
+        $validator = Validator::make($data, [
+            'shop_id' => 'required|integer',
+            'status' =>['required',Rule::in([0,1])],
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        $res = Shop::where(['shop_id'=>$data['shop_id']])->update(['status'=>$data['status']]);
+        if($res){
+            //下架商家所有商品
+            if($data['status'] == 0){
+                $product = new ProductRepository();
+                $upStatus = $product->upProductStatus($data['shop_id']);
+                dd($upStatus);
+                if($upStatus['status_code'] == 200){
+                    return  ['message'  => '成功','status_code'   => 200];
+                }
+            }
+        }else{
+            return $this->response->error("失败", 500);
+        }
+    }
     /**
      * @param Request $request
      * @return mixed
@@ -167,5 +197,27 @@ class ShopController extends Controller {
         }
     }
 
+    /**
+     * @param Request $request
+     * @return array
+     * 删除账号
+     */
+    public function deleteShopAccount(Request $request){
+        $data = $request->only('shop_id');
+        $validator = Validator::make($data, [
+            'id' => 'required|integer',
+            'shop_id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        $res = ShopAccount::where(['id'=>$data['id'],'shop_id'=>$data['shop_id']])->delete();
+        if($res){
+            //下架商家所有商品,放入队列
+            return  ['message'  => '成功','status_code'   => 200];
+        }else{
+            return $this->response->error("失败", 500);
+        }
+    }
 
 }

+ 27 - 0
app/Repositories/ProductRepository.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-05-20
+ * Time: 17:05
+ */
+
+namespace App\Repositories;
+use Tymon\JWTAuth\Facades\JWTAuth;
+
+class ProductRepository {
+
+    public function upProductStatus($shop_id) {
+        try {
+            //$sign = generateSign(['shop_id'=>$shop_id], config('customer.app_secret'));
+            $url = config("customer.app_service_url").'/operate/product/shop_status';
+            //$url = 'http://localhost:8080/userInfo';
+            $array = [
+                'json' => ['shop_id'=>$shop_id], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+            ];
+            return http($url,$array,false,'put');
+        } catch (\Exception $e) {
+            return [];
+        }
+    }
+}

+ 4 - 2
app/Repositories/ShopRepository.php

@@ -9,6 +9,7 @@ namespace App\Repositories;
 
 use App\Shop;
 use App\ShopAccount;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Hash;
 use League\Flysystem\Exception;
 
@@ -57,8 +58,6 @@ class ShopRepository {
             return false;
         }
     }
-
-
     //修改
     public function editShopAccount($data = []){
         $shop_id = $data['shop_id'];
@@ -69,6 +68,7 @@ class ShopRepository {
         unset($data['shop_id']);
         unset($data['shop_account_id']);
         unset($data['account']);
+        DB::beginTransaction();
         try{
             $shop = Shop::where(['shop_id'=>$shop_id])->update($data);
             $accountData = [];
@@ -78,8 +78,10 @@ class ShopRepository {
                 $accountData['password'] = Hash::make($password);
                 ShopAccount::where(['id'=>$shop_account_id])->update($accountData);
             }
+            DB::commit();
             return true;
         }catch (Exception $exception){
+            DB::rollBack();
             return false;
         }
     }

+ 7 - 7
app/Repositories/StatisticsRepository.php

@@ -21,7 +21,7 @@ class StatisticsRepository
     public function getOrderPayInfo($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,
@@ -89,7 +89,7 @@ class StatisticsRepository
     public function getDfhOrder($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,
@@ -147,7 +147,7 @@ class StatisticsRepository
     public function getFinishOrder($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,
@@ -209,7 +209,7 @@ class StatisticsRepository
     public function getOrderNum($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,
@@ -260,7 +260,7 @@ class StatisticsRepository
     public function getRefundOrderNum($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,
@@ -316,7 +316,7 @@ class StatisticsRepository
     public function getFeedBackOrderNum($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,
@@ -372,7 +372,7 @@ class StatisticsRepository
     public function getSaleMoney($start = '', $end = '', $shopId = '')
     {
         $params = [
-            "index" => "platform_order_statistics",
+            "index" => config('customer.order_statistics_index_name'),
             "type" => "_doc",
             "body" => [
                 "size" => 0,

+ 6 - 7
app/Transformers/LoginTransformer.php

@@ -6,20 +6,19 @@ use App\ShopAccount;
 use App\User;
 use League\Fractal\TransformerAbstract;
 
-class LoginTransformer extends TransformerAbstract
-{
+class LoginTransformer extends TransformerAbstract {
 
-    public function transform(User $user)
-    {
+    public function transform(User $user) {
         return [
-            'id'    => $user['id'],
-            'account'    => !empty($user['account'])?$user['account']:"",
-            'mobile'    => $user['mobile'],
+            'id' => $user['id'],
+            'account' => !empty($user['account']) ? $user['account'] : "",
+            'mobile' => $user['mobile'],
             'status' => $user['status'],
             'shop_id' => $user['shop_id'],
             'token' => "Bearer{$user['token']}",
             'token_ttl' => $user['token_ttl'],
             'shop_account' => $user->shop->toArray(),
+            'menu' => config("menu"),
         ];
     }
 }

+ 5 - 0
bootstrap/app.php

@@ -25,8 +25,11 @@ $app->withFacades();
 
 $app->withEloquent();
 $app->configure('api');
+$app->configure('apollo');
 $app->configure('auth');
 $app->configure('jwt');
+$app->configure('menu');
+$app->configure('database');
 
 /*
 |--------------------------------------------------------------------------
@@ -85,6 +88,8 @@ $app->register(App\Providers\AuthServiceProvider::class);
 
 $app->register(Dingo\Api\Provider\LumenServiceProvider::class);
 $app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
+$app->register(\Cviebrock\LaravelElasticsearch\ServiceProvider::class);
+class_alias('\Cviebrock\LaravelElasticsearch\Facade','Elasticsearch');
 /*
 |--------------------------------------------------------------------------
 | Load The Application Routes

+ 5 - 1
composer.json

@@ -11,6 +11,7 @@
         "hhxsv5/laravel-s": "~3.4.0",
         "laravel/lumen-framework": "5.8.*",
         "vlucas/phpdotenv": "^3.3",
+        "multilinguals/apollo-client": "^0.1.2",
         "tymon/jwt-auth": "1.0.0-rc.4.1"
     },
     "require-dev": {
@@ -25,7 +26,10 @@
         ],
         "psr-4": {
             "App\\": "app/"
-        }
+        },
+        "files" : [
+            "app/Helper/helper.php"
+        ]
     },
     "autoload-dev": {
         "classmap": [

+ 10 - 0
config/apollo.php

@@ -0,0 +1,10 @@
+<?php
+
+return [
+    'namespaces' => explode(',', env('APOLLO_NAMESPACES')),
+    'cluster' => env('APOLLO_CLUSTER'),
+    'save_dir' => storage_path('apollo'),
+    'config_server' => env('APOLLO_CONFIG_SERVER'),
+    'app_id' => env('APP_ID'),
+    'timeout_interval' => 70
+];

+ 7 - 0
config/customer.tpl

@@ -0,0 +1,7 @@
+<?php
+//此文件为apollo配置文件模板,禁止修改移动
+return [
+    'jwt_secret' => '{jwt_secret}',
+    'manage_service_url' => '{manage_service_url}',
+    'order_statistics_index_name' => '{order_statistics_index_name}',
+];

+ 131 - 0
config/database.php

@@ -0,0 +1,131 @@
+<?php
+
+return [
+
+    /*
+    |--------------------------------------------------------------------------
+    | Default Database Connection Name
+    |--------------------------------------------------------------------------
+    |
+    | Here you may specify which of the database connections below you wish
+    | to use as your default connection for all database work. Of course
+    | you may use many connections at once using the Database library.
+    |
+    */
+
+    'default' => env('DB_CONNECTION', 'mysql'),
+
+    /*
+    |--------------------------------------------------------------------------
+    | Database Connections
+    |--------------------------------------------------------------------------
+    |
+    | Here are each of the database connections setup for your application.
+    | Of course, examples of configuring each database platform that is
+    | supported by Laravel is shown below to make development simple.
+    |
+    |
+    | All database work in Laravel is done through the PHP PDO facilities
+    | so make sure you have the driver for your particular database of
+    | choice installed on your machine before you begin development.
+    |
+    */
+
+    'connections' => [
+
+        'sqlite' => [
+            'driver' => 'sqlite',
+            'database' => env('DB_DATABASE', database_path('database.sqlite')),
+            'prefix' => env('DB_PREFIX', ''),
+        ],
+
+        'mysql' => [
+            'driver' => 'mysql',
+            'host' => env('DB_HOST', '127.0.0.1'),
+            'port' => env('DB_PORT', 3306),
+            'database' => env('DB_DATABASE', 'forge'),
+            'username' => env('DB_USERNAME', 'forge'),
+            'password' => env('DB_PASSWORD', ''),
+            'unix_socket' => env('DB_SOCKET', ''),
+            'charset' => env('DB_CHARSET', 'utf8mb4'),
+            'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
+            'prefix' => env('DB_PREFIX', ''),
+            'strict' => env('DB_STRICT_MODE', true),
+            'engine' => env('DB_ENGINE', null),
+            'timezone' => env('DB_TIMEZONE', '+08:00'),
+        ],
+
+        'pgsql' => [
+            'driver' => 'pgsql',
+            'host' => env('DB_HOST', '127.0.0.1'),
+            'port' => env('DB_PORT', 5432),
+            'database' => env('DB_DATABASE', 'forge'),
+            'username' => env('DB_USERNAME', 'forge'),
+            'password' => env('DB_PASSWORD', ''),
+            'charset' => env('DB_CHARSET', 'utf8'),
+            'prefix' => env('DB_PREFIX', ''),
+            'schema' => env('DB_SCHEMA', 'public'),
+            'sslmode' => env('DB_SSL_MODE', 'prefer'),
+        ],
+
+        'sqlsrv' => [
+            'driver' => 'sqlsrv',
+            'host' => env('DB_HOST', 'localhost'),
+            'port' => env('DB_PORT', 1433),
+            'database' => env('DB_DATABASE', 'forge'),
+            'username' => env('DB_USERNAME', 'forge'),
+            'password' => env('DB_PASSWORD', ''),
+            'charset' => env('DB_CHARSET', 'utf8'),
+            'prefix' => env('DB_PREFIX', ''),
+        ],
+
+    ],
+
+    /*
+    |--------------------------------------------------------------------------
+    | Migration Repository Table
+    |--------------------------------------------------------------------------
+    |
+    | This table keeps track of all the migrations that have already run for
+    | your application. Using this information, we can determine which of
+    | the migrations on disk haven't actually been run in the database.
+    |
+    */
+
+    'migrations' => 'migrations',
+
+    /*
+    |--------------------------------------------------------------------------
+    | Redis Databases
+    |--------------------------------------------------------------------------
+    |
+    | Redis is an open source, fast, and advanced key-value store that also
+    | provides a richer set of commands than a typical key-value systems
+    | such as APC or Memcached. Laravel makes it easy to dig right in.
+    |
+    */
+
+    'redis' => [
+
+        'client' => 'predis',
+
+        'cluster' => env('REDIS_CLUSTER', false),
+
+        'default' => [
+            'host' => env('REDIS_HOST', '127.0.0.1'),
+            'password' => env('REDIS_PASSWORD', null),
+            'port' => env('REDIS_PORT', 6379),
+            'database' => env('REDIS_DB', 0),
+            'read_write_timeout' => 0,
+        ],
+
+        'cache' => [
+            'host' => env('REDIS_HOST', '127.0.0.1'),
+            'password' => env('REDIS_PASSWORD', null),
+            'port' => env('REDIS_PORT', 6379),
+            'database' => env('REDIS_CACHE_DB', 1),
+        ],
+
+    ],
+
+];

+ 1 - 1
config/jwt.php

@@ -25,7 +25,7 @@ return [
     |
     */
 
-    'secret' => env('JWT_SECRET'),
+    'secret' => env('JWT_SECRET',config('customer.jwt_secret')),
 
     /*
     |--------------------------------------------------------------------------

+ 53 - 0
config/menu.php

@@ -0,0 +1,53 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-05-16
+ * Time: 11:22
+ */
+return [
+    [
+        'name' => 'management',
+        'title' => '商家',
+        'children' => [
+            [
+                'name' => 'index',
+                'title' => '首页',
+                'children' => [
+                    [
+                        'name' => 'statistics-shop',
+                        'title' => '统计',
+                    ],
+                ],
+            ],
+            [
+                'name' => 'product',
+                'title' => '商品',
+                'children' => [
+                    [
+                        'name' => 'product-product',
+                        'title' => '商品管理',
+                    ],
+                    [
+                        'name' => 'product-comment',
+                        'title' => '评价管理',
+                    ],
+                    [
+                        'name' => 'product-trash',
+                        'title' => '回收站',
+                    ],
+                ],
+            ],
+            [
+                'name' => 'order',
+                'title' => '订单',
+                'children' => [
+                    [
+                        'name' => 'order-order',
+                        'title' => '订单管理',
+                    ],
+                ],
+            ],
+        ],
+    ]
+];

+ 3 - 8
routes/api.php

@@ -45,16 +45,11 @@ $api->version('v1', [
         //用户列表
         //商户详情
         $api->get('shopView', 'ShopController@view');
+        //修改状态
+        $api->put('isOpen', 'ShopController@isOpen');
 
         $api->get('user', 'UserController@index');
-        //用户列表
-        $api->get('memberList', 'MemberController@memberList');
-        //用户详情
-        $api->post('memberView', 'MemberController@view');
-        //修改状态
-        $api->put('updateStatus', 'MemberController@updateStatus');
-        //设置属性
-        $api->post('setAttr', 'MemberController@setAttr');
+
         //公共配置
         $api->post('configIndex', 'ConfigController@index');
     });