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.

208 lines
6.7 KiB

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 Illuminate\Database\Eloquent\Model;
  4. use App\Common\Token;
  5. class UserAddress extends BaseModel
  6. {
  7. //用户收货地址
  8. protected $table = 'user_address';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. /**
  12. * 不能被批量赋值的属性
  13. *
  14. * @var array
  15. */
  16. protected $guarded = array();
  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('user_id', Token::$uid);
  24. $res['count'] = $model->count();
  25. $res['list'] = array();
  26. if($res['count']>0)
  27. {
  28. $res['list'] = $model->skip($offset)->take($limit)->get()->toArray();
  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($address_id)
  48. {
  49. $arr = '';
  50. if ($address_id)
  51. {
  52. $arr = self::where('id',$address_id)->first();
  53. if($arr)
  54. {
  55. $arr->country_name = Region::getRegionName($arr->country);
  56. $arr->province_name = Region::getRegionName($arr->province);
  57. $arr->city_name = Region::getRegionName($arr->city);
  58. $arr->district_name = Region::getRegionName($arr->district);
  59. }
  60. return $arr;
  61. }
  62. if (Token::$uid > 0)
  63. {
  64. // 取默认地址
  65. $arr = self::join('user','user_address.id', '=', 'user.address_id')
  66. ->where('user.user_id',Token::$uid)
  67. ->first();
  68. if($arr)
  69. {
  70. $arr->country_name = Region::getRegionName($arr->country);
  71. $arr->province_name = Region::getRegionName($arr->province);
  72. $arr->city_name = Region::getRegionName($arr->city);
  73. $arr->district_name = Region::getRegionName($arr->district);
  74. }
  75. }
  76. return $arr;
  77. }
  78. public static function add(array $param)
  79. {
  80. extract($param);
  81. $model = new UserAddress;
  82. $model->user_id = Token::$uid;
  83. $model->name = $name;
  84. $model->email = isset($email) ? $email : '';
  85. $model->country = isset($country) ? $country : 0;
  86. $model->province = isset($province) ? $province : 0;
  87. $model->city = isset($city) ? $city : 0;
  88. $model->district = isset($district) ? $district : 0;
  89. $model->address = $address;
  90. $model->mobile = isset($mobile) ? $mobile : '';
  91. $model->telphone = isset($telphone) ? $telphone : '';
  92. $model->zipcode = isset($zipcode) ? $zipcode : '';
  93. $model->sign_building = isset($sign_building) ? $sign_building : '';
  94. $model->best_time = isset($best_time) ? $best_time : '';
  95. $model->is_default = isset($is_default) ? $is_default : 0;
  96. if ($model->save())
  97. {
  98. $user = User::where('id', Token::$uid)->first();
  99. if (!UserAddress::where('id', $user->address_id)->first() || $model->is_default!=0)
  100. {
  101. self::setDefault($model->id);
  102. }
  103. return $model->toArray();
  104. }
  105. return false;
  106. }
  107. public static function modify(array $param)
  108. {
  109. extract($param);
  110. if ($model = UserAddress::where('id', $id)->where('user_id', Token::$uid)->first())
  111. {
  112. $model->user_id = Token::$uid;
  113. $model->name = $name;
  114. $model->email = isset($email) ? $email : '';
  115. $model->country = isset($country) ? $country : 0;
  116. $model->province = isset($province) ? $province : 0;
  117. $model->city = isset($city) ? $city : 0;
  118. $model->district = isset($district) ? $district : 0;
  119. $model->address = $address;
  120. $model->mobile = isset($mobile) ? $mobile : '';
  121. $model->telphone = isset($telphone) ? $telphone : '';
  122. $model->zipcode = isset($zipcode) ? $zipcode : '';
  123. $model->sign_building = isset($sign_building) ? $sign_building : '';
  124. $model->best_time = isset($best_time) ? $best_time : '';
  125. $model->is_default = isset($is_default) ? $is_default : 0;
  126. if ($model->save())
  127. {
  128. if ($model->is_default!=0)
  129. {
  130. self::setDefault($model->id);
  131. }
  132. return $model->toArray();
  133. }
  134. }
  135. return false;
  136. }
  137. //删除一条记录
  138. public static function remove($id)
  139. {
  140. if (UserAddress::where('id', $id)->where('user_id', Token::$uid)->delete())
  141. {
  142. if ($address = UserAddress::where('user_id', Token::$uid)->first())
  143. {
  144. $user = User::where('id', Token::$uid)->first();
  145. if($user->address_id == $id)
  146. {
  147. $user->address_id = $address->id;
  148. $user->save();
  149. }
  150. }
  151. }
  152. return true;
  153. }
  154. //设为默认地址
  155. public static function setDefault($address_id)
  156. {
  157. if ($user_address = UserAddress::where('id', $address_id)->where('user_id', Token::$uid)->first())
  158. {
  159. $user_address->is_default = 1;
  160. $user_address->save();
  161. UserAddress::where('user_id', Token::$uid)->where('id', '<>', $address_id)->update(['is_default'=>0]);
  162. if($user = User::where('id', Token::$uid)->first())
  163. {
  164. $user->address_id = $address_id;
  165. $user->save();
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. }