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.

234 lines
7.4 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
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\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. return $res;
  43. }
  44. //全部列表
  45. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  46. {
  47. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  48. /* if($res)
  49. {
  50. foreach($res as $k=>$v)
  51. {
  52. $res[$k] = $this->getDataView($v);
  53. }
  54. } */
  55. return $res;
  56. }
  57. /**
  58. * 获取一条记录,不传id表示获取默认地址
  59. * @param int $where['id'] user_address表id,选填
  60. * @param int $where['user_id'] 用户id
  61. * @return array
  62. */
  63. public function getOne($where = array(), $field = '*')
  64. {
  65. //获取默认地址
  66. if (isset($where['id']) && $where['id']!='' && $where['id']!=0)
  67. {
  68. }
  69. else
  70. {
  71. $user_address_id = model('User')->getDb()->where(['id'=>$where['user_id']])->value('address_id');
  72. if($user_address_id){$where['id'] = $user_address_id;}else{$where['is_default'] = UserAddress::IS_DEFAULT;}
  73. }
  74. $res = $this->getModel()->getOne($where, $field);
  75. if(!$res){return false;}
  76. $res = $this->getDataView($res);
  77. $res = $this->getProvinceCityDistrictText($res);
  78. return $res;
  79. }
  80. //添加
  81. public function add($data = array(), $type=0)
  82. {
  83. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  84. $validator = $this->getValidate($data, 'add');
  85. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  86. if(model('UserAddress')->getDb()->where('user_id', $data['user_id'])->count() >= 10)
  87. {
  88. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'最多10个收货地址');
  89. }
  90. $res = $this->getModel()->add($data,$type);
  91. if($res)
  92. {
  93. $user_address = model('User')->getDb()
  94. ->join('user_address', 'user.address_id', '=', 'user_address.id')
  95. ->where('user.id',$data['user_id'])
  96. ->first();
  97. if (!$user_address || $data['is_default']==UserAddress::IS_DEFAULT)
  98. {
  99. $this->setDefault(['id'=>$res,'user_id'=>$data['user_id']]);
  100. }
  101. return ReturnData::create(ReturnData::SUCCESS,$res);
  102. }
  103. return ReturnData::create(ReturnData::FAIL);
  104. }
  105. //修改
  106. public function edit($data, $where = array())
  107. {
  108. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  109. $validator = $this->getValidate($data, 'edit');
  110. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  111. $res = $this->getModel()->edit($data,$where);
  112. if($res)
  113. {
  114. if ($data['is_default']==UserAddress::IS_DEFAULT)
  115. {
  116. $this->setDefault(['id'=>$where['id'],'user_id'=>$where['user_id']]);
  117. }
  118. return ReturnData::create(ReturnData::SUCCESS,$res);
  119. }
  120. return ReturnData::create(ReturnData::FAIL);
  121. }
  122. //删除
  123. public function del($where)
  124. {
  125. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  126. $validator = $this->getValidate($where,'del');
  127. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  128. $res = $this->getModel()->del($where);
  129. if($res)
  130. {
  131. if ($address = $this->getModel()->getOne(['user_id'=>$where['user_id']]))
  132. {
  133. if(model('User')->getDb()->where(['id'=>$where['user_id'],'address_id'=>$where['id']])->update(['address_id'=>$address->id]))
  134. {
  135. $this->getModel()->edit(array('is_default' => UserAddress::IS_DEFAULT),['id'=>$address->id]);
  136. }
  137. }
  138. return ReturnData::create(ReturnData::SUCCESS,$res);
  139. }
  140. return ReturnData::create(ReturnData::FAIL);
  141. }
  142. /**
  143. * 数据获取器
  144. * @param array $data 要转化的数据
  145. * @return array
  146. */
  147. private function getDataView($data = array())
  148. {
  149. return getDataAttr($this->getModel(),$data);
  150. }
  151. /**
  152. * 设为默认地址
  153. * @param int $where['id'] user_address表id
  154. * @param int $where['user_id'] 用户id
  155. * @return array
  156. */
  157. public function setDefault($where)
  158. {
  159. if ($this->getModel()->edit(['is_default'=>UserAddress::IS_DEFAULT],$where))
  160. {
  161. $this->getModel()->getDb()->where('user_id', $where['user_id'])->where('id', '<>', $where['id'])->update(['is_default'=>0]);
  162. model('User')->edit(['address_id'=>$where['id']],['id'=>$where['user_id']]);
  163. return true;
  164. }
  165. return false;
  166. }
  167. // 获取默认地址
  168. public function userDefaultAddress($where)
  169. {
  170. $arr = [];
  171. $arr = $this->getModel()->getOne(array('user_id'=>$where['user_id'],'is_default'=>UserAddress::IS_DEFAULT));
  172. if (!$arr)
  173. {
  174. $arr = $this->getModel()->getOne(array('user_id'=>$where['user_id']));
  175. }
  176. if($arr)
  177. {
  178. $arr = $this->getProvinceCityDistrictText($arr);
  179. }
  180. return $arr;
  181. }
  182. // 获取省市区名称
  183. public function getProvinceCityDistrictText($data)
  184. {
  185. $data->country_name = isset($data->country) ? model('Region')->getRegionName(['id'=>$data->country]) : '';
  186. $data->province_name = isset($data->province) ? model('Region')->getRegionName(['id'=>$data->province]) : '';
  187. $data->city_name = isset($data->city) ? model('Region')->getRegionName(['id'=>$data->city]) : '';
  188. $data->district_name = isset($data->district) ? model('Region')->getRegionName(['id'=>$data->district]) : '';
  189. return $data;
  190. }
  191. }