Info.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. $pingtai = 2;
  35. $data = array();
  36. if (!$city && !$lng && !$lat && !$address) {
  37. //Dever::alert('请传入城市');
  38. $pingtai = 1;
  39. $data = $this->fetch(3, $city, $lng, $lat, 10, $name, $method);
  40. }
  41. if ($pingtai == 2) {
  42. if ((!$lng || !$lat) && $address) {
  43. list($lng, $lat) = Dever::load('shop/lib/info')->geo($city, $address);
  44. }
  45. if (!$lng || !$lat) {
  46. $city_info = Dever::db('area/city')->find($city);
  47. list($lng, $lat) = Dever::load('shop/lib/info')->geo($city, $city_info['name']);
  48. if (!$lng || !$lat) {
  49. Dever::alert('请传入用户坐标');
  50. }
  51. }
  52. if ($city) {
  53. $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
  54. }
  55. }
  56. $type = Dever::input('type', 1);
  57. if (!$data) {
  58. Dever::setInput('km', 2);
  59. if ($type == 1) {
  60. # 获取平台店
  61. $data = $this->fetch(false, $city, $lng, $lat, 10, $name, $method);
  62. if ($data && $method == 'fetch') {
  63. # 提示是否进入平台
  64. $data['alert'] = 2;
  65. }
  66. } elseif ($type == 2) {
  67. # 获取城市外的其他店
  68. $data = $this->fetch(false, false, $lng, $lat, 1, $name, $method);
  69. if ($data && $method == 'fetch') {
  70. # 提示是否进入所有最近店铺
  71. $data['alert'] = 4;
  72. }
  73. } elseif ($type == 3) {
  74. # 获取城市内的其他店
  75. $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
  76. if (!$data) {
  77. # 获取平台店
  78. $data = $this->fetch(false, $city, $lng, $lat, 10, $name, $method);
  79. if ($data && $method == 'fetch') {
  80. # 提示是否进入平台
  81. $data['alert'] = 2;
  82. }
  83. } elseif ($method == 'fetch') {
  84. # 提示是否进入城市内最近店铺
  85. $data['alert'] = 3;
  86. }
  87. }
  88. } elseif ($method == 'fetch') {
  89. # 是否提示 1不提示
  90. $data['alert'] = 1;
  91. }
  92. if ($data) {
  93. if ($method == 'fetch') {
  94. $data = $this->getInfo($data);
  95. } else {
  96. foreach ($data as $k => $v) {
  97. $data[$k] = $this->getInfo($data[$k]);
  98. if ($shop_id == $v['id']) {
  99. $data[$k]['cur'] = 1;
  100. } else {
  101. $data[$k]['cur'] = 2;
  102. }
  103. }
  104. }
  105. }
  106. return $data;
  107. }
  108. # 获取店铺的商品列表
  109. public function getGoods($shop, $column = false, $price_type = false, $sell_type = false, $status = false, $uid = -1, $buy_type = 1)
  110. {
  111. $table = 'shop/goods';
  112. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  113. if ($column) {
  114. $where['column'] = $column;
  115. $method = 'getData';
  116. $where['status'] = 1;
  117. } else {
  118. $method = 'getDataPage';
  119. $method = 'getData';
  120. }
  121. if ($status) {
  122. $where['status'] = 1;
  123. }
  124. $name = Dever::input('name');
  125. if ($name) {
  126. $where['name'] = $name;
  127. }
  128. if ($price_type) {
  129. $where['price_type'] = $price_type;
  130. }
  131. if ($sell_type) {
  132. $where['sell_type'] = $sell_type;
  133. }
  134. $where['state'] = 1;
  135. $where['state_1'] = 1;
  136. $data = Dever::db($table)->$method($where);
  137. $result_1 = array();
  138. $result_2 = array();
  139. if ($data) {
  140. foreach ($data as $k => $v) {
  141. $d = $this->getGoodsInfo($where['shop_id'], $v, false, true, true, $uid);
  142. if ($d) {
  143. if ($buy_type == 1) {
  144. if ($d['total'] <= 0) {
  145. if ($d['sku_type'] == 2) {
  146. $result_2[] = $d;
  147. }
  148. // $result_2[] = $d;
  149. } else {
  150. $result_1[] = $d;
  151. }
  152. } else {
  153. if (!isset($d['buy_type'])) {
  154. $d['buy_type'] = 1;
  155. }
  156. if ($buy_type == 3) {
  157. $d['buy_type'] = 1;
  158. }
  159. if ($d['buy_type'] == 1) {
  160. if ($d['total'] <= 0) {
  161. $result_2[] = $d;
  162. } else {
  163. $result_1[] = $d;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. $data = array_merge($result_1, $result_2);
  170. }
  171. return $data;
  172. }
  173. # 获取店铺的商品SKU列表
  174. public function getGoodsSku($shop, $page = true)
  175. {
  176. $table = 'shop/goods_sku';
  177. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  178. if ($page) {
  179. $method = 'getDataPage';
  180. } else {
  181. $method = 'getAllData';
  182. }
  183. $method = 'getAllData';
  184. $name = Dever::input('name');
  185. if ($name) {
  186. $where['name'] = $name;
  187. }
  188. $total = Dever::input('total');
  189. if ($total) {
  190. $where['total'] = $total;
  191. }
  192. //$where['status'] = 1;
  193. $where['state'] = 1;
  194. $where['state_1'] = 1;
  195. $data = Dever::db($table)->$method($where);
  196. $result = array();
  197. $zero = array();
  198. if ($data) {
  199. foreach ($data as $k => $v) {
  200. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  201. if ($data[$k]['total'] <= 0) {
  202. if (!isset($zero[$v['id']])) {
  203. $zero[$v['id']] = $v;
  204. }
  205. } else {
  206. if (!isset($result[$v['id']])) {
  207. $result[$v['id']] = $v;
  208. }
  209. }
  210. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  211. if ($data[$k]['total'] <= 0) {
  212. $zero[$v['id']]['price_array'][] = array
  213. (
  214. 'name' => $data[$k]['sku_name'],
  215. 'total' => '0.00',
  216. 'price' => $data[$k]['price'],
  217. 's_price' => $data[$k]['s_price'],
  218. );
  219. } else {
  220. $result[$v['id']]['price_array'][] = array
  221. (
  222. 'name' => $data[$k]['sku_name'],
  223. 'total' => $data[$k]['total'],
  224. 'price' => $data[$k]['price'],
  225. 's_price' => $data[$k]['s_price'],
  226. );
  227. }
  228. }
  229. }
  230. }
  231. $result = array_merge($result, $zero);
  232. return $result;
  233. }
  234. # 获取详细信息
  235. private function getInfo($data)
  236. {
  237. if ($data) {
  238. if ($data['worktime']) {
  239. $time = date('Hi');
  240. $worktime = str_replace(':', '', $data['worktime']);
  241. $temp = explode('~', $worktime);
  242. if (isset($temp[0]) && isset($temp[1]) && ($time < $temp[0] || $time > $temp[1])) {
  243. $data['open'] = 2;
  244. }
  245. }
  246. $data['gotime'] = $data['worktime'];
  247. }
  248. return $data;
  249. }
  250. # 获取距离
  251. public function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  252. {
  253. $page = array();
  254. if ($method == 'fetchAll') {
  255. $page['template'] = 'list';
  256. $page['num'] = 10;
  257. } else {
  258. $page = false;
  259. }
  260. $where = 'status = 1 and state = 1 and type in(1,10) ';
  261. if ($type == 1) {
  262. $where .= ' and type in (1,2)';
  263. } elseif ($type) {
  264. $where .= ' and type = ' . $type;
  265. }
  266. if ($city) {
  267. $county = Dever::db('area/county')->find($city);
  268. if ($county) {
  269. $city = $county['city_id'];
  270. }
  271. if ($type < 10 && $city) {
  272. $where .= ' and city = ' . $city;
  273. }
  274. }
  275. if ($name) {
  276. $where .= ' and name like("%'.$name.'%")';
  277. }
  278. if ($id) {
  279. $where .= ' and id = ' . $id;
  280. }
  281. if ($type < 10) {
  282. $shop_method = Dever::input('method');
  283. if ($shop_method == 1) {
  284. $where .= ' and method in(1,3)';
  285. } elseif ($shop_method == 2) {
  286. $where .= ' and method in(2,3)';
  287. }
  288. }
  289. if ($lng && $lat) {
  290. $col = Dever::db('shop/info')->config['config_col'];
  291. $km = Dever::input('km');
  292. $distance = 'round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2)';
  293. if ($km && $km == 1 && $type < 10) {
  294. # 验证公里数
  295. $config = Dever::db('main/manage_config')->find();
  296. if ($config && $config['km'] > 0) {
  297. $where .= ' and '.$distance.' <= ' . $config['km'];
  298. }
  299. }
  300. $sql = 'select '.$col.','.$distance.' as distance from {table} where '.$where.' order by distance asc';
  301. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  302. } else {
  303. $data = Dever::db('shop/info')->getOne($id);
  304. }
  305. if ($data && isset($data['city']) && $data['city']) {
  306. $city = Dever::db('area/city')->one($data['city']);
  307. $data['city_name'] = $city['name'];
  308. }
  309. return $data;
  310. }
  311. # 获取库存
  312. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true, $check = true, $uid = -1)
  313. {
  314. $where['shop_id'] = $shop_id;
  315. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  316. /*
  317. if($where['goods_id'] && $where['goods_id'] == 10000264){
  318. $where['shop_id'] = 3;
  319. }
  320. */
  321. if ($sku_id) {
  322. $where['sku_id'] = $sku_id;
  323. }
  324. $total = 1;
  325. $other = Dever::db('shop/goods_sku')->getData($where);
  326. if (!$other && $check) {
  327. if ($check == -1) {
  328. $total = 2;
  329. } else {
  330. return false;
  331. }
  332. }
  333. $other_array = array('total', 'min', 'price_id');
  334. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  335. # 购买不需要读取goods_sku的min
  336. $other_array = array('total', 'price_id');
  337. }
  338. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, $other_array));
  339. if($data) {
  340. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  341. } else {
  342. $goods_sku = Dever::db('shop/goods_sku')->find($where);
  343. if ($goods_sku && isset($goods_sku['min'])) {
  344. $data['min'] = $goods_sku['min'];
  345. }
  346. }
  347. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  348. $data['total'] = 0;
  349. if ($data['price_type'] == 4) {
  350. if (isset($data['goods']) && is_array($data['goods'])) {
  351. foreach ($data['goods'] as $k => $v) {
  352. $where = array();
  353. $where['shop_id'] = $shop_id;
  354. $where['goods_id'] = $v['id'];
  355. $gother = Dever::db('shop/goods_sku')->getData($where);
  356. $data['goods'][$k]['total'] = $this->getTotal($gother, -1);
  357. if ($data['total'] == 0) {
  358. $data['total'] = $data['goods'][$k]['total'];
  359. }
  360. if ($data['total'] > $data['goods'][$k]['total']) {
  361. $data['total'] = $data['goods'][$k]['total'];
  362. }
  363. }
  364. }
  365. } else {
  366. $data['total'] = $this->getTotal($other, $sku_id);
  367. }
  368. if ($total == 2) {
  369. $data['total'] = 0;
  370. }
  371. if ($data['min'] && floor($data['min']) == $data['min']) {
  372. $data['min'] = intval($data['min']);
  373. }
  374. # 根据角色计算折扣
  375. $data['discount'] = (object) array();
  376. if ($uid && $uid > 0) {
  377. $role = $this->getRole($uid);
  378. if ($role) {
  379. $role_set = false;
  380. foreach ($role as $rk => $rv) {
  381. $discount_goods = Dever::db('act/discount_goods')->getData(array('discount_id' => $rv['id']));
  382. if ($discount_goods) {
  383. $discount_goods = array_keys($discount_goods);
  384. if (in_array($data['id'], $discount_goods)) {
  385. $role_set = $rv;
  386. break;
  387. }
  388. } elseif ($rv['category'] && in_array($data['top_category_id'], $rv['category'])) {
  389. $role_set = $rv;
  390. break;
  391. }
  392. }
  393. if ($role_set) {
  394. $data['discount'] = $role_set;
  395. }
  396. }
  397. }
  398. }
  399. return $data;
  400. }
  401. # 获取角色
  402. public function getRole($uid)
  403. {
  404. $bind = Dever::db('agent/user_bind')->find(array('uid' => $uid, 'status' => 2));
  405. if ($bind) {
  406. $discount = Dever::db('act/discount')->getAll(array('role' => 1));
  407. return $discount;
  408. }
  409. return false;
  410. }
  411. # 验证库存
  412. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  413. {
  414. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  415. if (!$info) {
  416. if ($state == 2) {
  417. Dever::alert('商品不存在');
  418. } else {
  419. return false;
  420. }
  421. }
  422. $total = $info['total'];
  423. $min = $info['min'];
  424. # 增加最小起购量
  425. if ($state < 3 && $min > 0 && $num < $min) {
  426. $num = $min;
  427. }
  428. if ($num > $total) {
  429. if ($state == 2) {
  430. Dever::alert('库存不足');
  431. }
  432. $num = $total;
  433. }
  434. return $total;
  435. }
  436. # 获取库存
  437. public function getTotal($other, $sku_id)
  438. {
  439. if (isset($other[$sku_id])) {
  440. return $other[$sku_id]['total'];
  441. } else {
  442. return 0;
  443. }
  444. }
  445. # 获取经纬度
  446. public function geo($code, $name)
  447. {
  448. # 获取经纬度
  449. $url = 'https://restapi.amap.com/v3/geocode/geo';
  450. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  451. //$param['city'] = $code;
  452. $param['address'] = $name;
  453. $result = json_decode(Dever::curl($url, $param), true);
  454. $lng = 0;
  455. $lat = 0;
  456. $map = '';
  457. if (isset($result['geocodes'][0]['location']) && $result['geocodes'][0]['location']) {
  458. $map = $code . ',' . $result['geocodes'][0]['location'] . ',11';
  459. $temp = explode(',', $result['geocodes'][0]['location']);
  460. $lng = $temp[0];
  461. $lat = $temp[1];
  462. }
  463. return array($lng, $lat, $map);
  464. }
  465. # 根据经纬度获取地址
  466. public function address($lng, $lat)
  467. {
  468. $url = 'http://restapi.amap.com/v3/geocode/regeo';
  469. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  470. $param['location'] = $lng . ',' . $lat;
  471. $param['radius'] = 2800;
  472. $result = json_decode(Dever::curl($url, $param), true);
  473. $address = '';
  474. if (isset($result['regeocode']['formatted_address'])) {
  475. $address = $result['regeocode']['formatted_address'];
  476. }
  477. return $address;
  478. }
  479. }