123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- <?php
- namespace Card\Src;
- use Dever;
- use Main\Lib\Core;
- class Api extends Core
- {
- public function __construct()
- {
- parent::__construct();
- $this->checkLogin();
- $this->shop_id = Dever::input('shop_id');
- $this->shop = array();
- if ($this->shop_id) {
- $lng = Dever::input('lng');
- $lat = Dever::input('lat');
- if ($lng && $lat) {
- $this->shop = Dever::load('shop/lib/info')->fetch($this->shop_id, false, $lng, $lat, false);
- } else {
- $this->shop = Dever::db('shop/info')->getOne($this->shop_id);
- }
- if (!$this->shop) {
- Dever::alert('门店不存在');
- }
- }
- }
-
- public function getCard()
- {
- $where['type'] = 1;
- $this->data['card'] = Dever::db('card/type')->getAll($where);
- if ($this->data['card']) {
- foreach ($this->data['card'] as $k => $v) {
- $w['type_id'] = $v['id'];
- $w['status'] = 1;
- $w['create_type'] = 1;
- $w['buy'] = 1;
- $list = Dever::db('card/info')->select($w);
- if (!$list) {
- unset($this->data['card'][$k]);
- } else {
- $this->data['card'][$k]['value'] = $list;
- }
- }
- }
-
- $this->data['city'] = Dever::load('shop/lib/info')->getCity();
- $config = Dever::db('main/sell_config')->find();
- $this->data['card_name'] = $config['card_name'];
- return $this->data;
- }
-
- public function confirm()
- {
- $this->data['uid'] = $this->uid;
- $this->data['user'] = $this->user;
- $this->data['shop'] = $this->shop;
- Dever::load('card/lib/buy')->getCard($this->data);
- return $this->data;
- }
-
- public function pay_commit()
- {
- $refer = Dever::input('refer');
- $address_id = Dever::input('address_id');
- $this->data['uid'] = $this->uid;
- $this->data['user'] = $this->user;
- $this->data['shop'] = $this->shop;
- Dever::load('card/lib/buy')->getCard($this->data);
- if (!$address_id) {
- Dever::alert('收货地址不能为空');
- }
- $pay = Dever::load('card/lib/buy')->pay($this->user, $this->shop, $this->data['name'], $this->data['num'], $this->data['list'], $this->data['price'], $address_id, 5, $refer);
- return $pay;
- }
-
- public function r_pay()
- {
- $refer = Dever::input('refer');
- $order_id = Dever::input('order_id');
- $pay = Dever::load('card/lib/buy')->rpay($order_id, 5, $refer);
- return $pay;
- }
-
- public function c_pay()
- {
- $refer = Dever::input('refer');
- $order_id = Dever::input('order_id');
- $pay = Dever::load('card/lib/buy')->cpay($order_id, 5, $refer);
- return $pay;
- }
-
- public function order()
- {
- return Dever::load('card/lib/buy')->set(1)->getList($this->uid);
- }
-
- public function order_view()
- {
- $id = Dever::input('order_id');
- return Dever::load('card/lib/buy')->set(2)->getView($this->uid, $id);
- }
-
- public function order_cancel_commit()
- {
- $order_id = Dever::input('order_id');
- return Dever::load('card/lib/buy')->set(2)->cancel($this->uid, $order_id);
- }
-
- public function dh_card_commit()
- {
- $card = Dever::input('card');
- if (!$card) {
- Dever::alert('请输入正确的卡号');
- }
- $test = Dever::input('test');
- if ($test == 1) {
- $data = Dever::db('card/code')->find(array('card' => $card));
- if (!$data) {
- Dever::alert('礼品卡卡号错误或礼品卡已兑换');
- }
- } else {
- $data = Dever::db('card/code')->find(array('card' => $card));
- if (!$data) {
- Dever::alert('礼品卡卡号错误或礼品卡已兑换');
- }
- if ($data['status'] > 2) {
- Dever::alert('礼品卡卡号错误或礼品卡已兑换');
- }
- $info['info'] = Dever::db('card/info')->find($data['card_id']);
- if (!$info['info']) {
- Dever::alert('兑换失败,礼品卡已下架');
- }
- if ($info['info']['status'] == 2) {
- Dever::alert('兑换失败,礼品卡已下架');
- }
- if ($info['info']['end_dh_day'] && $info['info']['end_dh_day'] > 0) {
- $data['eddate'] = $data['bdate'] + ($info['info']['end_dh_day'] * 86400);
- }
- if (isset($data['eddate']) && $data['eddate'] && $data['eddate'] > 0) {
- $time = time();
- if ($data['eddate'] <= $time) {
- Dever::alert('礼品卡已过兑换时间');
- }
- }
- $update['where_id'] = $data['id'];
- $update['dh_uid'] = $this->uid;
- $update['status'] = 3;
- $update['ddate'] = time();
- $state = Dever::db('card/code')->update($update);
- if (!$state) {
- Dever::alert('兑换失败,请重新兑换');
- }
-
- if ($data['type'] == 2) {
- $where['sku_id'] = $data['id'];
- $where['type'] = 2;
- $where['type_id'] = $data['card_id'];
- $agent = Dever::db('agent/member_goods')->find($where);
- if ($agent) {
- Dever::db('agent/member_goods')->update(array('where_id' => $agent['id'], 'sell_num' => $agent['total_num']));
- }
- }
- }
- $info = array();
- $config = Dever::db('main/sell_config')->find();
- $info['card_name'] = $config['card_name'];
- $info['info'] = Dever::db('card/info')->find($data['card_id']);
- if ($info['info']) {
- $info['type'] = Dever::db('card/type')->find($info['info']['type_id']);
- }
- if (!$data['total_cash']) {
- $data['total_cash'] = $info['info']['value'];
- }
- if (!$data['use_cash']) {
- $data['use_cash'] = 0;
- }
- $data['cash'] = $data['total_cash'] - $data['use_cash'];
- $info['card'] = $data;
- return $info;
- }
-
- public function card()
- {
- $config = Dever::db('main/sell_config')->find();
- $this->data['card_pic'] = $config['card'];
- $this->data['card_name'] = $config['card_name'];
- $this->data['card'] = Dever::db('card/code')->getPageData(array('dh_uid' => $this->uid));
- $city = Dever::input('city');
- if ($city) {
- $county = Dever::db('area/county')->find($city);
- if ($county) {
- $city = $county['city_id'];
- }
- }
- if ($this->data['card']) {
- foreach ($this->data['card'] as $k => $v) {
- $v['info'] = $this->data['card'][$k]['info'] = Dever::db('card/info')->find($v['card_id']);
- if (!$v['total_cash']) {
- $v['total_cash'] = $v['info']['value'];
- }
- if (!$v['use_cash']) {
- $v['use_cash'] = 0;
- }
- $this->data['card'][$k]['cash'] = $v['total_cash'] - $v['use_cash'];
-
- $info = $this->data['card'][$k]['type'] = Dever::db('card/type')->find($this->data['card'][$k]['info']['type_id']);
- $this->data['card'][$k]['minfo'] = '';
- if ($info['city_type'] == 2) {
- $city = $v['city'];
- if (!$city) {
- $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
- if ($card_order && $card_order['city']) {
- $city = $card_order['city'];
- }
- }
- if ($city) {
- $city = Dever::db('area/city')->find($city);
- if ($city) {
- $this->data['card'][$k]['minfo'] = '仅限' . $city['name'] . '可用';
- }
- }
- } elseif ($info['type'] == 2 && $v['shop_id']) {
- $shop = Dever::db('shop/info')->find($v['shop_id']);
- if ($shop) {
- $this->data['card'][$k]['minfo'] = '限' . $shop['name'] . '可用';
- }
- } else {
- $config_type = Dever::db('shop/info')->config['config_type'];
-
-
- if ($info['shop_type'] && strstr($info['shop_type'], ',')) {
- $this->data['card'][$k]['minfo'] = '多店铺可用';
- } elseif ($info['shop_type'] && isset($config_type[$info['shop_type']])) {
- $this->data['card'][$k]['minfo'] = $config_type[$info['shop_type']] . '可用';
- }
- }
- $this->data['card'][$k]['cinfo'] = '';
- if ($v['info']['category']) {
- $this->data['card'][$k]['cinfo'] = '限部分要求商品可用';
- } else {
- $goods = Dever::db('card/info_goods')->find(array('card_id' => $v['info']['id']));
- if ($goods) {
- $this->data['card'][$k]['cinfo'] = '限部分要求商品可用';
- }
- }
- }
- }
- return $this->data;
- }
-
- public function use_card()
- {
-
- $goods_id = Dever::input('goods_id');
- if ($goods_id) {
- $data = array();
- $data['shop'] = $this->shop;
- Dever::load('shop/lib/sell')->goods($data);
- }
- $config = Dever::db('main/sell_config')->find();
- $this->data['card_name'] = $config['card_name'];
- $shop_type = $this->shop['type'];
- $cur_city = Dever::input('city');
- $cur_city = $this->shop['city'];
- if ($cur_city) {
- $county = Dever::db('area/county')->find($cur_city);
- if ($county) {
- $cur_city = $county['city_id'];
- }
- }
- $config_type = Dever::db('shop/info')->config['config_type'];
- $card = Dever::db('card/code')->getData(array('dh_uid' => $this->uid));
- $this->data['card'] = array();
- $this->data['no_card'] = array();
- if ($card) {
- foreach ($card as $k => $v) {
- $v['info'] = Dever::db('card/info')->find($v['card_id']);
- if (!$v['info']) {
- continue;
- }
- if (!$v['total_cash']) {
- $v['total_cash'] = $v['info']['value'];
- }
- if (!$v['use_cash']) {
- $v['use_cash'] = 0;
- }
- $v['cash'] = $v['total_cash'] - $v['use_cash'];
- if ($v['cash'] <= 0) {
- continue;
- }
-
- $card_goods = array();
- $check = false;
- $xian = false;
- if ($this->shop['type'] == 1 && $v['info']['shop_set'] == 1) {
- $check = false;
- $xian = false;
- } else {
- $xian = true;
- $goods = Dever::db('card/info_goods')->getData(array('card_id' => $v['info']['id']));
- if ($goods || $v['info']['category']) {
- $check = true;
- }
- }
-
- $v['minfo'] = '';
- $v['cinfo'] = '';
- if ($check) {
- $v['cinfo'] = '限部分要求商品可用';
- $price = 0;
- $v['info']['category'] = explode(',', $v['info']['category']);
- if ($goods) {
- $goods = array_keys($goods);
- }
-
- foreach ($data['list'] as $k1 => $v1) {
- if (!isset($v1['cash'])) {
- $v1['cash'] = $v1['price'];
- }
- if ($goods) {
- if (in_array($v1['id'], $goods)) {
- if ($v['info']['category']) {
- if (in_array($v1['top_category_id'], $v['info']['category'])) {
- $price += ($v1['cash'] * $v1['num']);
- }
- } else {
- $price += ($v1['cash'] * $v1['num']);
- }
- }
- } elseif ($v['info']['category']) {
- if (in_array($v1['top_category_id'], $v['info']['category'])) {
- $price += ($v1['cash'] * $v1['num']);
- }
- }
- }
- } else {
- $price = $data['price'];
- }
- if ($xian && ($price <= 0 || ($v['info']['min'] > 0 && $price < $v['info']['min']))) {
-
- $no = true;
- } else {
-
- $no = false;
- }
- $v['type'] = $type = Dever::db('card/type')->find($v['info']['type_id']);
- if ($no) {
- $this->data['no_card'][] = $v;
- } else {
-
- if ($type['shop_type'] && strstr($type['shop_type'], ',')) {
- $v['minfo'] = '多店铺可用';
- } elseif ($type['shop_type'] && isset($config_type[$type['shop_type']])) {
- $v['minfo'] = $config_type[$type['shop_type']] . '可用';
- }
- $type['shop_type'] = explode(',', $type['shop_type']);
- if (in_array($shop_type, $type['shop_type'])) {
-
- if ($type['city_type'] == 1) {
- $this->data['card'][] = $v;
- } else {
- $city = $v['city'];
- if (!$city) {
- $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
- if ($card_order && $card_order['city']) {
- $city = $card_order['city'];
- }
- }
- if ($city) {
- $city_info = Dever::db('area/city')->find($city);
- $v['minfo'] = '仅限' . $city_info['name'] . '可用';
- if ($city == $cur_city) {
- $this->data['card'][] = $v;
- } else {
- $this->data['no_card'][] = $v;
- }
- } else {
- $this->data['no_card'][] = $v;
- }
-
-
- }
- } elseif ($v['shop_id']) {
- $shop = Dever::db('shop/info')->find($v['shop_id']);
- $v['minfo'] = '限' . $shop['name'] . '可用';
- if ($v['shop_id'] == $this->shop_id) {
- $this->data['card'][] = $v;
- } else {
- $this->data['no_card'][] = $v;
- }
- } else {
- $this->data['no_card'][] = $v;
- }
- }
- }
- }
- $this->data['card_num'] = count($this->data['card']);
- $this->data['no_card_num'] = count($this->data['no_card']);
- return $this->data;
- }
- }
|