RegionLogic.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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,1)->where('first','<>','')->field('id,name,merge_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,1)->where($map)->field('id,pid,name,merge_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. if(empty($result['result']['address_component']['city'])){
  59. throw new Exception('定位获取shibai');
  60. }
  61. $city_id = PostageRegion::where('name','like',"%".$result['result']['address_component']['city']."%")->value('id');
  62. $result = $result['result']['address_component'];
  63. $result['city_id'] = $city_id;
  64. return $result;
  65. }
  66. } catch (\Exception $e) {
  67. self::$error = $e->getMessage();
  68. return false;
  69. }
  70. }
  71. public static function get_first_char($s)
  72. {
  73. $s0 = mb_substr(self::my_trim($s), 0, 1); //获取名字的姓
  74. try {
  75. $s = iconv('UTF-8', 'GB2312', $s0); //将UTF-8转换成GB2312编码
  76. } catch (\Exception $e) {
  77. return 'Z#';
  78. }
  79. if (ord($s0) > 128) { //汉字开头,汉字没有以U、V开头的
  80. $asc = ord($s[0]) * 256 + ord($s[1]) - 65536;
  81. if ($asc >= -20319 and $asc <= -20284) return "A";
  82. if ($asc >= -20283 and $asc <= -19776) return "B";
  83. if ($asc >= -19775 and $asc <= -19219) return "C";
  84. if ($asc >= -19218 and $asc <= -18711) return "D";
  85. if ($asc >= -18710 and $asc <= -18527) return "E";
  86. if ($asc >= -18526 and $asc <= -18240) return "F";
  87. if ($asc >= -18239 and $asc <= -17760) return "G";
  88. if ($asc >= -17759 and $asc <= -17248) return "H";
  89. if ($asc >= -17247 and $asc <= -17418) return "I";
  90. if ($asc >= -17417 and $asc <= -16475) return "J";
  91. if ($asc >= -16474 and $asc <= -16213) return "K";
  92. if ($asc >= -16212 and $asc <= -15641) return "L";
  93. if ($asc >= -15640 and $asc <= -15166) return "M";
  94. if ($asc >= -15165 and $asc <= -14923) return "N";
  95. if ($asc >= -14922 and $asc <= -14915) return "O";
  96. if ($asc >= -14914 and $asc <= -14631) return "P";
  97. if ($asc >= -14630 and $asc <= -14150) return "Q";
  98. if ($asc >= -14149 and $asc <= -14091) return "R";
  99. if ($asc >= -14090 and $asc <= -13319) return "S";
  100. if ($asc >= -13318 and $asc <= -12839) return "T";
  101. if ($asc >= -12838 and $asc <= -12557) return "W";
  102. if ($asc >= -12556 and $asc <= -11848) return "X";
  103. if ($asc >= -11847 and $asc <= -11056) return "Y";
  104. if ($asc >= -11055 and $asc <= -10247) return "Z";
  105. } else if (ord($s) >= 48 and ord($s) <= 57) { //数字开头
  106. switch (iconv_substr($s, 0, 1, 'utf-8')) {
  107. case 1:
  108. return "Y";
  109. case 2:
  110. return "E";
  111. case 3:
  112. return "S";
  113. case 4:
  114. return "S";
  115. case 5:
  116. return "W";
  117. case 6:
  118. return "L";
  119. case 7:
  120. return "Q";
  121. case 8:
  122. return "B";
  123. case 9:
  124. return "J";
  125. case 0:
  126. return "L";
  127. }
  128. } else if (ord($s) >= 65 and ord($s) <= 90) { //大写英文开头
  129. return substr($s, 0, 1);
  130. } else if (ord($s) >= 97 and ord($s) <= 122) { //小写英文开头
  131. return strtoupper(substr($s, 0, 1));
  132. } else {
  133. return iconv_substr($s0, 0, 1, 'utf-8');//中英混合的词语,不适合上面的各种情况,因此直接提取首个字符即可
  134. }
  135. }
  136. public static function array_group_by($arr, $key)
  137. {
  138. $grouped = array();
  139. foreach ($arr as $value) {
  140. $grouped[$value[$key]][] = $value;
  141. }
  142. if (func_num_args() > 2) {
  143. $args = func_get_args();
  144. foreach ($grouped as $key => $value) {
  145. $parms = array_merge($value, array_slice($args, 2, func_num_args()));
  146. $grouped[$key] = call_user_func_array('array_group_by', $parms);
  147. }
  148. }
  149. return $grouped;
  150. }
  151. public static function my_trim($str)
  152. {
  153. $search = array(" ", " ", "\n", "\r", "\t");
  154. $replace = array("", "", "", "", "");
  155. return str_replace($search, $replace, $str);
  156. }
  157. }