Browse Source

reginmodel,useraddressmodel

master
林一峰 7 years ago
parent
commit
690ea15098
  1. 31
      app/Common/ReturnData.php
  2. 9
      app/Http/Model/BaseModel.php
  3. 48
      app/Http/Model/Region.php
  4. 159
      app/Http/Model/UserAddress.php

31
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 = '系统错误';
}

9
app/Http/Model/BaseModel.php

@ -0,0 +1,9 @@
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
}

48
app/Http/Model/Region.php

@ -0,0 +1,48 @@
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Common\Token;
class Region extends BaseModel
{
//地区
protected $table = 'region';
public $timestamps = false;
public static function getRegionName($id)
{
$res = self::where('id', $id)->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;
}
}

159
app/Http/Model/UserAddress.php

@ -0,0 +1,159 @@
<?php
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use App\Common\Token;
class UserAddress extends BaseModel
{
//用户收货地址
protected $table = 'user_address';
public $timestamps = false;
protected $hidden = array();
/**
* 不能被批量赋值的属性
*
* @var array
*/
protected $guarded = array();
//获取列表
public static function getList()
{
return self::where('user_id', Token::$uid)->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;
}
}
Loading…
Cancel
Save