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.

242 lines
7.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Region;
  5. use App\Http\Model\UserAddress;
  6. use App\Http\Requests\UserAddressRequest;
  7. use Validator;
  8. class UserAddressLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return new UserAddress();
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new UserAddressRequest();
  22. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  23. }
  24. //列表
  25. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  26. {
  27. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  28. if($res['count'] > 0)
  29. {
  30. foreach($res['list'] as $k=>$v)
  31. {
  32. $res['list'][$k] = $this->getDataView($v);
  33. $res['list'][$k] = $this->getProvinceCityDistrictText($res['list'][$k]);
  34. }
  35. }
  36. return $res;
  37. }
  38. //分页html
  39. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  40. {
  41. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  42. if($res->count() > 0)
  43. {
  44. foreach($res as $k=>$v)
  45. {
  46. $res[$k] = $this->getDataView($v);
  47. }
  48. }
  49. return $res;
  50. }
  51. //全部列表
  52. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  53. {
  54. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  55. if($res)
  56. {
  57. foreach($res as $k=>$v)
  58. {
  59. $res[$k] = $this->getDataView($v);
  60. }
  61. }
  62. return $res;
  63. }
  64. /**
  65. * 获取一条记录,不传id表示获取默认地址
  66. * @param int $where['id'] user_address表id,选填
  67. * @param int $where['user_id'] 用户id
  68. * @return array
  69. */
  70. public function getOne($where = array(), $field = '*')
  71. {
  72. //获取默认地址
  73. if (isset($where['id']) && $where['id']!='' && $where['id']!=0)
  74. {
  75. }
  76. else
  77. {
  78. $user_address_id = model('User')->getDb()->where(['id'=>$where['user_id']])->value('address_id');
  79. if($user_address_id){$where['id'] = $user_address_id;}else{$where['is_default'] = UserAddress::IS_DEFAULT;}
  80. }
  81. $res = $this->getModel()->getOne($where, $field);
  82. if(!$res){return false;}
  83. $res = $this->getDataView($res);
  84. $res = $this->getProvinceCityDistrictText($res);
  85. return $res;
  86. }
  87. //添加
  88. public function add($data = array(), $type=0)
  89. {
  90. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  91. $validator = $this->getValidate($data, 'add');
  92. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  93. if(model('UserAddress')->getDb()->where('user_id', $data['user_id'])->count() >= 10)
  94. {
  95. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'最多10个收货地址');
  96. }
  97. $res = $this->getModel()->add($data,$type);
  98. if($res)
  99. {
  100. $user_address = model('User')->getDb()
  101. ->join('user_address', 'user.address_id', '=', 'user_address.id')
  102. ->where('user.id',$data['user_id'])
  103. ->first();
  104. if (!$user_address || $data['is_default']==UserAddress::IS_DEFAULT)
  105. {
  106. $this->setDefault(['id'=>$res,'user_id'=>$data['user_id']]);
  107. }
  108. return ReturnData::create(ReturnData::SUCCESS,$res);
  109. }
  110. return ReturnData::create(ReturnData::FAIL);
  111. }
  112. //修改
  113. public function edit($data, $where = array())
  114. {
  115. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  116. $validator = $this->getValidate($data, 'edit');
  117. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  118. $res = $this->getModel()->edit($data,$where);
  119. if($res)
  120. {
  121. if ($data['is_default']==UserAddress::IS_DEFAULT)
  122. {
  123. $this->setDefault(['id'=>$where['id'],'user_id'=>$where['user_id']]);
  124. }
  125. return ReturnData::create(ReturnData::SUCCESS,$res);
  126. }
  127. return ReturnData::create(ReturnData::FAIL);
  128. }
  129. //删除
  130. public function del($where)
  131. {
  132. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  133. $validator = $this->getValidate($where,'del');
  134. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  135. $res = $this->getModel()->del($where);
  136. if($res)
  137. {
  138. if ($address = $this->getModel()->getOne(['user_id'=>$where['user_id']]))
  139. {
  140. if(model('User')->getDb()->where(['id'=>$where['user_id'],'address_id'=>$where['id']])->update(['address_id'=>$address->id]))
  141. {
  142. $this->getModel()->edit(array('is_default' => UserAddress::IS_DEFAULT),['id'=>$address->id]);
  143. }
  144. }
  145. return ReturnData::create(ReturnData::SUCCESS,$res);
  146. }
  147. return ReturnData::create(ReturnData::FAIL);
  148. }
  149. /**
  150. * 数据获取器
  151. * @param array $data 要转化的数据
  152. * @return array
  153. */
  154. private function getDataView($data = array())
  155. {
  156. return getDataAttr($this->getModel(),$data);
  157. }
  158. /**
  159. * 设为默认地址
  160. * @param int $where['id'] user_address表id
  161. * @param int $where['user_id'] 用户id
  162. * @return array
  163. */
  164. public function setDefault($where)
  165. {
  166. if ($this->getModel()->edit(['is_default'=>UserAddress::IS_DEFAULT],$where))
  167. {
  168. $this->getModel()->getDb()->where('user_id', $where['user_id'])->where('id', '<>', $where['id'])->update(['is_default'=>0]);
  169. model('User')->edit(['address_id'=>$where['id']],['id'=>$where['user_id']]);
  170. return true;
  171. }
  172. return false;
  173. }
  174. // 获取默认地址
  175. public function userDefaultAddress($where)
  176. {
  177. $arr = [];
  178. $arr = $this->getModel()->getOne(array('user_id'=>$where['user_id'],'is_default'=>UserAddress::IS_DEFAULT));
  179. if (!$arr)
  180. {
  181. $arr = $this->getModel()->getOne(array('user_id'=>$where['user_id']));
  182. }
  183. if($arr)
  184. {
  185. $arr = $this->getProvinceCityDistrictText($arr);
  186. }
  187. return $arr;
  188. }
  189. // 获取省市区名称
  190. public function getProvinceCityDistrictText($data)
  191. {
  192. $data->country_name = isset($data->country) ? model('Region')->getRegionName(['id'=>$data->country]) : '';
  193. $data->province_name = isset($data->province) ? model('Region')->getRegionName(['id'=>$data->province]) : '';
  194. $data->city_name = isset($data->city) ? model('Region')->getRegionName(['id'=>$data->city]) : '';
  195. $data->district_name = isset($data->district) ? model('Region')->getRegionName(['id'=>$data->district]) : '';
  196. return $data;
  197. }
  198. }