人人商城

qr.ctrl.php 9.2KB

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