Api.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. $this->data['card'][$k]['value'] = Dever::db('card/info')->select($w);
  36. }
  37. }
  38. # 获取选择的城市
  39. $this->data['city'] = Dever::load('shop/lib/info')->getCity();
  40. $config = Dever::db('main/sell_config')->find();
  41. $this->data['card_name'] = $config['card_name'];
  42. return $this->data;
  43. }
  44. # 确认订单页面
  45. public function confirm()
  46. {
  47. $this->data['uid'] = $this->uid;
  48. $this->data['user'] = $this->user;
  49. $this->data['shop'] = $this->shop;
  50. Dever::load('card/lib/buy')->getCard($this->data);
  51. return $this->data;
  52. }
  53. # 开始下单
  54. public function pay_commit()
  55. {
  56. $refer = Dever::input('refer');
  57. $address_id = Dever::input('address_id');
  58. $this->data['uid'] = $this->uid;
  59. $this->data['user'] = $this->user;
  60. $this->data['shop'] = $this->shop;
  61. Dever::load('card/lib/buy')->getCard($this->data);
  62. if (!$address_id) {
  63. Dever::alert('收货地址不能为空');
  64. }
  65. $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);
  66. return $pay;
  67. }
  68. # 再次付款
  69. public function r_pay()
  70. {
  71. $refer = Dever::input('refer');
  72. $order_id = Dever::input('order_id');
  73. $pay = Dever::load('card/lib/buy')->rpay($order_id, 5, $refer);
  74. return $pay;
  75. }
  76. # 查询是否支付
  77. public function c_pay()
  78. {
  79. $refer = Dever::input('refer');
  80. $order_id = Dever::input('order_id');
  81. $pay = Dever::load('card/lib/buy')->cpay($order_id, 5, $refer);
  82. return $pay;
  83. }
  84. # 我的订单列表
  85. public function order()
  86. {
  87. return Dever::load('card/lib/buy')->set(1)->getList($this->uid);
  88. }
  89. # 查看订单详情
  90. public function order_view()
  91. {
  92. $id = Dever::input('order_id');
  93. return Dever::load('card/lib/buy')->set(2)->getView($this->uid, $id);
  94. }
  95. # 取消订单
  96. public function order_cancel_commit()
  97. {
  98. $order_id = Dever::input('order_id');
  99. return Dever::load('card/lib/buy')->set(2)->cancel($this->uid, $order_id);
  100. }
  101. # 兑换
  102. public function dh_card_commit()
  103. {
  104. $card = Dever::input('card');
  105. if (!$card) {
  106. Dever::alert('请输入正确的卡号');
  107. }
  108. $test = Dever::input('test');
  109. if ($test == 1) {
  110. $data = Dever::db('card/code')->find(array('card' => $card));
  111. if (!$data) {
  112. Dever::alert('礼品卡卡号错误或礼品卡已兑换');
  113. }
  114. } else {
  115. $data = Dever::db('card/code')->find(array('card' => $card, 'status' => 1));
  116. if (!$data) {
  117. Dever::alert('礼品卡卡号错误或礼品卡已兑换');
  118. }
  119. $update['where_id'] = $data['id'];
  120. $update['dh_uid'] = $this->uid;
  121. $update['status'] = 3;
  122. $update['ddate'] = time();
  123. $state = Dever::db('card/code')->update($update);
  124. if (!$state) {
  125. Dever::alert('兑换失败,请重新兑换');
  126. }
  127. }
  128. $info = array();
  129. $config = Dever::db('main/sell_config')->find();
  130. $info['card_name'] = $config['card_name'];
  131. $info['info'] = Dever::db('card/info')->find($data['card_id']);
  132. if ($info['info']) {
  133. $info['type'] = Dever::db('card/type')->find($info['info']['type_id']);
  134. }
  135. if (!$data['total_cash']) {
  136. $data['total_cash'] = $info['info']['value'];
  137. }
  138. if (!$data['use_cash']) {
  139. $data['use_cash'] = 0;
  140. }
  141. $data['cash'] = $data['total_cash'] - $data['use_cash'];
  142. $info['card'] = $data;
  143. return $info;
  144. }
  145. # 查看兑换列表
  146. public function card()
  147. {
  148. $config = Dever::db('main/sell_config')->find();
  149. $this->data['card_pic'] = $config['card'];
  150. $this->data['card_name'] = $config['card_name'];
  151. $this->data['card'] = Dever::db('card/code')->getPageData(array('dh_uid' => $this->uid));
  152. $city = Dever::input('city');
  153. if ($city) {
  154. $county = Dever::db('area/county')->find($city);
  155. if ($county) {
  156. $city = $county['city_id'];
  157. }
  158. }
  159. if ($this->data['card']) {
  160. foreach ($this->data['card'] as $k => $v) {
  161. $v['info'] = $this->data['card'][$k]['info'] = Dever::db('card/info')->find($v['card_id']);
  162. if (!$v['total_cash']) {
  163. $v['total_cash'] = $v['info']['value'];
  164. }
  165. if (!$v['use_cash']) {
  166. $v['use_cash'] = 0;
  167. }
  168. $this->data['card'][$k]['cash'] = $v['total_cash'] - $v['use_cash'];
  169. $info = $this->data['card'][$k]['type'] = Dever::db('card/type')->find($this->data['card'][$k]['info']['type_id']);
  170. $this->data['card'][$k]['minfo'] = '';
  171. if ($info['city_type'] == 2) {
  172. $city = $v['city'];
  173. if (!$city) {
  174. $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
  175. if ($card_order && $card_order['city']) {
  176. $city = $card_order['city'];
  177. }
  178. }
  179. if ($city) {
  180. $city = Dever::db('area/city')->find($city);
  181. if ($city) {
  182. $this->data['card'][$k]['minfo'] = '仅限' . $city['name'] . '可用';
  183. }
  184. }
  185. } elseif ($info['type'] == 1) {
  186. $config_type = Dever::db('shop/info')->config['config_type'];
  187. if ($info['shop_type'] && strstr($info['shop_type'], ',')) {
  188. $this->data['card'][$k]['minfo'] = '全部店铺可用';
  189. } elseif ($info['shop_type'] && isset($config_type[$info['shop_type']])) {
  190. $this->data['card'][$k]['minfo'] = $config_type[$info['shop_type']] . '可用';
  191. }
  192. } elseif ($info['type'] == 2 && $v['shop_id']) {
  193. $shop = Dever::db('shop/info')->find($v['shop_id']);
  194. if ($shop) {
  195. $this->data['card'][$k]['minfo'] = '限' . $shop['name'] . '可用';
  196. }
  197. }
  198. }
  199. }
  200. return $this->data;
  201. }
  202. # 查看可用的礼品卡
  203. public function use_card()
  204. {
  205. $config = Dever::db('main/sell_config')->find();
  206. $this->data['card_name'] = $config['card_name'];
  207. $shop_type = $this->shop['type'];
  208. $cur_city = Dever::input('city');
  209. $cur_city = $this->shop['city'];
  210. if ($cur_city) {
  211. $county = Dever::db('area/county')->find($cur_city);
  212. if ($county) {
  213. $cur_city = $county['city_id'];
  214. }
  215. }
  216. $card = Dever::db('card/code')->getData(array('dh_uid' => $this->uid));
  217. $this->data['card'] = array();
  218. if ($card) {
  219. foreach ($card as $k => $v) {
  220. $v['info'] = Dever::db('card/info')->find($v['card_id']);
  221. if (!$v['total_cash']) {
  222. $v['total_cash'] = $v['info']['value'];
  223. }
  224. if (!$v['use_cash']) {
  225. $v['use_cash'] = 0;
  226. }
  227. $v['cash'] = $v['total_cash'] - $v['use_cash'];
  228. if ($v['cash'] <= 0) {
  229. continue;
  230. }
  231. $v['type'] = $type = Dever::db('card/type')->find($v['info']['type_id']);
  232. $type['shop_type'] = explode(',', $type['shop_type']);
  233. if (in_array($shop_type, $type['shop_type'])) {
  234. if ($type['city_type'] == 1) {
  235. $this->data['card'][] = $v;
  236. } else {
  237. $city = $v['city'];
  238. if (!$city) {
  239. $card_order = Dever::db('card/order_card')->find($v['order_card_id']);
  240. if ($card_order && $card_order['city']) {
  241. $city = $card_order['city'];
  242. }
  243. }
  244. if ($city && $city == $cur_city) {
  245. $this->data['card'][] = $v;
  246. }
  247. /*
  248. $type['city'] = explode(',', $type['city']);
  249. if (in_array($city, $type['city'])) {
  250. $this->data['card'][] = $v;
  251. }
  252. */
  253. }
  254. } elseif ($v['shop_id'] && $v['shop_id'] == $this->shop_id) {
  255. $this->data['card'][] = $v;
  256. }
  257. }
  258. }
  259. return $this->data;
  260. }
  261. }