StationMessageAction.class.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class StationMessageAction extends CommonAction{
  10. public function index()
  11. {
  12. parent::index();
  13. }
  14. public function add()
  15. {
  16. $send_user = M("User")->where("is_admin=1")->findAll();
  17. $this->assign ( 'send_user', $send_user );
  18. $this->display();
  19. }
  20. public function insert()
  21. {
  22. B('FilterString');
  23. $ajax = intval($_REQUEST['ajax']);
  24. $data = M(MODULE_NAME)->create();
  25. //开始验证有效性
  26. $this->assign("jumpUrl",u(MODULE_NAME."/add"));
  27. if($data['content']=='')
  28. {
  29. $this->error(l("MESSAGE_CONTENT_EMPTY_TIP"));
  30. }
  31. $account_id = $data['send_user_id'];
  32. if(!$account_id){
  33. $this->error(l("请选择发送人,若没有请到机器人头像列表添加系统管理员!"));
  34. }
  35. $data['send_type'] = intval($data['send_type']);
  36. if($data['send_type']==1)
  37. {
  38. if($data['send_define_data']=='')
  39. {
  40. $this->error(l("SEND_DEFINE_DATE_EMPTY_TIP"));
  41. }
  42. }
  43. $data['send_time'] = NOW_TIME;
  44. $data['send_status'] = 0;
  45. $data['send_define_data'] = strim($data['send_define_data']);
  46. // 更新数据
  47. $log_info = $data['content'];
  48. $message_id = M(MODULE_NAME)->add($data);
  49. if ($message_id) {
  50. fanwe_require(APP_ROOT_PATH.'system/tim/TimApi.php');
  51. $api = createTimAPI();
  52. $content = $data['content'];
  53. $ext = array();
  54. $ext['type'] = 20;
  55. $ext['text'] = $content;
  56. //$ext['desc2'] = $content;
  57. $sender = array();
  58. $user_info = M("User")->where("id=".$account_id)->find();
  59. $sender['user_id'] = $user_info['id'];//发送人昵称
  60. $sender['nick_name'] = $user_info['nick_name'];//发送人昵称
  61. $sender['nick_name'] = emoji_decode($user_info['nick_name']);//发送人昵称
  62. $sender['head_image'] = get_spec_image($user_info['head_image']);//发送人头像
  63. $sender['user_level'] = $user_info['user_level'];//用户等级
  64. $sender['v_icon'] = $user_info['v_icon'];//认证图标
  65. $ext['sender'] = $sender;
  66. $msg_content = array();
  67. //18:直播结束(全体推送的,用于更新用户列表状态)
  68. $to = array();
  69. if($data['send_type']==1){
  70. $to = preg_split("/[\s]+/",$data['send_define_data']);
  71. $condition = array ('id' => array ('in', implode(',',$to)));
  72. $user = M('User')->where($condition)->findAll();
  73. if(count($user)<count($to)){
  74. $this->error ($data['id'].'您填写的账号不存在',$ajax);
  75. }
  76. }else if($data['send_type']==2){
  77. $all = preg_split("/[\s]+/",$data['send_define_data']);
  78. foreach($all as $k=>$v){
  79. $group_id = M("Video")->where("id=".$v)->getField("group_id");
  80. if(!$group_id){
  81. $group_id = M("VideoHistory")->where("id=".$v)->getField("group_id");
  82. }
  83. if($group_id){
  84. $ext['group_id']=$group_id;
  85. $group_info = $api->group_get_group_member_info($group_id,0,0);
  86. if($group_info['MemberList']){
  87. $members = array_column($group_info['MemberList'],"Member_Account");
  88. $to = array_merge($to,$members);
  89. }
  90. //$ret = $api->group_send_group_msg2($account_id,$group_id, $msg_content);
  91. }
  92. }
  93. }else{
  94. }
  95. //创建array 所需元素
  96. $msg_content_elem = array(
  97. 'MsgType' => 'TIMCustomElem', //自定义类型
  98. 'MsgContent' => array(
  99. 'Data' => json_encode($ext),
  100. 'Text' => $content
  101. )
  102. );
  103. array_push($msg_content, $msg_content_elem);
  104. if($data['send_type']==0){
  105. $ret = $api->openim_push($account_id,$msg_content);
  106. }else{
  107. $exceed = 500; //一次最多发送500条
  108. if(sizeof($to)>$exceed){
  109. $num = ceil(sizeof($to)/$exceed);
  110. for($i=0;$i<$num;$i++){
  111. $to_account = array_slice($to,$i*$exceed,$exceed);
  112. $ret = $api->openim_batchsendmsg($account_id, $msg_content,$to_account);
  113. }
  114. }else{
  115. $ret = $api->openim_batchsendmsg($account_id, $msg_content,$to);
  116. }
  117. }
  118. if ($ret['ActionStatus'] == 'FAIL'){
  119. $GLOBALS['db']->query("update ".DB_PREFIX."station_message set send_status = 1,ret_data='".$ret['ErrorCode']."' where id=".$message_id);
  120. }else{
  121. $GLOBALS['db']->query("update ".DB_PREFIX."station_message set send_status = 2 where id=".$message_id);
  122. }
  123. //成功提示
  124. save_log($log_info.L("INSERT_SUCCESS"),1);
  125. $this->success(L("INSERT_SUCCESS"));
  126. } else {
  127. //错误提示
  128. save_log($log_info.L("INSERT_FAILED"),0);
  129. $this->error(L("INSERT_FAILED"));
  130. }
  131. }
  132. public function foreverdelete() {
  133. //彻底删除指定记录
  134. $ajax = intval($_REQUEST['ajax']);
  135. $id = $_REQUEST ['id'];
  136. if (isset ( $id )) {
  137. //$MODULE_NAME='PromoteMsg';
  138. $condition = array ('id' => array ('in', explode ( ',', $id ) ) );
  139. $rel_data = M(MODULE_NAME)->where($condition)->findAll();
  140. foreach($rel_data as $data)
  141. {
  142. $info[] = $data['id'];
  143. }
  144. if($info) $info = implode(",",$info);
  145. $list = M(MODULE_NAME)->where ( $condition )->delete();
  146. if ($list!==false) {
  147. save_log(l("FOREVER_DELETE_SUCCESS"),1);
  148. $this->success (l("FOREVER_DELETE_SUCCESS"),$ajax);
  149. } else {
  150. save_log(l("FOREVER_DELETE_FAILED"),0);
  151. $this->error (l("FOREVER_DELETE_FAILED"),$ajax);
  152. }
  153. } else {
  154. $this->error (l("INVALID_OPERATION"),$ajax);
  155. }
  156. }
  157. public function edit() {
  158. $id = intval($_REQUEST ['id']);
  159. $condition['id'] = $id;
  160. $vo = M(MODULE_NAME)->where($condition)->find();
  161. $this->assign ( 'vo', $vo );
  162. $this->display ();
  163. }
  164. public function update()
  165. {
  166. B('FilterString');
  167. $data = M(MODULE_NAME)->create();
  168. $this->assign("jumpUrl",u(MODULE_NAME."/edit",array("id"=>$data['id'])));
  169. //开始验证
  170. if($data['content']=='')
  171. {
  172. $this->error(l("MESSAGE_CONTENT_EMPTY_TIP"));
  173. }
  174. $data['send_type'] = intval($data['send_type']);
  175. if($data['send_type']==1)
  176. {
  177. if($data['send_define_data']=='')
  178. {
  179. $this->error(l("SEND_DEFINE_DATE_EMPTY_TIP"));
  180. }
  181. }
  182. $rs = M(MODULE_NAME)->save($data);
  183. if($rs)
  184. {
  185. if($data['send_status']!=2){
  186. fanwe_require(APP_ROOT_PATH.'system/tim/TimApi.php');
  187. $api = createTimAPI();
  188. if($data['send_type']==1){
  189. $to = explode(',',$data['send_define_data']);
  190. }else{
  191. $to = $GLOBALS['db']->getAll("select id from ".DB_PREFIX."user where is_robot = 0 and is_effect =1",true,true);
  192. $to = array_column($to,"id");
  193. }
  194. $content = $data['content'];
  195. $ret = $api->openim_batchsendmsg($to,$content);
  196. if ($ret['ActionStatus'] == 'FAIL'){
  197. $GLOBALS['db']->query("update ".DB_PREFIX."station_message set send_status = 1 where id=".$data['id']);
  198. }else{
  199. $GLOBALS['db']->query("update ".DB_PREFIX."station_message set send_status = 2 where id=".$data['id']);
  200. }
  201. }
  202. save_log($data['content'].L("UPDATE_SUCCESS"),1);
  203. $this->success(L("UPDATE_SUCCESS"));
  204. }
  205. else
  206. {
  207. $this->error(L("UPDATE_FAILED"));
  208. }
  209. }
  210. }
  211. ?>