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.

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