瀏覽代碼

商户功能添加

zhangchangchun 6 年之前
父節點
當前提交
747386a15a

+ 77 - 0
app/Http/Controllers/V1/AuthController.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-29
+ * Time: 9:54
+ */
+
+namespace App\Http\Controllers\V1;
+
+use App\ShopAccount;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Validator;
+use Tymon\JWTAuth\Token;
+use Tymon\JWTAuth\Facades\JWTAuth;
+use App\Transformers\LoginTransformer;
+use League\Fractal\Manager;
+use League\Fractal\Resource\Item;
+
+class AuthController extends Controller {
+    public function __construct(JWTAuth $jwt)
+    {
+
+    }
+    public function refresh(Request $request)
+    {
+        $user = Auth::user();
+        $user->token = Auth::refresh();
+        $user->token_ttl = config('jwt.ttl');
+        $user->is_password = !empty($user->password)?1:0;
+        $fractal = new Manager();
+        $res = new Item($user,new LoginTransformer());
+        $array = $fractal->createData($res)->toArray();
+        //同一类型登陆只允许登陆一个
+        return $this->jsonSuccess($array);
+    }
+    /**
+     * 登出
+     * @return mixed
+     */
+    public function logout()
+    {
+        Auth::logout();
+        return $this->jsonSuccess([],'登出成功');
+    }
+    //登陆
+    public function login(Request $request){
+        $data = $request->all();
+        $validator = Validator::make($data, [
+            'account' => 'required|max:50',
+            'password' => 'required|max:32',
+        ]);
+        if ($validator->fails()) {
+            return $this->jsonError($validator->errors()->first());
+        }
+        $account = ShopAccount::where('account',$data['account'])->first();
+        if(!$account){
+            return $this->jsonError('登录失败,请重试');
+        }
+        $token = Auth::attempt(['mobile'=>$request->get('mobile'),'password'=>$request->get('password')]);
+        if(!$token){
+            return $this->jsonError('登陆失败');
+        }else{
+            $shopAccount = Auth::user();
+            $shopAccount->token = $token;
+            $shopAccount->token_ttl = config('jwt.ttl');
+            //如果有绑定微信,显示微信open_id
+            $fractal = new Manager();
+            $res = new Item($shopAccount,new LoginTransformer());
+            $array = $fractal->createData($res)->toArray();
+            //日志
+            return $this->jsonSuccess($array);
+        }
+    }
+
+}

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

@@ -0,0 +1,105 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-28
+ * Time: 15:31
+ */
+
+namespace App\Http\Controllers\V1;
+
+
+use App\Repositories\ShopRepository;
+use App\Shop;
+use App\ShopAccount;
+use App\Transformers\ShopsTransformer;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rule;
+use League\Fractal\Manager;
+use League\Fractal\Resource\Item;
+use League\Fractal\Resource\Collection;
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
+
+class ShopController extends Controller {
+
+    public function __construct(ShopRepository $shopRepository)
+    {
+        $this->shopRepository = $shopRepository;
+    }
+    //新增商家信息
+    public function addShop(Request $request)
+    {
+        $data = $request->all();
+        $validator = Validator::make($data, [
+            'shop_name' => 'required|max:11',
+            'shop_short_name' => 'required|string|max:100',
+            'mobile' => 'required|max:11',
+            'address' => 'required|string',
+            'province_name' => 'required|string|max:50',
+            'province_id' => 'required|integer|max:6',
+            'city_name' => 'required|string:max:50',
+            'city_id' => 'required|integer|max:6',
+            'contact_name' => 'string|max:50',
+            'contact_mobile' => 'max:16',
+            'shop_desc' => 'string|max:500',
+            'status' =>['required',Rule::in([0,1])],
+            'logo_img' => 'string',
+            'license_img' => 'required|string',
+            'food_trans_license' => 'required|string',
+            'other_license' => 'string',
+            'proportion'=>'required|digits_between:1,100|integer',
+            'verify_type'=>['required',Rule::in([0,1])],
+            'account'=>'string|max:20',
+            'password'=>'string|min:6|max:18'
+        ]);
+        if ($validator->fails()) {
+            return $this->jsonError($validator->errors()->first());
+        }
+        if($data['account']){
+            $shop_account = ShopAccount::where(['account'=>$data['account']])->first();
+            if($shop_account){
+                return $this->jsonError('该账号已存在,请重新输入');
+            }
+        }
+        $shopRepository = new ShopRepository();
+        $res = $shopRepository->saveShopAccount($data);
+        if($res){
+            return $this->jsonSuccess();
+        }else{
+            return $this->jsonError('添加失败');
+        }
+    }
+    //详情
+    public function view(Request $request)
+    {
+        $data = $request->all();
+        $validator = Validator::make($data, [
+            'shop_id' => 'required|integer',
+        ]);
+        if ($validator->fails()) {
+            return $this->jsonError($validator->errors()->first());
+        }
+        $shop = Shop::where(['shop_id'=>$data['shop_id']])->first();
+        $fractal = new Manager();
+        $res = new Item($shop,new ShopsTransformer());
+        $data = $fractal->createData($res)->toArray();
+        if($data){
+            return  $this->jsonSuccess($data);
+        }else{
+            return $this->jsonError("操作失败");
+        }
+    }
+    //列表
+    public function list(Request $request){
+        $shops = $this->shopRepository->shopList($request->all());
+        $fractal = new Manager();
+        $resource = new Collection($shops, new ShopsTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($shops));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra']['filters'] = ['shop_name','status'];
+        $data['extra']['columns'] = ['shop_id','mobile','province_name','city_name','product_count','proportion','status'];
+        return $data;
+    }
+}

