material.ctrl.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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('material');
  8. load()->model('mc');
  9. load()->model('account');
  10. load()->model('attachment');
  11. load()->func('file');
  12. $dos = array('display', 'sync', 'delete', 'send');
  13. $do = in_array($do, $dos) ? $do : 'display';
  14. $_W['page']['title'] = '永久素材-微信素材';
  15. if ($do == 'send') {
  16. $group = intval($_GPC['group']);
  17. $type = trim($_GPC['type']);
  18. $id = intval($_GPC['id']);
  19. $media = pdo_get('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  20. if (empty($media)) {
  21. iajax(1, '素材不存在', '');
  22. }
  23. $group = $group > 0 ? $group : -1;
  24. $account_api = WeAccount::create();
  25. $result = $account_api->fansSendAll($group, $type, $media['media_id']);
  26. if (is_error($result)) {
  27. iajax(1, $result['message'], '');
  28. }
  29. $groups = pdo_get('mc_fans_groups', array('uniacid' => $_W['uniacid'], 'acid' => $_W['acid']));
  30. if (!empty($groups)) {
  31. $groups = iunserializer($groups['groups']);
  32. }
  33. if ($group == -1) {
  34. $groups = array(
  35. $group => array(
  36. 'name' => '全部粉丝',
  37. 'count' => 0
  38. )
  39. );
  40. }
  41. $record = array(
  42. 'uniacid' => $_W['uniacid'],
  43. 'acid' => $_W['acid'],
  44. 'groupname' => $groups[$group]['name'],
  45. 'fansnum' => $groups[$group]['count'],
  46. 'msgtype' => $type,
  47. 'group' => $group,
  48. 'attach_id' => $id,
  49. 'media_id' => $media['media_id'],
  50. 'status' => 0,
  51. 'type' => 0,
  52. 'sendtime' => TIMESTAMP,
  53. 'createtime' => TIMESTAMP,
  54. );
  55. pdo_insert('mc_mass_record', $record);
  56. iajax(0, '发送成功!', '');
  57. }
  58. if ($do == 'display') {
  59. $type = in_array(trim($_GPC['type']), array('news', 'image', 'voice', 'video')) ? trim($_GPC['type']) : 'news';
  60. $server = in_array(trim($_GPC['server']), array(MATERIAL_LOCAL, MATERIAL_WEXIN)) ? trim($_GPC['server']) : '';
  61. $group = mc_fans_groups(true);
  62. $page_index = max(1, intval($_GPC['page']));
  63. $page_size = 24;
  64. $search = addslashes($_GPC['title']);
  65. if ($type == 'news') {
  66. $material_news_list = material_news_list($server, $search, array('page_index' => $page_index, 'page_size' => $page_size));
  67. } else {
  68. if (empty($server)) {
  69. $server = MATERIAL_WEXIN;
  70. }
  71. $material_news_list = material_list($type, $server, array('page_index' => $page_index, 'page_size' => $page_size));
  72. }
  73. $material_list = $material_news_list['material_list'];
  74. $pager = $material_news_list['page'];
  75. }
  76. if ($do == 'delete') {
  77. if(isset($_GPC['uniacid'])) { $requniacid = intval($_GPC['uniacid']);
  78. attachment_reset_uniacid($requniacid);
  79. }
  80. $material_id = intval($_GPC['material_id']);
  81. $server = $_GPC['server'] == 'local' ? 'local' : 'wechat';
  82. $type = trim($_GPC['type']);
  83. $cron_record = pdo_get('mc_mass_record', array('uniacid' => $_W['uniacid'], 'attach_id' => $material_id), array('id'));
  84. if (!empty($cron_record)) {
  85. iajax('-1', '有群发消息未发送,不可删除');
  86. }
  87. if ($type == 'news'){
  88. $result = material_news_delete($material_id);
  89. } else {
  90. $result = material_delete($material_id, $server);
  91. }
  92. if (is_error($result)){
  93. iajax('-1', $result['message']);
  94. }
  95. iajax('0', '删除素材成功');
  96. }
  97. if ($do == 'sync') {
  98. $account_api = WeAccount::create($_W['acid']);
  99. $pageindex = max(1, $_GPC['pageindex']);
  100. $type = empty($_GPC['type']) ? 'news' : $_GPC['type'];
  101. $news_list = $account_api->batchGetMaterial($type, ($pageindex - 1) * 20);
  102. $wechat_existid = empty($_GPC['wechat_existid']) ? array() : $_GPC['wechat_existid'];
  103. if ($pageindex == 1) {
  104. $original_newsid = pdo_getall('wechat_attachment', array('uniacid' => $_W['uniacid'], 'type' => $type, 'model' => 'perm'), array('id'), 'id');
  105. $original_newsid = array_keys($original_newsid);
  106. $wechat_existid = material_sync($news_list['item'], array(), $type);
  107. if ($news_list['total_count'] > 20) {
  108. $total = ceil($news_list['total_count']/20);
  109. iajax('1', array('type' => $type,'total' => $total, 'pageindex' => $pageindex+1, 'wechat_existid' => $wechat_existid, 'original_newsid' => $original_newsid), '');
  110. }
  111. } else {
  112. $wechat_existid = material_sync($news_list['item'], $wechat_existid, $type);
  113. $total = intval($_GPC['total']);
  114. $original_newsid = $_GPC['original_newsid'];
  115. if ($total != $pageindex) {
  116. iajax('1', array('type' => $type, 'total' => $total, 'pageindex' => $pageindex+1, 'wechat_existid' => $wechat_existid, 'original_newsid' => $original_newsid), '');
  117. }
  118. if (empty($original_newsid)) {
  119. $original_newsid = array();
  120. }
  121. $original_newsid = array_filter($original_newsid, function($item){
  122. return is_int($item);
  123. });
  124. }
  125. $delete_id = array_diff($original_newsid, $wechat_existid);
  126. if (!empty($delete_id) && is_array($delete_id)) {
  127. foreach ($delete_id as $id) {
  128. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  129. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $id));
  130. }
  131. }
  132. iajax(0, '更新成功!', '');
  133. }
  134. template('platform/material');