SysAreaChina.Class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * 地区数据(中国)
  4. * Created by PhpStorm.
  5. * User: QianNiao-C
  6. * Date: 2019/10/26
  7. * Time: 9:57
  8. */
  9. namespace Jindouyun\Controller\Common;
  10. use Mall\Framework\Core\ErrorCode;
  11. use JinDouYun\Cache\SysAreaChinaCache;
  12. use Jindouyun\Controller\BaseController;
  13. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  14. class SysAreaChina extends BaseController
  15. {
  16. private $objMSysAreaChina;
  17. private $objSysAreaChinaCache;
  18. public function __construct($isCheckAcl = false, $isMustLogin = false)
  19. {
  20. parent::__construct($isCheckAcl, $isMustLogin);
  21. $this->objMSysAreaChina = new MSysAreaChina();
  22. $this->objSysAreaChinaCache = new SysAreaChinaCache('mapping');
  23. }
  24. /**
  25. * 获取所有省份
  26. */
  27. public function getAllProvince()
  28. {
  29. if($this->objSysAreaChinaCache->isHaveProvinceKey()) {
  30. $returnData = $this->objSysAreaChinaCache->getAllProvince();//获取缓存
  31. }else{
  32. $result = $this->objMSysAreaChina->getAllProvince();
  33. if (!$result->isSuccess()) {
  34. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  35. }
  36. $returnData = $result->getData();
  37. if(!empty($returnData)){
  38. $this->objSysAreaChinaCache->cacheAllProvince($returnData);
  39. }
  40. }
  41. parent::sendOutput($returnData, 0, ['pageTotal'=>count($returnData)]);
  42. }
  43. /**
  44. * 根据省份id获取所有城市
  45. */
  46. public function getAllCityByProvinceCode()
  47. {
  48. $code = $this->request->param('request_id');
  49. if (empty($code)) {
  50. $this->sendOutput('请指定省份id', ErrorCode::$paramError);
  51. }
  52. if($this->objSysAreaChinaCache->isHaveCityKeyByProvinceCode($code)){
  53. $returnData = $this->objSysAreaChinaCache->getCityByProvinceCode($code);
  54. }else{
  55. $result = $this->objMSysAreaChina->getAllCityByProvinceCode(['pcode' => $code]);
  56. if (!$result->isSuccess()) {
  57. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  58. }
  59. $returnData = $result->getData();
  60. if(!empty($returnData)){
  61. $this->objSysAreaChinaCache->cacheCityByCode($returnData, $code);
  62. }
  63. }
  64. parent::sendOutput($returnData, 0, ['pageTotal'=>count($returnData)]);
  65. }
  66. /**
  67. * 获取指定城市下的所有区
  68. */
  69. public function getAllAreaByCityCode()
  70. {
  71. $code = $this->request->param('request_id');
  72. if (empty($code)) {
  73. $this->sendOutput('请指定城市id', ErrorCode::$paramError);
  74. }
  75. if($this->objSysAreaChinaCache->isHaveAreaKeyByCityCode($code)){
  76. $returnData = $this->objSysAreaChinaCache->getAreaByCityCode($code);
  77. }else{
  78. $result = $this->objMSysAreaChina->getAllAreaByCityCode(['pcode' => $code]);
  79. if (!$result->isSuccess()) {
  80. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  81. }
  82. $returnData = $result->getData();
  83. if(!empty($returnData)){
  84. $this->objSysAreaChinaCache->cacheAreaByCode($returnData, $code);
  85. }
  86. }
  87. parent::sendOutput($returnData, 0, ['pageTotal'=>count($returnData)]);
  88. }
  89. }