From 690ea15098094b107ab164d6bfe31653d4bd2c58 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: Thu, 3 Aug 2017 00:27:52 +0800 Subject: [PATCH] =?UTF-8?q?reginmodel=EF=BC=8Cuseraddressmodel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Common/ReturnData.php | 31 ++++--- app/Http/Model/BaseModel.php | 9 ++ app/Http/Model/Region.php | 48 ++++++++++ app/Http/Model/UserAddress.php | 159 +++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+), 14 deletions(-) create mode 100644 app/Http/Model/BaseModel.php create mode 100644 app/Http/Model/Region.php create mode 100644 app/Http/Model/UserAddress.php diff --git a/app/Common/ReturnData.php b/app/Common/ReturnData.php index 73e602e..daf9ac1 100644 --- a/app/Common/ReturnData.php +++ b/app/Common/ReturnData.php @@ -4,17 +4,18 @@ namespace App\Common; class ReturnData { //通用 - const SUCCESS = 0; //成功 - const FORBIDDEN = 8001; //权限不足 - const SYSTEM_FAIL = 8002; //系统错误,如数据写入失败之类的 - const PARAMS_ERROR = 8003; //参数错误 - const NOT_FOUND = 8004; //资源未找到 - const TOKEN_ERROR = 8005; //token错误 - const SIGN_FAIL = 8006; //签名错误 - const RECORD_EXIST = 8007; //记录已存在 - const RECORD_NOT_EXIST = 8008; //记录不存在 - const NOT_MODIFY = 8009; //没有变动 - const IMG_TYPE_FALSE = 8010; //图片格式不正确 + const SUCCESS = 0; //操作成功 + const FORBIDDEN = 8001; //权限不足 + const SYSTEM_FAIL = 8002; //系统错误,如数据写入失败之类的 + const PARAMS_ERROR = 8003; //参数错误 + const NOT_FOUND = 8004; //资源未找到 + const TOKEN_ERROR = 8005; //token错误 + const SIGN_FAIL = 8006; //签名错误 + const RECORD_EXIST = 8007; //记录已存在 + const RECORD_NOT_EXIST = 8008; //记录不存在 + const NOT_MODIFY = 8009; //没有变动 + const UNKNOWN_ERROR = 8010; //未知错误 + const IMG_TYPE_FALSE = 8011; //图片格式不正确 //参数相关 const EMAIL_EXIST = 8201; //邮箱已存在 @@ -62,7 +63,9 @@ class ReturnData 8007 => '记录已存在', 8008 => '记录不存在', 8009 => '没有变动', - 8010 => '图片格式不正确', + 8010 => '未知错误', + 8011 => '图片格式不正确', + //参数错误 8201 => '邮箱已存在', 8202 => '邮箱格式不对正确', @@ -122,9 +125,9 @@ class ReturnData $msg = self::$codeTexts[$code]; } - if ($code == ReturnCode::SUCCESS) + if ($code == self::SUCCESS) { - $code = ReturnCode::SYSTEM_FAIL; + $code = self::SYSTEM_FAIL; $msg = '系统错误'; } diff --git a/app/Http/Model/BaseModel.php b/app/Http/Model/BaseModel.php new file mode 100644 index 0000000..989bb1d --- /dev/null +++ b/app/Http/Model/BaseModel.php @@ -0,0 +1,9 @@ +value('name'); + if (!empty($res)) + { + return $res; + } + + return false; + } + + public static function getList($parent_id=86) + { + $key = 'region'; + + if (!$model = Cache::get($key)) + { + $model = self::where('parent_id', $parent_id)->get()->toArray(); + Cache::put($key, $model, 10); + } + + return $model; + } + + public static function getOne($id) + { + $res = self::where('id', $id)->first()->toArray(); + if (!empty($res)) + { + return $res; + } + + return false; + } +} diff --git a/app/Http/Model/UserAddress.php b/app/Http/Model/UserAddress.php new file mode 100644 index 0000000..2ebf42a --- /dev/null +++ b/app/Http/Model/UserAddress.php @@ -0,0 +1,159 @@ +get()->toArray(); + } + + //获取一条记录 + public static function getOne($address_id) + { + $arr = array(); + + if ($address_id) + { + return self::where('id',$address_id)->first()->toArray(); + } + + if (Token::$uid > 0) + { + // 取默认地址 + $arr = self::join('user','user_address.id', '=', 'user.address_id') + ->where('user.user_id',Token::$uid) + ->first()->toArray(); + } + + return $arr; + } + + public static function add(array $param) + { + extract($param); + $arr = Region::getParentId($region); + + $model = new UserAddress; + $model->user_id = Token::$uid; + $model->name = $name; + $model->email = isset($email) ? $email : ''; + $model->country = isset($country) ? $country : 0; + $model->province = isset($province) ? $province : 0; + $model->city = isset($city) ? $city : 0; + $model->district = isset($district) ? $district : 0; + $model->address = $address; + $model->mobile = isset($mobile) ? $mobile : ''; + $model->telphone = isset($telphone) ? $telphone : ''; + $model->zipcode = isset($zipcode) ? $zipcode : ''; + $model->sign_building = isset($sign_building) ? $sign_building : ''; + $model->best_time = isset($best_time) ? $best_time : ''; + $model->is_default = isset($is_default) ? $is_default : 0; + + if ($model->save()) + { + $user = User::where('user_id', Token::$uid)->first(); + + if (!UserAddress::where('id', $user->address_id)->first()) + { + $user->address_id = $model->id; + $user->save(); + } + + return $model->toArray(); + } + + return false; + } + + public static function update(array $param) + { + extract($param); + + if ($model = UserAddress::where('id', $id)->where('user_id', Token::$uid)->first()) + { + $arr = Region::getParentId($region); + + $model->user_id = Token::$uid; + $model->name = $name; + $model->email = isset($email) ? $email : ''; + $model->country = isset($country) ? $country : 0; + $model->province = isset($province) ? $province : 0; + $model->city = isset($city) ? $city : 0; + $model->district = isset($district) ? $district : 0; + $model->address = $address; + $model->mobile = isset($mobile) ? $mobile : ''; + $model->telphone = isset($telphone) ? $telphone : ''; + $model->zipcode = isset($zipcode) ? $zipcode : ''; + $model->sign_building = isset($sign_building) ? $sign_building : ''; + $model->best_time = isset($best_time) ? $best_time : ''; + $model->is_default = isset($is_default) ? $is_default : 0; + + if ($model->save()) + { + return $model->toArray(); + } + } + + return false; + } + + //删除一条记录 + public static function delete(array $param) + { + extract($param); + + if (UserAddress::where('id', $id)->where('user_id', Token::$uid)->delete()) + { + if ($address = UserAddress::where('user_id', Token::$uid)->first()) + { + $user = User::where('id', Token::$uid)->first(); + + if($user->address_id == $id) + { + $user->address_id = $address->id; + $user->save(); + } + } + } + + return true; + } + + //设为默认地址 + public static function setDefault(array $param) + { + extract($param); + + if (UserAddress::where('id', $id)->where('user_id', Token::$uid)->first()) + { + if($user = User::where('id', Token::$uid)->first()) + { + $user->address_id = $id; + $user->save(); + + return true; + } + } + + return false; + } +} \ No newline at end of file