Browse Source

useraddresscontroller

master
林一峰 7 years ago
parent
commit
5b977dbfa2
  1. 140
      app/Http/Controllers/Api/UserAddressController.php
  2. 140
      app/Http/Controllers/Api/UserController.php
  3. 6
      app/Http/Kernel.php
  4. 5
      app/Http/Model/Region.php
  5. 3
      app/Http/Model/User.php
  6. 60
      app/Http/Model/UserAddress.php
  7. 2
      routes/web.php

140
app/Http/Controllers/Api/UserAddressController.php

@ -0,0 +1,140 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\Common\ReturnData;
use App\Http\Model\UserAddress;
class UserAddressController extends CommonController
{
public function __construct()
{
parent::__construct();
}
//用户收货地址列表
public function userAddressList(Request $request)
{
//参数
$data['limit'] = $request->input('limit', 10);
$data['offset'] = $request->input('offset', 0);
$res = UserAddress::getList($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//用户收货地址详情
public function userAddressDetail(Request $request)
{
//参数
$id = $request->input('id',null);
$res = UserAddress::getOne($id);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//设为默认地址
public function userAddressSetDefault(Request $request)
{
//参数
$id = $request->input('id',null);
$res = UserAddress::setDefault($id);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//添加收货地址
public function userAddressAdd(Request $request)
{
//参数
$data['name'] = $request->input('name',null);
$data['mobile'] = $request->input('mobile',null);
$data['country'] = $request->input('country',null);
$data['province'] = $request->input('province',null);
$data['city'] = $request->input('city',null);
$data['district'] = $request->input('district',null);
$data['address'] = $request->input('address',null);
if($request->input('telphone',null)!==null){$data['telphone'] = $request->input('telphone');}
if($request->input('zipcode',null)!==null){$data['zipcode'] = $request->input('zipcode');}
if($request->input('email',null)!==null){$data['email'] = $request->input('email');}
if($request->input('best_time',null)!==null){$data['best_time'] = $request->input('best_time');}
if($request->input('is_default',null)!==null){$data['is_default'] = $request->input('is_default');}
if($data['name']===null || $data['mobile']===null || $data['address']===null || $data['country']===null || $data['province']===null || $data['city']===null || $data['district']===null)
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserAddress::add($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//修改收货地址
public function userAddressUpdate(Request $request)
{
//参数
$data['id'] = $request->input('id',null);
$data['name'] = $request->input('name',null);
$data['mobile'] = $request->input('mobile',null);
$data['country'] = $request->input('country',null);
$data['province'] = $request->input('province',null);
$data['city'] = $request->input('city',null);
$data['district'] = $request->input('district',null);
$data['address'] = $request->input('address',null);
if($request->input('telphone',null)!==null){$data['telphone'] = $request->input('telphone');}
if($request->input('zipcode',null)!==null){$data['zipcode'] = $request->input('zipcode');}
if($request->input('email',null)!==null){$data['email'] = $request->input('email');}
if($request->input('best_time',null)!==null){$data['best_time'] = $request->input('best_time');}
if($request->input('is_default',null)!==null){$data['is_default'] = $request->input('is_default');}
if($data['id']===null || $data['name']===null || $data['mobile']===null || $data['address']===null || $data['country']===null || $data['province']===null || $data['city']===null || $data['district']===null)
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserAddress::modify($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//删除收货地址
public function userAddressDelete(Request $request)
{
//参数
$id = $request->input('id',null);
$res = UserAddress::remove($id);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
}

140
app/Http/Controllers/Api/UserController.php

@ -0,0 +1,140 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\CommonController;
use Illuminate\Http\Request;
use App\Common\ReturnData;
use App\Http\Model\User;
class UserController extends CommonController
{
public function __construct()
{
parent::__construct();
}
//用户收货地址列表
public function userAddressList(Request $request)
{
//参数
$data['limit'] = $request->input('limit', 10);
$data['offset'] = $request->input('offset', 0);
$res = UserAddress::getList($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//用户收货地址详情
public function userAddressDetail(Request $request)
{
//参数
$id = $request->input('id',null);
$res = UserAddress::getOne($id);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//设为默认地址
public function userAddressSetDefault(Request $request)
{
//参数
$id = $request->input('id',null);
$res = UserAddress::setDefault($id);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//添加收货地址
public function userAddressAdd(Request $request)
{
//参数
$data['name'] = $request->input('name',null);
$data['mobile'] = $request->input('mobile',null);
$data['country'] = $request->input('country',null);
$data['province'] = $request->input('province',null);
$data['city'] = $request->input('city',null);
$data['district'] = $request->input('district',null);
$data['address'] = $request->input('address',null);
if($request->input('telphone',null)!==null){$data['telphone'] = $request->input('telphone');}
if($request->input('zipcode',null)!==null){$data['zipcode'] = $request->input('zipcode');}
if($request->input('email',null)!==null){$data['email'] = $request->input('email');}
if($request->input('best_time',null)!==null){$data['best_time'] = $request->input('best_time');}
if($request->input('is_default',null)!==null){$data['is_default'] = $request->input('is_default');}
if($data['name']===null || $data['mobile']===null || $data['address']===null || $data['country']===null || $data['province']===null || $data['city']===null || $data['district']===null)
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserAddress::add($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//修改收货地址
public function userAddressUpdate(Request $request)
{
//参数
$data['id'] = $request->input('id',null);
$data['name'] = $request->input('name',null);
$data['mobile'] = $request->input('mobile',null);
$data['country'] = $request->input('country',null);
$data['province'] = $request->input('province',null);
$data['city'] = $request->input('city',null);
$data['district'] = $request->input('district',null);
$data['address'] = $request->input('address',null);
if($request->input('telphone',null)!==null){$data['telphone'] = $request->input('telphone');}
if($request->input('zipcode',null)!==null){$data['zipcode'] = $request->input('zipcode');}
if($request->input('email',null)!==null){$data['email'] = $request->input('email');}
if($request->input('best_time',null)!==null){$data['best_time'] = $request->input('best_time');}
if($request->input('is_default',null)!==null){$data['is_default'] = $request->input('is_default');}
if($data['id']===null || $data['name']===null || $data['mobile']===null || $data['address']===null || $data['country']===null || $data['province']===null || $data['city']===null || $data['district']===null)
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$res = UserAddress::modify($data);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//删除收货地址
public function userAddressDelete(Request $request)
{
//参数
$id = $request->input('id',null);
$res = UserAddress::remove($id);
if(!$res)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
}

6
app/Http/Kernel.php

@ -32,7 +32,7 @@ class Kernel extends HttpKernel
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
// \App\Http\Middleware\VerifyCsrfToken::class, //禁用VerifyCsrfToken,FLi
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
@ -59,7 +59,7 @@ class Kernel extends HttpKernel
'check.login' => \App\Http\Middleware\CheckLogin::class,
'token' => \App\Http\Middleware\TokenAuth::class,
'sign' => \App\Http\Middleware\Sign::class,
'token' => \App\Http\Middleware\TokenAuth::class, //Token验证,FLi
'sign' => \App\Http\Middleware\Sign::class, //Sign验证,FLi
];
}

5
app/Http/Model/Region.php

@ -3,7 +3,6 @@ namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Common\Token;
use Cache;
class Region extends BaseModel
{
@ -14,13 +13,15 @@ class Region extends BaseModel
public static function getRegionName($id)
{
if(empty($id)){return '';}
$res = self::where('id', $id)->value('name');
if (!empty($res))
{
return $res;
}
return false;
return '';
}
public static function getList($parent_id=86)

3
app/Http/Model/User.php

@ -2,8 +2,9 @@
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Common\Token;
class User extends Model
class User extends BaseModel
{
//用户模型

60
app/Http/Model/UserAddress.php

@ -36,6 +36,17 @@ class UserAddress extends BaseModel
if($res['count']>0)
{
$res['list'] = $model->skip($offset)->take($limit)->get()->toArray();
if($res['list'])
{
foreach($res['list'] as $k=>$v)
{
$res['list'][$k]['country_name'] = Region::getRegionName($v['country']);
$res['list'][$k]['province_name'] = Region::getRegionName($v['province']);
$res['list'][$k]['city_name'] = Region::getRegionName($v['city']);
$res['list'][$k]['district_name'] = Region::getRegionName($v['district']);
}
}
}
else
{
@ -45,14 +56,24 @@ class UserAddress extends BaseModel
return $res;
}
//获取一条记录
//获取一条记录,不传address_id表示获取默认地址
public static function getOne($address_id)
{
$arr = array();
$arr = '';
if ($address_id)
{
return self::where('id',$address_id)->first()->toArray();
$arr = self::where('id',$address_id)->first();
if($arr)
{
$arr->country_name = Region::getRegionName($arr->country);
$arr->province_name = Region::getRegionName($arr->province);
$arr->city_name = Region::getRegionName($arr->city);
$arr->district_name = Region::getRegionName($arr->district);
}
return $arr;
}
if (Token::$uid > 0)
@ -60,7 +81,15 @@ class UserAddress extends BaseModel
// 取默认地址
$arr = self::join('user','user_address.id', '=', 'user.address_id')
->where('user.user_id',Token::$uid)
->first()->toArray();
->first();
if($arr)
{
$arr->country_name = Region::getRegionName($arr->country);
$arr->province_name = Region::getRegionName($arr->province);
$arr->city_name = Region::getRegionName($arr->city);
$arr->district_name = Region::getRegionName($arr->district);
}
}
return $arr;
@ -88,12 +117,11 @@ class UserAddress extends BaseModel
if ($model->save())
{
$user = User::where('user_id', Token::$uid)->first();
$user = User::where('id', Token::$uid)->first();
if (!UserAddress::where('id', $user->address_id)->first())
if (!UserAddress::where('id', $user->address_id)->first() || $model->is_default!=0)
{
$user->address_id = $model->id;
$user->save();
self::setDefault($model->id);
}
return $model->toArray();
@ -125,6 +153,11 @@ class UserAddress extends BaseModel
if ($model->save())
{
if ($model->is_default!=0)
{
self::setDefault($model->id);
}
return $model->toArray();
}
}
@ -153,13 +186,18 @@ class UserAddress extends BaseModel
}
//设为默认地址
public static function setDefault($id)
public static function setDefault($address_id)
{
if (UserAddress::where('id', $id)->where('user_id', Token::$uid)->first())
if ($user_address = UserAddress::where('id', $address_id)->where('user_id', Token::$uid)->first())
{
$user_address->is_default = 1;
$user_address->save();
UserAddress::where('user_id', Token::$uid)->where('id', '<>', $address_id)->update(['is_default'=>0]);
if($user = User::where('id', Token::$uid)->first())
{
$user->address_id = $id;
$user->address_id = $address_id;
$user->save();
return true;

2
routes/web.php

@ -185,7 +185,7 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web
});
//接口路由,需token验证
Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web','token']], function () {
Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['token']], function () {
//轮播图
Route::get('/slide_list', 'SlideController@slideList');
//收货地址

Loading…
Cancel
Save