From 5b977dbfa2cd6b31472573bb03528b09d00538de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=B8=80=E5=B3=B0?= <1feng.0595@gmail.com> Date: Fri, 4 Aug 2017 18:28:19 +0800 Subject: [PATCH] useraddresscontroller --- .../Controllers/Api/UserAddressController.php | 140 ++++++++++++++++++ app/Http/Controllers/Api/UserController.php | 140 ++++++++++++++++++ app/Http/Kernel.php | 6 +- app/Http/Model/Region.php | 5 +- app/Http/Model/User.php | 3 +- app/Http/Model/UserAddress.php | 60 ++++++-- routes/web.php | 2 +- 7 files changed, 338 insertions(+), 18 deletions(-) create mode 100644 app/Http/Controllers/Api/UserAddressController.php create mode 100644 app/Http/Controllers/Api/UserController.php diff --git a/app/Http/Controllers/Api/UserAddressController.php b/app/Http/Controllers/Api/UserAddressController.php new file mode 100644 index 0000000..1abf6bd --- /dev/null +++ b/app/Http/Controllers/Api/UserAddressController.php @@ -0,0 +1,140 @@ +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); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/UserController.php b/app/Http/Controllers/Api/UserController.php new file mode 100644 index 0000000..326d776 --- /dev/null +++ b/app/Http/Controllers/Api/UserController.php @@ -0,0 +1,140 @@ +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); + } +} \ No newline at end of file diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index b0a1f0a..dcfef5f 100644 --- a/app/Http/Kernel.php +++ b/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 ]; } diff --git a/app/Http/Model/Region.php b/app/Http/Model/Region.php index 3941563..2c413cd 100644 --- a/app/Http/Model/Region.php +++ b/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) diff --git a/app/Http/Model/User.php b/app/Http/Model/User.php index 8502640..d546bae 100644 --- a/app/Http/Model/User.php +++ b/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 { //用户模型 diff --git a/app/Http/Model/UserAddress.php b/app/Http/Model/UserAddress.php index c1c2143..d910618 100644 --- a/app/Http/Model/UserAddress.php +++ b/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; diff --git a/routes/web.php b/routes/web.php index 7dc3c39..614e781 100644 --- a/routes/web.php +++ b/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'); //收货地址