top_category = $top_category; # 必有有分类才能进入到需求中 if (!$this->top_category) { $this->top_category = -1; //Dever::alert('错误的分类信息'); } } private function setAttr($cate, $order = 'list_reorder') { if ($order != 'list_reorder') { Dever::config('base')->attrOrder = $order; } Dever::setInput('search_option_category', $cate); $attr = Dever::db($this->table_attr, array(), $cate); return $attr; } public function getAttr($cate) { return Dever::load('attr/api')->getAttrByCate($cate, 'list'); } # 根据分类获取需求列表数据 public function getData($cate = false, $limit = '0,10', $page = false, $attr = true) { if (!$limit) { $limit = '0,10'; } $where = $data = array(); if (!$cate) { if ($this->top_category > 0) { $where['top_category_id'] = $this->top_category; } } elseif (strstr($cate, ',')) { $where['category'] = $this->top_category . ',' . $cate; } else { $where['second_category_id'] = $cate; } if ($page) { $method = 'getPageAll'; } else { $where['limit'] = $limit; $method = 'getAll'; } $name = Dever::input('name'); if ($name) { $where['name'] = $name; } $data = Dever::db($this->table_info)->$method($where); if ($data && $attr) { foreach ($data as $k => $v) { $data[$k] = $this->info($v, 'list_reorder'); } } return $data; } # 根据属性获取需求列表数据 这里带有检索属性的功能 此处还需优化 public function getDataByAttr($cate = false, $where = array()) { if (!$cate) { Dever::alert('错误的分类信息'); } $where_sql = array(); $name = Dever::input('name'); if ($name) { //$where['name'] = $name; $where_sql[] = ' t_2.name = "'.$name.'"'; } $attr = $this->setAttr($this->top_category . ',' . $cate); //print_r($attr->config['attr']); $where_sql = Dever::load('attr/api')->getSql($attr->config['attr'], $where, $where_sql); if (!$where_sql) { return array(); } # 这里用pdo的数据绑定,字符串字段的区间查询就是有问题。。。只能用sql了 //$data = $attr->getPageAll($where); $page['template'] = 'list'; $page['num'] = 10; $where_sql = implode(' and ', $where_sql); $sql = 'SELECT t_2.* FROM `{project}_goods_'.$attr->config['name'].'` as t_1 Left Join `{project}_goods_info` AS t_2 ON t_1.`info_id`=t_2.`id` WHERE '.$where_sql.' order by t_2.`reorder` desc,t_2.`id` desc'; $data = $attr->fetchAll($sql, array(), $page); if ($data) { foreach ($data as $k => $v) { $data[$k] = $this->info($v, 'list_reorder'); } } return $data; } # 获取列表页的搜索条件 public function getSearch($cate = false) { # 获取搜索条件 if (!$cate) { $search_cate = $cate = $this->top_category; } else { $search_cate = $cate; } # 1是当前用户所在的城市,暂时写死 # 获取搜索条件 $search = Dever::load('attr/api')->getSearch($search_cate, true, 1, false, 2); # 下级分类 $search['cate'] = Dever::load('category/api')->getList($cate); # 上级分类 $search['parent_cate'] = Dever::load('category/api')->getList($this->top_category); return $search; } # 获取支付信息 public function getPayState($uid, $id, $category) { $pay_where['category'] = $category; $pay = Dever::db('goods/pay')->one($pay_where); $pay_state = 0; if ($pay && $pay['num'] > 0) { $pay_state = $pay['num']; } elseif (strstr($category, ',')) { $category = explode(',', $category); $category = array_pop($category); $pay_state = $this->getPayState($uid, $id, $category); } //$update['where_id'] = $id; //$update['poster_pay_type'] = $pay_state; //Dever::db($this->table_info)->update($update); return $pay_state; } # 更新需求 public function update($top, $cate, $data, $id, $uid) { if ($cate) { $category = $top . ',' . $cate; } else { $category = $top; } $attr = $this->setAttr($category); $attr_update = Dever::preInput('attr_'); if ($attr_update) { foreach ($attr_update as $k => $v) { if (strstr($k, '_s')) { $k = str_replace('_s', '', $k); $attr_update[$k] = $v; } if (strstr($k, '_e')) { $k = str_replace('_e', '', $k); $attr_update[$k] .= ',' . $v; } } } $update = array(); $update['name'] = $data['name']; $update['category'] = $category; $update['pic'] = $data['pic']; //$update['pic_cover'] = $data['pic']; $update['poster_uid'] = $uid; if (isset($data['view_price']) && $data['view_price']) { $update['view_price'] = $data['view_price']; } if ($uid && $id) { # 此处要判断是用户自己的 $info = Dever::db($this->table_info)->one($id); /* if ($uid == $info['poster_uid']) { Dever::alert('错误的用户参数'); } */ $update['where_id'] = $attr_update['where_id'] = $id; Dever::db($this->table_info)->update($update); Dever::db($this->table_attr)->update($attr_update); $pay = 0; } else { $id = Dever::db($this->table_info)->insert($update); $attr_update['category'] = $update['category']; $attr_update['info_id'] = $id; $attr_update['id'] = $id; Dever::db($this->table_attr)->insert($attr_update); # 验证是否需要支付,根据category判断 $pay = false; //$pay = $this->getPayState($uid, $id, $category); $info = Dever::db($this->table_info)->one($id); $info = $this->getInfoLink($info); } return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info); } # 获取支付所需要的信息 public function getPayInfo($id, $sku, $num = 1, $user = array()) { $info = Dever::db($this->table_info)->getOne($id); if ($info) { $info['freight_id'] = 0; $info['freight_price'] = 0; if ($sku > 0) { $where['info_id'] = $info['id']; $where['id'] = $sku; $sku = Dever::db('goods/info_sku')->getOne($where); if ($sku) { $info['price'] = $sku['price']; $info['num'] = $sku['num']; } } $freight = $this->freight($info, $info['price'], $user, $num); if ($freight) { $info['freight_id'] = $freight['id']; $info['freight_price'] = $freight['price']; } # 佣金计算 $info['reward'] = $this->reward($info, $info['price']*$num); } return $info; } # 获取自提信息 public function storeInfo($goods_id, $area_id = '') { $info = Dever::db($this->table_info)->getOne($goods_id); if (!$info) { return array(); } $array = explode(',', $info['category']); $cate = $store = array(); $total = count($array); $total -= 1; # 用户所在地理位置 if ($area_id) { $where['area'] = $area_id; } foreach ($array as $k => $v) { $cate[$k] = $v; if ($k < $total) { $cate[] = '-1'; } $where['category'] = $cate; $data = Dever::db('goods/store')->state($where); if ($data) { $store += $data; } } return $store; } # 获取运费信息 public function getFreight($id, $goods_price, $user, $num) { $info = Dever::db($this->table_info)->getOne($id); return $this->freight($info, $goods_price, $user, $num); } # 获取基本信息 public function getInfo($id, $attr = true, $order = 'view_reorder', $user = array(), $reward = false) { $info = Dever::db($this->table_info)->getOne($id); if ($info && $attr) { $info = $this->info($info, $order, false, $user, $reward); } if ($info['pic']) { $info['pic'] = explode(',', $info['pic']); } return $info; } # 获取基本信息 public function info($info, $key = 'list_reorder', $is_sell = false, $user = array(), $reward = false) { if ($info['price_type'] == 2) { $attr = $this->setAttr($info['category'], $key); $attr_data = $attr->one($info['id']); $info['category_array'] = Dever::load('category/api')->string($info['category']); $info['attr'] = $info['sell_attr'] = array(); if ($attr->config['attr']) { foreach ($attr->config['attr'] as $k => $v) { if (isset($v[$key]) && $v[$key] > 0) { if (isset($attr_data['attr_' . $v['id']])) { $v['value'] = $attr_data['attr_' . $v['id']]; } else { continue; $v['value'] = ''; } $v['value_string'] = Dever::load('attr/api')->getValue($v); if ($v['ename'] == 'price') { $info['price'] = $v['value_string']; } else { $v['bg'] = 'background-image: url('.$v['icon'].')'; $v['pic'] = ''; if ($is_sell > 0) { if ($is_sell == $v['is_sell']) { $info['attr'][] = $v; } } else { $info['attr'][] = $v; # 设置sku $this->sku($v, $info); } } } } } } else { $price = $info['price']; $s_price = $info['s_price']; $info['price'] = array(); $info['price']['list'] = array(); $info['price']['min'] = array ( 'price' => $price, 's_price' => $s_price ); $info['price']['max'] = array(); } $info['platform_name'] = Dever::db('goods/info')->config['config_platform'][$info['platform']]; if (isset($info['cdate']) && $info['cdate']) { $info['cdate_string'] = Dever::mdate($info['cdate'], 2); } $info = $this->getInfoLink($info); $info['freight'] = 0; if ($info['platform'] == 1 && $user) { # 获取运费 $info['freight'] = $this->freight($info, $info['price']['min']['price'], $user); } # 佣金计算 if ($reward) { $info['reward'] = $this->reward($info, $info['price']['min']['price']); } return $info; } # 佣金计算 private function reward($info, $price) { if ($info['reward_value'] && $info['reward_value'] > 0) { if ($info['reward_type'] == 2) { $info['reward_value'] = round(($info['reward_value']/100) * $price, 2); } return $info['reward_value']; } $array = explode(',', $info['category']); $cate = $reward = array(); $total = count($array); $total -= 1; foreach ($array as $k => $v) { $cate[$k] = $v; if ($k < $total) { $cate[] = '-1'; } $where['category'] = $cate; $data = Dever::db('goods/reward')->one($where); if ($data) { $reward = $data; } } if ($reward) { if ($reward['value'] && $reward['value'] > 0) { if ($reward['type'] == 2) { $reward['value'] = round(($reward['value']/100) * $price, 2); } return $reward['value']; } } return 0; } # 运费计算,暂时用省份代表地区 private function freight($info, $goods_price, $user = array(), $num = 1) { $array = explode(',', $info['category']); $cate = $freight = array(); $total = count($array); $total -= 1; if ($info && $info['goods_area']) { $where['goods_area'] = $info['goods_area']; } # 用户所在地理位置 if ($user && isset($user['area_id']) && $user['area_id']) { $area = explode(',', $user['area_id']); $where['area'] = $area[0]; } foreach ($array as $k => $v) { $cate[$k] = $v; if ($k < $total) { $cate[] = '-1'; } $where['category'] = $cate; $data = Dever::db('goods/freight')->one($where); if ($data) { $freight = $data; } } if ($freight) { $price = $freight['first_price']; if ($num > $freight['first_num']) { $num = $num - $freight['first_num']; $price = $freight['first_price'] + ($freight['next_price']*$num); } if ($freight['type'] == 2) { $freight['price'] = round($goods_price*$num*($price/100), 2); } else { $freight['price'] = $price; } } return $freight; } private function sku($data, &$info) { # 获取销售属性 if ($data['is_sell'] == 2) { if ($data['option']) { $value = explode(',', $data['value']); foreach($data['option'] as $k => $v) { if (in_array($v['id'], $value)) { $data['option_sku'][] = array ( 'id' => $v['id'], 'info_id' => $v['info_id'], 'name' => $v['name'], 'icon' => $v['icon'], 'value' => $v['info_id'] . '-' . $v['id'], ); } } unset($data['option']); } $info['sell_attr'][] = $data; # 获取最便宜的价格 $where['info_id'] = $info['id']; $info['price'] = array(); $info['price']['list'] = Dever::db('goods/info_sku')->getData($where); $info['price']['min'] = Dever::db('goods/info_sku')->getMinOne($where); $info['price']['max'] = Dever::db('goods/info_sku')->getMaxOne($where); } } public function getInfoLink($info) { if (isset($this->view_path)) { $info['view_path'] = $this->view_path . '?top=' . $info['top_category_id'] . '&id=' . $info['id']; $info['view_link'] = Dever::url($info['view_path']); } return $info; } public function setViewPath($path) { $this->view_path = $path; } }