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.

375 lines
11 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
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use DB;
  4. use Log;
  5. class UserAddress extends BaseModel
  6. {
  7. //用户收货地址
  8. protected $table = 'user_address';
  9. public $timestamps = false;
  10. protected $hidden = array();
  11. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  12. const IS_DEFAULT = 1; //是默认地址
  13. public function getDb()
  14. {
  15. return DB::table($this->table);
  16. }
  17. /**
  18. * 列表
  19. * @param array $where 查询条件
  20. * @param string $order 排序
  21. * @param string $field 字段
  22. * @param int $offset 偏移量
  23. * @param int $limit 取多少条
  24. * @return array
  25. */
  26. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 10)
  27. {
  28. $model = $this->getDb();
  29. if($where){$model = $model->where($where);}
  30. $res['count'] = $model->count();
  31. $res['list'] = array();
  32. if($res['count'] > 0)
  33. {
  34. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  35. if($order){$model = parent::getOrderByData($model, $order);}
  36. if($offset){}else{$offset = 0;}
  37. if($limit){}else{$limit = 10;}
  38. $res['list'] = $model->skip($offset)->take($limit)->get();
  39. }
  40. return $res;
  41. }
  42. /**
  43. * 分页,用于前端html输出
  44. * @param array $where 查询条件
  45. * @param string $order 排序
  46. * @param string $field 字段
  47. * @param int $limit 每页几条
  48. * @param int $page 当前第几页
  49. * @return array
  50. */
  51. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 10)
  52. {
  53. $res = $this->getDb();
  54. if($where){$res = $res->where($where);}
  55. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  56. if($order){$res = parent::getOrderByData($res, $order);}
  57. if($limit){}else{$limit = 10;}
  58. return $res->paginate($limit);
  59. }
  60. /**
  61. * 查询全部
  62. * @param array $where 查询条件
  63. * @param string $order 排序
  64. * @param string $field 字段
  65. * @param int $limit 取多少条
  66. * @return array
  67. */
  68. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  69. {
  70. $res = $this->getDb();
  71. if($where){$res = $res->where($where);}
  72. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  73. if($order){$res = parent::getOrderByData($res, $order);}
  74. if($offset){$res = $res->skip($offset);}
  75. if($limit){$res = $res->take($limit);}
  76. $res = $res->get();
  77. return $res;
  78. }
  79. /**
  80. * 获取一条
  81. * @param array $where 条件
  82. * @param string $field 字段
  83. * @return array
  84. */
  85. public function getOne($where, $field = '*')
  86. {
  87. $res = $this->getDb();
  88. if($where){$res = $res->where($where);}
  89. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  90. $res = $res->first();
  91. return $res;
  92. }
  93. /**
  94. * 添加
  95. * @param array $data 数据
  96. * @return int
  97. */
  98. public function add(array $data,$type = 0)
  99. {
  100. if($type==0)
  101. {
  102. // 新增单条数据并返回主键值
  103. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  104. }
  105. elseif($type==1)
  106. {
  107. /**
  108. * 添加单条数据
  109. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  110. * 添加多条数据
  111. * $data = [
  112. * ['foo' => 'bar', 'bar' => 'foo'],
  113. * ['foo' => 'bar1', 'bar' => 'foo1'],
  114. * ['foo' => 'bar2', 'bar' => 'foo2']
  115. * ];
  116. */
  117. return self::insert($data);
  118. }
  119. }
  120. /**
  121. * 修改
  122. * @param array $data 数据
  123. * @param array $where 条件
  124. * @return bool
  125. */
  126. public function edit($data, $where = array())
  127. {
  128. $res = $this->getDb();
  129. $res = $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  130. if ($res === false)
  131. {
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * 删除
  138. * @param array $where 条件
  139. * @return bool
  140. */
  141. public function del($where)
  142. {
  143. $res = $this->getDb();
  144. $res = $res->where($where)->delete();
  145. return $res;
  146. }
  147. /*
  148. //获取列表
  149. public static function getList(array $param)
  150. {
  151. extract($param); //参数:limit,offset
  152. $limit = isset($limit) ? $limit : 10;
  153. $offset = isset($offset) ? $offset : 0;
  154. $model = self::where(array('user_id'=>$user_id));
  155. $res['count'] = $model->count();
  156. $res['list'] = array();
  157. if($res['count']>0)
  158. {
  159. $res['list'] = $model->skip($offset)->take($limit)->get();
  160. if($res['list'])
  161. {
  162. foreach($res['list'] as $k=>$v)
  163. {
  164. $res['list'][$k]['country_name'] = Region::getRegionName($v['country']);
  165. $res['list'][$k]['province_name'] = Region::getRegionName($v['province']);
  166. $res['list'][$k]['city_name'] = Region::getRegionName($v['city']);
  167. $res['list'][$k]['district_name'] = Region::getRegionName($v['district']);
  168. }
  169. }
  170. }
  171. else
  172. {
  173. return false;
  174. }
  175. return $res;
  176. }
  177. //获取一条记录,不传address_id表示获取默认地址
  178. public static function getOne($user_id,$address_id='')
  179. {
  180. $arr = '';
  181. if ($address_id)
  182. {
  183. $arr = self::where(array('id'=>$address_id,'user_id'=>$user_id))->first();
  184. return $arr;
  185. }
  186. if ($user_id > 0)
  187. {
  188. // 取默认地址
  189. $arr = self::join('user','user_address.id', '=', 'user.address_id')
  190. ->where('user.id',$user_id)->select('user_address.id','user_address.name','country','province','city','district','address','user_address.mobile','zipcode')
  191. ->first();
  192. }
  193. if($arr)
  194. {
  195. $arr->country_name = Region::getRegionName($arr->country);
  196. $arr->province_name = Region::getRegionName($arr->province);
  197. $arr->city_name = Region::getRegionName($arr->city);
  198. $arr->district_name = Region::getRegionName($arr->district);
  199. }
  200. return $arr;
  201. }
  202. public static function add(array $param)
  203. {
  204. extract($param);
  205. if(UserAddress::where('user_id', $user_id)->count() >= 10)
  206. {
  207. return ReturnData::create(ReturnData::PARAMS_ERROR,null,'最多10个收货地址');
  208. }
  209. $model = new UserAddress;
  210. $model->user_id = $user_id;
  211. $model->name = $name;
  212. $model->address = $address;
  213. $model->mobile = $mobile;
  214. $model->is_default = isset($is_default) ? $is_default : 0;
  215. if(isset($country)){$model->country = isset($country) ? $country : 0;}
  216. if(isset($province)){$model->province = isset($province) ? $province : 0;}
  217. if(isset($city)){$model->city = isset($city) ? $city : 0;}
  218. if(isset($district)){$model->district = isset($district) ? $district : 0;}
  219. if(isset($telphone)){$model->telphone = isset($telphone) ? $telphone : '';}
  220. if(isset($zipcode)){$model->zipcode = isset($zipcode) ? $zipcode : '';}
  221. if ($model->save())
  222. {
  223. $user = User::where('id', $user_id)->first();
  224. if (!UserAddress::where('id', $user->address_id)->first() || $model->is_default!=0)
  225. {
  226. self::setDefault($model->id,$user_id);
  227. }
  228. return ReturnData::create(ReturnData::SUCCESS,$model);
  229. }
  230. return ReturnData::create(ReturnData::SYSTEM_FAIL);
  231. }
  232. public static function modify(array $param)
  233. {
  234. extract($param);
  235. if ($model = UserAddress::where('id', $id)->where('user_id', $user_id)->first())
  236. {
  237. $model->user_id = $user_id;
  238. $model->is_default = isset($is_default) ? $is_default : 0;
  239. if(isset($name)){$model->name = $name;}
  240. if(isset($country)){$model->country = $country;}
  241. if(isset($province)){$model->province = $province;}
  242. if(isset($city)){$model->city = $city;}
  243. if(isset($district)){$model->district = $district;}
  244. if(isset($address)){$model->address = $address;}
  245. if(isset($mobile)){$model->mobile = $mobile;}
  246. if(isset($telphone)){$model->telphone = $telphone;}
  247. if(isset($zipcode)){$model->zipcode = $zipcode;}
  248. if ($model->save())
  249. {
  250. if ($model->is_default!=0)
  251. {
  252. self::setDefault($model->id,$user_id);
  253. }
  254. return $model->toArray();
  255. }
  256. }
  257. return false;
  258. }
  259. //删除一条记录
  260. public static function remove($id,$user_id)
  261. {
  262. if (UserAddress::where('id', $id)->where('user_id', $user_id)->delete())
  263. {
  264. if ($address = UserAddress::where('user_id', $user_id)->first())
  265. {
  266. $user = User::where('id', $user_id)->first();
  267. if($user->address_id == $id)
  268. {
  269. $user->address_id = $address->id;
  270. $user->save();
  271. self::where('id',$address->id)->update(array('is_default' => 1));
  272. }
  273. }
  274. }
  275. return true;
  276. }
  277. //设为默认地址
  278. public static function setDefault($address_id,$user_id)
  279. {
  280. if ($user_address = UserAddress::where('id', $address_id)->where('user_id', $user_id)->first())
  281. {
  282. $user_address->is_default = 1;
  283. $user_address->save();
  284. UserAddress::where('user_id', $user_id)->where('id', '<>', $address_id)->update(['is_default'=>0]);
  285. if($user = User::where('id', $user_id)->first())
  286. {
  287. $user->address_id = $address_id;
  288. $user->save();
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. // 获取默认地址
  295. public static function userDefaultAddress($user_id)
  296. {
  297. $arr = '';
  298. $arr = self::where(array('user_id'=>$user_id,'is_default'=>self::IS_DEFAULT))->first();
  299. if (!$arr)
  300. {
  301. $arr = self::where(array('user_id'=>$user_id))->first();
  302. }
  303. if($arr)
  304. {
  305. $arr->country_name = Region::getRegionName($arr->country);
  306. $arr->province_name = Region::getRegionName($arr->province);
  307. $arr->city_name = Region::getRegionName($arr->city);
  308. $arr->district_name = Region::getRegionName($arr->district);
  309. }
  310. return $arr;
  311. } */
  312. }