Info.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. namespace Shop\Lib;
  3. use Dever;
  4. class Info
  5. {
  6. # 获取城市
  7. public function getCity()
  8. {
  9. $city = Dever::db('shop/info')->getCity();
  10. $result = array();
  11. if ($city) {
  12. foreach ($city as $k => $v) {
  13. $c = Dever::db('area/city')->find($v['city']);
  14. if ($c) {
  15. $result[] = array
  16. (
  17. 'id' => $v['city'],
  18. 'name' => $c['name'],
  19. );
  20. }
  21. }
  22. }
  23. return $result;
  24. }
  25. # 获取店铺基本信息
  26. public function getOne($shop_id, $lng, $lat)
  27. {
  28. $shop = $this->fetch($shop_id, false, $lng, $lat);
  29. return $shop;
  30. }
  31. # 获取店铺基本信息
  32. public function get($city, $lng, $lat, $name = '', $method = 'fetch', $shop_id = false, $address = false)
  33. {
  34. if (!$city) {
  35. Dever::alert('请传入城市');
  36. }
  37. if ((!$lng || !$lat) && $address) {
  38. list($lng, $lat) = Dever::load('shop/lib/info')->geo($city, $address);
  39. }
  40. if (!$lng || !$lat) {
  41. $city_info = Dever::db('area/city')->find($city);
  42. list($lng, $lat) = Dever::load('shop/lib/info')->geo($city, $city_info['name']);
  43. if (!$lng || !$lat) {
  44. Dever::alert('请传入用户坐标');
  45. }
  46. }
  47. $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
  48. $type = Dever::input('type', 1);
  49. if (!$data) {
  50. Dever::setInput('km', 2);
  51. if ($type == 1) {
  52. # 获取平台店
  53. $data = $this->fetch(false, $city, $lng, $lat, 10, $name, $method);
  54. if ($data && $method == 'fetch') {
  55. # 提示是否进入平台
  56. $data['alert'] = 2;
  57. }
  58. } elseif ($type == 2) {
  59. # 获取城市外的其他店
  60. $data = $this->fetch(false, false, $lng, $lat, 1, $name, $method);
  61. if ($data && $method == 'fetch') {
  62. # 提示是否进入所有最近店铺
  63. $data['alert'] = 4;
  64. }
  65. } elseif ($type == 3) {
  66. # 获取城市内的其他店
  67. $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
  68. if (!$data) {
  69. # 获取平台店
  70. $data = $this->fetch(false, $city, $lng, $lat, 10, $name, $method);
  71. if ($data && $method == 'fetch') {
  72. # 提示是否进入平台
  73. $data['alert'] = 2;
  74. }
  75. } elseif ($method == 'fetch') {
  76. # 提示是否进入城市内最近店铺
  77. $data['alert'] = 3;
  78. }
  79. }
  80. } elseif ($method == 'fetch') {
  81. # 是否提示 1不提示
  82. $data['alert'] = 1;
  83. }
  84. if ($data) {
  85. if ($method == 'fetch') {
  86. $data = $this->getInfo($data);
  87. } else {
  88. foreach ($data as $k => $v) {
  89. $data[$k] = $this->getInfo($data[$k]);
  90. if ($shop_id == $v['id']) {
  91. $data[$k]['cur'] = 1;
  92. } else {
  93. $data[$k]['cur'] = 2;
  94. }
  95. }
  96. }
  97. }
  98. return $data;
  99. }
  100. # 获取店铺的商品列表
  101. public function getGoods($shop, $column = false, $price_type = false, $sell_type = false, $status = false)
  102. {
  103. $table = 'shop/goods';
  104. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  105. if ($column) {
  106. $where['column'] = $column;
  107. $method = 'getData';
  108. $where['status'] = 1;
  109. } else {
  110. $method = 'getDataPage';
  111. }
  112. if ($status) {
  113. $where['status'] = 1;
  114. }
  115. $name = Dever::input('name');
  116. if ($name) {
  117. $where['name'] = $name;
  118. }
  119. if ($price_type) {
  120. $where['price_type'] = $price_type;
  121. }
  122. if ($sell_type) {
  123. $where['sell_type'] = $sell_type;
  124. }
  125. $where['state'] = 1;
  126. $where['state_1'] = 1;
  127. $data = Dever::db($table)->$method($where);
  128. $result_1 = array();
  129. $result_2 = array();
  130. if ($data) {
  131. foreach ($data as $k => $v) {
  132. $d = $this->getGoodsInfo($where['shop_id'], $v);
  133. if ($d) {
  134. if ($d['total'] <= 0) {
  135. $result_2[] = $d;
  136. } else {
  137. $result_1[] = $d;
  138. }
  139. }
  140. }
  141. $data = array_merge($result_1, $result_2);
  142. }
  143. return $data;
  144. }
  145. # 获取店铺的商品SKU列表
  146. public function getGoodsSku($shop)
  147. {
  148. $table = 'shop/goods_sku';
  149. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  150. $method = 'getDataPage';
  151. $name = Dever::input('name');
  152. if ($name) {
  153. $where['name'] = $name;
  154. }
  155. $total = Dever::input('total');
  156. if ($total) {
  157. $where['total'] = $total;
  158. }
  159. //$where['status'] = 1;
  160. $where['state'] = 1;
  161. $where['state_1'] = 1;
  162. $data = Dever::db($table)->$method($where);
  163. $result = array();
  164. if ($data) {
  165. foreach ($data as $k => $v) {
  166. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  167. if (!isset($result[$v['id']])) {
  168. $result[$v['id']] = $v;
  169. }
  170. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  171. $result[$v['id']]['price_array'][] = array
  172. (
  173. 'name' => $data[$k]['sku_name'],
  174. 'total' => $data[$k]['total'] <= 0 ? 0 : $data[$k]['total'],
  175. 'price' => $data[$k]['price'],
  176. 's_price' => $data[$k]['s_price'],
  177. );
  178. }
  179. }
  180. }
  181. return $result;
  182. }
  183. # 获取详细信息
  184. private function getInfo($data)
  185. {
  186. if ($data) {
  187. if ($data['worktime']) {
  188. $time = date('Hi');
  189. $worktime = str_replace(':', '', $data['worktime']);
  190. $temp = explode('~', $worktime);
  191. if (isset($temp[0]) && isset($temp[1]) && ($time < $temp[0] || $time > $temp[1])) {
  192. $data['open'] = 2;
  193. }
  194. }
  195. $data['gotime'] = $data['worktime'];
  196. }
  197. return $data;
  198. }
  199. # 获取距离
  200. public function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  201. {
  202. $page = array();
  203. if ($method == 'fetchAll') {
  204. $page['template'] = 'list';
  205. $page['num'] = 10;
  206. } else {
  207. $page = false;
  208. }
  209. $where = 'status = 1 and state = 1';
  210. if ($type == 1) {
  211. $where .= ' and type in (1,2)';
  212. } elseif ($type) {
  213. $where .= ' and type = ' . $type;
  214. }
  215. if ($city) {
  216. $county = Dever::db('area/county')->find($city);
  217. if ($county) {
  218. $city = $county['city_id'];
  219. }
  220. if ($type < 10 && $city) {
  221. $where .= ' and city = ' . $city;
  222. }
  223. }
  224. if ($name) {
  225. $where .= ' and name like("%'.$name.'%")';
  226. }
  227. if ($id) {
  228. $where .= ' and id = ' . $id;
  229. }
  230. if ($type < 10) {
  231. $shop_method = Dever::input('method');
  232. if ($shop_method == 1) {
  233. $where .= ' and method in(1,3)';
  234. } elseif ($shop_method == 2) {
  235. $where .= ' and method in(2,3)';
  236. }
  237. }
  238. if ($lng && $lat) {
  239. $col = Dever::db('shop/info')->config['config_col'];
  240. $km = Dever::input('km');
  241. $distance = 'round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2)';
  242. if ($km && $km == 1 && $type < 10) {
  243. # 验证公里数
  244. $config = Dever::db('main/manage_config')->find();
  245. if ($config && $config['km'] > 0) {
  246. $where .= ' and '.$distance.' <= ' . $config['km'];
  247. }
  248. }
  249. $sql = 'select '.$col.','.$distance.' as distance from {table} where '.$where.' order by distance asc';
  250. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  251. } else {
  252. $data = Dever::db('shop/info')->getOne($id);
  253. }
  254. if ($data && isset($data['city']) && $data['city']) {
  255. $city = Dever::db('area/city')->one($data['city']);
  256. $data['city_name'] = $city['name'];
  257. }
  258. return $data;
  259. }
  260. # 获取库存
  261. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true, $check = true)
  262. {
  263. $where['shop_id'] = $shop_id;
  264. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  265. if ($sku_id) {
  266. $where['sku_id'] = $sku_id;
  267. }
  268. $total = 1;
  269. $other = Dever::db('shop/goods_sku')->getData($where);
  270. if (!$other && $check) {
  271. if ($check == -1) {
  272. $total = 2;
  273. } else {
  274. return false;
  275. }
  276. }
  277. $other_array = array('total', 'min');
  278. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  279. $other_array = array('total');
  280. }
  281. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, $other_array));
  282. if($data) {
  283. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  284. } else {
  285. $goods_sku = Dever::db('shop/goods_sku')->find($where);
  286. if ($goods_sku && isset($goods_sku['min'])) {
  287. $data['min'] = $goods_sku['min'];
  288. }
  289. }
  290. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  291. $data['total'] = 0;
  292. if ($data['price_type'] == 4) {
  293. if (isset($data['goods']) && is_array($data['goods'])) {
  294. foreach ($data['goods'] as $k => $v) {
  295. $where = array();
  296. $where['shop_id'] = $shop_id;
  297. $where['goods_id'] = $v['id'];
  298. $gother = Dever::db('shop/goods_sku')->getData($where);
  299. $data['goods'][$k]['total'] = $this->getTotal($gother, -1);
  300. if ($data['total'] == 0) {
  301. $data['total'] = $data['goods'][$k]['total'];
  302. }
  303. if ($data['total'] > $data['goods'][$k]['total']) {
  304. $data['total'] = $data['goods'][$k]['total'];
  305. }
  306. }
  307. }
  308. } else {
  309. $data['total'] = $this->getTotal($other, $sku_id);
  310. }
  311. if ($total == 2) {
  312. $data['total'] = 0;
  313. }
  314. }
  315. return $data;
  316. }
  317. # 验证库存
  318. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  319. {
  320. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  321. if (!$info) {
  322. if ($state == 2) {
  323. Dever::alert('商品不存在');
  324. } else {
  325. return false;
  326. }
  327. }
  328. $total = $info['total'];
  329. $min = $info['min'];
  330. # 增加最小起购量
  331. if ($state < 3 && $min > 0 && $num < $min) {
  332. $num = $min;
  333. }
  334. if ($num > $total) {
  335. if ($state == 2) {
  336. Dever::alert('库存不足');
  337. }
  338. $num = $total;
  339. }
  340. return $total;
  341. }
  342. # 获取库存
  343. public function getTotal($other, $sku_id)
  344. {
  345. if (isset($other[$sku_id])) {
  346. return $other[$sku_id]['total'];
  347. } else {
  348. return 0;
  349. }
  350. }
  351. # 获取经纬度
  352. public function geo($code, $name)
  353. {
  354. # 获取经纬度
  355. $url = 'https://restapi.amap.com/v3/geocode/geo';
  356. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  357. //$param['city'] = $code;
  358. $param['address'] = $name;
  359. $result = json_decode(Dever::curl($url, $param), true);
  360. $lng = 0;
  361. $lat = 0;
  362. $map = '';
  363. if (isset($result['geocodes'][0]['location']) && $result['geocodes'][0]['location']) {
  364. $map = $code . ',' . $result['geocodes'][0]['location'] . ',11';
  365. $temp = explode(',', $result['geocodes'][0]['location']);
  366. $lng = $temp[0];
  367. $lat = $temp[1];
  368. }
  369. return array($lng, $lat, $map);
  370. }
  371. # 根据经纬度获取地址
  372. public function address($lng, $lat)
  373. {
  374. $url = 'http://restapi.amap.com/v3/geocode/regeo';
  375. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  376. $param['location'] = $lng . ',' . $lat;
  377. $param['radius'] = 2800;
  378. $result = json_decode(Dever::curl($url, $param), true);
  379. $address = '';
  380. if (isset($result['regeocode']['formatted_address'])) {
  381. $address = $result['regeocode']['formatted_address'];
  382. }
  383. return $address;
  384. }
  385. }