display.ctrl.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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['data'] as $data) {
  55. if($type != 'news') {
  56. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  57. $is_down = 0;
  58. $url = $tag = '';
  59. if($type == 'image') {
  60. if(!empty($media) && !empty($media['attachment'])) {
  61. if(strexists($media['attachment'], 'https://mmbiz.qlogo.cn') || (file_exists(ATTACHMENT_ROOT . $media['attachment']))) {
  62. $has[] = $media['id'];
  63. continue;
  64. }
  65. }
  66. if(strexists($data['url'], 'https://mmbiz.qlogo.cn')) {
  67. $url = $tag = $data['url'];
  68. $is_down = 1;
  69. }
  70. } elseif($types == 'voice') {
  71. if(!empty($media) && !empty($media['attachment'])) {
  72. if(file_exists(ATTACHMENT_ROOT . $media['attachment'])) {
  73. $has[] = $media['id'];
  74. continue;
  75. }
  76. }
  77. }
  78. if(!$is_down) {
  79. $stream = $acc->getMaterial($data['media_id'], $types);
  80. if(is_error($stream)) {
  81. $data['message'] = $stream['message'];
  82. $fail[$data['media_id']] = $data;
  83. continue;
  84. }
  85. if($type == 'image' || $type == 'audio') {
  86. $path = ATTACHMENT_ROOT . "/{$type}s/{$_W['uniacid']}/material";
  87. mkdirs($path);
  88. $is_ok = file_put_contents($path."/{$data['media_id']}", $stream);
  89. if(!$is_ok && !$fail[$data['media_id']]) {
  90. $data['message'] = '保存文件失败,请检查目录权限';
  91. $fail[$data['media_id']] = $data;
  92. }
  93. $tag = '';
  94. $url = "{$type}s/{$_W['uniacid']}/material/{$data['media_id']}";
  95. } elseif($type == 'video') {
  96. $tag = $url = '';
  97. $tag = array(
  98. 'title' => $stream['title'],
  99. 'description' => $stream['description'],
  100. 'down_url' => $stream['down_url'],
  101. );
  102. $tag = iserializer($tag);
  103. }
  104. }
  105. $insert = array(
  106. 'uniacid' => $_W['uniacid'],
  107. 'acid' => $_W['acid'],
  108. 'uid' => $_W['uid'],
  109. 'filename' => $data['name'],
  110. 'attachment' => $url,
  111. 'media_id' => $data['media_id'],
  112. 'type' => $types,
  113. 'model' => 'perm',
  114. 'tag' => $tag,
  115. 'createtime' => $data['update_time']
  116. );
  117. if(empty($media)) {
  118. pdo_insert('wechat_attachment', $insert);
  119. $media['id'] = pdo_insertid();
  120. } else {
  121. pdo_update('wechat_attachment', $insert, array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  122. $media_id = $media['id'];
  123. }
  124. $has[] = $media['id'];
  125. } else {
  126. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  127. if(empty($media)) {
  128. $insert = array(
  129. 'uniacid' => $_W['uniacid'],
  130. 'acid' => $_W['acid'],
  131. 'uid' => $_W['uid'],
  132. 'media_id' => $data['media_id'],
  133. 'type' => $types,
  134. 'model' => 'perm',
  135. 'createtime' => $data['update_time']
  136. );
  137. pdo_insert('wechat_attachment', $insert);
  138. $insert_id = pdo_insertid();
  139. } else {
  140. pdo_update('wechat_attachment', array('createtime' => $data['update_time']), array('uniacid' => $_W['uniacid'], 'media_id' => $data['media_id']));
  141. $insert_id = $media['id'];
  142. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $insert_id));
  143. }
  144. $items = $data['content']['news_item'];
  145. if(!empty($items)) {
  146. foreach($items as $item) {
  147. $item['attach_id'] = $insert_id;
  148. $item['uniacid'] = $_W['uniacid'];
  149. pdo_insert('wechat_news', $item);
  150. }
  151. }
  152. $has[] = $insert_id;
  153. }
  154. }
  155. cache_write($cache_key, $has);
  156. message(error($result['total_count'], array('fail' => $fail, 'item_count' => $result['item_count'])), '', 'ajax');
  157. }
  158. if($do == 'list') {
  159. $type = trim($_GPC['type']) ? trim($_GPC['type']) : 'image';
  160. $condition = " WHERE uniacid = :uniacid AND type = :type AND model = :model AND media_id != ''";
  161. $params = array(':uniacid' => $_W['uniacid'], ':type' => $type, ':model' => 'perm');
  162. $id = intval($_GPC['id']);
  163. if($id > 0) {
  164. $condition .= ' AND id = :id';
  165. $params[':id'] = $id;
  166. }
  167. $pindex = max(1, intval($_GPC['page']));
  168. $psize = 20;
  169. $limit = " ORDER BY id DESC LIMIT " . ($pindex - 1) * $psize . ", {$psize}";
  170. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('wechat_attachment') . $condition, $params);
  171. $lists = pdo_fetchall('SELECT * FROM ' . tablename('wechat_attachment') . $condition . $limit, $params);
  172. if(!empty($lists)) {
  173. foreach($lists as &$row) {
  174. if($type == 'video') {
  175. $row['tag'] = iunserializer($row['tag']);
  176. } elseif($type == 'news') {
  177. $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']));
  178. }
  179. }
  180. }
  181. $pager = pagination($total, $pindex, $psize);
  182. $groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  183. if(!empty($groups)) {
  184. $groups = iunserializer($groups['groups']);
  185. }
  186. }
  187. if($do == 'purview') {
  188. $wxname = trim($_GPC['wxname']);
  189. if(empty($wxname)) {
  190. exit('微信号不能为空');
  191. }
  192. $type = trim($_GPC['type']);
  193. $media_id = trim($_GPC['media_id']);
  194. $acc = WeAccount::create();
  195. $data = $acc->fansSendPreview($wxname, $media_id, $type);
  196. if(is_error($data)) {
  197. exit($data['message']);
  198. }
  199. exit('success');
  200. }
  201. if($do == 'send') {
  202. $group = intval($_GPC['group']);
  203. $type = trim($_GPC['type']);
  204. $id = intval($_GPC['id']);
  205. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  206. if(empty($media)) {
  207. exit('素材不存在或已经删除');
  208. }
  209. $media_id = trim($media['media_id']);
  210. $acc = WeAccount::create();
  211. $data = $acc->fansSendAll($group, $type, $media['media_id']);
  212. if(is_error($data)) {
  213. exit($data['message']);
  214. }
  215. $groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $_W['acid']));
  216. if(!empty($groups)) {
  217. $groups = iunserializer($groups['groups']);
  218. }
  219. $record = array(
  220. 'uniacid' => $_W['uniacid'],
  221. 'acid' => $_W['acid'],
  222. 'groupname' => $groups[$group]['name'],
  223. 'fansnum' => $groups[$group]['count'],
  224. 'msgtype' => $type,
  225. 'group' => $group,
  226. 'attach_id' => $id,
  227. 'status' => 0,
  228. 'type' => 0,
  229. 'sendtime' => TIMESTAMP,
  230. 'createtime' => TIMESTAMP,
  231. );
  232. pdo_insert('mc_mass_record', $record);
  233. exit('success');
  234. }
  235. if($do == 'del') {
  236. $id = intval($_GPC['id']);
  237. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  238. if(empty($media)) {
  239. exit('素材不存在或已经删除');
  240. }
  241. $media_id = trim($media['media_id']);
  242. $acc = WeAccount::create();
  243. $data = $acc->delMaterial($media_id);
  244. if(is_error($data)) {
  245. exit($data['message']);
  246. } else {
  247. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  248. if($type == 'image' || $type == 'voice') {
  249. $path = ATTACHMENT_ROOT . "/{$media['type']}s/{$_W['uniacid']}/material/{$media['$media_id']}";
  250. @unlink($path);
  251. } elseif($type == 'news') {
  252. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $id));
  253. }
  254. exit('success');
  255. }
  256. }
  257. template('material/display');