qr.ctrl.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. load()->model('module');
  8. load()->model('account');
  9. load()->func('communication');
  10. load()->model('setting');
  11. $dos = array('display', 'post', 'list', 'del', 'extend', 'SubDisplay', 'check_scene_str', 'down_qr', 'change_status');
  12. $do = !empty($_GPC['do']) && in_array($do, $dos) ? $do : 'list';
  13. if ($do == 'check_scene_str') {
  14. $scene_str = trim($_GPC['scene_str']);
  15. $is_exist = pdo_fetchcolumn('SELECT id FROM ' . tablename('qrcode') . ' WHERE uniacid = :uniacid AND acid = :acid AND scene_str = :scene_str AND model = 2', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid'], ':scene_str' => $scene_str));
  16. if (!empty($is_exist)) {
  17. iajax(1, 'repeat', '');
  18. }
  19. iajax(0, 'success', '');
  20. }
  21. if ($do == 'list') {
  22. $_W['page']['title'] = '二维码管理 - 高级功能';
  23. $wheresql = " WHERE uniacid = :uniacid AND acid = :acid AND type = 'scene'";
  24. $param = array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']);
  25. $keyword = trim($_GPC['keyword']);
  26. if(!empty($keyword)) {
  27. $wheresql .= " AND name LIKE '%{$keyword}%'";
  28. }
  29. $pindex = max(1, intval($_GPC['page']));
  30. $psize = 20;
  31. $list = pdo_fetchall("SELECT * FROM ".tablename('qrcode'). $wheresql . ' ORDER BY `id` DESC LIMIT '.($pindex - 1) * $psize.','. $psize, $param);
  32. if (!empty($list)) {
  33. foreach ($list as $index => &$qrcode) {
  34. $qrcode['showurl'] = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($qrcode['ticket']);
  35. $qrcode['endtime'] = $qrcode['createtime'] + $qrcode['expire'];
  36. if (TIMESTAMP > $qrcode['endtime']) {
  37. $qrcode['endtime'] = '<font color="red">已过期</font>';
  38. } else {
  39. $qrcode['endtime'] = date('Y-m-d H:i:s',$qrcode['endtime']);
  40. }
  41. if ($qrcode['model'] == 2) {
  42. $qrcode['modellabel']="永久";
  43. $qrcode['expire']="永不";
  44. $qrcode['endtime'] = '<font color="green">永不</font>';
  45. } else {
  46. $qrcode['modellabel']="临时";
  47. }
  48. }
  49. unset($qrcode);
  50. }
  51. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('qrcode') . $wheresql, $param);
  52. $pager = pagination($total, $pindex, $psize);
  53. pdo_query("UPDATE ".tablename('qrcode')." SET status = '0' WHERE uniacid = '{$_W['uniacid']}' AND model = '1' AND createtime < '{$_W['timestamp']}' - expire");
  54. template('platform/qr-list');
  55. }
  56. if ($do == 'del') {
  57. if ($_GPC['scgq']) {
  58. $list = pdo_fetchall("SELECT id FROM ".tablename('qrcode')." WHERE uniacid = :uniacid AND acid = :acid AND status = '0' AND type='scene'", array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']), 'id');
  59. if (!empty($list)) {
  60. pdo_query("DELETE FROM ".tablename('qrcode')." WHERE id IN (".implode(',', array_keys($list)).")");
  61. pdo_query("DELETE FROM ".tablename('qrcode_stat')." WHERE qid IN (".implode(',', array_keys($list)).")");
  62. }
  63. itoast('执行成功<br />删除二维码:'.count($list), url('platform/qr/list'),'success');
  64. } else {
  65. $id = intval($_GPC['id']);
  66. pdo_delete('qrcode', array('id' =>$id, 'uniacid' => $_W['uniacid']));
  67. pdo_delete('qrcode_stat',array('qid' => $id, 'uniacid' => $_W['uniacid']));
  68. itoast('删除成功',url('platform/qr/list'),'success');
  69. }
  70. }
  71. if ($do == 'post') {
  72. $_W['page']['title'] = '生成二维码 - 二维码管理 - 高级功能';
  73. if (checksubmit('submit')){
  74. $barcode = array(
  75. 'expire_seconds' => '',
  76. 'action_name' => '',
  77. 'action_info' => array(
  78. 'scene' => array(),
  79. ),
  80. );
  81. $qrctype = intval($_GPC['qrc-model']);
  82. $acid = intval($_W['acid']);
  83. $uniacccount = WeAccount::create($acid);
  84. $id = intval($_GPC['id']);
  85. $keyword_id = intval(trim(htmlspecialchars_decode($_GPC['reply']['reply_keyword']), "\""));;
  86. $keyword = pdo_get('rule_keyword', array('id' => $keyword_id), array('content'));
  87. if (!empty($id)) {
  88. $update = array(
  89. 'keyword' => $keyword['content'],
  90. 'name' => trim($_GPC['scene-name'])
  91. );
  92. pdo_update('qrcode', $update, array('uniacid' => $_W['uniacid'], 'id' => $id));
  93. itoast('恭喜,更新带参数二维码成功!', url('platform/qr/list'), 'success');
  94. }
  95. if ($qrctype == 1) {
  96. $qrcid = pdo_fetchcolumn("SELECT qrcid FROM ".tablename('qrcode')." WHERE acid = :acid AND model = '1' AND type = 'scene' ORDER BY qrcid DESC LIMIT 1", array(':acid' => $acid));
  97. $barcode['action_info']['scene']['scene_id'] = !empty($qrcid) ? ($qrcid + 1) : 100001;
  98. $barcode['expire_seconds'] = intval($_GPC['expire-seconds']);
  99. $barcode['action_name'] = 'QR_SCENE';
  100. $result = $uniacccount->barCodeCreateDisposable($barcode);
  101. } else if ($qrctype == 2) {
  102. $scene_str = trim($_GPC['scene_str']) ? trim($_GPC['scene_str']) : itoast('场景值不能为空', '', '');
  103. $is_exist = pdo_fetchcolumn('SELECT id FROM ' . tablename('qrcode') . ' WHERE uniacid = :uniacid AND acid = :acid AND scene_str = :scene_str AND model = 2', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid'], ':scene_str' => $scene_str));
  104. if (!empty($is_exist)) {
  105. itoast("场景值:{$scene_str}已经存在,请更换场景值", '', 'error');
  106. }
  107. $barcode['action_info']['scene']['scene_str'] = $scene_str;
  108. $barcode['action_name'] = 'QR_LIMIT_STR_SCENE';
  109. $result = $uniacccount->barCodeCreateFixed($barcode);
  110. } else {
  111. itoast('抱歉,此公众号暂不支持您请求的二维码类型!', '', '');
  112. }
  113. if (!is_error($result)) {
  114. $insert = array(
  115. 'uniacid' => $_W['uniacid'],
  116. 'acid' => $acid,
  117. 'qrcid' => $barcode['action_info']['scene']['scene_id'],
  118. 'scene_str' => $barcode['action_info']['scene']['scene_str'],
  119. 'keyword' => $keyword['content'],
  120. 'name' => $_GPC['scene-name'],
  121. 'model' => $_GPC['qrc-model'],
  122. 'ticket' => $result['ticket'],
  123. 'url' => $result['url'],
  124. 'expire' => $result['expire_seconds'],
  125. 'createtime' => TIMESTAMP,
  126. 'status' => '1',
  127. 'type' => 'scene',
  128. );
  129. pdo_insert('qrcode', $insert);
  130. itoast('恭喜,生成带参数二维码成功!', url('platform/qr/list', array('name' => 'qrcode')), 'success');
  131. } else {
  132. itoast("公众平台返回接口错误. <br />错误代码为: {$result['errorcode']} <br />错误信息为: {$result['message']}", '', '');
  133. }
  134. }
  135. if (!empty($_GPC['id'])) {
  136. $id = intval($_GPC['id']);
  137. $row = pdo_fetch("SELECT * FROM ".tablename('qrcode')." WHERE uniacid = {$_W['uniacid']} AND id = '{$id}'");
  138. $rid = pdo_get('rule_keyword', array('uniacid' => $_W['uniacid'], 'content' => $row['keyword']), array('rid'));
  139. $rid = $rid['rid'];
  140. $setting_keyword = $row['keyword'];
  141. }
  142. template('platform/qr-post');
  143. }
  144. if ($do == 'extend') {
  145. $id = intval($_GPC['id']);
  146. if (!empty($id)) {
  147. $qrcrow = pdo_fetch("SELECT * FROM ".tablename('qrcode')." WHERE uniacid = :uniacid AND id = :id LIMIT 1", array(':uniacid' => $_W['uniacid'], ':id' => $id));
  148. $update = array();
  149. if ($qrcrow['model'] == 1) {
  150. $uniacccount = WeAccount::create($qrcrow['acid']);
  151. $barcode['action_info']['scene']['scene_id'] = $qrcrow['qrcid'];
  152. $barcode['expire_seconds'] = 2592000;
  153. $barcode['action_name'] = 'QR_SCENE';
  154. $result = $uniacccount->barCodeCreateDisposable($barcode);
  155. if (is_error($result)) {
  156. itoast($result['message'], '', 'error');
  157. }
  158. $update['ticket'] = $result['ticket'];
  159. $update['url'] = $result['url'];
  160. $update['expire'] = $result['expire_seconds'];
  161. $update['createtime'] = TIMESTAMP;
  162. pdo_update('qrcode', $update, array('id' => $id, 'uniacid' => $_W['uniacid']));
  163. }
  164. itoast('恭喜,延长临时二维码时间成功!', referer(), 'success');
  165. }
  166. }
  167. if ($do == 'display' || $do == 'change_status') {
  168. $status_setting = setting_load('qr_status');
  169. $status = $status_setting['qr_status']['status'];
  170. }
  171. if ($do == 'display') {
  172. $_W['page']['title'] = '扫描统计 - 二维码管理 - 高级功能';
  173. $pindex = max(1, intval($_GPC['page']));
  174. $psize = 30;
  175. $qrcode_table = table('qrcode');
  176. $starttime = empty($_GPC['time']['start']) ? TIMESTAMP - 86399 * 30 : strtotime($_GPC['time']['start']);
  177. $endtime = empty($_GPC['time']['end']) ? TIMESTAMP + 6*86400 : strtotime($_GPC['time']['end']) + 86399;
  178. $qrcode_table->searchTime($starttime, $endtime);
  179. $keyword = trim($_GPC['keyword']);
  180. if (!empty($keyword)) {
  181. $qrcode_table->searchKeyword($keyword);
  182. }
  183. $qrcode_table->searchWithPage($pindex, $psize);
  184. $list = $qrcode_table->qrcodeStaticList($status);
  185. $total = $count = $qrcode_table->getLastQueryTotal();
  186. if (!empty($list)) {
  187. $openid = array();
  188. foreach ($list as $qrcode) {
  189. if (!in_array($qrcode['openid'], $openid)) {
  190. $openid[] = $qrcode['openid'];
  191. }
  192. }
  193. unset($qrcode);
  194. $fans_table = table('fans');
  195. $nickname = $fans_table->fansAll($openid);
  196. }
  197. $pager = pagination($total, $pindex, $psize);
  198. template('platform/qr-display');
  199. }
  200. if ($do == 'down_qr') {
  201. $id = intval($_GPC['id']);
  202. $down = pdo_get('qrcode', array('id' => $id));
  203. $pic = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($down['ticket']);
  204. header("Cache-control:private");
  205. header('content-type:image/jpeg');
  206. header('content-disposition: attachment;filename="'.$down['name'].'.jpg"');
  207. readfile($pic);
  208. exit();
  209. }
  210. if ($do == 'change_status') {
  211. $up_status['status'] = empty($status) ? 1 : 0;
  212. $update = setting_save($up_status, 'qr_status');
  213. if ($update) {
  214. iajax(0, '');
  215. }
  216. iajax(-1, '更新失败', url('platform/qr/display'));
  217. }