人人商城

display.ctrl.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. uni_user_permission_check('material_mass');
  8. $_W['page']['title'] = '永久素材-微信素材';
  9. $dos = array('image', 'del', 'export', 'news', 'down', 'list', 'purview', 'modal', 'send');
  10. $do = in_array($do, $dos) ? $do : 'list';
  11. if($do == 'down') {
  12. set_time_limit(0);
  13. load()->func('file');
  14. $acc = WeAccount::create();
  15. if(is_error($acc)) {
  16. message($acc, '', 'ajax');
  17. }
  18. $post = $_GPC['__input'];
  19. $type = $types = $post['type'];
  20. $count = $acc->getMaterialCount();
  21. if(is_error($count)) {
  22. message($count, '', 'ajax');
  23. }
  24. $cache_key = "media_sync:{$_W['uniacid']}";
  25. $has = cache_read($cache_key);
  26. if(!is_array($has)) {
  27. $has = array(0);
  28. }
  29. $key = $type . '_count';
  30. $total = $count[$key];
  31. $pindex = max(1, intval($post['page']));
  32. $psize = 15;
  33. $offset = ($pindex - 1) * $psize;
  34. if($total == 0 || ($pindex > ceil($total / $psize))) {
  35. $has_str = implode(',', $has);
  36. pdo_query('DELETE FROM ' . tablename('wechat_attachment') . " WHERE uniacid = :uniacid AND type = :type AND id NOT IN ({$has_str})", array('uniacid' => $_W['uniacid'], ':type' => $type));
  37. if($type == 'news') {
  38. pdo_query('DELETE FROM ' . tablename('wechat_news') . " WHERE uniacid = :uniacid AND attach_id NOT IN ({$has_str})", array('uniacid' => $_W['uniacid']));
  39. }
  40. cache_delete($cache_key);
  41. message(error(-2, $total), '', 'ajax');
  42. }
  43. if($pindex == ceil($total / $psize)) {
  44. $psize = $total % $psize == 0 ? 15 : ($total % $psize);
  45. }
  46. $result = $acc->batchGetMaterial($type, $offset, $psize);
  47. if(is_error($result)) {
  48. message($result, '', 'ajax');
  49. }
  50. $fail = array();
  51. if($type == 'voice') {
  52. $type = 'audio';
  53. }
  54. foreach($result['item'] as $data) {
  55. $pathinfo = pathinfo($data['name']);
  56. $data['name'] = $pathinfo['filename'] . '.' . $pathinfo['extension'];
  57. if($type != 'news') {
  58. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  59. $is_down = 0;
  60. $url = $tag = '';
  61. if($type == 'image') {
  62. if(!empty($media) && !empty($media['attachment'])) {
  63. if(strexists($media['attachment'], '//mmbiz.qlogo.cn') || (file_exists(ATTACHMENT_ROOT . $media['attachment']))) {
  64. $has[] = $media['id'];
  65. continue;
  66. }
  67. }
  68. if(strexists($data['url'], '//mmbiz.qlogo.cn')) {
  69. $url = $tag = $data['url'];
  70. $is_down = 1;
  71. }
  72. } elseif($types == 'voice') {
  73. if(!empty($media) && !empty($media['attachment'])) {
  74. if(file_exists(ATTACHMENT_ROOT . $media['attachment'])) {
  75. $has[] = $media['id'];
  76. continue;
  77. }
  78. }
  79. }
  80. if(!$is_down) {
  81. $stream = $acc->getMaterial($data['media_id']);
  82. if(is_error($stream)) {
  83. $data['message'] = $stream['message'];
  84. $fail[$data['media_id']] = $data;
  85. continue;
  86. }
  87. }
  88. $insert = array(
  89. 'uniacid' => $_W['uniacid'],
  90. 'acid' => $_W['acid'],
  91. 'uid' => $_W['uid'],
  92. 'filename' => $data['name'],
  93. 'attachment' => $stream,
  94. 'media_id' => $data['media_id'],
  95. 'type' => $types,
  96. 'model' => 'perm',
  97. 'tag' => $tag,
  98. 'createtime' => $data['update_time']
  99. );
  100. if(empty($media)) {
  101. pdo_insert('wechat_attachment', $insert);
  102. $media['id'] = pdo_insertid();
  103. } else {
  104. pdo_update('wechat_attachment', $insert, array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  105. $media_id = $media['id'];
  106. }
  107. $has[] = $media['id'];
  108. } else {
  109. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  110. if(empty($media)) {
  111. $insert = array(
  112. 'uniacid' => $_W['uniacid'],
  113. 'acid' => $_W['acid'],
  114. 'uid' => $_W['uid'],
  115. 'media_id' => $data['media_id'],
  116. 'type' => $types,
  117. 'model' => 'perm',
  118. 'createtime' => $data['update_time']
  119. );
  120. pdo_insert('wechat_attachment', $insert);
  121. $insert_id = pdo_insertid();
  122. } else {
  123. pdo_update('wechat_attachment', array('createtime' => $data['update_time']), array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  124. $insert_id = $media['id'];
  125. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $insert_id));
  126. }
  127. $items = $data['content']['news_item'];
  128. if(!empty($items)) {
  129. foreach($items as $item) {
  130. $item['attach_id'] = $insert_id;
  131. $item['uniacid'] = $_W['uniacid'];
  132. pdo_insert('wechat_news', $item);
  133. }
  134. }
  135. $has[] = $insert_id;
  136. }
  137. }
  138. cache_write($cache_key, $has);
  139. message(error($result['total_count'], array('fail' => $fail, 'item_count' => $result['item_count'])), '', 'ajax');
  140. }
  141. if($do == 'list') {
  142. $type = trim($_GPC['type']) ? trim($_GPC['type']) : 'image';
  143. $condition = " WHERE uniacid = :uniacid AND type = :type AND model = :model AND media_id != ''";
  144. $params = array(':uniacid' => $_W['uniacid'], ':type' => $type, ':model' => 'perm');
  145. $id = intval($_GPC['id']);
  146. if($id > 0) {
  147. $condition .= ' AND id = :id';
  148. $params[':id'] = $id;
  149. }
  150. $pindex = max(1, intval($_GPC['page']));
  151. $psize = 20;
  152. $limit = " ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ", {$psize}";
  153. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('wechat_attachment') . $condition, $params);
  154. $lists = pdo_fetchall('SELECT * FROM ' . tablename('wechat_attachment') . $condition . $limit, $params);
  155. if(!empty($lists)) {
  156. foreach($lists as &$row) {
  157. if($type == 'video') {
  158. $row['tag'] = iunserializer($row['tag']);
  159. } elseif($type == 'news') {
  160. $row['items'] = pdo_fetchall("SELECT * FROM " . tablename('wechat_news') . " WHERE uniacid = :uniacid AND attach_id = :attach_id ORDER BY id ASC", array(':uniacid' => $_W['uniacid'], ':attach_id' => $row['id']));
  161. }
  162. }
  163. }
  164. $pager = pagination($total, $pindex, $psize);
  165. $groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  166. if(!empty($groups)) {
  167. $groups = iunserializer($groups['groups']);
  168. }
  169. }
  170. if($do == 'purview') {
  171. $wxname = trim($_GPC['wxname']);
  172. if(empty($wxname)) {
  173. exit('微信号不能为空');
  174. }
  175. $type = trim($_GPC['type']);
  176. $media_id = trim($_GPC['media_id']);
  177. $acc = WeAccount::create();
  178. $data = $acc->fansSendPreview($wxname, $media_id, $type);
  179. if(is_error($data)) {
  180. exit($data['message']);
  181. }
  182. exit('success');
  183. }
  184. if($do == 'send') {
  185. $group = intval($_GPC['group']);
  186. $type = trim($_GPC['type']);
  187. $id = intval($_GPC['id']);
  188. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  189. if(empty($media)) {
  190. exit('素材不存在或已经删除');
  191. }
  192. $media_id = trim($media['media_id']);
  193. $acc = WeAccount::create();
  194. $data = $acc->fansSendAll($group, $type, $media['media_id']);
  195. if(is_error($data)) {
  196. exit($data['message']);
  197. }
  198. $groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  199. if(!empty($groups)) {
  200. $groups = iunserializer($groups['groups']);
  201. }
  202. $record = array(
  203. 'uniacid' => $_W['uniacid'],
  204. 'acid' => $_W['acid'],
  205. 'groupname' => $groups[$group]['name'],
  206. 'fansnum' => $groups[$group]['count'],
  207. 'msgtype' => $type,
  208. 'group' => $group,
  209. 'attach_id' => $id,
  210. 'status' => 0,
  211. 'type' => 0,
  212. 'sendtime' => TIMESTAMP,
  213. 'createtime' => TIMESTAMP,
  214. );
  215. pdo_insert('mc_mass_record', $record);
  216. exit('success');
  217. }
  218. if($do == 'del') {
  219. $id = intval($_GPC['id']);
  220. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  221. if(empty($media)) {
  222. exit('素材不存在或已经删除');
  223. }
  224. $media_id = trim($media['media_id']);
  225. $acc = WeAccount::create();
  226. $data = $acc->delMaterial($media_id);
  227. if(is_error($data)) {
  228. exit($data['message']);
  229. } else {
  230. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  231. if($type == 'image' || $type == 'voice') {
  232. $path = ATTACHMENT_ROOT . "/{$media['type']}s/{$_W['uniacid']}/material/{$media['$media_id']}";
  233. @unlink($path);
  234. } elseif($type == 'news') {
  235. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $id));
  236. }
  237. exit('success');
  238. }
  239. }
  240. template('material/display');