+ 78 - 0
app/Repositories/ShopRepository.php

@@ -0,0 +1,78 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-28
+ * Time: 15:49
+ */
+namespace App\Repositories;
+
+use App\Shop;
+use App\ShopAccount;
+use Illuminate\Support\Facades\Hash;
+use League\Flysystem\Exception;
+
+class ShopRepository {
+
+    public function __construct() {
+        //$this->shop = $shop;
+    }
+    //新增
+    public function addShopAccount($data = []){
+        $accountData = ['account'=>$data['account'],'password'=>Hash::make($data['password'])];
+        unset($data['account']);
+        unset($data['password']);
+        $shopData = $data;
+        try{
+            $shop = Shop::create($shopData);
+            if($shop){
+                $accountData['shop_id'] = $shop->shop_id;
+                ShopAccount::create($accountData);
+            }
+            return true;
+        }catch (Exception $exception){
+            return false;
+        }
+    }
+    //修改
+    public function saveShopAccount($data = []){
+        $shop_id = isset($data['shop_id'])?$data['shop_id']:0;
+        $shop_account_id = isset($data['shop_account_id'])?$data['shop_account_id']:0;
+        unset($data['shop_id']);
+        unset($data['shop_account_id']);
+        $shopData = $data;
+        try{
+            $shop = Shop::updateOrCreate(['shop_id'=>$shop_id],$shopData);
+            $accountData = [];
+            if($shop){
+                $accountData['shop_id'] = $shop->shop_id;
+            }
+            if(isset($data['account']) && isset($data['password'])){
+                $accountData['account'] = $data['account'];
+                $accountData['password'] = Hash::make($data['password']);
+                ShopAccount::updateOrCreate(['shop_id'=>$shop->shop_id],$accountData);
+            }
+            return true;
+        }catch (Exception $exception){
+            return false;
+        }
+    }
+
+    public function shopList($request)
+    {
+
+        $where = [];
+        if (isset($request['status'])) {
+            $where[] = ['status', '=', $request['uid']];
+        }
+        if (isset($request['shop_name'])) {
+            $where[] = ['shop_name', 'like', '%'.$request['username'].'%'];
+        }
+
+       $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
+//        $where = $this->memberWhereList($request);
+        return Shop::where($where)
+            ->orderBy('shop_id', 'desc')
+            ->paginate($perPage);
+    }
+}

+ 31 - 0
app/Repositories/UploadFileRepository.php

