123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- namespace Shop\Lib;
- use Dever;
- class Info
- {
- # 获取店铺基本信息
- public function getOne($shop_id, $lng, $lat)
- {
- $shop = $this->fetch($shop_id, false, $lng, $lat);
- return $shop;
- }
- # 获取店铺基本信息
- public function get($city, $lng, $lat, $name = '', $method = 'fetch', $shop_id = false, $address = false)
- {
- if (!$city) {
- Dever::alert('请传入城市');
- }
- if ((!$lng || !$lat) && $address) {
- list($lng, $lat) = Dever::load('shop/lib/info')->geo($city, $address);
- }
- if (!$lng || !$lat) {
- $city_info = Dever::db('area/city')->find($city);
- list($lng, $lat) = Dever::load('shop/lib/info')->geo($city, $city_info['name']);
- if (!$lng || !$lat) {
- Dever::alert('请传入用户坐标');
- }
- }
- $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
- # 验证本城市内有没有店
- if (!$data) {
- $data = $this->fetch(false, $city, $lng, $lat, 10, $name, $method);
- }
- if ($data) {
- if ($method == 'fetch') {
- $data = $this->getInfo($data);
- } else {
- foreach ($data as $k => $v) {
- $data[$k] = $this->getInfo($data[$k]);
- if ($shop_id == $v['id']) {
- $data[$k]['cur'] = 1;
- } else {
- $data[$k]['cur'] = 2;
- }
- }
- }
- }
- return $data;
- }
- # 获取店铺的商品列表
- public function getGoods($shop, $column = false, $price_type = false)
- {
- $table = 'shop/goods';
- $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
- if ($column) {
- $where['column'] = $column;
- $method = 'getData';
- } else {
- $method = 'getDataPage';
- }
-
- $name = Dever::input('name');
- if ($name) {
- $where['name'] = $name;
- }
- if ($price_type) {
- $where['price_type'] = $price_type;
- }
- $where['status'] = 1;
- $where['state'] = 1;
-
- $data = Dever::db($table)->$method($where);
- $result_1 = array();
- $result_2 = array();
- if ($data) {
- foreach ($data as $k => $v) {
- $d = $this->getGoodsInfo($where['shop_id'], $v);
- if ($d) {
- if ($d['total'] <= 0) {
- $result_2[] = $d;
- } else {
- $result_1[] = $d;
- }
- }
- }
- $data = array_merge($result_1, $result_2);
- }
- return $data;
- }
- # 获取店铺的商品SKU列表
- public function getGoodsSku($shop)
- {
- $table = 'shop/goods_sku';
- $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
- $method = 'getDataPage';
-
- $name = Dever::input('name');
- if ($name) {
- $where['name'] = $name;
- }
- $total = Dever::input('total');
- if ($total) {
- $where['total'] = $total;
- }
- $where['status'] = 1;
- $where['state'] = 1;
- $where['state_1'] = 1;
- $data = Dever::db($table)->$method($where);
- $result = array();
- if ($data) {
- foreach ($data as $k => $v) {
- $data[$k] = Dever::load('goods/lib/info')->getPayInfo($v, $v['sku_id']);
- if (!isset($result[$v['id']])) {
- $result[$v['id']] = $v;
- }
- if (isset($data[$k]['attr']) && $data[$k]['attr']) {
- $result[$v['id']]['price_array'][] = array
- (
- 'name' => $data[$k]['sku_name'],
- 'total' => $data[$k]['total'] <= 0 ? 0 : $data[$k]['total'],
- 'price' => $data[$k]['price'],
- 's_price' => $data[$k]['s_price'],
- );
- }
-
- }
- }
- return $result;
- }
- # 获取详细信息
- private function getInfo($data)
- {
- if ($data['worktime']) {
- $time = date('Hi');
- $worktime = str_replace(':', '', $data['worktime']);
- $temp = explode('~', $worktime);
- if (isset($temp[0]) && isset($temp[1]) && ($time < $temp[0] || $time > $temp[1])) {
- $data['open'] = 2;
- }
- }
- $data['gotime'] = $data['worktime'];
- return $data;
- }
- # 获取距离
- public function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
- {
- $page = array();
- if ($method == 'fetchAll') {
- $page['template'] = 'list';
- $page['num'] = 10;
- } else {
- $page = false;
- }
- $where = 'status = 1 and state = 1';
- if ($type) {
- $where .= ' and type = ' . $type;
- }
- if ($city) {
- $county = Dever::db('area/county')->find($city);
- if ($county) {
- $city = $county['city_id'];
- }
- if ($type == 1 && $city) {
- $where .= ' and city = ' . $city;
- }
- }
-
- if ($name) {
- $where .= ' and name like("%'.$name.'%")';
- }
- if ($id) {
- $where .= ' and id = ' . $id;
- }
- if ($lng && $lat) {
- $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';
- $data = Dever::db('shop/info')->$method($sql, array(), $page);
- } else {
- $data = Dever::db('shop/info')->getOne($id);
- }
- return $data;
- }
- # 获取库存
- public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
- {
- $where['shop_id'] = $shop_id;
- $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
- $where['sku_id'] = $sku_id ? $sku_id : -1;
- $other = Dever::db('shop/goods_sku')->getData($where);
- if (!$other) {
- return false;
- }
- $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
- if($data) {
- $sku_id = $sku_id ? $sku_id : $data['sku_id'];
- $data['total'] = 0;
- if ($data['price_type'] == 4) {
- if (isset($data['goods']) && is_array($data['goods'])) {
- foreach ($data['goods'] as $k => $v) {
- $where = array();
- $where['shop_id'] = $shop_id;
- $where['goods_id'] = $v['id'];
- $gother = Dever::db('shop/goods_sku')->getData($where);
- $data['goods'][$k]['total'] = $this->getTotal($gother, -1);
- if ($data['total'] == 0) {
- $data['total'] = $data['goods'][$k]['total'];
- }
- if ($data['total'] > $data['goods'][$k]['total']) {
- $data['total'] = $data['goods'][$k]['total'];
- }
- }
- }
- } else {
- $data['total'] = $this->getTotal($other, $sku_id);
- }
- }
- return $data;
- }
- # 验证库存
- public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
- {
- $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
- if (!$info) {
- if ($state == 2) {
- Dever::alert('商品不存在');
- } else {
- return false;
- }
- }
- $total = $info['total'];
- $min = $info['min'];
- # 增加最小起购量
- if ($min > 0 && $num < $min) {
- $num = $min;
- }
- if ($num > $total) {
- if ($state == 2) {
- Dever::alert('库存不足');
- }
- $num = $total;
- }
- return $total;
- }
- # 获取库存
- public function getTotal($other, $sku_id)
- {
- if (isset($other[$sku_id])) {
- return $other[$sku_id]['total'];
- } else {
- return 0;
- }
- }
- # 获取经纬度
- public function geo($code, $name)
- {
- # 获取经纬度
- $url = 'https://restapi.amap.com/v3/geocode/geo';
- $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
- //$param['city'] = $code;
- $param['address'] = $name;
- $result = json_decode(Dever::curl($url, $param), true);
- $lng = 0;
- $lat = 0;
- $map = '';
- if (isset($result['geocodes'][0]['location']) && $result['geocodes'][0]['location']) {
- $map = $code . ',' . $result['geocodes'][0]['location'] . ',11';
- $temp = explode(',', $result['geocodes'][0]['location']);
- $lng = $temp[0];
- $lat = $temp[1];
- }
- return array($lng, $lat, $map);
- }
- # 根据经纬度获取地址
- public function address($lng, $lat)
- {
- $url = 'http://restapi.amap.com/v3/geocode/regeo';
- $param['key'] = 'f18cb42560b8aa54e3b53a6265bfd764';
- $param['location'] = $lng . ',' . $lat;
- $param['radius'] = 2800;
- $result = json_decode(Dever::curl($url, $param), true);
- $address = '';
- if (isset($result['regeocode']['formatted_address'])) {
- $address = $result['regeocode']['formatted_address'];
- }
- return $address;
- }
- }
|