market.ctrl.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. $_W['page']['title'] = '卡券营销';
  8. load()->model('activity');
  9. load()->model('mc');
  10. load()->classs('coupon');
  11. $dos = array('list', 'post', 'delete', 'get_member_num', 'checkcoupon');
  12. $do = in_array($do, $dos) ? $do : 'list';
  13. if ($do == 'checkcoupon') {
  14. $coupon_id = intval($_GPC['coupon']);
  15. $coupon = activity_coupon_info($coupon_id);
  16. $result = $coupon_api->fetchCard($coupon['card_id']);
  17. $type = strtolower($result['card_type']);
  18. if ($result[$type]['base_info']['status'] == 'CARD_STATUS_VERIFY_OK' || empty($coupon_id)) {
  19. message(error(0, '卡券可用'), '', 'ajax');
  20. } else {
  21. message(error(1, $coupon['title']), '', 'ajax');
  22. }
  23. }
  24. if ($do == 'get_member_num') {
  25. $type = trim($_GPC['type']);
  26. $param = $_GPC['param'];
  27. if ($type == 'cash_time') {
  28. $param['start'] = strtotime($param['start']);
  29. $param['end'] = strtotime($param['end']);
  30. }
  31. $members = activity_get_member($type, $param);
  32. message(error(0, $members['total']),'', 'ajax');
  33. }
  34. if($do == 'list') {
  35. $_W['page']['title'] = '卡券活动列表';
  36. $pindex = max(1, intval($_GPC['page']));
  37. $psize = 30;
  38. $condition = '';
  39. if (!empty($_GPC['title'])) {
  40. $condition .= "AND title LIKE '%".$_GPC['title']."%'";
  41. }
  42. $list = pdo_fetchall('SELECT * FROM '. tablename('coupon_activity')." WHERE uniacid = {$_W['uniacid']} AND type = ".COUPON_TYPE." ".$condition." ORDER BY id LIMIT ".($pindex-1)*$psize.",". $psize);
  43. foreach ($list as &$data) {
  44. $data['members'] = empty($data['members']) ? array() : iunserializer($data['members']);
  45. if (!empty($data['members'])) {
  46. if(in_array('group_member', $data['members'])) {
  47. $groups = pdo_getall('mc_groups', array('uniacid' => $_W['uniacid']), array(), 'groupid');
  48. $data['members']['group_name'] = $groups[$data['members']['groupid']]['title'];
  49. }
  50. }
  51. }
  52. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM '. tablename('coupon_activity')." WHERE uniacid = {$_W['uniacid']} AND type = 1 ".$condition);
  53. $pager = pagination($total, $pindex, $psize);
  54. }
  55. elseif ($do == 'post') {
  56. if (checksubmit('submit')) {
  57. if (COUPON_TYPE == SYSTEM_COUPON) {
  58. $post = array(
  59. 'uniacid' => $_W['uniacid'],
  60. 'title' => trim($_GPC['title']),
  61. 'type' => 1,
  62. 'status' => intval($_GPC['status']),
  63. 'thumb' => empty($_GPC['thumb'])? '' : $_GPC['thumb'],
  64. 'coupons' => serialize($_GPC['coupons']),
  65. 'members' => $_GPC['members'],
  66. 'description' => trim($_GPC['description']),
  67. );
  68. if (in_array('group_member', $post['members'])) {
  69. $post['members']['groupid'] = $_GPC['group'];
  70. }
  71. if (in_array('openids', $post['members'])) {
  72. $post['members']['openids'] = json_decode($_COOKIE['fans_openids'.$_W['uniacid']]);
  73. $array = array();
  74. for ($i = 0; $i < count($post['members']['openids']); $i++) {
  75. $array[$i] = '';
  76. }
  77. $post['members']['openids'] = array_diff($post['members']['openids'], $array);
  78. if (empty($post['members']['openids'])) {
  79. message('请选择粉丝', referer(), 'info');
  80. }
  81. }
  82. if (in_array('cash_time', $post['members'])) {
  83. $post['members']['cash_time'] = $_GPC['daterange'];
  84. }
  85. $post['members'] = serialize($post['members']);
  86. if (!empty($id)) {
  87. pdo_update('coupon_activity', $post, array('id' => $id, 'uniacid' => $_W['uniacid']));
  88. message('更新活动成功', url('activity/market'), 'success');
  89. } else {
  90. pdo_insert('coupon_activity', $post);
  91. message('添加活动成功', url('activity/market'), 'success');
  92. }
  93. } else {
  94. $post = array(
  95. 'uniacid' => $_W['uniacid'],
  96. 'title' => trim($_GPC['title']),
  97. 'type' => 2,
  98. 'status' => 0,
  99. 'coupons' => $_GPC['coupons'],
  100. 'members' => $_GPC['members'],
  101. );
  102. if (!empty($_GPC['group'])) {
  103. $post['members']['groupid'] = $_GPC['group'];
  104. }
  105. if (empty($id)) {
  106. $openids = array();
  107. $param = array();
  108. if ($post['members'][0] == 'cash_time') {
  109. $param['start'] = strtotime($_GPC['daterange']['start']);
  110. $param['end'] = strtotime($_GPC['daterange']['end']);
  111. }
  112. if ($post['members'][0] == 'group_member') {
  113. $param['groupid'] = intval($_GPC['groupid']);
  114. }
  115. $openids = activity_get_member($post['members'][0], $param);
  116. $openids = $openids['members'];
  117. $account_api = WeAccount::create();
  118. foreach ($post['coupons'] as $coupon) {
  119. $post['members'] = serialize($post['members']);
  120. $post['coupons'] = serialize($post['coupons']);
  121. foreach ($openids as $openid) {
  122. $result = activity_coupon_grant($coupon, $openid);
  123. $coupon_info = activity_coupon_info($coupon);
  124. $send['touser'] = $openid;
  125. $send['msgtype'] = 'text';
  126. $send['text'] = array('content' => urlencode($_W['account']['name'].'赠送了您一张'.$coupon_info['title'].',请到会员中心查收'));
  127. $data = $account_api->sendCustomNotice($send);
  128. }
  129. $post['msg_id'] = $result['msg_id'];
  130. pdo_insert('coupon_activity', $post);
  131. }
  132. message('卡券发放成功', url('activity/market'), 'success');
  133. }
  134. }
  135. }
  136. $id = intval($_GPC['id']);
  137. $coupons = pdo_getall('coupon', array('uniacid' => $_W['uniacid'], 'source' => COUPON_TYPE));
  138. foreach ($coupons as $key => &$coupon) {
  139. $coupon = activity_coupon_info($coupon['id']);
  140. if (strtotime(date('Y-m-d')) < strtotime(str_replace('.', '-', $coupon['date_info']['time_limit_start'])) || strtotime(date('Y-m-d')) > strtotime(str_replace('.', '-', $coupon['date_info']['time_limit_end']))) {
  141. if ($coupon['date_info']['time_type'] == 1) {
  142. unset($coupons[$key]);
  143. }
  144. }
  145. $coupon['extra'] = iunserializer($coupon['extra']);
  146. }
  147. if (!empty($id)) {
  148. $item = pdo_get('coupon_activity', array('id' => $id, 'uniacid' => $_W['uniacid']));
  149. $item['coupons'] = empty($item['coupons']) ? array() : iunserializer($item['coupons']);
  150. foreach ($item['coupons'] as $key => $couponid) {
  151. $couponid = pdo_get('coupon', array('id' => $couponid));
  152. if (empty($couponid)) {
  153. unset($item['coupons'][$key]);
  154. continue;
  155. }
  156. unset($item['coupons'][$key]);
  157. $item['coupons'][$couponid['id']] = $couponid;
  158. }
  159. $item['members'] = iunserializer($item['members']);
  160. if (COUPON_TYPE == SYSTEM_COUPON) {
  161. if (!empty($item['members']['openids'])) {
  162. setcookie('fans_openids'.$_W['uniacid'], json_encode($item['members']['openids']));
  163. } else {
  164. setcookie('fans_openids'.$_W['uniacid'], '');
  165. }
  166. }
  167. } else {
  168. setcookie('fans_openids'.$_W['uniacid'], '');
  169. }
  170. if (COUPON_TYPE == SYSTEM_COUPON) {
  171. $groups = pdo_getall('mc_groups', array('uniacid' => $_W['uniacid']), array(), 'groupid');
  172. } else {
  173. $groups = mc_fans_groups();
  174. }
  175. }
  176. if ($do == 'delete') {
  177. $id = intval($_GPC['id']);
  178. pdo_delete('coupon_activity', array('id' => $id, 'uniacid' => $_W['uniacid']));
  179. message('删除活动成功', url('activity/market'), 'success');
  180. }
  181. template('activity/market');