Info.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. $data = Dever::db($table)->$method($where);
  102. $result = array();
  103. if ($data) {
  104. foreach ($data as $k => $v) {
  105. $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
  106. if (!isset($result[$v['id']])) {
  107. $result[$v['id']] = $v;
  108. }
  109. if (isset($data[$k]['attr']) && $data[$k]['attr']) {
  110. $result[$v['id']]['price_array'][] = array
  111. (
  112. 'name' => $data[$k]['sku_name'],
  113. 'total' => $data[$k]['total'] <= 0 ? 0 : $data[$k]['total'],
  114. 'price' => $data[$k]['price'],
  115. 's_price' => $data[$k]['s_price'],
  116. );
  117. }
  118. }
  119. }
  120. return $result;
  121. }
  122. # 获取详细信息
  123. private function getInfo($data)
  124. {
  125. if ($data['worktime']) {
  126. $time = date('Hi');
  127. $worktime = str_replace(':', '', $data['worktime']);
  128. $temp = explode('~', $worktime);
  129. if (isset($temp[0]) && isset($temp[1]) && ($time < $temp[0] || $time > $temp[1])) {
  130. $data['open'] = 2;
  131. }
  132. }
  133. $data['gotime'] = $data['worktime'];
  134. return $data;
  135. }
  136. # 获取距离
  137. public function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
  138. {
  139. $page = array();
  140. if ($method == 'fetchAll') {
  141. $page['template'] = 'list';
  142. $page['num'] = 10;
  143. } else {
  144. $page = false;
  145. }
  146. $where = 'status = 1 and state = 1';
  147. if ($type) {
  148. $where .= ' and type = ' . $type;
  149. }
  150. if ($city) {
  151. $county = Dever::db('area/county')->find($city);
  152. if ($county) {
  153. $city = $county['city_id'];
  154. }
  155. if ($type == 1 && $city) {
  156. $where .= ' and city = ' . $city;
  157. }
  158. }
  159. if ($name) {
  160. $where .= ' and name like("%'.$name.'%")';
  161. }
  162. if ($id) {
  163. $where .= ' and id = ' . $id;
  164. }
  165. if ($lng && $lat) {
  166. $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';
  167. $data = Dever::db('shop/info')->$method($sql, array(), $page);
  168. } else {
  169. $data = Dever::db('shop/info')->getOne($id);
  170. }
  171. return $data;
  172. }
  173. # 获取库存
  174. public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
  175. {
  176. $where['shop_id'] = $shop_id;
  177. $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
  178. $other = Dever::db('shop/goods_sku')->getData($where);
  179. $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
  180. if($data) {
  181. $sku_id = $sku_id ? $sku_id : $data['sku_id'];
  182. $data['total'] = 0;
  183. if ($data['price_type'] == 4) {
  184. if (isset($data['goods']) && is_array($data['goods'])) {
  185. foreach ($data['goods'] as $k => $v) {
  186. $where = array();
  187. $where['shop_id'] = $shop_id;
  188. $where['goods_id'] = $v['id'];
  189. $gother = Dever::db('shop/goods_sku')->getData($where);
  190. $data['goods'][$k]['total'] = $this->getTotal($gother, -1);
  191. if ($data['total'] == 0) {
  192. $data['total'] = $data['goods'][$k]['total'];
  193. }
  194. if ($data['total'] > $data['goods'][$k]['total']) {
  195. $data['total'] = $data['goods'][$k]['total'];
  196. }
  197. }
  198. }
  199. } else {
  200. $data['total'] = $this->getTotal($other, $sku_id);
  201. }
  202. }
  203. return $data;
  204. }
  205. # 验证库存
  206. public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
  207. {
  208. $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
  209. if (!$info) {
  210. Dever::alert('商品不存在');
  211. }
  212. $total = $info['total'];
  213. $min = $info['min'];
  214. # 增加最小起购量
  215. if ($min > 0 && $num < $min) {
  216. $num = $min;
  217. }
  218. if ($num > $total) {
  219. if ($state == 2) {
  220. Dever::alert('库存不足');
  221. }
  222. $num = $total;
  223. }
  224. return $total;
  225. }
  226. # 获取库存
  227. public function getTotal($other, $sku_id)
  228. {
  229. if (isset($other[$sku_id])) {
  230. return $other[$sku_id]['total'];
  231. } else {
  232. return 0;
  233. }
  234. }
  235. # 获取经纬度
  236. public function geo($code, $name)
  237. {
  238. # 获取经纬度
  239. $url = 'https://restapi.amap.com/v3/geocode/geo';
  240. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  241. //$param['city'] = $code;
  242. $param['address'] = $name;
  243. $result = json_decode(Dever::curl($url, $param), true);
  244. $lng = 0;
  245. $lat = 0;
  246. $map = '';
  247. if (isset($result['geocodes'][0]['location']) && $result['geocodes'][0]['location']) {
  248. $map = $code . ',' . $result['geocodes'][0]['location'] . ',11';
  249. $temp = explode(',', $result['geocodes'][0]['location']);
  250. $lng = $temp[0];
  251. $lat = $temp[1];
  252. }
  253. return array($lng, $lat, $map);
  254. }
  255. # 根据经纬度获取地址
  256. public function address($lng, $lat)
  257. {
  258. $url = 'http://restapi.amap.com/v3/geocode/regeo';
  259. $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
  260. $param['location'] = $lng . ',' . $lat;
  261. $param['radius'] = 2800;
  262. $result = json_decode(Dever::curl($url, $param), true);
  263. $address = '';
  264. if (isset($result['regeocode']['formatted_address'])) {
  265. $address = $result['regeocode']['formatted_address'];
  266. }
  267. return $address;
  268. }
  269. }