RegionLogic.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\logic\BaseLogic;
  4. use app\common\model\article\Article;
  5. use app\common\model\decorate\DecoratePage;
  6. use app\common\model\decorate\DecorateTabbar;
  7. use app\common\model\setting\PostageRegion;
  8. use app\common\service\ConfigService;
  9. use app\common\service\FileService;
  10. use think\Exception;
  11. /**
  12. * index
  13. * Class RegionLogic
  14. * @package app\api\logic
  15. */
  16. class RegionLogic extends BaseLogic
  17. {
  18. /**
  19. * 获取城市列表
  20. * @return array
  21. */
  22. public static function getRegionList()
  23. {
  24. $area_list = PostageRegion::cache(true,86400)->where('first','<>','')->field('id,name,pid')->order('sorts asc,id asc')->select()->toArray();
  25. foreach ($area_list as &$vo) {
  26. $vo['field'] = self::get_first_char($vo['name']);
  27. }
  28. $arr = self::array_group_by($area_list, 'field');
  29. ksort($arr);
  30. $citys = array();
  31. foreach ($arr as $keys=>$vo){
  32. $citys[] = ['key'=>$keys,'list'=>$vo];
  33. }
  34. ksort($citys);
  35. //获取热门城市
  36. $map['id'] = ['in','1710'];
  37. $hot_list = PostageRegion::cache(true,86400)->where($map)->field('id,pid,name')->order('first asc')->select()->toArray();
  38. $list = ['hot_list'=>$hot_list,'city_list'=> $citys];
  39. return $list;
  40. }
  41. /**
  42. * 获取定位信息
  43. * @param $params
  44. * @return false|mixed|void
  45. */
  46. public static function getLocation($params)
  47. {
  48. try {
  49. $location = $params['latitude'].','.$params['longitude'];
  50. $key = 'SEGBZ-P3PK6-CU5SB-MQLQP-ZDPJS-PHFR6';
  51. $url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' . $location . '&key='.$key.'&get_poi=1';
  52. $result = http_request($url);
  53. if(empty($result)){
  54. throw new Exception('定位请求失败');
  55. }
  56. if ($result['status'] === 0) {
  57. //获取城市id
  58. $city_id = Area::where(['name'=>['like',"%".$result['result']['address_component']['city']."%"]])->value('id');
  59. $result['result']['address_component']['city_id'] = $city_id;
  60. return $result['result']['address_component'];
  61. }
  62. } catch (\Exception $e) {
  63. self::$error = $e->getMessage();
  64. return false;
  65. }
  66. }
  67. public static function get_first_char($s)
  68. {
  69. $s0 = mb_substr(self::my_trim($s), 0, 1); //获取名字的姓
  70. try {
  71. $s = iconv('UTF-8', 'GB2312', $s0); //将UTF-8转换成GB2312编码
  72. } catch (\Exception $e) {
  73. return 'Z#';
  74. }
  75. if (ord($s0) > 128) { //汉字开头,汉字没有以U、V开头的
  76. $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
  77. if ($asc >= -20319 and $asc <= -20284) return "A";
  78. if ($asc >= -20283 and $asc <= -19776) return "B";
  79. if ($asc >= -19775 and $asc <= -19219) return "C";
  80. if ($asc >= -19218 and $asc <= -18711) return "D";
  81. if ($asc >= -18710 and $asc <= -18527) return "E";
  82. if ($asc >= -18526 and $asc <= -18240) return "F";
  83. if ($asc >= -18239 and $asc <= -17760) return "G";
  84. if ($asc >= -17759 and $asc <= -17248) return "H";
  85. if ($asc >= -17247 and $asc <= -17418) return "I";
  86. if ($asc >= -17417 and $asc <= -16475) return "J";
  87. if ($asc >= -16474 and $asc <= -16213) return "K";
  88. if ($asc >= -16212 and $asc <= -15641) return "L";
  89. if ($asc >= -15640 and $asc <= -15166) return "M";
  90. if ($asc >= -15165 and $asc <= -14923) return "N";
  91. if ($asc >= -14922 and $asc <= -14915) return "O";
  92. if ($asc >= -14914 and $asc <= -14631) return "P";
  93. if ($asc >= -14630 and $asc <= -14150) return "Q";
  94. if ($asc >= -14149 and $asc <= -14091) return "R";
  95. if ($asc >= -14090 and $asc <= -13319) return "S";
  96. if ($asc >= -13318 and $asc <= -12839) return "T";
  97. if ($asc >= -12838 and $asc <= -12557) return "W";
  98. if ($asc >= -12556 and $asc <= -11848) return "X";
  99. if ($asc >= -11847 and $asc <= -11056) return "Y";
  100. if ($asc >= -11055 and $asc <= -10247) return "Z";
  101. } else if (ord($s) >= 48 and ord($s) <= 57) { //数字开头
  102. switch (iconv_substr($s, 0, 1, 'utf-8')) {
  103. case 1:
  104. return "Y";
  105. case 2:
  106. return "E";
  107. case 3:
  108. return "S";
  109. case 4:
  110. return "S";
  111. case 5:
  112. return "W";
  113. case 6:
  114. return "L";
  115. case 7:
  116. return "Q";
  117. case 8:
  118. return "B";
  119. case 9:
  120. return "J";
  121. case 0:
  122. return "L";
  123. }
  124. } else if (ord($s) >= 65 and ord($s) <= 90) { //大写英文开头
  125. return substr($s, 0, 1);
  126. } else if (ord($s) >= 97 and ord($s) <= 122) { //小写英文开头
  127. return strtoupper(substr($s, 0, 1));
  128. } else {
  129. return iconv_substr($s0, 0, 1, 'utf-8');//中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可
  130. }
  131. }
  132. public static function array_group_by($arr, $key)
  133. {
  134. $grouped = array();
  135. foreach ($arr as $value) {
  136. $grouped[$value[$key]][] = $value;
  137. }
  138. if (func_num_args() > 2) {
  139. $args = func_get_args();
  140. foreach ($grouped as $key => $value) {
  141. $parms = array_merge($value, array_slice($args, 2, func_num_args()));
  142. $grouped[$key] = call_user_func_array('array_group_by', $parms);
  143. }
  144. }
  145. return $grouped;
  146. }
  147. public static function my_trim($str)
  148. {
  149. $search = array(" ", " ", "\n", "\r", "\t");
  150. $replace = array("", "", "", "", "");
  151. return str_replace($search, $replace, $str);
  152. }
  153. }