Info.php 14 KB

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