人人商城

material.ctrl.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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'] = '永久素材-' . $_W['account']['type_name'] . '素材';
  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::createByUniacid();
  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 = trim($_GPC['type']);
  60. $type = in_array($type, array('news', 'image', 'voice', 'video')) ? $type : 'news';
  61. permission_check_account_user('platform_material_' . $type);
  62. $server = in_array(trim($_GPC['server']), array(MATERIAL_LOCAL, MATERIAL_WEXIN)) ? trim($_GPC['server']) : '';
  63. $group = mc_fans_groups(true);
  64. $page_index = max(1, intval($_GPC['page']));
  65. $page_size = 24;
  66. $search = addslashes($_GPC['title']);
  67. if ($type == 'news') {
  68. $material_news_list = material_news_list($server, $search, array('page_index' => $page_index, 'page_size' => $page_size));
  69. } else {
  70. if (empty($server)) {
  71. $server = MATERIAL_WEXIN;
  72. }
  73. $material_news_list = material_list($type, $server, array('page_index' => $page_index, 'page_size' => $page_size));
  74. }
  75. $material_list = $material_news_list['material_list'];
  76. $pager = $material_news_list['page'];
  77. }
  78. if ($do == 'delete') {
  79. if(isset($_GPC['uniacid'])) { $requniacid = intval($_GPC['uniacid']);
  80. attachment_reset_uniacid($requniacid);
  81. }
  82. $material_id = intval($_GPC['material_id']);
  83. $server = $_GPC['server'] == 'local' ? 'local' : 'wechat';
  84. $type = trim($_GPC['type']);
  85. $cron_record = pdo_get('mc_mass_record', array('uniacid' => $_W['uniacid'], 'attach_id' => $material_id, 'sendtime >=' => TIMESTAMP), array('id'));
  86. if (!empty($cron_record)) {
  87. iajax('-1', '有群发消息未发送,不可删除');
  88. }
  89. if ($type == 'news'){
  90. $result = material_news_delete($material_id);
  91. } else {
  92. $result = material_delete($material_id, $server);
  93. }
  94. if (is_error($result)){
  95. iajax('-1', $result['message']);
  96. }
  97. iajax('0', '删除素材成功');
  98. }
  99. if ($do == 'sync') {
  100. $account_api = WeAccount::createByUniacid();
  101. $pageindex = max(1, $_GPC['pageindex']);
  102. $type = empty($_GPC['type']) ? 'news' : $_GPC['type'];
  103. $news_list = $account_api->batchGetMaterial($type, ($pageindex - 1) * 20);
  104. $wechat_existid = empty($_GPC['wechat_existid']) ? array() : $_GPC['wechat_existid'];
  105. if ($pageindex == 1) {
  106. $original_newsid = pdo_getall('wechat_attachment', array('uniacid' => $_W['uniacid'], 'type' => $type, 'model' => 'perm'), array('id'), 'id');
  107. $original_newsid = array_keys($original_newsid);
  108. $wechat_existid = material_sync($news_list['item'], array(), $type);
  109. if ($news_list['total_count'] > 20) {
  110. $total = ceil($news_list['total_count']/20);
  111. iajax('1', array('type' => $type,'total' => $total, 'pageindex' => $pageindex+1, 'wechat_existid' => $wechat_existid, 'original_newsid' => $original_newsid), '');
  112. }
  113. } else {
  114. $wechat_existid = material_sync($news_list['item'], $wechat_existid, $type);
  115. $total = intval($_GPC['total']);
  116. $original_newsid = $_GPC['original_newsid'];
  117. if ($total != $pageindex) {
  118. iajax('1', array('type' => $type, 'total' => $total, 'pageindex' => $pageindex+1, 'wechat_existid' => $wechat_existid, 'original_newsid' => $original_newsid), '');
  119. }
  120. if (empty($original_newsid)) {
  121. $original_newsid = array();
  122. }
  123. $original_newsid = array_filter($original_newsid, function($item){
  124. return is_int($item);
  125. });
  126. }
  127. $delete_id = array_diff($original_newsid, $wechat_existid);
  128. if (!empty($delete_id) && is_array($delete_id)) {
  129. foreach ($delete_id as $id) {
  130. pdo_delete('wechat_attachment', array('uniacid' => $_W['uniacid'], 'id' => $id));
  131. pdo_delete('wechat_news', array('uniacid' => $_W['uniacid'], 'attach_id' => $id));
  132. }
  133. }
  134. iajax(0, '更新成功!', '');
  135. }
  136. template('platform/material');