Api.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. namespace Card\Src;
  3. use Dever;
  4. use Main\Lib\Core;
  5. class Api extends Core
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->checkLogin();
  11. $this->shop_id = Dever::input('shop_id');
  12. $this->shop = array();
  13. if ($this->shop_id) {
  14. $lng = Dever::input('lng');
  15. $lat = Dever::input('lat');
  16. if ($lng && $lat) {
  17. $this->shop = Dever::load('shop/lib/info')->fetch($this->shop_id, false, $lng, $lat, false);
  18. } else {
  19. $this->shop = Dever::db('shop/info')->getOne($this->shop_id);
  20. }
  21. if (!$this->shop) {
  22. Dever::alert('门店不存在');
  23. }
  24. }
  25. }
  26. # 获取礼品卡
  27. public function getCard()
  28. {
  29. $where['type'] = 1;
  30. $this->data['card'] = Dever::db('card/type')->getAll($where);
  31. if ($this->data['card']) {
  32. foreach ($this->data['card'] as $k => $v) {
  33. $w['type_id'] = $v['id'];
  34. $w['status'] = 1;
  35. $w['create_type'] = 1;
  36. $w['buy'] = 1;
  37. $list = Dever::db('card/info')->select($w);
  38. if (!$list) {
  39. unset($this->data['card'][$k]);
  40. } else {
  41. $this->data['card'][$k]['value'] = $list;
  42. }
  43. }
  44. }
  45. # 获取选择的城市
  46. $this->data['city'] = Dever::load('shop/lib/info')->getCity();
  47. $config = Dever::db('main/sell_config')->find();
  48. $this->data['card_name'] = $config['card_name'];
  49. return $this->data;
  50. }
  51. # 确认订单页面
  52. public function confirm()
  53. {
  54. $this->data['uid'] = $this->uid;
  55. $this->data['user'] = $this->user;
  56. $this->data['shop'] = $this->shop;
  57. Dever::load('card/lib/buy')->getCard($this->data);
  58. return $this->data;
  59. }
  60. # 开始下单
  61. public function pay_commit()
  62. {
  63. $refer = Dever::input('refer');
  64. $address_id = Dever::input('address_id');
  65. $this->data['uid'] = $this->uid;
  66. $this->data['user'] = $this->user;
  67. $this->data['shop'] = $this->shop;
  68. Dever::load('card/lib/buy')->getCard($this->data);
  69. if (!$address_id) {
  70. Dever::alert('收货地址不能为空');
  71. }
  72. $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);
  73. return $pay;
  74. }
  75. # 再次付款
  76. public function r_pay()
  77. {
  78. $refer = Dever::input('refer');
  79. $order_id = Dever::input('order_id');
  80. $pay = Dever::load('card/lib/buy')->rpay($order_id, 5, $refer);
  81. return $pay;
  82. }
  83. # 查询是否支付
  84. public function c_pay()
  85. {
  86. $refer = Dever::input('refer');
  87. $order_id = Dever::input('order_id');
  88. $pay = Dever::load('card/lib/buy')->cpay($order_id, 5, $refer);
  89. return $pay;
  90. }
  91. # 我的订单列表
  92. public function order()
  93. {
  94. return Dever::load('card/lib/buy')->set(1)->getList($this->uid);
  95. }
  96. # 查看订单详情
  97. public function order_view()
  98. {
  99. $id = Dever::input('order_id');
  100. return Dever::load('card/lib/buy')->set(2)->getView($this->uid, $id);
  101. }
  102. # 取消订单
  103. public function order_cancel_commit()
  104. {
  105. $order_id = Dever::input('order_id');
  106. return Dever::load('card/lib/buy')->set(2)->cancel($this->uid, $order_id);
  107. }
  108. # 兑换
  109. public function dh_card_commit()
  110. {
  111. $card = Dever::input('card');
  112. if (!$card) {
  113. Dever::alert('请输入正确的卡号');
  114. }
  115. $test = Dever::input('test');
  116. if ($test == 1) {
  117. $data = Dever::db('card/code')->find(array('card' => $card));
  118. if (!$data) {
  119. Dever::alert('礼品卡卡号错误或礼品卡已兑换');
  120. }
  121. } else {
  122. $data = Dever::db('card/code')->find(array('card' => $card));
  123. if (!$data) {
  124. Dever::alert('礼品卡卡号错误或礼品卡已兑换');
  125. }
  126. if ($data['status'] > 2) {
  127. Dever::alert('礼品卡卡号错误或礼品卡已兑换');
  128. }
  129. $info['info'] = Dever::db('card/info')->find($data['card_id']);
  130. if (!$info['info']) {
  131. Dever::alert('兑换失败,礼品卡已下架');
  132. }
  133. if ($info['info']['status'] == 2) {
  134. Dever::alert('兑换失败,礼品卡已下架');
  135. }
  136. if ($info['info']['end_dh_day'] && $info['info']['end_dh_day'] > 0) {
  137. $data['eddate'] = $data['bdate'] + ($info['info']['end_dh_day'] * 86400);
  138. }
  139. if (isset($data['eddate']) && $data['eddate'] && $data['eddate'] > 0) {
  140. $time = time();
  141. if ($data['eddate'] <= $time) {
  142. Dever::alert('礼品卡已过兑换时间');
  143. }
  144. }
  145. $update['where_id'] = $data['id'];
  146. $update['dh_uid'] = $this->uid;
  147. $update['status'] = 3;
  148. $update['ddate'] = time();
  149. $state = Dever::db('card/code')->update($update);
  150. if (!$state) {
  151. Dever::alert('兑换失败,请重新兑换');
  152. }
  153. # 验证代理商权益
  154. if ($data['type'] == 2) {
  155. $where['sku_id'] = $data['id'];
  156. $where['type'] = 2;
  157. $where['type_id'] = $data['card_id'];
  158. $agent = Dever::db('agent/member_goods')->find($where);
  159. if ($agent) {
  160. Dever::db('agent/member_goods')->update(array('where_id' => $agent['id'], 'sell_num' => $agent['total_num']));
  161. }
  162. }
  163. }
  164. $info = array();
  165. $config = Dever::db('main/sell_config')->find();
  166. $info['card_name'] = $config['card_name'];
  167. $info['info'] = Dever::db('card/info')->find($data['card_id']);
  168. if ($info['info']) {
  169. $info['type'] = Dever::db('card/type')->find($info['info']['type_id']);
  170. }
  171. if (!$data['total_cash']) {
  172. $data['total_cash'] = $info['info']['value'];
  173. }
  174. if (!$data['use_cash']) {
  175. $data['use_cash'] = 0;
  176. }
  177. $data['cash'] = $data['total_cash'] - $data['use_cash'];
  178. $info['card'] = $data;
  179. return $info;
  180. }
  181. # 查看兑换列表
  182. public function card()
  183. {
  184. $config = Dever::db('main/sell_config')->find();
  185. $this->data['card_pic'] = $config['card'];
  186. $this->data['card_name'] = $config['card_name'];
  187. $this->data['card'] = Dever::db('card/code')->getPageData(array('dh_uid' => $this->uid));
  188. $city = Dever::input('city');
  189. if ($city) {
  190. $county = Dever::db('area/county')->find($city);
  191. if ($county) {
  192. $city = $county['city_id'];
  193. }
  194. }
  195. if ($this->data['card']) {
  196. foreach ($this->data['card'] as $k => $v) {
  197. $v['info'] = $this->data['card'][$k]['info'] = Dever::db('card/info')->find($v['card_id']);
  198. if (!$v['total_cash']) {
  199. $v['total_cash'] = $v['info']['value'];
  200. }
  201. if (!$v['use_cash']) {
  202. $v['use_cash'] = 0;
  203. }
  204. $this->data['card'][$k]['cash'] = $v['total_cash'] - $v['use_cash'];
  205. $info = $this->data['card'][$k]['type'] = Dever::db('card/type')->find($this->data['card'][$k]['info']['type_id']);
  206. $this->data['card'][$k]['minfo'] = '';
  207. if ($info['city_type'] == 2) {
  208. $city = $v['city'];
  209. if (!$city) {
  210. $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
  211. if ($card_order && $card_order['city']) {
  212. $city = $card_order['city'];
  213. }
  214. }
  215. if ($city) {
  216. $city = Dever::db('area/city')->find($city);
  217. if ($city) {
  218. $this->data['card'][$k]['minfo'] = '仅限' . $city['name'] . '可用';
  219. }
  220. }
  221. } elseif ($info['type'] == 2 && $v['shop_id']) {
  222. $shop = Dever::db('shop/info')->find($v['shop_id']);
  223. if ($shop) {
  224. $this->data['card'][$k]['minfo'] = '限' . $shop['name'] . '可用';
  225. }
  226. } else {
  227. $config_type = Dever::db('shop/info')->config['config_type'];
  228. /*
  229. $info['shop_type'] = explode(',', $info['shop_type']);
  230. $this->data['card'][$k]['minfo'] = array();
  231. foreach ($info['shop_type'] as $tv) {
  232. if (isset($config_type[$tv])) {
  233. $this->data['card'][$k]['minfo'][] = $config_type[$tv];
  234. }
  235. }
  236. $this->data['card'][$k]['minfo'] = implode('、', $this->data['card'][$k]['minfo']) . '可用';
  237. */
  238. if ($info['shop_type'] && strstr($info['shop_type'], ',')) {
  239. $this->data['card'][$k]['minfo'] = '多店铺可用';
  240. } elseif ($info['shop_type'] && isset($config_type[$info['shop_type']])) {
  241. $this->data['card'][$k]['minfo'] = $config_type[$info['shop_type']] . '可用';
  242. }
  243. }
  244. $this->data['card'][$k]['cinfo'] = '';
  245. if ($v['info']['category']) {
  246. $this->data['card'][$k]['cinfo'] = '限部分要求商品可用';
  247. } else {
  248. $goods = Dever::db('card/info_goods')->find(array('card_id' => $v['info']['id']));
  249. if ($goods) {
  250. $this->data['card'][$k]['cinfo'] = '限部分要求商品可用';
  251. }
  252. }
  253. }
  254. }
  255. return $this->data;
  256. }
  257. # 查看可用的礼品卡
  258. public function use_card()
  259. {
  260. # 对商品进行处理
  261. $goods_id = Dever::input('goods_id');
  262. if ($goods_id) {
  263. $data = array();
  264. $data['shop'] = $this->shop;
  265. Dever::load('shop/lib/sell')->goods($data);
  266. }
  267. $config = Dever::db('main/sell_config')->find();
  268. $this->data['card_name'] = $config['card_name'];
  269. $shop_type = $this->shop['type'];
  270. $cur_city = Dever::input('city');
  271. $cur_city = $this->shop['city'];
  272. if ($cur_city) {
  273. $county = Dever::db('area/county')->find($cur_city);
  274. if ($county) {
  275. $cur_city = $county['city_id'];
  276. }
  277. }
  278. $config_type = Dever::db('shop/info')->config['config_type'];
  279. $card = Dever::db('card/code')->getData(array('dh_uid' => $this->uid));
  280. $this->data['card'] = array();
  281. $this->data['no_card'] = array();
  282. if ($card) {
  283. foreach ($card as $k => $v) {
  284. $v['info'] = Dever::db('card/info')->find($v['card_id']);
  285. if (!$v['info']) {
  286. continue;
  287. }
  288. if (!$v['total_cash']) {
  289. $v['total_cash'] = $v['info']['value'];
  290. }
  291. if (!$v['use_cash']) {
  292. $v['use_cash'] = 0;
  293. }
  294. $v['cash'] = $v['total_cash'] - $v['use_cash'];
  295. if ($v['cash'] <= 0) {
  296. continue;
  297. }
  298. # 验证是否符合当前商品
  299. $card_goods = array();
  300. $check = false;
  301. $xian = false;
  302. if ($this->shop['type'] == 1 && $v['info']['shop_set'] == 1) {
  303. $check = false;
  304. $xian = false;
  305. } else {
  306. $xian = true;
  307. $goods = Dever::db('card/info_goods')->getData(array('card_id' => $v['info']['id']));
  308. if ($goods || $v['info']['category']) {
  309. $check = true;
  310. }
  311. }
  312. $v['minfo'] = '';
  313. $v['cinfo'] = '';
  314. if ($check) {
  315. $v['cinfo'] = '限部分要求商品可用';
  316. $price = 0;
  317. $v['info']['category'] = explode(',', $v['info']['category']);
  318. if ($goods) {
  319. $goods = array_keys($goods);
  320. }
  321. # 检测当前商品是否符合该卡的限制
  322. foreach ($data['list'] as $k1 => $v1) {
  323. if (!isset($v1['cash'])) {
  324. $v1['cash'] = $v1['price'];
  325. }
  326. if ($goods) {
  327. if (in_array($v1['id'], $goods)) {
  328. if ($v['info']['category']) {
  329. if (in_array($v1['top_category_id'], $v['info']['category'])) {
  330. $price += ($v1['cash'] * $v1['num']);
  331. }
  332. } else {
  333. $price += ($v1['cash'] * $v1['num']);
  334. }
  335. }
  336. } elseif ($v['info']['category']) {
  337. if (in_array($v1['top_category_id'], $v['info']['category'])) {
  338. $price += ($v1['cash'] * $v1['num']);
  339. }
  340. }
  341. }
  342. } else {
  343. $price = $data['price'];
  344. }
  345. if ($xian && ($price <= 0 || ($v['info']['min'] > 0 && $price < $v['info']['min']))) {
  346. # 不满足限额 不可用
  347. $no = true;
  348. } else {
  349. # 可用
  350. $no = false;
  351. }
  352. $v['type'] = $type = Dever::db('card/type')->find($v['info']['type_id']);
  353. if ($no) {
  354. $this->data['no_card'][] = $v;
  355. } else {
  356. /*
  357. $type['shop_type'] = explode(',', $type['shop_type']);
  358. $v['minfo'] = array();
  359. foreach ($type['shop_type'] as $tv) {
  360. if (isset($config_type[$tv])) {
  361. $v['minfo'][] = $config_type[$tv];
  362. }
  363. }
  364. $v['minfo'] = implode('、', $v['minfo']) . '可用';
  365. */
  366. if ($type['shop_type'] && strstr($type['shop_type'], ',')) {
  367. $v['minfo'] = '多店铺可用';
  368. } elseif ($type['shop_type'] && isset($config_type[$type['shop_type']])) {
  369. $v['minfo'] = $config_type[$type['shop_type']] . '可用';
  370. }
  371. $type['shop_type'] = explode(',', $type['shop_type']);
  372. if (in_array($shop_type, $type['shop_type'])) {
  373. if ($type['city_type'] == 1) {
  374. $this->data['card'][] = $v;
  375. } else {
  376. $city = $v['city'];
  377. if (!$city) {
  378. $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
  379. if ($card_order && $card_order['city']) {
  380. $city = $card_order['city'];
  381. }
  382. }
  383. if ($city) {
  384. $city_info = Dever::db('area/city')->find($city);
  385. $v['minfo'] = '仅限' . $city_info['name'] . '可用';
  386. if ($city == $cur_city) {
  387. $this->data['card'][] = $v;
  388. } else {
  389. $this->data['no_card'][] = $v;
  390. }
  391. } else {
  392. $this->data['no_card'][] = $v;
  393. }
  394. /*
  395. $type['city'] = explode(',', $type['city']);
  396. if (in_array($city, $type['city'])) {
  397. $this->data['card'][] = $v;
  398. }
  399. */
  400. }
  401. } elseif ($v['shop_id']) {
  402. $shop = Dever::db('shop/info')->find($v['shop_id']);
  403. $v['minfo'] = '限' . $shop['name'] . '可用';
  404. if ($v['shop_id'] == $this->shop_id) {
  405. $this->data['card'][] = $v;
  406. } else {
  407. $this->data['no_card'][] = $v;
  408. }
  409. } else {
  410. $this->data['no_card'][] = $v;
  411. }
  412. }
  413. }
  414. }
  415. $this->data['card_num'] = count($this->data['card']);
  416. $this->data['no_card_num'] = count($this->data['no_card']);
  417. return $this->data;
  418. }
  419. }