Info.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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)
  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. $name = Dever::input('name');
  67. if ($name) {
  68. $where['name'] = $name;
  69. }
  70. if ($price_type) {
  71. $where['price_type'] = $price_type;
  72. }
  73. if ($sell_type) {
  74. $where['sell_type'] = $sell_type;
  75. }
  76. $where['state'] = 1;
  77. $data = Dever::db($table)->$method($where);
  78. $result_1 = array();
  79. $result_2 = array();
  80. if ($data) {
  81. foreach ($data as $k => $v) {
  82. $d = $this->getGoodsInfo($where['shop_id'], $v);
  83. if ($d) {
  84. if ($d['total'] <= 0) {
  85. $result_2[] = $d;
  86. } else {
  87. $result_1[] = $d;
  88. }
  89. }
  90. }
  91. $data = array_merge($result_1, $result_2);
  92. }
  93. return $data;
  94. }
  95. # 获取店铺的商品SKU列表
  96. public function getGoodsSku($shop)
  97. {
  98. $table = 'shop/goods_sku';
  99. $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
  100. $method = 'getDataPage';
  101. $name = Dever::input('name');
  102. if ($name) {
  103. $where['name'] = $name;
  104. }
  105. $total = Dever::input('total');
  106. if ($total) {
  107. $where['total'] = $total;
  108. }
  109. //$where['status'] = 1;
  110. $where['state'] = 1;
  111. $where['state_1'] = 1;
  112. $data = Dever::db($table)->$method($where);
  113. $result = array();
  114. if ($data) {
  115. foreach ($data as $k => $v) {
  116. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  117. if (!isset($result[$v['id']])) {
  118. $result[$v['id']] = $v;
  119. }
  120. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  121. $result[$v['id']]['price_array'][] = array
  122. (
  123. 'name' => $data[$k]['sku_name'],
  124. 'total' => $data[$k]['total'] <= 0 ? 0 : $data[$k]['total'],
  125. 'price' => $data[$k]['price'],
  126. 's_price' => $data[$k]['s_price'],
  127. );
  128. }
  129. }
  130. }
  131. return $result;
  132. }
  133. # 获取详细信息
  134. private function getInfo($data)
  135. {
  136. if ($data['worktime']) {
  137. $time = date('Hi');
  138. $worktime = str_replace(':', '', $data['worktime']);
  139. $temp = explode('~', $worktime);
  140. if (isset($temp[0]) && isset($temp[1]) && ($time < $temp[0] || $time > $temp[1])) {
  141. $data['open'] = 2;
  142. }
  143. }
  144. $data['gotime'] = $data['worktime'];
  145. return $data;
  146. }
  147. # 获取距离
  148. public function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  149. {
  150. $page = array();
  151. if ($method == 'fetchAll') {
  152. $page['template'] = 'list';
  153. $page['num'] = 10;
  154. } else {
  155. $page = false;
  156. }
  157. $where = 'status = 1 and state = 1';
  158. if ($type) {
  159. $where .= ' and type = ' . $type;
  160. }
  161. if ($city) {
  162. $county = Dever::db('area/county')->find($city);
  163. if ($county) {
  164. $city = $county['city_id'];
  165. }
  166. if ($type < 10 && $city) {
  167. $where .= ' and city = ' . $city;
  168. }
  169. }
  170. if ($name) {
  171. $where .= ' and name like("%'.$name.'%")';
  172. }
  173. if ($id) {
  174. $where .= ' and id = ' . $id;
  175. }
  176. if ($type < 10) {
  177. $shop_method = Dever::input('method');
  178. if ($shop_method == 1) {
  179. $where .= ' and method in(1,3)';
  180. } elseif ($shop_method == 2) {
  181. $where .= ' and method in(2,3)';
  182. }
  183. }
  184. if ($lng && $lat) {
  185. $col = Dever::db('shop/info')->config['config_col'];
  186. $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';
  187. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  188. } else {
  189. $data = Dever::db('shop/info')->getOne($id);
  190. }
  191. if ($data && isset($data['city']) && $data['city']) {
  192. $city = Dever::db('area/city')->one($data['city']);
  193. $data['city_name'] = $city['name'];
  194. }
  195. return $data;
  196. }
  197. # 获取库存
  198. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
  199. {
  200. $where['shop_id'] = $shop_id;
  201. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  202. if ($sku_id) {
  203. $where['sku_id'] = $sku_id;
  204. }
  205. $other = Dever::db('shop/goods_sku')->getData($where);
  206. if (!$other) {
  207. return false;
  208. }
  209. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
  210. if($data) {
  211. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  212. $data['total'] = 0;
  213. if ($data['price_type'] == 4) {
  214. if (isset($data['goods']) && is_array($data['goods'])) {
  215. foreach ($data['goods'] as $k => $v) {
  216. $where = array();
  217. $where['shop_id'] = $shop_id;
  218. $where['goods_id'] = $v['id'];
  219. $gother = Dever::db('shop/goods_sku')->getData($where);
  220. $data['goods'][$k]['total'] = $this->getTotal($gother, -1);
  221. if ($data['total'] == 0) {
  222. $data['total'] = $data['goods'][$k]['total'];
  223. }
  224. if ($data['total'] > $data['goods'][$k]['total']) {
  225. $data['total'] = $data['goods'][$k]['total'];
  226. }
  227. }
  228. }
  229. } else {
  230. $data['total'] = $this->getTotal($other, $sku_id);
  231. }
  232. }
  233. return $data;
  234. }
  235. # 验证库存
  236. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  237. {
  238. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  239. if (!$info) {
  240. if ($state == 2) {
  241. Dever::alert('商品不存在');
  242. } else {
  243. return false;
  244. }
  245. }
  246. $total = $info['total'];
  247. $min = $info['min'];
  248. # 增加最小起购量
  249. if ($min > 0 && $num < $min) {
  250. $num = $min;
  251. }
  252. if ($num > $total) {
  253. if ($state == 2) {
  254. Dever::alert('库存不足');
  255. }
  256. $num = $total;
  257. }
  258. return $total;
  259. }
  260. # 获取库存
  261. public function getTotal($other, $sku_id)
  262. {
  263. if (isset($other[$sku_id])) {
  264. return $other[$sku_id]['total'];
  265. } else {
  266. return 0;
  267. }
  268. }
  269. # 获取经纬度
  270. public function geo($code, $name)
  271. {
  272. # 获取经纬度
  273. $url = 'https://restapi.amap.com/v3/geocode/geo';
  274. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  275. //$param['city'] = $code;
  276. $param['address'] = $name;
  277. $result = json_decode(Dever::curl($url, $param), true);
  278. $lng = 0;
  279. $lat = 0;
  280. $map = '';
  281. if (isset($result['geocodes'][0]['location']) && $result['geocodes'][0]['location']) {
  282. $map = $code . ',' . $result['geocodes'][0]['location'] . ',11';
  283. $temp = explode(',', $result['geocodes'][0]['location']);
  284. $lng = $temp[0];
  285. $lat = $temp[1];
  286. }
  287. return array($lng, $lat, $map);
  288. }
  289. # 根据经纬度获取地址
  290. public function address($lng, $lat)
  291. {
  292. $url = 'http://restapi.amap.com/v3/geocode/regeo';
  293. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  294. $param['location'] = $lng . ',' . $lat;
  295. $param['radius'] = 2800;
  296. $result = json_decode(Dever::curl($url, $param), true);
  297. $address = '';
  298. if (isset($result['regeocode']['formatted_address'])) {
  299. $address = $result['regeocode']['formatted_address'];
  300. }
  301. return $address;
  302. }
  303. }