Info.php 9.8 KB

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