@@ -0,0 +1,31 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-28
+ * Time: 16:56
+ */
+
+namespace App\Repositories;
+
+
+class UploadFileRepository {
+    //验证短信
+    public function upload($file){
+        //Log::debug($data);
+
+        $client = new \GuzzleHttp\Client();
+        $url = 'https://manage.dev.caihongxingqiu.com/config/upload';
+        $array = [
+            'json' => [
+                'image'=>$file,
+            ],
+            'query' => [],
+            'http_errors' => false
+        ];
+        $response = $client->request('post', $url, $array);
+        //dump($response->getStatusCode());   #打印响应信息
+        // Log::debug($response->getBody()->getContents());
+        return json_decode($response->getBody()->getContents(),true);
+    }
+}

+ 40 - 0
app/Shop.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-28
+ * Time: 15:30
+ */
+
+namespace App;
+
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class Shop extends Model
+{
+    use SoftDeletes;
+
+    protected $primaryKey = 'shop_id';
+
+    public $table = 'shop';
+    /**
+     * The attributes that are mass assignable.
+     *
+     * @var array
+     */
+    protected $fillable = ['shop_name','shop_short_name','mobile','address','province_id','province_name','city_id','city_name','contact_mobile','contact_name','shop_desc','status','logo_img','license_img','food_trans_license','other_license','proportion','verify_type','star'];
+
+    /**
+     * The attributes excluded from the model's JSON form.
+     *
+     * @var array
+     */
+    protected $hidden = [];
+    //关联账户
+    public function account()
+    {
+        return $this->hasOne('App\ShopAccount', 'shop_id', 'shop_id');
+    }
+}

+ 32 - 0
app/ShopAccount.php

@@ -0,0 +1,32 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-28
+ * Time: 15:30
+ */
+
+namespace App;
+
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+class ShopAccount extends Model {
+    use SoftDeletes;
+
+    public $table = 'shop_account';
+    /**
+     * The attributes that are mass assignable.
+     *
+     * @var array
+     */
+    protected $fillable = ['id','account','mobile','password','status','ip','shop_id'];
+
+    /**
+     * The attributes excluded from the model's JSON form.
+     *
+     * @var array
+     */
+    protected $hidden = ['password'];
+}

+ 25 - 0
app/Transformers/LoginTransformer.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace App\Transformers;
+
+use App\ShopAccount;
+use App\User;
+use League\Fractal\TransformerAbstract;
+
+class LoginTransformer extends TransformerAbstract
+{
+
+    public function transform(User $user)
+    {
+        return [
+            '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(),
+        ];
+    }
+}

+ 45 - 0
app/Transformers/ShopsTransformer.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019-04-28
+ * Time: 19:10
+ */
+
+namespace App\Transformers;
+
+use App\Shop;
+use League\Fractal\TransformerAbstract;
+use Carbon\Carbon;
+
+class ShopsTransformer extends  TransformerAbstract{
+
+    public function transform(Shop $shop)
+    {
+        return [
+            'shop_id'    => $shop['shop_id'],
+            'shop_name'    => $shop['shop_name'],
+            'shop_short_name'    => $shop['shop_short_name'],
+            'mobile'    => $shop['mobile'],
+            'address'    => $shop['address'],
+            'province_id'    => $shop['province_id'],
+            'province_name'    => $shop['province_name'],
+            'city_id'    => $shop['city_id'],
+            'city_name'    => $shop['city_name'],
+            'contact_mobile'    => $shop['contact_mobile'],
+            'contact_name'    => $shop['contact_name'],
+            'shop_desc'    => $shop['shop_desc'],
+            'status'    => $shop['status'],
+            'logo_img'    => $shop['logo_img'],
+            'license_img'    => $shop['license_img'],
+            'food_trans_license'    => $shop['food_trans_license'],
+            'other_license'    => $shop['other_license'],
+            'proportion'    => $shop['proportion'],
+            'verify_type'    => $shop['verify_type'],
+            'star'    => $shop['star'],
+            'created_at' => Carbon::parse($shop['created_at'])->format('Y-m-d H:i:s'),
+            'updated_at' => Carbon::parse($shop['updated_at'])->format('Y-m-d H:i:s'),
+            'shop_account' => $shop->account->toArray(),
+        ];
+    }
+}

