token.ctrl.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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', 'use');
  8. $do = in_array($_GPC['do'], $dos) ? $_GPC['do'] : 'display';
  9. if($do == 'display') {
  10. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM '. tablename('activity_coupon'). " WHERE uniacid = :uniacid AND type = :type AND endtime > :endtime" , array(':uniacid' => $_W['uniacid'], ':type' => 2, ':endtime' => TIMESTAMP));
  11. $pindex = max(1, intval($_GPC['page']));
  12. $psize = 10;
  13. $lists = pdo_fetchall('SELECT couponid,title,thumb,type,credittype,credit,endtime,description FROM ' . tablename('activity_coupon') . " WHERE uniacid = :uniacid AND type = :type AND endtime > :endtime {$condition} ORDER BY endtime ASC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, array(':uniacid' => $_W['uniacid'], ':type' => 2, ':endtime' => TIMESTAMP));
  14. $pager = pagination($total, $pindex, $psize);
  15. }
  16. if($do == 'post') {
  17. $id = intval($_GPC['id']);
  18. $token = activity_token_info($id, $_W['uniacid']);
  19. if(empty($token)){
  20. message(error(-1, '没有指定的礼品兑换'), '', 'ajax');
  21. }
  22. $credit = mc_credit_fetch($_W['member']['uid'], array($token['credittype']));
  23. if ($credit[$token['credittype']] < $token['credit']) {
  24. message(error(-1, "您的 {$creditnames[$token['credittype']]} 数量不够,无法兑换."), '', 'ajax');
  25. }
  26. $ret = activity_token_grant($_W['member']['uid'], $id, '', '用户使用' . $token['credit'] . $creditnames[$token['credittype']] . '兑换');
  27. if(is_error($ret)) {
  28. message($ret, '', 'ajax');
  29. }
  30. mc_credit_update($_W['member']['uid'], $token['credittype'], -1 * $token['credit'], array($_W['member']['uid'], '礼品兑换:' . $token['title'] . ' 消耗 ' . $creditnames[$token['credittype']] . ':' . $token['credit']));
  31. if($token['credittype'] == 'credit1') {
  32. mc_notice_credit1($_W['openid'], $_W['member']['uid'], -1 * $token['credit'], '兑换代金券消耗积分');
  33. } else {
  34. mc_notice_credit2($_W['openid'], $_W['member']['uid'], -1 * $token['credit'], 0, '线上消费,兑换代金券');
  35. }
  36. message(error(0, "兑换成功,您消费了 {$token['credit']} {$creditnames[$token['credittype']]}"), '', 'ajax');
  37. }
  38. if($do == 'mine') {
  39. $psize = 10;
  40. $pindex = max(1, intval($_GPC['page']));
  41. $params = array(':uid' => $_W['member']['uid']);
  42. $filter['used'] = '1';
  43. $type = 1;
  44. if($_GPC['type'] == 'used') {
  45. $filter['used'] = '2';
  46. $type = 2;
  47. }
  48. $coupon = activity_token_owned($_W['member']['uid'], $filter, $pindex, $psize);
  49. $data = $coupon['data'];
  50. $total = $coupon['total'];
  51. unset($coupon);
  52. $pager = pagination($total , $pindex, $psize);
  53. }
  54. if($do == 'use') {
  55. $id = intval($_GPC['id']);
  56. $recid = intval($_GPC['recid']);
  57. $data = activity_token_owned($_W['member']['uid'], array('couponid' => $id, 'used' => 1));
  58. $data = $data['data'][$id];
  59. if(checksubmit('submit')) {
  60. load()->model('user');
  61. $password = $_GPC['password'];
  62. $sql = 'SELECT * FROM ' . tablename('activity_clerks') . " WHERE `uniacid` = :uniacid AND `password` = :password";
  63. $clerk = pdo_fetch($sql, array(':uniacid' => $_W['uniacid'], ':password' => $password));
  64. if(!empty($clerk)) {
  65. $status = activity_token_use($_W['member']['uid'], $id, $clerk['name'], $clerk['id'], '', 'system', 3, $clerk['storeid']);
  66. if (!is_error($status)) {
  67. message('代金券使用成功!', url('activity/token/mine', array('type' => $_GPC['type'])), 'success');
  68. } else {
  69. message($status['message'], url('activity/token/mine', array('type' => $_GPC['type'])), 'error');
  70. }
  71. }
  72. message('密码错误!', referer(), 'error');
  73. }
  74. }
  75. template('activity/token');