You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\ReturnData;
  4. class UserAddress extends BaseModel
  5. {
  6. //用户收货地址
  7. protected $table = 'user_address';
  8. public $timestamps = false;
  9. protected $hidden = array();
  10. /**
  11. * 不能被批量赋值的属性
  12. *
  13. * @var array
  14. */
  15. protected $guarded = array();
  16. const IS_DEFAULT = 1; //是默认地址
  17. //获取列表
  18. public static function getList(array $param)
  19. {
  20. extract($param); //参数:limit,offset
  21. $limit = isset($limit) ? $limit : 10;
  22. $offset = isset($offset) ? $offset : 0;
  23. $model = self::where(array('user_id'=>$user_id));
  24. $res['count'] = $model->count();
  25. $res['list'] = array();
  26. if($res['count']>0)
  27. {
  28. $res['list'] = $model->skip($offset)->take($limit)->get();
  29. if($res['list'])
  30. {
  31. foreach($res['list'] as $k=>$v)
  32. {
  33. $res['list'][$k]['country_name'] = Region::getRegionName($v['country']);
  34. $res['list'][$k]['province_name'] = Region::getRegionName($v['province']);
  35. $res['list'][$k]['city_name'] = Region::getRegionName($v['city']);
  36. $res['list'][$k]['district_name'] = Region::getRegionName($v['district']);
  37. }
  38. }
  39. }
  40. else
  41. {
  42. return false;
  43. }
  44. return $res;
  45. }
  46. //获取一条记录,不传address_id表示获取默认地址
  47. public static function getOne($user_id,$address_id='')
  48. {
  49. $arr = '';
  50. if ($address_id)
  51. {
  52. $arr = self::where(array('id'=>$address_id,'user_id'=>$user_id))->first();
  53. return $arr;
  54. }
  55. if ($user_id > 0)
  56. {
  57. // 取默认地址
  58. $arr = self::join('user','user_address.id', '=', 'user.address_id')
  59. ->where('user.id',$user_id)->select('user_address.id','user_address.name','country','province','city','district','address','user_address.mobile','zipcode')
  60. ->first();
  61. }
  62. if($arr)
  63. {
  64. $arr->country_name = Region::getRegionName($arr->country);
  65. $arr->province_name = Region::getRegionName($arr->province);
  66. $arr->city_name = Region::getRegionName($arr->city);
  67. $arr->district_name = Region::getRegionName($arr->district);
  68. }
  69. return $arr;
  70. }
  71. public static function add(array $param)
  72. {
  73. extract($param);
  74. if(UserAddress::where('user_id', $user_id)->count() >= 10)
  75. {
  76. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'最多10个收货地址');
  77. }
  78. $model = new UserAddress;
  79. $model->user_id = $user_id;
  80. $model->name = $name;
  81. $model->address = $address;
  82. $model->mobile = $mobile;
  83. $model->is_default = isset($is_default) ? $is_default : 0;
  84. if(isset($country)){$model->country = isset($country) ? $country : 0;}
  85. if(isset($province)){$model->province = isset($province) ? $province : 0;}
  86. if(isset($city)){$model->city = isset($city) ? $city : 0;}
  87. if(isset($district)){$model->district = isset($district) ? $district : 0;}
  88. if(isset($telphone)){$model->telphone = isset($telphone) ? $telphone : '';}
  89. if(isset($zipcode)){$model->zipcode = isset($zipcode) ? $zipcode : '';}
  90. if ($model->save())
  91. {
  92. $user = User::where('id', $user_id)->first();
  93. if (!UserAddress::where('id', $user->address_id)->first() || $model->is_default!=0)
  94. {
  95. self::setDefault($model->id,$user_id);
  96. }
  97. return ReturnData::create(ReturnData::SUCCESS,$model);
  98. }
  99. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  100. }
  101. public static function modify(array $param)
  102. {
  103. extract($param);
  104. if ($model = UserAddress::where('id', $id)->where('user_id', $user_id)->first())
  105. {
  106. $model->user_id = $user_id;
  107. $model->is_default = isset($is_default) ? $is_default : 0;
  108. if(isset($name)){$model->name = $name;}
  109. if(isset($country)){$model->country = $country;}
  110. if(isset($province)){$model->province = $province;}
  111. if(isset($city)){$model->city = $city;}
  112. if(isset($district)){$model->district = $district;}
  113. if(isset($address)){$model->address = $address;}
  114. if(isset($mobile)){$model->mobile = $mobile;}
  115. if(isset($telphone)){$model->telphone = $telphone;}
  116. if(isset($zipcode)){$model->zipcode = $zipcode;}
  117. if ($model->save())
  118. {
  119. if ($model->is_default!=0)
  120. {
  121. self::setDefault($model->id,$user_id);
  122. }
  123. return $model->toArray();
  124. }
  125. }
  126. return false;
  127. }
  128. //删除一条记录
  129. public static function remove($id,$user_id)
  130. {
  131. if (UserAddress::where('id', $id)->where('user_id', $user_id)->delete())
  132. {
  133. if ($address = UserAddress::where('user_id', $user_id)->first())
  134. {
  135. $user = User::where('id', $user_id)->first();
  136. if($user->address_id == $id)
  137. {
  138. $user->address_id = $address->id;
  139. $user->save();
  140. self::where('id',$address->id)->update(array('is_default' => 1));
  141. }
  142. }
  143. }
  144. return true;
  145. }
  146. //设为默认地址
  147. public static function setDefault($address_id,$user_id)
  148. {
  149. if ($user_address = UserAddress::where('id', $address_id)->where('user_id', $user_id)->first())
  150. {
  151. $user_address->is_default = 1;
  152. $user_address->save();
  153. UserAddress::where('user_id', $user_id)->where('id', '<>', $address_id)->update(['is_default'=>0]);
  154. if($user = User::where('id', $user_id)->first())
  155. {
  156. $user->address_id = $address_id;
  157. $user->save();
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. // 获取默认地址
  164. public static function userDefaultAddress($user_id)
  165. {
  166. $arr = '';
  167. $arr = self::where(array('user_id'=>$user_id,'is_default'=>self::IS_DEFAULT))->first();
  168. if (!$arr)
  169. {
  170. $arr = self::where(array('user_id'=>$user_id))->first();
  171. }
  172. if($arr)
  173. {
  174. $arr->country_name = Region::getRegionName($arr->country);
  175. $arr->province_name = Region::getRegionName($arr->province);
  176. $arr->city_name = Region::getRegionName($arr->city);
  177. $arr->district_name = Region::getRegionName($arr->district);
  178. }
  179. return $arr;
  180. }
  181. }