Address.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\system;
  13. use think\facade\Cache;
  14. use app\model\BaseModel;
  15. /**
  16. * 地区表
  17. */
  18. class Address extends BaseModel
  19. {
  20. /**
  21. * 获取地区列表
  22. * @param unknown $condition
  23. * @param string $field
  24. * @param string $order
  25. * @param string $limit
  26. * @return multitype:string mixed
  27. */
  28. public function getAreaList($condition = [], $field = '*', $order = '', $limit = null){
  29. $data = json_encode([$condition, $field, $order, $limit]);
  30. $cache = Cache::get("area_getAreaList_".$data);
  31. if(!empty($cache))
  32. {
  33. return $this->success($cache);
  34. }
  35. $area_list = model("area")->getList($condition, $field, $order, $limit);
  36. Cache::tag("area")->set("area_getAreaList_".$data, $area_list);
  37. return $this->success($area_list);
  38. }
  39. /**
  40. * 获取地区详情
  41. */
  42. public function getAreaInfo($circle){
  43. $cache = Cache::get("area_getAreaInfo_".$circle);
  44. if(!empty($cache))
  45. {
  46. return $this->success($cache);
  47. }
  48. $info = model("area")->getInfo([['id', '=', $circle]]);
  49. Cache::tag("area")->set("area_getAreaInfo_".$circle, $info);
  50. return $this->success($info);
  51. }
  52. /**
  53. * 获取省市子项
  54. */
  55. public function getAreas($circle = 0){
  56. $cache = Cache::get("area_getAreas_".$circle);
  57. if(!empty($cache))
  58. {
  59. return $this->success($cache);
  60. }
  61. $list = model("area")->getList([['pid', '=', $circle]]);
  62. Cache::tag("area")->set("area_getAreas_".$circle, $list);
  63. return $this->success($list);
  64. }
  65. /**
  66. * 获取整理后的地址
  67. */
  68. public function getAddressTree($level = 4) {
  69. $condition = [['level', '<=', $level]];
  70. $json_condition = json_encode($condition);
  71. $cache = Cache::get("area_getAddressTree".$json_condition);
  72. if(!empty($cache))
  73. {
  74. return $this->success($cache);
  75. }
  76. $area_list = $this->getAreaList($condition, "id, pid, name, level", "id asc")['data'];
  77. //组装数据
  78. $refer_list = [];
  79. foreach($area_list as $key=>$val){
  80. $refer_list[$val['level']][$val['pid']]['child_list'][$val['id']] = $area_list[$key];
  81. if(isset($refer_list[$val['level']][$val['pid']]['child_num'])) {
  82. $refer_list[$val['level']][$val['pid']]['child_num'] += 1;
  83. }else {
  84. $refer_list[$val['level']][$val['pid']]['child_num'] = 1;
  85. }
  86. }
  87. Cache::tag("area")->set("area_getAddressTree".$json_condition, $refer_list);
  88. return $this->success($refer_list);
  89. }
  90. /**
  91. * 获取地址
  92. * @param array $condition
  93. * @param string $field
  94. * @return multitype:number unknown
  95. */
  96. public function getAreasInfo(array $condition, string $field = '*'){
  97. $info = model("area")->getInfo($condition, $field);
  98. if ($info) return $this->success($info);
  99. return $this->error();
  100. }
  101. }