Info.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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, $uid = -1)
  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, false, true, true, $uid);
  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, $page = true)
  150. {
  151. $table = 'shop/goods_sku';
  152. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  153. if ($page) {
  154. $method = 'getDataPage';
  155. } else {
  156. $method = 'getAllData';
  157. }
  158. $name = Dever::input('name');
  159. if ($name) {
  160. $where['name'] = $name;
  161. }
  162. $total = Dever::input('total');
  163. if ($total) {
  164. $where['total'] = $total;
  165. }
  166. //$where['status'] = 1;
  167. $where['state'] = 1;
  168. $where['state_1'] = 1;
  169. $data = Dever::db($table)->$method($where);
  170. $result = array();
  171. if ($data) {
  172. foreach ($data as $k => $v) {
  173. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  174. if (!isset($result[$v['id']])) {
  175. $result[$v['id']] = $v;
  176. }
  177. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  178. $result[$v['id']]['price_array'][] = array
  179. (
  180. 'name' => $data[$k]['sku_name'],
  181. 'total' => $data[$k]['total'] <= 0 ? 0 : $data[$k]['total'],
  182. 'price' => $data[$k]['price'],
  183. 's_price' => $data[$k]['s_price'],
  184. );
  185. }
  186. }
  187. }
  188. return $result;
  189. }
  190. # 获取详细信息
  191. private function getInfo($data)
  192. {
  193. if ($data) {
  194. if ($data['worktime']) {
  195. $time = date('Hi');
  196. $worktime = str_replace(':', '', $data['worktime']);
  197. $temp = explode('~', $worktime);
  198. if (isset($temp[0]) && isset($temp[1]) && ($time < $temp[0] || $time > $temp[1])) {
  199. $data['open'] = 2;
  200. }
  201. }
  202. $data['gotime'] = $data['worktime'];
  203. }
  204. return $data;
  205. }
  206. # 获取距离
  207. public function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  208. {
  209. $page = array();
  210. if ($method == 'fetchAll') {
  211. $page['template'] = 'list';
  212. $page['num'] = 10;
  213. } else {
  214. $page = false;
  215. }
  216. $where = 'status = 1 and state = 1 and type in(1,10) ';
  217. if ($type == 1) {
  218. $where .= ' and type in (1,2)';
  219. } elseif ($type) {
  220. $where .= ' and type = ' . $type;
  221. }
  222. if ($city) {
  223. $county = Dever::db('area/county')->find($city);
  224. if ($county) {
  225. $city = $county['city_id'];
  226. }
  227. if ($type < 10 && $city) {
  228. $where .= ' and city = ' . $city;
  229. }
  230. }
  231. if ($name) {
  232. $where .= ' and name like("%'.$name.'%")';
  233. }
  234. if ($id) {
  235. $where .= ' and id = ' . $id;
  236. }
  237. if ($type < 10) {
  238. $shop_method = Dever::input('method');
  239. if ($shop_method == 1) {
  240. $where .= ' and method in(1,3)';
  241. } elseif ($shop_method == 2) {
  242. $where .= ' and method in(2,3)';
  243. }
  244. }
  245. if ($lng && $lat) {
  246. $col = Dever::db('shop/info')->config['config_col'];
  247. $km = Dever::input('km');
  248. $distance = 'round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2)';
  249. if ($km && $km == 1 && $type < 10) {
  250. # 验证公里数
  251. $config = Dever::db('main/manage_config')->find();
  252. if ($config && $config['km'] > 0) {
  253. $where .= ' and '.$distance.' <= ' . $config['km'];
  254. }
  255. }
  256. $sql = 'select '.$col.','.$distance.' as distance from {table} where '.$where.' order by distance asc';
  257. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  258. } else {
  259. $data = Dever::db('shop/info')->getOne($id);
  260. }
  261. if ($data && isset($data['city']) && $data['city']) {
  262. $city = Dever::db('area/city')->one($data['city']);
  263. $data['city_name'] = $city['name'];
  264. }
  265. return $data;
  266. }
  267. # 获取库存
  268. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true, $check = true, $uid = -1)
  269. {
  270. $where['shop_id'] = $shop_id;
  271. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  272. if ($sku_id) {
  273. $where['sku_id'] = $sku_id;
  274. }
  275. $total = 1;
  276. $other = Dever::db('shop/goods_sku')->getData($where);
  277. if (!$other && $check) {
  278. if ($check == -1) {
  279. $total = 2;
  280. } else {
  281. return false;
  282. }
  283. }
  284. $other_array = array('total', 'min', 'price_id');
  285. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  286. # 购买不需要读取goods_sku的min
  287. $other_array = array('total', 'price_id');
  288. }
  289. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, $other_array));
  290. if($data) {
  291. if (Dever::config('base')->buy && Dever::config('base')->buy == 1) {
  292. } else {
  293. $goods_sku = Dever::db('shop/goods_sku')->find($where);
  294. if ($goods_sku && isset($goods_sku['min'])) {
  295. $data['min'] = $goods_sku['min'];
  296. }
  297. }
  298. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  299. $data['total'] = 0;
  300. if ($data['price_type'] == 4) {
  301. if (isset($data['goods']) && is_array($data['goods'])) {
  302. foreach ($data['goods'] as $k => $v) {
  303. $where = array();
  304. $where['shop_id'] = $shop_id;
  305. $where['goods_id'] = $v['id'];
  306. $gother = Dever::db('shop/goods_sku')->getData($where);
  307. $data['goods'][$k]['total'] = $this->getTotal($gother, -1);
  308. if ($data['total'] == 0) {
  309. $data['total'] = $data['goods'][$k]['total'];
  310. }
  311. if ($data['total'] > $data['goods'][$k]['total']) {
  312. $data['total'] = $data['goods'][$k]['total'];
  313. }
  314. }
  315. }
  316. } else {
  317. $data['total'] = $this->getTotal($other, $sku_id);
  318. }
  319. if ($total == 2) {
  320. $data['total'] = 0;
  321. }
  322. if ($data['min'] && floor($data['min']) == $data['min']) {
  323. $data['min'] = intval($data['min']);
  324. }
  325. # 根据角色计算折扣
  326. $data['discount'] = (object) array();
  327. if ($uid && $uid > 0) {
  328. $role = $this->getRole($uid);
  329. if ($role) {
  330. $role_state = false;
  331. $discount_goods = Dever::db('act/discount_goods')->getData(array('discount_id' => $role['id']));
  332. if ($discount_goods) {
  333. $discount_goods = array_keys($discount_goods);
  334. if (in_array($data['id'], $discount_goods)) {
  335. $role_state = true;
  336. }
  337. } elseif ($role['category'] && in_array($data['top_category_id'], $role['category'])) {
  338. $role_state = true;
  339. }
  340. if ($role_state) {
  341. $data['discount'] = $role;
  342. }
  343. }
  344. }
  345. }
  346. return $data;
  347. }
  348. # 获取角色
  349. public function getRole($uid)
  350. {
  351. $bind = Dever::db('agent/user_bind')->find(array('uid' => $uid, 'status' => 2));
  352. if ($bind) {
  353. $discount = Dever::db('act/discount')->find(array('role' => 1));
  354. return $discount;
  355. }
  356. return false;
  357. }
  358. # 验证库存
  359. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  360. {
  361. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  362. if (!$info) {
  363. if ($state == 2) {
  364. Dever::alert('商品不存在');
  365. } else {
  366. return false;
  367. }
  368. }
  369. $total = $info['total'];
  370. $min = $info['min'];
  371. # 增加最小起购量
  372. if ($state < 3 && $min > 0 && $num < $min) {
  373. $num = $min;
  374. }
  375. if ($num > $total) {
  376. if ($state == 2) {
  377. Dever::alert('库存不足');
  378. }
  379. $num = $total;
  380. }
  381. return $total;
  382. }
  383. # 获取库存
  384. public function getTotal($other, $sku_id)
  385. {
  386. if (isset($other[$sku_id])) {
  387. return $other[$sku_id]['total'];
  388. } else {
  389. return 0;
  390. }
  391. }
  392. # 获取经纬度
  393. public function geo($code, $name)
  394. {
  395. # 获取经纬度
  396. $url = 'https://restapi.amap.com/v3/geocode/geo';
  397. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  398. //$param['city'] = $code;
  399. $param['address'] = $name;
  400. $result = json_decode(Dever::curl($url, $param), true);
  401. $lng = 0;
  402. $lat = 0;
  403. $map = '';
  404. if (isset($result['geocodes'][0]['location']) && $result['geocodes'][0]['location']) {
  405. $map = $code . ',' . $result['geocodes'][0]['location'] . ',11';
  406. $temp = explode(',', $result['geocodes'][0]['location']);
  407. $lng = $temp[0];
  408. $lat = $temp[1];
  409. }
  410. return array($lng, $lat, $map);
  411. }
  412. # 根据经纬度获取地址
  413. public function address($lng, $lat)
  414. {
  415. $url = 'http://restapi.amap.com/v3/geocode/regeo';
  416. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  417. $param['location'] = $lng . ',' . $lat;
  418. $param['radius'] = 2800;
  419. $result = json_decode(Dever::curl($url, $param), true);
  420. $address = '';
  421. if (isset($result['regeocode']['formatted_address'])) {
  422. $address = $result['regeocode']['formatted_address'];
  423. }
  424. return $address;
  425. }
  426. }