Info.php 11 KB

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