Info.php 8.2 KB

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