+ 48 - 0
app/User.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace App;
+
+use Illuminate\Auth\Authenticatable;
+use Laravel\Lumen\Auth\Authorizable;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
+use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
+use Tymon\JWTAuth\Contracts\JWTSubject;
+
+class User extends Model implements JWTSubject,AuthenticatableContract, AuthorizableContract
+{
+    use Authenticatable, Authorizable;
+    public $table='shop_account';
+    /**
+     * The attributes that are mass assignable.
+     *
+     * @var array
+     */
+    protected $fillable = [
+        'id','shop_id','account','password','mobile','status','ip',
+    ];
+
+    public function shop()
+    {
+        return $this->hasOne('App\Shop', 'shop_id', 'shop_id');
+    }
+    /**
+     * The attributes excluded from the model's JSON form.
+     *
+     * @var array
+     */
+    protected $hidden = [
+        'password',
+    ];
+
+    public function getJWTIdentifier()
+    {
+        return $this->getKey();
+    }
+
+    public function getJWTCustomClaims()
+    {
+        return [];
+    }
+
+}

+ 1 - 1
config/jwt.php

@@ -101,7 +101,7 @@ return [
     |
     */
 
-    'ttl' => env('JWT_TTL', 60),
+    'ttl' => env('JWT_TTL', 60 * 24 * 7),
 
     /*
     |--------------------------------------------------------------------------

+ 51 - 0
database/migrations/2019_04_28_172009_create_shops_table.php

@@ -0,0 +1,51 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateShopsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('shop', function (Blueprint $table) {
+            $table->bigIncrements('shop_id');
+            $table->string('shop_name')->comment('商家名称');
+            $table->string('shop_short_name')->nullable()->comment('商家简称');
+            $table->string('mobile',16)->nullable()->comment('商家电话');
+            $table->string('address')->nullable()->comment('商家地址');
+            $table->integer('province_id')->comment('省份id');
+            $table->string('province_name')->nullable()->comment('省份名称');
+            $table->integer('city_id')->comment('城市id');
+            $table->string('city_name')->nullable()->comment('城市名称');
+            $table->string('contact_mobile',16)->nullable()->default('')->comment('联系人电话');
+            $table->string('contact_name')->nullable()->default('')->comment('联系人名称');
+            $table->string('shop_desc')->nullable()->default('')->comment('商家介绍');
+            $table->tinyInteger('status')->default(1)->comment('状态 0启用 1禁用');
+            $table->string('logo_img')->nullable()->default('')->comment('商家logo');
+            $table->string('license_img')->comment('营业执照');
+            $table->string('food_trans_license')->comment('食品流通许可证');
+            $table->string('other_license')->nullable()->default('')->comment('其他证书');
+            $table->tinyInteger('proportion')->comment('利润分成');
+            $table->tinyInteger('verify_type')->default(0)->comment('商品审核方式');
+            $table->integer('star')->nullable()->default(0)->comment('商家评分');
+            $table->softDeletes();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('shops');
+    }
+}

+ 38 - 0
database/migrations/2019_04_28_172025_create_shop_accounts_table.php

@@ -0,0 +1,38 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateShopAccountsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('shop_account', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->integer('shop_id')->comment('店铺id');
+            $table->string('account')->nullable()->comment('账号');
+            $table->string('mobile',16)->nullable()->comment('手机号码');
+            $table->string('password')->nullable()->comment('密码');
+            $table->integer('status')->default(0)->comment('状态 0启用 1禁用');
+            $table->string('ip')->default('')->comment('登陆ip');
+            $table->softDeletes();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('shop_accounts');
+    }
+}

+ 9 - 1
routes/api.php

@@ -18,7 +18,15 @@ $api->version('v1', [
 ], function ($api) {
     //登录
 
-    $api->post('login', 'AuthController@authenticate');
+    //$api->post('login', 'AuthController@authenticate');
+    //新增编辑
+    $api->post('addShop', 'ShopController@addShop');
+    //商户详情
+    $api->post('shopView', 'ShopController@view');
+    //商户列表
+    $api->get('shopList', 'ShopController@list');
+    //登陆
+    $api->post('login', 'AuthController@login');
 
 
     $api->group(['middleware' => 'auth:api'], function ($api) {