exchange.ctrl.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. $dos = array('display', 'post', 'mine', 'confirm', 'shipping');
  8. $do = in_array($_GPC['do'], $dos) ? $_GPC['do'] : 'display';
  9. checkauth();
  10. load()->model('activity');
  11. load()->model('mc');
  12. $uid = $_W['member']['uid'];
  13. $uniacid = $_W['uniacid'];
  14. $creditnames = array();
  15. $unisettings = uni_setting($uniacid, array('creditnames'));
  16. if (!empty($unisettings) && !empty($unisettings['creditnames'])) {
  17. foreach ($unisettings['creditnames'] as $key=>$credit) {
  18. $creditnames[$key] = $credit['title'];
  19. }
  20. }
  21. $sql = 'SELECT `status` FROM ' . tablename('mc_card') . " WHERE `uniacid` = :uniacid";
  22. $cardstatus = pdo_fetch($sql, array(':uniacid' => $_W['uniacid']));
  23. if($do == 'display') {
  24. $page = intval($_GPC['__input']['page']);
  25. $dtype = intval($_GPC['dtype']);
  26. $pindex = max(1, $page);
  27. $where = ' WHERE uniacid=:uniacid ';
  28. if($dtype) {
  29. $where .= ' AND type = ' . $dtype;
  30. } else {
  31. $where .= " AND type = '1'";
  32. }
  33. $params = array(':uniacid'=>$_W['uniacid']);
  34. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('activity_exchange'). $where , $params);
  35. $mycredits = mc_credit_fetch($uid);
  36. if($_W['isajax']) {
  37. $psize = 10;
  38. $list = pdo_fetchall('SELECT id,title,thumb,type,credittype,endtime,description,credit FROM '.tablename('activity_exchange')." $where ORDER BY id ASC LIMIT ".($pindex - 1) * $psize.','.$psize, $params);
  39. if(!empty($list)) {
  40. foreach($list as &$li) {
  41. $li['credittype_cn'] = activity_type_title($li['type']);
  42. $li['credit_cn'] = $creditnames[$li['credittype']];
  43. $li['url'] = $li['type'] != 3 ? url('activity/exchange/post', array('exid' => $li['id'])) : url('activity/exchange/shipping', array('exid' => $li['id']));
  44. }
  45. $list = json_encode($list);
  46. exit($list);
  47. } else {
  48. exit('dataempty');
  49. }
  50. } else {
  51. $list = pdo_fetchall('SELECT id,title,thumb,type,credittype,endtime,description,credit FROM '.tablename('activity_exchange')." $where ORDER BY id ASC LIMIT 10", $params);
  52. foreach($list as &$li) {
  53. $li['credittype_cn'] = activity_type_title($li['type']);
  54. $li['credit_cn'] = $creditnames[$li['credittype']];
  55. $li['url'] = $li['type'] != 3 ? url('activity/exchange/post', array('exid' => $li['id'])) : url('activity/exchange/shipping', array('exid' => $li['id']));
  56. }
  57. }
  58. foreach ($list as &$value) {
  59. $value['endtime'] = date('Y年m月d日', $value['endtime']);
  60. $value['thumb'] = tomedia($value['thumb']);
  61. $value['description'] = htmlspecialchars_decode($value['description']);
  62. }
  63. $_W['page']['title'] = '积分兑换';
  64. $_W['page']['toolbar']['bottom'] = true;
  65. $_W['page']['toolbar']['jumps'] = array(
  66. array(
  67. 'title' => '礼品兑换',
  68. 'url' => url('activity/exchange/display', array('dtype' => 3)),
  69. 'active' => true
  70. ),
  71. array(
  72. 'title' => '折扣券兑换',
  73. 'url' => url('activity/exchange/display', array('dtype' => 1)),
  74. ),
  75. array(
  76. 'title' => '代金券兑换',
  77. 'url' => url('activity/exchange/display', array('dtype' => 2)),
  78. ),
  79. array(
  80. 'title' => '抽奖机会',
  81. 'url' => url('activity/exchange/display', array('dtype' => 5)),
  82. ),
  83. array(
  84. 'divider' => true
  85. ),
  86. array(
  87. 'title' => '所有兑换',
  88. 'url' => url('activity/exchange/display', array('dtype' => '')),
  89. )
  90. );
  91. template('activity/exchange');
  92. } elseif($do == 'post') {
  93. $exid = intval($_GPC['exid']);
  94. if (!empty($exid)) {
  95. $exchange = activity_exchange_info($exid, $_W['uniacid']);
  96. }
  97. if (empty($exchange)){
  98. message('没有指定的礼品兑换.');
  99. }
  100. $credit = mc_credit_fetch($uid, array($exchange['credittype']));
  101. if ($credit[$exchange['credittype']] < $exchange['credit']) {
  102. message($creditnames[$exchange['credittype']].'数量不够,无法兑换.');
  103. }
  104. $extype = intval($exchange['type']);
  105. if($extype == 1) {
  106. $ret = activity_coupon_grant($uid,$exchange['extra']['id']);
  107. if (is_error($ret)) {
  108. message($ret['message']);
  109. }
  110. } elseif($extype == 2) {
  111. $ret = activity_token_grant($uid,$exchange['extra']['id']);
  112. if (is_error($ret)) {
  113. message($ret['message']);
  114. }
  115. } elseif($extype == 5) {
  116. $ret = activity_module_grant($uid,$exid);
  117. if(is_error($ret)) {
  118. message($ret['message']);
  119. }
  120. }
  121. $trade = array(
  122. 'uniacid'=>$_W['uniacid'],
  123. 'uid'=>$uid,
  124. 'exid'=>$exid,
  125. 'type'=>$exchange['type'],
  126. 'createtime'=>TIMESTAMP,
  127. 'extra'=>iserializer($exchange)
  128. );
  129. $tradetype = array(
  130. '1' => 'coupons',
  131. '2' => 'tokens'
  132. );
  133. pdo_insert('activity_exchange_trades',$trade);
  134. mc_credit_update($uid, $exchange['credittype'], -1 * $exchange['credit'], array($uid, '礼品兑换:' . $exchange['title'] . ' 消耗 ' . $creditnames[$exchange['credittype']] . ':' . $exchange['credit']));
  135. message("兑换成功,您消费了 {$exchange['credit']} {$creditnames[$exchange['credittype']]}",url("mc/bond/{$tradetype[$extype]}", array('type' => $extype)));
  136. } elseif($do == 'shipping') {
  137. load()->func('tpl');
  138. $exid = intval($_GPC['exid']);
  139. if (!empty($exid)) {
  140. $exchange = activity_exchange_info($exid);
  141. }
  142. if (empty($exchange)){
  143. message('没有指定的礼品兑换.');
  144. }
  145. $ret = activity_shipping_grant($uid,$exid);
  146. if(is_error($ret)) {
  147. message($ret['message']);
  148. }
  149. $member = mc_fetch($uid, array('uid','realname',$exchange['credittype'],'resideprovince','residecity','residedist','address','zipcode','mobile'));
  150. if ($member[$exchange['credittype']] < $exchange['credit']) {
  151. message($creditnames[$exchange['credittype']].'数量不够,无法兑换.');
  152. }
  153. $shipping = array(
  154. 'exid'=>$exid,
  155. 'uniacid'=>$uniacid,
  156. 'uid'=>$uid,
  157. 'realname'=>$member['realname'],
  158. 'mobile'=>$member['mobile'],
  159. 'province'=>$member['resideprovince'],
  160. 'city'=>$member['residecity'],
  161. 'district'=>$member['residedist'],
  162. 'address'=>$member['address'],
  163. 'zipcode'=>$member['zipcode'],
  164. );
  165. if(checksubmit('submit')) {
  166. $data = array(
  167. 'uniacid'=>$_W['uniacid'],
  168. 'exid'=>$exid,
  169. 'uid'=>$uid,
  170. 'status'=>0,
  171. 'createtime'=>time(),
  172. 'name'=>$_GPC['realname'],
  173. 'mobile'=>$_GPC['mobile'],
  174. 'province'=>$_GPC['reside']['province'],
  175. 'city'=>$_GPC['reside']['city'],
  176. 'district'=>$_GPC['reside']['district'],
  177. 'address'=>$_GPC['address'],
  178. 'zipcode'=>$_GPC['zipcode'],
  179. );
  180. $trade_log = array('uniacid' => $_W['uniacid'], 'uid' => $uid, 'exid' => $exid, 'type' => 3, 'createtime' => time(), 'extra' => iserializer($exchange));
  181. pdo_insert('activity_exchange_trades', $trade_log);
  182. pdo_insert('activity_exchange_trades_shipping', $data);
  183. pdo_update('activity_exchange', array('num' => $exchange['num'] + 1), array('id' => $exchange['id'], 'uniacid' => $_W['uniacid']));
  184. mc_credit_update($uid, $exchange['credittype'], -1*$exchange['credit'], array($uid, '礼品兑换:' . $exchange['title'] . ' 消耗 ' . $creditnames[$exchange['credittype']] . ':' . $exchange['credit']));
  185. message("兑换成功,您消费了 {$exchange['credit']} {$creditnames[$exchange['credittype']]}",url('activity/exchange/mine', array('type' => 3)));
  186. }
  187. template('activity/shipping');
  188. } elseif($do == 'mine') {
  189. $type = intval($_GPC['type']);
  190. $type = empty($type) ? 1 : $type;
  191. $type = in_array($type, array(1,2,3,4,5)) ? $type : 1;
  192. $page = intval($_GPC['__input']['page']);
  193. switch ($type){
  194. case 1:
  195. case 2:
  196. break;
  197. case 3:
  198. $pindex = max(1, $page);
  199. $psize = 10;
  200. $where = ' WHERE uniacid=:uniacid AND uid=:uid';
  201. $params = array(':uniacid'=>$uniacid,':uid'=>$uid,);
  202. $sql = 'SELECT * FROM '.tablename('activity_exchange_trades_shipping')." $where ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize;
  203. $list = pdo_fetchall($sql, $params);
  204. if(!empty($list)){
  205. foreach ($list as &$row) {
  206. $row['exchange'] = activity_exchange_info($row['exid']);
  207. $row['createtime_cn'] = date('Y-m-d', $row['createtime']);
  208. if($row['status'] == 1) {
  209. $row['status_cn'] = "<a onclick=\"return confirm('确认收货吗?');\" class=\"btn btn-primary\" href=\"" . url('activity/exchange/confirm',array('id'=>$row['id'])) . "\">收货</a>";
  210. } else {
  211. $row['status_cn'] = activity_shipping_status_title($row['status']);
  212. }
  213. }
  214. }
  215. if($_W['isajax'] && $_W['ispost']) {
  216. $list = json_encode($list);
  217. exit($list);
  218. }
  219. break;
  220. case 4:
  221. break;
  222. case 5:
  223. $pindex = max(1, $page);
  224. $psize = 10;
  225. $where = ' WHERE uniacid=:uniacid AND uid=:uid AND available > 0';
  226. $params = array(':uniacid'=>$uniacid,':uid'=>$uid);
  227. $sql = 'SELECT * FROM '.tablename('activity_modules')." $where ORDER BY mid DESC LIMIT ".($pindex - 1) * $psize.','.$psize;
  228. $list = pdo_fetchall($sql, $params);
  229. $total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('activity_modules'). $where , $params);
  230. $pager = pagination($total, $pindex, $psize);
  231. break;
  232. default:
  233. break;
  234. }
  235. template('activity/mine');
  236. } elseif($do == 'confirm') {
  237. $id = intval($_GPC['id']);
  238. $sql = 'SELECT * FROM '.tablename('activity_exchange_trades_shipping').' WHERE uid=:uid AND id=:id AND uniacid=:uniacid ';
  239. $params = array(
  240. ':id'=>$id,
  241. ':uid'=>$uid,
  242. ':uniacid'=>$uniacid
  243. );
  244. $shipping = pdo_fetch($sql, $params);
  245. if (empty($shipping)) {
  246. message('未找到指定的实体礼品,无法确认收货.');
  247. }
  248. if (intval($shipping['status'])!=1) {
  249. message('指定的实体礼品未发货,无法确认收货.');
  250. }
  251. $params = array(
  252. 'uid'=>$uid,
  253. 'id'=>$id,
  254. 'uniacid'=>$_W['uniacid']
  255. );
  256. pdo_update('activity_exchange_trades_shipping',array('status'=>2),$params);
  257. message('确认收货完成.',url('activity/exchange/mine', array('type' => 3)),'success');
  258. }