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.

170 lines
5.1 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. }
  30. else
  31. {
  32. return false;
  33. }
  34. return $res;
  35. }
  36. //获取一条记录
  37. public static function getOne($address_id)
  38. {
  39. $arr = array();
  40. if ($address_id)
  41. {
  42. return self::where('id',$address_id)->first()->toArray();
  43. }
  44. if (Token::$uid > 0)
  45. {
  46. // 取默认地址
  47. $arr = self::join('user','user_address.id', '=', 'user.address_id')
  48. ->where('user.user_id',Token::$uid)
  49. ->first()->toArray();
  50. }
  51. return $arr;
  52. }
  53. public static function add(array $param)
  54. {
  55. extract($param);
  56. $model = new UserAddress;
  57. $model->user_id = Token::$uid;
  58. $model->name = $name;
  59. $model->email = isset($email) ? $email : '';
  60. $model->country = isset($country) ? $country : 0;
  61. $model->province = isset($province) ? $province : 0;
  62. $model->city = isset($city) ? $city : 0;
  63. $model->district = isset($district) ? $district : 0;
  64. $model->address = $address;
  65. $model->mobile = isset($mobile) ? $mobile : '';
  66. $model->telphone = isset($telphone) ? $telphone : '';
  67. $model->zipcode = isset($zipcode) ? $zipcode : '';
  68. $model->sign_building = isset($sign_building) ? $sign_building : '';
  69. $model->best_time = isset($best_time) ? $best_time : '';
  70. $model->is_default = isset($is_default) ? $is_default : 0;
  71. if ($model->save())
  72. {
  73. $user = User::where('user_id', Token::$uid)->first();
  74. if (!UserAddress::where('id', $user->address_id)->first())
  75. {
  76. $user->address_id = $model->id;
  77. $user->save();
  78. }
  79. return $model->toArray();
  80. }
  81. return false;
  82. }
  83. public static function modify(array $param)
  84. {
  85. extract($param);
  86. if ($model = UserAddress::where('id', $id)->where('user_id', Token::$uid)->first())
  87. {
  88. $model->user_id = Token::$uid;
  89. $model->name = $name;
  90. $model->email = isset($email) ? $email : '';
  91. $model->country = isset($country) ? $country : 0;
  92. $model->province = isset($province) ? $province : 0;
  93. $model->city = isset($city) ? $city : 0;
  94. $model->district = isset($district) ? $district : 0;
  95. $model->address = $address;
  96. $model->mobile = isset($mobile) ? $mobile : '';
  97. $model->telphone = isset($telphone) ? $telphone : '';
  98. $model->zipcode = isset($zipcode) ? $zipcode : '';
  99. $model->sign_building = isset($sign_building) ? $sign_building : '';
  100. $model->best_time = isset($best_time) ? $best_time : '';
  101. $model->is_default = isset($is_default) ? $is_default : 0;
  102. if ($model->save())
  103. {
  104. return $model->toArray();
  105. }
  106. }
  107. return false;
  108. }
  109. //删除一条记录
  110. public static function remove($id)
  111. {
  112. if (UserAddress::where('id', $id)->where('user_id', Token::$uid)->delete())
  113. {
  114. if ($address = UserAddress::where('user_id', Token::$uid)->first())
  115. {
  116. $user = User::where('id', Token::$uid)->first();
  117. if($user->address_id == $id)
  118. {
  119. $user->address_id = $address->id;
  120. $user->save();
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126. //设为默认地址
  127. public static function setDefault($id)
  128. {
  129. if (UserAddress::where('id', $id)->where('user_id', Token::$uid)->first())
  130. {
  131. if($user = User::where('id', Token::$uid)->first())
  132. {
  133. $user->address_id = $id;
  134. $user->save();
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. }