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.

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