reimModel.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  1. <?php
  2. class reimClassModel extends Model
  3. {
  4. private $groupname = '';
  5. public $serverrecid = 'rockreim';
  6. public $serverpushurl = '';
  7. public $serverhosturl = '';
  8. public $servertitle = '';
  9. public function initModel()
  10. {
  11. $this->settable('im_mess');
  12. $this->hisobj = m('im_history');
  13. $this->option = m('option');
  14. $this->inithost();
  15. }
  16. private function inithost()
  17. {
  18. if($this->serverpushurl!='')return;
  19. $dbs = $this->option;
  20. $this->optiondb = $dbs;
  21. $this->serverrecid = $dbs->getval('reimrecidsystem','rockxinhu');
  22. $this->serverpushurl = $dbs->getval('reimpushurlsystem');
  23. $this->serverhosturl = $dbs->getval('reimhostsystem');
  24. $this->servertitle = $dbs->getval('reimtitlesystem');
  25. $this->serverpushurl = getconfig('reim_push', $this->serverpushurl);
  26. $this->serverhosturl = getconfig('reim_host', $this->serverhosturl);
  27. if($this->isempt($this->servertitle))$this->servertitle='信呼';
  28. }
  29. public function isanwx()
  30. {
  31. $bo = false;
  32. return $bo;
  33. }
  34. /**
  35. * 返回判断是否有安装微信企业号/企业微信 $lx=0企业号,1企业微信,2钉钉,3微信公众号号,4是否微信模版消息
  36. */
  37. public function installwx($lx=0)
  38. {
  39. if($lx==0)return $this->isanwx();
  40. $bo = false;
  41. if($lx==1){
  42. if(!isempt($this->optiondb->getval('weixinqy_corpid')))$bo=true;
  43. return $bo;
  44. }
  45. if($lx==2){
  46. if(!isempt($this->optiondb->getval('dingding_token0')))$bo=true;
  47. return $bo;
  48. }
  49. if($lx==3){
  50. if(!isempt($this->optiondb->getval('wxgzh_appid')))$bo=true;
  51. return $bo;
  52. }
  53. if($lx==4){
  54. if($this->optiondb->getval('wxgzh_tplmess')=='1')$bo=true;
  55. return $bo;
  56. }
  57. if($lx==5){
  58. if(!isempt($this->optiondb->getval('reimplat_cnum')))$bo=true;
  59. return $bo;
  60. }
  61. return false;
  62. }
  63. public function getreims()
  64. {
  65. $this->inithost();
  66. $chehui = (int)$this->optiondb->getval('reimchehuisystem',0);
  67. if($chehui<0)$chehui = 0;
  68. return array(
  69. 'recid' => $this->serverrecid,
  70. 'title' => $this->servertitle,
  71. 'chehui' => $chehui * 60,
  72. 'wsurl' => $this->rock->jm->base64encode($this->serverhosturl)
  73. );
  74. }
  75. private function getgroupid($gname)
  76. {
  77. $agesta = explode(',', $gname);
  78. $name = $agesta[0];
  79. $sid = (int)$this->db->getmou('[Q]im_group','id', "`name`='$name' and `type`=2");
  80. if($sid==0 && count($agesta)>1)$sid = $this->getgroupid($agesta[1]);
  81. $this->groupname = $name;
  82. return $sid;
  83. }
  84. /**
  85. * REIM推送的
  86. */
  87. public function sendsystem($sendid, $receid, $gname, $cont, $table='',$mid='', $url='')
  88. {
  89. $gid = $this->getgroupid($gname);
  90. $gname = $this->groupname;
  91. if($gid==0)return false;
  92. if($this->isempt($receid))return 'not receuid';
  93. $receids = $receid;
  94. $wheres = " and `id` in($receid)";
  95. if($receid=='all')$wheres='';
  96. $allsid = '';
  97. $recrarr = $this->db->getall("select id from [Q]admin where `status`=1 $wheres");
  98. foreach($recrarr as $k=>$rs){
  99. $allsid.=','.$rs['id'].'';
  100. }
  101. $messid = 0;
  102. if($allsid != ''){
  103. $allsid = substr($allsid, 1);
  104. $this->insert(array(
  105. 'type' => 'agent',
  106. 'optdt' => $this->rock->now,
  107. 'zt' => 0,
  108. 'cont' => $this->rock->jm->base64encode($cont),
  109. 'sendid'=> $sendid,
  110. 'receid'=> $gid,
  111. 'optid' => $sendid,
  112. 'receuid' => $allsid,
  113. 'table' => $table,
  114. 'mid' => $mid,
  115. 'url' => $url
  116. ));
  117. $messid = $this->db->insert_id();
  118. $this->db->insert('[Q]im_messzt','`mid`,`uid`,`gid`','select '.$messid.',id,'.$gid.' from `[Q]admin` where id in('.$allsid.') and `status`=1 ', true);
  119. }
  120. $resid = $receids;
  121. if($resid!='all')$resid = m('admin')->getonline($resid);
  122. if($resid!='' && $messid>0)$this->sendpush($sendid, $resid, array(
  123. 'agent' => $gname,
  124. 'optdt' => $this->rock->now,
  125. 'type' => 'agent',
  126. 'messid' => $messid,
  127. 'agentid' => $gid,
  128. 'cont' => $this->rock->jm->base64encode($cont),
  129. 'table' => $table,
  130. 'mid' => $mid,
  131. 'url' => $url
  132. ));
  133. //if($messid>0)$this->addhistory('agent', $gid, $allsid);
  134. return true;
  135. }
  136. /**
  137. * 应用信息推送
  138. * $slx 0,1发送给pc,0,2发送给移动端,3不发送
  139. * $xgurl 相关地址,一般是单据详情:模块编号|id
  140. */
  141. public function pushagent($receid, $gname, $cont, $title='', $url='', $wxurl='', $slx=0, $xgurl='')
  142. {
  143. if($slx==3 || isempt($receid))return false;
  144. $cont = str_replace(array("\n",'\n','<br>'),' ', $cont);
  145. $gid = $this->getgroupid($gname);
  146. $grs = $this->getgroupxinxi($gid);
  147. $gname = $this->groupname;
  148. $admdb = m('admin');
  149. $sarr = array(
  150. 'gname' => $gname,
  151. 'optdt' => $this->rock->now,
  152. 'type' => 'agent',
  153. 'pushtype' => 'agent',
  154. 'title' => $title,
  155. 'gface' => arrvalue($grs,'face'),
  156. 'gid' => $gid,
  157. 'cont' => $this->rock->jm->base64encode($cont),
  158. 'url' => $url
  159. );
  160. if($title=='')$title = $gname;
  161. //保存到推送会话列表上历史记录上
  162. if($gid>0){
  163. $receids = $admdb->gjoins($receid);
  164. if($receids!='all' &&
  165. !isempt($receids)
  166. )$this->addhistory($sarr['type'], $gid, $receids, $sarr['optdt'], $sarr['cont'], $this->adminid, $title, $xgurl);
  167. }
  168. $resid = $receid;
  169. if($slx == 0 || $slx==1){
  170. if($resid != 'all')$resid = $admdb->getonline($resid);
  171. if($resid != '')$this->sendpush($this->adminid, $resid, $sarr);//PC端
  172. }
  173. //推送到APP上
  174. if($slx == 0 || $slx==2){
  175. if($wxurl!='')$sarr['url'] = $wxurl;
  176. $this->pushapp($receid, $title, $sarr, $slx);
  177. }
  178. }
  179. //获取REIM未读的
  180. public function getwdarr($mid=0, $ldt='')
  181. {
  182. $rows = array();
  183. if($mid==0)$mid = $this->adminid;
  184. $whes = $this->rock->dbinstr('receuid', $mid);
  185. $wher = '';
  186. if(!$this->isempt($ldt))$wher=" and `optdt`>='$ldt' ";
  187. $arr = $this->getall("`zt`=0 and receid='$mid' and `type`='user' $wher group by `sendid`", "`sendid`,count(1) as stotal,max(optdt) as optdts,cont");
  188. foreach($arr as $k=>$rs){
  189. $uid = $rs['sendid'];
  190. $urs = $this->db->getone('[Q]admin',"`id`='$uid'",'`name`,`face`');
  191. if($urs){
  192. $rows[] = array(
  193. 'type' => 'user',
  194. 'id' => $uid,
  195. 'stotal'=> $rs['stotal'],
  196. 'optdt' => $rs['optdts'],
  197. 'name' => $urs['name'],
  198. 'cont' => $rs['cont'],
  199. 'face' => $this->getface($urs['face'])
  200. );
  201. }
  202. }
  203. // 讨论组 群
  204. $groupa = $this->db->getarr('[Q]im_group','1=1','`name`,`face`,`type`');
  205. $gid = '0';
  206. foreach($groupa as $_gid=>$kvs)$gid.=','.$_gid.'';
  207. $arr = $this->getall("`type`='group' and `receid` in($gid) and $whes $wher and id in(select mid from [Q]im_messzt where uid='$mid') group by `receid`", "`receid`,count(1) as stotal,max(optdt) as optdts,cont");
  208. $typea = array('group','group');
  209. foreach($arr as $k=>$rs){
  210. $grs = $groupa[$rs['receid']];
  211. $typ = $typea[$grs['type']];
  212. $rows[] = array(
  213. 'type' => 'group',
  214. 'id' => $rs['receid'],
  215. 'stotal'=> $rs['stotal'],
  216. 'optdt' => $rs['optdts'],
  217. 'name' => $grs['name'],
  218. 'cont' => $rs['cont'],
  219. 'face' => $this->getface($grs['face'],'images/'.$typ.'.png')
  220. );
  221. }
  222. //应用的信息
  223. $arr = $this->getall("`type`='agent' and `receid` in($gid) and $whes $wher and id in(select mid from [Q]im_messzt where uid='$mid') group by `receid`", "`receid`,count(1) as stotal,max(optdt) as optdts,cont");
  224. foreach($arr as $k=>$rs){
  225. $grs = $groupa[$rs['receid']];
  226. $rows[] = array(
  227. 'type' => 'agent',
  228. 'id' => $rs['receid'],
  229. 'stotal'=> $rs['stotal'],
  230. 'optdt' => $rs['optdts'],
  231. 'cont' => $rs['cont'],
  232. 'name' => $grs['name'],
  233. 'face' => $this->getface($grs['face'])
  234. );
  235. }
  236. return $rows;
  237. }
  238. public function getweitotal($uid, $type, $sid=0, $blx=0)
  239. {
  240. $whes = $this->rock->dbinstr('receuid', $uid);
  241. $where = "`type`='$type' and `receid` ='$sid' and $whes and id in(select mid from [Q]im_messzt where uid='$uid')";
  242. if($type == 'user'){
  243. $where = "`zt`=0 and `receid`='$uid' and `type`='user' and $whes";
  244. }
  245. if($blx==1)return $where;
  246. $to = $this->rows($where);
  247. return $to;
  248. }
  249. /**
  250. * 获取未读的会话消息数量
  251. */
  252. public function getreimwd($uid)
  253. {
  254. $to = $this->db->getmou('[Q]im_history','sum(stotal)','uid='.$uid.'');
  255. if(isempt($to))$to = '0';
  256. return $to;
  257. }
  258. /**
  259. * 获取人员所在的会话上
  260. */
  261. public function getgroup($uid)
  262. {
  263. $ids = '0';
  264. $idrsa = m('im_groupuser')->getall("uid='$uid'",'gid');
  265. foreach($idrsa as $k=>$rs){
  266. $ids.=','.$rs['gid'];
  267. }
  268. $sql = "select a.gid,count(1)utotal from `[Q]im_groupuser` a left join `[Q]admin` b on a.uid=b.id where a.gid in($ids) and b.status=1 group by a.gid";
  269. $urows = $this->db->getall($sql);
  270. $ugarr = array();
  271. foreach($urows as $k=>$rs)$ugarr[$rs['gid']] = $rs['utotal'];
  272. $rows = m('im_group')->getall("`id`>0 and ((`type` in(0,1) and `id` in($ids) ) ) order by `type`,`sort` ",'`id`,`type`,`name`,`face`,`sort`,`deptid`');
  273. $facarr = array('images/group.png','images/group.png','images/system.png');
  274. foreach($rows as $k=>$rs){
  275. $rows[$k]['face'] = $this->getface($rs['face'], $facarr[$rs['type']]);
  276. $rows[$k]['utotal'] = arrvalue($ugarr,$rs['id'], '0');
  277. }
  278. return $rows;
  279. }
  280. public function getgroupuser($gid, $type)
  281. {
  282. $sql = "select b.id,b.name,b.face from `[Q]im_groupuser` a left join `[Q]admin` b on a.uid=b.id where a.gid='$gid' and b.status=1";
  283. if($type=='user')$sql = "select id,name,face from `[Q]admin` where id in(".$gid.",".$this->adminid.")";
  284. $rows = $this->db->getall($sql);
  285. foreach($rows as $k=>$rs){
  286. $rows[$k]['face'] = $this->getface($rs['face']);
  287. }
  288. $arr['uarr'] = $rows;
  289. if($type=='user'){
  290. $arr['infor'] = array();
  291. }else{
  292. $arr['infor'] = $this->getgroupxinxi($gid);
  293. }
  294. return $arr;
  295. }
  296. public function getgroupxinxi($gid)
  297. {
  298. $rs = m('im_group')->getone($gid,'`id`,`type`,`name`,`face`,`deptid`,`createname`,`createid`');
  299. $facarr = array('images/group.png','images/group.png','images/todo.png');
  300. if(!$rs){
  301. $rs = array(
  302. 'face' => '',
  303. 'type' => 2,
  304. 'name' => '',
  305. 'id' => $gid,
  306. );
  307. }
  308. $rs['face'] = $this->getface($rs['face'], $facarr[$rs['type']]);
  309. $rs['utotal'] = $this->db->rows('[Q]im_groupuser','gid='.$gid.'');
  310. $rs['innei'] = $this->db->rows('[Q]im_groupuser','gid='.$gid.' and uid='.$this->adminid.''); //是否在会话中
  311. return $rs;
  312. }
  313. private function getface($face, $mr='')
  314. {
  315. if($mr=='')$mr = 'images/noface.png';
  316. $url = URL;
  317. if(!$url)$url = getconfig('outurl');
  318. if(substr($mr,0,4)!='http')$mr = $url.''.$mr.'';
  319. if(substr($face,0,4)!='http' && !$this->isempt($face))$face = $url.''.$face.'';
  320. $face = $this->rock->repempt($face, $mr);
  321. return $face;
  322. }
  323. /**
  324. 设置已读
  325. */
  326. public function setyd($ids, $receid)
  327. {
  328. $this->update("`zt`=1", "`id` in($ids) and receid='$receid' and `type` ='user' ");
  329. m('im_messzt')->delete("uid='$receid' and `mid` in($ids)");
  330. }
  331. public function setallyd($type,$uid, $gid)
  332. {
  333. if($type=='user'){
  334. $this->update("`zt`=1", "`sendid` ='$gid' and receid='$uid' and `type`='user'");
  335. }else{
  336. m('im_messzt')->delete("uid='$uid' and `gid`=$gid");
  337. }
  338. $this->hisobj->update('stotal=0', "`type`='$type' and `uid`='$uid' and `receid`='$gid'");
  339. }
  340. /**
  341. * 桌面版PC客户端获取应用
  342. */
  343. public function getagent($uid=0, $whe='', $pid=0)
  344. {
  345. if($uid==0)$uid = $this->adminid;
  346. $yylx = '2';
  347. if($this->rock->get('cfrom')=='reim')$yylx='1';
  348. $dboaj = m('admin');
  349. $where = $dboaj->getjoinstr('receid', $this->adminid);
  350. $rows = $this->db->getrows('[Q]im_group',"`valid`=1 and `type`=2 and `yylx` in(0,".$yylx.") $where $whe",'`id`,`name`,`url`,`face`,`num`,`pid`,`iconfont`,`iconcolor`,`types`,`urlpc`,`urlm`','`sort`');
  351. $dbs = m('im_menu');
  352. $mdbs = m('menu');
  353. $barr = $carr = array();
  354. $mids = '0';
  355. foreach($rows as $k=>$rs)$mids.=','.$rs['id'].'';
  356. $allmenu = $cmenu = array();
  357. $allmenua = $dbs->getall("`mid` in($mids)",'`pid`,`mid`,`id`,`name`,`type`,`url`,`num`,`color`,`receid`','`sort`');
  358. foreach($allmenua as $k1=>$rs1){
  359. if(isempt($rs1['receid'])){
  360. $allmenu[] = $rs1;
  361. }else{
  362. $bo = $dboaj->containjoin($rs1['receid'], $uid);
  363. if($bo)$allmenu[] = $rs1;
  364. }
  365. }
  366. foreach($allmenu as $k=>$rs){
  367. if($rs['pid']=='0'){
  368. $submenu = array();
  369. foreach($allmenu as $k1=>$rs1){
  370. if($rs1['pid']==$rs['id'])$submenu[] = $rs1;
  371. }
  372. $rs['submenu'] = $submenu;
  373. $cmenu[$rs['mid']][] = $rs;
  374. }
  375. }
  376. foreach($rows as $k=>$rs){
  377. if(isempt($rs['num']))continue;
  378. $rs['face'] = $this->getface($rs['face']);
  379. $stotal = 0;
  380. $btosr = m('agent:'.$rs['num'].'')->gettotal();
  381. $stotal = $btosr['stotal'];
  382. $rs['titles'] = $btosr['titles'];
  383. $menu = array();
  384. if(isset($cmenu[$rs['id']]))$menu = $cmenu[$rs['id']];
  385. $rs['menu'] = $menu;
  386. $rs['stotal'] = $stotal;
  387. $rs['totals'] = 0;
  388. //连接地址转化
  389. if($rs['url']=='link' || $rs['url']=='linko'){
  390. $urlpc = $rs['urlpc'];
  391. if(!isempt($urlpc) && $mrs = $mdbs->getone("`num`='$urlpc'")){
  392. $urlpc = 'index.php?m=index&homeurl='.$this->rock->jm->base64encode($mrs['url']).'&homename='.$this->rock->jm->base64encode($mrs['name']).'&menuid='.$this->rock->jm->base64encode($mrs['id']).'';
  393. $rs['urlpc'] = $urlpc;
  394. }
  395. }
  396. $barr[] = $rs;
  397. }
  398. foreach($barr as $k=>$rs){
  399. $types = $rs['types'];
  400. if(isempt($types))$types='应用';
  401. if(!isset($carr[$types]))$carr[$types]=array();
  402. $rs['types'] = $types;
  403. $carr[$types][] = $rs;
  404. }
  405. //应用统计
  406. $gcarr = array();
  407. foreach($carr as $types=>$rows){
  408. $ntypes = $types.'('.count($rows).')';
  409. foreach($rows as $k=>$rs)$rows[$k]['types'] = $ntypes;
  410. $gcarr[$ntypes] = $rows;
  411. }
  412. $barr = array();
  413. foreach($gcarr as $types=>$rs){
  414. $barr = array_merge($barr, $rs);
  415. }
  416. return $barr;
  417. }
  418. /**
  419. * 手机app/手机网页版上获取应用
  420. */
  421. public function getappagent($uid=0)
  422. {
  423. $uid = $this->adminid;
  424. $where = m('admin')->getjoinstr('receid', $this->adminid);
  425. $rows = $this->db->getrows('[Q]im_group',"`valid`=1 and `type`=2 and `yylx` in(0,2) $where ",'`id`,`name`,`url`,`face`,`num`,`types`,`urlm`','`sort`');
  426. $dbs = m('im_menu');
  427. $barr = $carr = array();
  428. $stotalt= 0;
  429. foreach($rows as $k=>$rs){
  430. if(isempt($rs['num']))continue;
  431. $rs['face'] = $this->getface($rs['face']);
  432. $stotal = 0;
  433. $btosr = m('agent:'.$rs['num'].'')->gettotal(); //统计红点数
  434. $stotal = $btosr['stotal'];
  435. $stotalt+=$stotal;
  436. $rs['titles'] = $btosr['titles'];
  437. $rs['stotal'] = $stotal;
  438. $barr[] = $rs;
  439. }
  440. foreach($barr as $k=>$rs){
  441. $types = $rs['types'];
  442. if(isempt($types))$types='应用';
  443. if(!isset($carr[$types]))$carr[$types]=array();
  444. $rs['types'] = $types;
  445. $carr[$types][] = $rs;
  446. }
  447. //应用统计
  448. $gcarr = array();
  449. foreach($carr as $types=>$rows){
  450. $ntypes = $types.'('.count($rows).')';
  451. foreach($rows as $k=>$rs)$rows[$k]['types'] = $ntypes;
  452. $gcarr[$ntypes] = $rows;
  453. }
  454. $barr = array();
  455. foreach($gcarr as $types=>$rs){
  456. $barr = array_merge($barr, $rs);
  457. }
  458. return array(
  459. 'rows' => $barr,
  460. 'stotal' => $stotalt,
  461. );
  462. }
  463. /**
  464. * 获取历史记录
  465. */
  466. public function gethistory($uid=0, $optdt='', $whes='')
  467. {
  468. if($uid==0)$uid = $this->adminid;
  469. $where = $whes;
  470. if($optdt!='')$where = "and `optdt`>'$optdt'";
  471. $rows = $this->db->getall("select * from `[Q]im_history` where `uid`=$uid $where order by `optdt` desc");
  472. $dt = $this->rock->date;
  473. foreach($rows as $k=>$rs){
  474. $rows[$k]['optdts'] = substr($rs['optdt'],11,5);
  475. if(!contain($rs['optdt'], $dt))$rows[$k]['optdts'] = substr($rs['optdt'],5,5);
  476. $rows[$k]['id'] = $rs['receid'];
  477. $name = '';
  478. $rson = false;
  479. if($rs['type']=='user'){
  480. $rson = $this->db->getone('[Q]admin', $rs['receid'], 'name,face');
  481. $face = 'images/noface.png';
  482. }else{
  483. $face = 'images/group.png';
  484. $rows[$k]['gid'] = $rs['receid'];
  485. $rson = $this->db->getone('[Q]im_group', $rs['receid'], 'name,face,deptid');
  486. if(!isempt($rs['title']) && $rson)$rson['name'] = $rs['title'];
  487. }
  488. if($rson){
  489. $name = $rson['name'];
  490. $face = $this->getface($rson['face'], $face);
  491. if($rs['type']=='group')$rows[$k]['deptid'] = $rson['deptid'];
  492. }
  493. $rows[$k]['face'] = $face;
  494. $rows[$k]['name'] = $name;
  495. }
  496. return $rows;
  497. }
  498. /**
  499. * 微信上获取未读消息
  500. */
  501. public function getuntodo($uid)
  502. {
  503. $rows = $this->gethistory($uid, '', 'and `stotal`>0');
  504. $str = '';
  505. foreach($rows as $k=>$rs){
  506. if($k>0)$str.= "\n";
  507. $str.=''.($k+1).'、'.$rs['name'].':'.$rs['stotal'].'条';
  508. }
  509. return $str;
  510. }
  511. /**
  512. * 添加到历史记录,用户不显示历史记录让从新显示
  513. */
  514. public function addhistory($type, $receid, $uids,$optdt, $cont,$sendid=0, $title='', $xgurl='',$messid=0)
  515. {
  516. $uidsas = explode(',', $uids);
  517. $db = $this->hisobj;
  518. $isuar = array();
  519. $uarrs = $db->getrows("`type`='$type' and `receid`='$receid' and `uid` in($uids)", '`uid`,`id`');
  520. foreach($uarrs as $k=>$rs)$isuar[$rs['uid']]=$rs['id'];
  521. $iarr = $garr = array();
  522. $gids = '';
  523. foreach($uidsas as $uid){
  524. $where = '';
  525. if(isset($isuar[$uid]))$where = $isuar[$uid];
  526. $arr = array();
  527. $arr['optdt'] = $optdt;
  528. $arr['cont'] = substr($cont, 0, 190);
  529. $arr['sendid'] = $sendid;
  530. $arr['title'] = $title;
  531. $arr['xgurl'] = $xgurl;
  532. $arr['messid'] = $messid;
  533. if($where==''){
  534. $arr['type'] = $type;
  535. $arr['receid'] = $receid;
  536. $arr['uid'] = $uid;
  537. $arr['stotal'] = 1;
  538. }else{
  539. $arr['stotal'] = '(&;)`stotal`+1';
  540. }
  541. if($where==''){
  542. $iarr[] = $arr;
  543. }else{
  544. if(!$garr)$garr = $arr;
  545. $gids.=','.$where.'';
  546. }
  547. }
  548. if($iarr)$db->insertAll($iarr);
  549. if($gids!='')$db->update($garr,'`id` in('.substr($gids,1).')');
  550. $db->update('`stotal`=0',"`type`='$type' and `receid`='$receid' and `uid`='$this->adminid'");
  551. }
  552. public function delhistory($type, $receid, $uid=0)
  553. {
  554. $where = "`type`='$type' and `receid`='$receid'";
  555. if($uid>0)$where.=" and `uid`='$uid'";
  556. if($type=='all'){
  557. $where = "`uid`='$uid'";
  558. }
  559. $this->hisobj->delete($where);
  560. }
  561. /**
  562. * 获取聊天会话记录
  563. */
  564. public function getrecord($type, $uid, $gid, $minid=0, $lastdt='')
  565. {
  566. $arr = array();
  567. $rows= array();
  568. $loadci = (int)$this->rock->get('loadci','0');
  569. if($type == 'user'){
  570. $arr = $this->getuserinfor($uid, $gid, $minid, $lastdt);
  571. }
  572. if($type=='group'){
  573. $arr = $this->getgroupinfor($uid, $gid, $minid, $lastdt);
  574. }
  575. $arr['receinfor'] = $this->getreceinfor($type, $gid);
  576. $arr['nowdt'] = time();
  577. $arr['servernow'] = $this->rock->now;
  578. if($loadci==0){
  579. $arr['sendinfo'] = m('admin')->getinfor($uid);
  580. }
  581. if(isset($arr['rows']))$arr['rows'] = $this->replacefileid($arr['rows']);
  582. $this->hisobj->update('stotal=0',"`type`='$type' and `receid`='$gid' and `uid`='$uid'");
  583. return $arr;
  584. }
  585. public function getreceinfor($type, $gid)
  586. {
  587. $info = array();
  588. if($type == 'user'){
  589. $info = m('admin')->getinfor($gid);
  590. }
  591. if($type=='group'){
  592. $info = $this->getgroupxinxi($gid);
  593. }
  594. $bsear = $this->getreims();
  595. $info['type'] = $type;
  596. $info['gid'] = $gid;
  597. $info['chehui'] = $bsear['chehui'];
  598. return $info;
  599. }
  600. private function replacefileid($rows)
  601. {
  602. $fileids = '0';
  603. if($rows)foreach($rows as $k=>$rs){
  604. if($rs['fileid'])$fileids.=','.$rs['fileid'].'';
  605. }
  606. $imgext = ',gif,png,jpg,jpeg,bmp,';
  607. $fobj = m('file');
  608. if($fileids!='0'){
  609. $farr = array();
  610. $frows = $fobj->getrows("id in ($fileids)", 'id,fileext,filenum,filepath,filename,thumbpath,filetype,filesizecn,optid,optname,adddt,filesize,thumbplat');
  611. foreach($frows as $k=>$rs)$farr[$rs['id']]=$rs;
  612. if($farr)foreach($rows as $k=>$rs){
  613. $frs = array();
  614. $fid = $rs['fileid'];
  615. if(isset($farr[$fid]))$frs=$farr[$fid];
  616. if($frs){
  617. $type = $frs['fileext'];
  618. $path = $frs['filepath'];
  619. $boc = false;
  620. if(substr($path,0,4)=='http' || !isempt($frs['filenum'])){
  621. $boc = true;
  622. }else{
  623. if(file_exists($path))$boc = true;
  624. }
  625. if($boc){
  626. if($this->contain($imgext, ','.$type.',')){
  627. $frs['thumbpath'] = $fobj->getthumbpath($frs);
  628. //$cont = '<img fid="'.$fid.'" src="'.$frs['thumbpath'].'">';
  629. //$rows[$k]['cont'] = $this->rock->jm->base64encode($cont);
  630. }else{
  631. }
  632. $frs['fileid'] = $fid;
  633. $rows[$k]['filers'] = $frs;
  634. }else{
  635. $rows[$k]['fileid'] = 0;
  636. }
  637. }
  638. }
  639. }
  640. return $rows;
  641. }
  642. /**
  643. * 获取人员信息
  644. * $uid 当前用户
  645. */
  646. public function getuserinfor($uid, $receid, $minid=0, $lastdt='')
  647. {
  648. $type = 'user';
  649. $whes = $this->rock->dbinstr('receuid', $uid);
  650. $soulx = $this->rock->get('soulx');
  651. $soukey = $this->rock->get('soukey');
  652. $wdtotal= 0;
  653. $where1 = "`type`='$type' and `zt`=0 and `receid`='$uid' and `sendid`='$receid' and $whes";
  654. if($lastdt=='' && $this->rock->get('laiyuan')!='not')$wdtotal= $this->rows($where1);
  655. if($wdtotal > 0){
  656. $where = "$where1 order by `id` desc limit 10";
  657. }else{
  658. $where = "`type`='$type' and ((`receid`='$uid' and `sendid`='$receid') or (`sendid`='$uid' and `receid`='$receid')) and $whes ";
  659. if($lastdt != ''){
  660. $where .= " and `optdt`>'$lastdt'";
  661. if($this->rock->get('laiyuan')!='new')$where .=' and `sendid`<>'.$uid.'';
  662. }
  663. if($soulx=='sou0' | $soulx=='sou1')$where.= " and `fileid`>0 ";
  664. if($soukey)$where.=" and `cont` like '%".$soukey."%'";
  665. if($minid==0){
  666. $where .= ' order by `id` desc limit 5';
  667. }else{
  668. $where .= ' and `id`<'.$minid.' order by `id` desc limit 10';
  669. }
  670. }
  671. $rows = $this->getall($where, 'SQL_CALC_FOUND_ROWS optdt,zt,id,cont,sendid,fileid,type');
  672. $total = $this->db->found_rows();
  673. $len = 0;
  674. $suids = '0';
  675. $ids = '0';
  676. foreach($rows as $k=>$rs){
  677. $len++;
  678. if($rs['zt']==0)$ids .= ','.$rs['id'].'';
  679. $suids.= ','.$rs['sendid'];
  680. $wdtotal--;
  681. }
  682. $rows = $this->ivaregarr($suids, $rows);
  683. if($ids!='0')$this->setyd($ids, $uid);
  684. if($wdtotal<0)$wdtotal=0;
  685. $total = $total-$len;
  686. if($total<=0)$total = 0;
  687. return array(
  688. 'rows' => $rows,
  689. 'wdtotal' => $wdtotal,
  690. 'systotal' => $total,
  691. );
  692. }
  693. public function getgroupinfor($uid, $receid, $minid=0, $lastdt='')
  694. {
  695. $whes = $this->rock->dbinstr('receuid', $uid);
  696. $order = '';
  697. $type = 'group';
  698. $wdtotal = 0;
  699. if($lastdt=='' && $this->rock->get('laiyuan')!='not')$wdtotal = $this->getweitotal($uid, $type, $receid);
  700. $soulx = $this->rock->get('soulx');
  701. $soukey = $this->rock->get('soukey');
  702. if($wdtotal > 0){
  703. $wdwhere = $this->getweitotal($uid, $type, $receid, 1);
  704. $zwhere = " $wdwhere order by `id` desc limit 10";
  705. }else{
  706. $zwhere = " `receid`='$receid' and `type`='$type' and $whes";
  707. if($lastdt != ''){
  708. $zwhere .= " and `optdt`>'$lastdt'";
  709. if($this->rock->get('laiyuan')!='new')$zwhere .=' and `sendid`<>'.$uid.'';
  710. }
  711. if($soulx=='sou0' | $soulx=='sou1')$zwhere.= " and `fileid`>0 ";
  712. if($soukey)$zwhere.=" and `cont` like '%".$soukey."%'";
  713. if($minid==0){
  714. $zwhere .= ' order by `id` desc limit 5';
  715. }else{
  716. $zwhere .= ' and `id`<'.$minid.' order by `id` desc limit 10';
  717. }
  718. }
  719. $rows = $this->getall($zwhere, 'SQL_CALC_FOUND_ROWS optdt,zt,id,cont,sendid,fileid');
  720. $total = $this->db->found_rows();
  721. $ids = '0';
  722. $suids = '0';
  723. $len = 0;
  724. foreach($rows as $k=>$rs){
  725. $len++;
  726. $ids .= ','.$rs['id'].'';
  727. $suids.= ','.$rs['sendid'];
  728. $wdtotal--;
  729. }
  730. $rows = $this->ivaregarr($suids, $rows);
  731. if($ids!='0')$this->setyd($ids, $uid);
  732. if($wdtotal<0)$wdtotal=0;
  733. $total = $total-$len;
  734. if($total<=0)$total = 0;
  735. return array(
  736. 'rows' => $rows,
  737. 'wdtotal' => $wdtotal,
  738. 'systotal' => $total,
  739. );
  740. }
  741. public function ivaregarr($suids,$rows,$fid='')
  742. {
  743. if($suids=='' || $suids=='0')return $rows;
  744. if($fid=='')$fid='sendid';
  745. $farr = $this->db->getarr('[Q]admin', "`id` in($suids)",'`face`,`name`');
  746. foreach($rows as $k=>$rs){
  747. $face = $name = '';
  748. if(isset($farr[$rs[$fid]])){
  749. $face = $farr[$rs[$fid]]['face'];
  750. $name = $farr[$rs[$fid]]['name'];
  751. }
  752. $rows[$k]['face'] = $this->getface($face);
  753. $rows[$k]['sendname'] = $name;
  754. }
  755. return $rows;
  756. }
  757. private function ivarggarr($sgids,$rows, $fid='')
  758. {
  759. if($sgids=='' || $sgids=='0')return $rows;
  760. if($fid=='')$fid='receid';
  761. $farr = $this->db->getarr('[Q]im_group', "`id` in($suids)",'`face`,`name`');
  762. foreach($rows as $k=>$rs){
  763. $face = $name = '';
  764. if(isset($farr[$rs[$fid]])){
  765. $face = $farr[$rs[$fid]]['face'];
  766. $name = $farr[$rs[$fid]]['name'];
  767. }
  768. $rows[$k]['face'] = $this->getface($face);
  769. $rows[$k]['sendname'] = $name;
  770. }
  771. return $rows;
  772. }
  773. /**
  774. 发送单人信息
  775. $lx = 0 app发送 1web客户端
  776. */
  777. public function senduser($sendid,$receid, $cans=array(), $lx=0)
  778. {
  779. $cont = '';
  780. if(isset($cans['cont']))$cont=$cans['cont'];
  781. $optdt = $this->rock->now;
  782. $fileid = 0;
  783. $msgid = '';
  784. if(isset($cans['optdt']))$optdt=$cans['optdt'];
  785. if(isset($cans['sendid']))$sendid=$cans['sendid'];
  786. if(isset($cans['fileid']))$fileid=$cans['fileid'];
  787. if(isset($cans['msgid']))$msgid=$cans['msgid'];
  788. $pushcont = arrvalue($cans, 'pushcont');
  789. $arr = array(
  790. 'cont' => $cont,
  791. 'sendid' => $sendid,
  792. 'receid' => $receid,
  793. 'type' => 'user',
  794. 'optdt' => $optdt,
  795. 'zt' => '0',
  796. 'fileid' => $fileid,
  797. 'msgid' => $msgid
  798. );
  799. $arr['receuid'] = $arr['sendid'].','.$arr['receid'];
  800. $bo = $this->insert($arr);
  801. $arr['id'] = $this->db->insert_id();
  802. $arr['nuid'] = $this->rock->request('nuid');
  803. $farr = array();
  804. if($fileid>0){
  805. m('file')->addfile($fileid, 'im_mess', $arr['id']);
  806. $farr = m('file')->getone($fileid,'filesizecn,fileext,thumbpath,filename');
  807. if($farr)foreach($farr as $fk=>$fv)$arr[$fk] = $fv;
  808. }
  809. //给服务端发送0
  810. if($lx==0){
  811. $receids = m('admin')->getonline($arr['receid']);
  812. if($receids != ''){
  813. $pusharr = array(
  814. 'cont' => $cont,
  815. 'type' => 'user',
  816. 'optdt' => $optdt,
  817. 'messid' => $arr['id'],
  818. 'fileid' => $fileid
  819. );
  820. if($farr)foreach($farr as $fk=>$fv)$pusharr[$fk] = $fv;
  821. $this->sendpush($arr['sendid'], $receids , $pusharr);
  822. }
  823. }
  824. //告诉app端也有推送,因为app也用到websocket连接服务端
  825. $this->addhistory('user', $receid, $sendid, $optdt, $cont, $sendid,'','', $arr['id']);
  826. if($sendid!=$receid)$this->addhistory('user', $sendid, $receid, $optdt, $cont, $sendid,'','', $arr['id']);
  827. //推送的原生App上(使用异步推送哦)
  828. $tuicont['sendid'] = $arr['sendid'];
  829. $tuicont['sendname'] = $this->adminname;
  830. $tuicont['name'] = $this->adminname;
  831. $tuicont['cont'] = $cont;
  832. $tuicont['pushcont'] = $pushcont;
  833. $tuicont['pushtype'] = 'chat'; //推送消息类型
  834. $tuicont['receid'] = $receid;
  835. $tuicont['fileid'] = $fileid;
  836. $tuicont['type'] = 'user';
  837. $tuicont['id'] = $arr['id'];
  838. $tuicont['optdt'] = $optdt;
  839. $tuicont['optdts'] = substr($optdt,11,5);
  840. $this->pushapp($receid, '['.$this->adminname.']发来一条消息', $tuicont, $lx);
  841. /*
  842. $last = date('Y-m-d H:i:s', time()-15);
  843. $where = "`uid`='$receid' and `online`=1 and `cfrom` in('appandroid','appios') and `moddt`<'$last'";
  844. $tos = m('logintoken')->rows($where);
  845. if($tos>0){//没有打开应用
  846. $conts = substr($this->rock->jm->base64decode($cont),0,99);
  847. c('JPush')->send($receid,'['.$this->adminname.']发来一条消息', ''.$this->adminname.':'.$conts, 1);
  848. }*/
  849. return $arr;
  850. }
  851. /**
  852. 发送群讨论信息
  853. $lx = 0 app发送 1web客户端
  854. */
  855. public function sendgroup($sendid, $gid, $cans=array(), $lx=0)
  856. {
  857. $cont = '';
  858. if(isset($cans['cont']))$cont=$cans['cont'];
  859. $receid = $gid;
  860. $grs = $this->getgroupxinxi($gid);
  861. $gname = $grs['name'];
  862. $type = 'group';
  863. $fileid = 0;
  864. $msgid = '';
  865. $optdt = $this->rock->now;
  866. if(isset($cans['optdt']))$optdt=$cans['optdt'];
  867. if(isset($cans['type']))$type=$cans['type'];
  868. if(isset($cans['sendid']))$sendid=$cans['sendid'];
  869. if(isset($cans['fileid']))$fileid=$cans['fileid'];
  870. if(isset($cans['msgid']))$msgid=$cans['msgid'];
  871. $aors = m('im_groupuser')->getall("`gid`='$receid'",'uid');
  872. $asid = $asids = '';
  873. foreach($aors as $k=>$rs){
  874. $_uid = $rs['uid'];
  875. if($_uid != $sendid)$asid.=','.$_uid;
  876. $asids.=','.$_uid;
  877. }
  878. if($asids != '')$asids = substr($asids, 1);
  879. $arr = array(
  880. 'cont' => $cont,
  881. 'sendid' => $sendid,
  882. 'receid' => $receid,
  883. 'receuid' => $asids,
  884. 'type' => $type,
  885. 'optdt' => $optdt,
  886. 'zt' => '1',
  887. 'fileid' => $fileid,
  888. 'msgid' => $msgid
  889. );
  890. $bo = $this->insert($arr);
  891. $arr['id'] = $this->db->insert_id();
  892. $arr['nuid'] = $this->rock->request('nuid');
  893. $arr['gid'] = $receid;
  894. if($asid != ''){
  895. $asid = substr($asid, 1);
  896. $this->db->insert('[Q]im_messzt','`mid`,`uid`,`gid`','select '.$arr['id'].',`id`,'.$gid.' from `[Q]admin` where id in('.$asid.') and `status`=1', true);
  897. }
  898. $arr['receid'] = $asid;
  899. $farr = array();
  900. if($fileid>0){
  901. m('file')->addfile($fileid, 'im_mess', $arr['id']);
  902. $farr = m('file')->getone($fileid,'filesizecn,fileext,thumbpath,filename');
  903. if($farr)foreach($farr as $fk=>$fv)$arr[$fk] = $fv;
  904. }
  905. //推送到PC客户端上
  906. if($lx==0 && $asid != ''){
  907. $receids = m('admin')->getonline($asid);
  908. if($receids != ''){
  909. $pusharr = array(
  910. 'cont' => $cont,
  911. 'type' => 'group',
  912. 'gid' => $gid,
  913. 'gname' => $gname,
  914. 'optdt' => $optdt,
  915. 'gface' => arrvalue($grs,'face'),
  916. 'messid' => $arr['id'],
  917. 'fileid' => $fileid
  918. );
  919. if($farr)foreach($farr as $fk=>$fv)$pusharr[$fk] = $fv;
  920. $this->sendpush($arr['sendid'], $receids , $pusharr);
  921. }
  922. }
  923. $cont1 = $this->rock->jm->base64encode(''.$this->adminname.':'.$this->rock->jm->base64decode($cont).'');
  924. $this->addhistory('group', $gid, $arr['receuid'], $optdt, $cont1,$sendid,'','', $arr['id']);
  925. //推送的原生App上(使用异步推送哦)
  926. if($asid != ''){
  927. $tuicont['sendid'] = $arr['sendid'];
  928. $tuicont['sendname'] = $this->adminname;
  929. $tuicont['cont'] = $cont1;
  930. $tuicont['name'] = $gname;
  931. $tuicont['pushtype'] = 'chat'; //推送消息类型
  932. $tuicont['receid'] = $receid;
  933. $tuicont['fileid'] = $fileid;
  934. $tuicont['type'] = 'group';
  935. $tuicont['id'] = $arr['id'];
  936. $tuicont['optdt'] = $optdt;
  937. $tuicont['optdts'] = substr($optdt,11,5);
  938. $this->pushapp($asid, '['.$gname.']发来一条消息', $tuicont, $lx);
  939. }
  940. /*
  941. if($asid != ''){
  942. $last = date('Y-m-d H:i:s', time()-15);
  943. $where = "`uid` in($asid) and `online`=1 and `cfrom` in('appandroid','appios') and `moddt`<'$last'";
  944. $tos = m('logintoken')->rows($where);
  945. if($tos>0){//有打开应用
  946. $conts = substr($this->rock->jm->base64decode($cont),0,99);
  947. c('JPush')->send($asid,'['.$gname.']发来一条消息', ''.$this->adminname.':'.$conts, 1);
  948. }
  949. }*/
  950. $arr['gname'] = $gname;
  951. return $arr;
  952. }
  953. public function sendinfor($type, $sendid, $gid, $cans=array(), $lx=0)
  954. {
  955. $arr = array();
  956. if($type == 'user'){
  957. $arr = $this->senduser($sendid, $gid, $cans, $lx);
  958. }
  959. if($type == 'group'){
  960. $arr = $this->sendgroup($sendid, $gid, $cans, $lx);
  961. }
  962. return $arr;
  963. }
  964. /**
  965. * 获取用户的app上设置别名,也就是token拉
  966. */
  967. public function getalias($uid, $lx=0)
  968. {
  969. if($uid=='')return false;
  970. $where = "id in($uid) and ";
  971. if($uid=='all'){
  972. $where='';
  973. }else{
  974. if($this->contain($uid,'u') || $this->contain($uid,'d')){
  975. $uid = m('admin')->gjoin($uid);
  976. if($uid=='')return false;
  977. $where = "id in($uid) and ";
  978. }
  979. }
  980. $uwhere = "$where `status`=1";
  981. $rows = m('logintoken')->getrows("`uid` in(select id from `[Q]admin` where $uwhere) and `cfrom` in ('appandroid','nppandroid','nppios') and `online`=1",'*','id desc');
  982. $alias = $uida = $xmalias = $oldalias = $pushuids = $newalias = $alias2019 = $uid2019 =array();
  983. $uids = '0';
  984. $times = date('Y-m-d H:i:s', time()-5*60);//5分钟
  985. foreach($rows as $k=>$rs){
  986. $_uid = $rs['uid'];
  987. $_web = $rs['web'];
  988. //if(in_array($_uid, $uida))continue;
  989. $uida[] = $_uid;
  990. $uids .= ','.$_uid.'';
  991. if($_web=='xiaomi'){
  992. $xmalias[] = $rs['token'];
  993. }else if(in_array($rs['cfrom'], array('nppandroid','nppios'))){//2019-11-25最新新app
  994. $nestr = ''.$rs['token'].'|'.$rs['web'].'|'.$_uid.'|';
  995. if(contain($rs['web'],'huawei') && !contain($rs['ip'],'.'))$nestr.=''.$rs['ip'].'';
  996. if(contain($rs['web'],'xiaomi'))$nestr.=''.$rs['pushtoken'].'';
  997. $alias2019[] = $nestr;
  998. $uid2019[] = $_uid;
  999. }else if(substr($_web,0,4)=='app_'){
  1000. $newalias[] = $rs['token'];
  1001. }else if(substr($_web,0,4)=='apk_'){
  1002. $oldalias[] = $rs['token'];
  1003. }else{
  1004. $alias[] = $rs['token'];
  1005. }
  1006. if($rs['ispush']=='1')$pushuids[] = $_uid;//可以手机推送的用户
  1007. }
  1008. return array('alias' => $alias, 'uids'=>$uids, 'xmalias'=>$xmalias, 'oldalias'=>$oldalias, 'newalias'=>$newalias,'alias2019'=>$alias2019,'uid2019'=>$uid2019,'pushuids'=>$pushuids);
  1009. }
  1010. /**
  1011. * 推送到原生app上
  1012. */
  1013. public function pushapp($receid, $title, $conta, $lx)
  1014. {
  1015. $alias = $this->getalias($receid, $lx);
  1016. if(!$alias)return false;
  1017. //$alias = $garr['alias'];
  1018. //if(!$alias)return false;
  1019. $uids = $alias['uids'];
  1020. if($uids=='0')return;
  1021. $contjson = '';
  1022. foreach($conta as $k=>$v)$contjson.=',"'.$k.'":"'.$v.'"';
  1023. $contjson = '{'.substr($contjson,1).'}';
  1024. //最新webapp也用服务端推送
  1025. $uid2019 = $alias['uid2019'];
  1026. $alias2019 = $alias['alias2019'];
  1027. if($uid2019){
  1028. $reimtype = $this->option->getval('reimservertype');
  1029. $reimappwx= $this->option->getval('reimappwxsystem');
  1030. if($reimtype=='1' && $reimappwx=='1'){
  1031. $gbarr = $this->pushserver('sendapp', array(
  1032. 'receid' => join(',', $uid2019)
  1033. ));
  1034. //服务端返回{"zshu":2,"yfuid":"1,8","wfuid":""}
  1035. if($gbarr && $gbarr['success'] && $bstr = arrvalue($gbarr, 'data')){
  1036. $data = json_decode($bstr, true);
  1037. $yfuid= explode(',', arrvalue($data, 'yfuid'));
  1038. if($yfuid){
  1039. $nealas = array();
  1040. foreach($alias2019 as $alis){
  1041. $bo = false;
  1042. foreach($yfuid as $yfid){if(contain($alis,'|'.$yfid.'|'))$bo=true;};
  1043. if(!$bo)$nealas[] = $alis;
  1044. }
  1045. $alias['alias2019'] = $nealas;
  1046. }
  1047. }
  1048. }
  1049. }
  1050. $pushcont = arrvalue($conta,'pushcont');
  1051. if(!$pushcont)$pushcont = arrvalue($conta,'cont'); //推送的内容已经是base64的
  1052. return c('JPush')->push($title, $pushcont, $contjson, $alias);
  1053. }
  1054. /**
  1055. * 推送到服务端运行
  1056. */
  1057. public function sendpush($sendid, $receid, $conarr=array())
  1058. {
  1059. $bsarr = array('msg'=>'notpushurl','code'=>2);
  1060. $bstt = json_encode($bsarr);
  1061. if($sendid==0)$sendid = 1;
  1062. $sers = $this->db->getone('[Q]admin',"`id`='$sendid'", "`name`,`face`");
  1063. if(!$sers)return $bstt;
  1064. $face = $sers['face'];
  1065. $carr['adminid'] = $sendid;
  1066. $carr['optdt'] = $this->rock->now;
  1067. $carr['sendname'] = $sers['name'];
  1068. $carr['face'] = $this->getface($face); //发送人头像
  1069. $carr['receid'] = $receid;
  1070. foreach($conarr as $k=>$v)$carr[$k]=$v;
  1071. return $this->pushserver('send', $carr);
  1072. }
  1073. /**
  1074. * 推送发送命令类型
  1075. */
  1076. public function sendcmd($receid, $conarr=array())
  1077. {
  1078. $conarr['type'] = 'cmd';
  1079. return $this->sendpush($this->adminid, $receid, $conarr);
  1080. }
  1081. /**
  1082. * 向服务端发送异步任务
  1083. * $runtime 运行的时间贞
  1084. */
  1085. public function asynurl($m, $a,$can=array(), $runtime=0)
  1086. {
  1087. $asyn = (int)getconfig('asynsend','0');
  1088. $runurl = m('base')->getasynurl($m, $a,$can);
  1089. $queuelogid= m('log')->addlogs('异步队列','', 3);
  1090. if($queuelogid)$runurl.= '&queuelogid='.$queuelogid.'';
  1091. //用官网VIP异步
  1092. if($asyn==2){
  1093. $barr = c('xinhuapi')->sendanay($m, $a,$can, $runtime);
  1094. if($barr['success'])return true;
  1095. }
  1096. $barr = $this->pushserver('runurl', array(
  1097. 'url' => $runurl,
  1098. 'runtime' => $runtime
  1099. ));
  1100. if($queuelogid){
  1101. m('log')->update(array(
  1102. 'url' => $runurl,
  1103. 'remark'=> '[asynurl]'.$runtime.'',
  1104. ),$queuelogid);
  1105. }
  1106. return $barr['success'];
  1107. }
  1108. /**
  1109. * 获取得到推送的端口号
  1110. */
  1111. public function getpushhostport($str)
  1112. {
  1113. $host = ''; $port = 0;
  1114. $stra = explode('//', $str);
  1115. if(isset($stra[1])){
  1116. $strb = explode(':', str_replace('/','', $stra[1]));
  1117. $host = $strb[0];
  1118. $port = (int)arrvalue($strb, 1, '0');
  1119. }
  1120. return array('host'=>$host,'port'=>$port);
  1121. }
  1122. /**
  1123. * 服务端推送,返回boolean看是否成功
  1124. */
  1125. public function pushserver($atype, $cans=array())
  1126. {
  1127. if(isempt($this->serverpushurl))return returnerror('没配置服务端');
  1128. $carr['from'] = $this->serverrecid;
  1129. $carr['adminid']= $this->adminid;
  1130. $carr['atype'] = $atype;
  1131. $carr['qtype'] = 'reim';
  1132. foreach($cans as $k=>$v)$carr[$k]=$v;
  1133. $reimtype = $this->option->getval('reimservertype');
  1134. if($reimtype=='1')return c('rockqueue')->pushdata($carr);
  1135. $str = json_encode($carr);
  1136. //echo 'abc ';return array('code'=>0);
  1137. $posts = $this->getpushhostport($this->serverpushurl);
  1138. $barr = c('socket')->udppush($str, $posts['host'], $posts['port']);
  1139. return $barr;
  1140. }
  1141. /**
  1142. * 判断异步地址是否可以使用
  1143. */
  1144. public function asynurlbo()
  1145. {
  1146. $url = $this->serverpushurl;
  1147. if(isempt($url))return false;
  1148. $ishttp = substr($url,0, 4)=='http';
  1149. if($ishttp){
  1150. $str = c('curl')->getcurl($url);
  1151. return contain($str, 'msg');
  1152. }else{
  1153. $spath = str_replace('\\','/', $url);
  1154. return is_writable($spath.'/Rock/push');
  1155. }
  1156. }
  1157. //创建群等
  1158. public function creategroup($name, $receid, $type=1, $explain='')
  1159. {
  1160. $arr['name'] = $name;
  1161. $arr['type'] = $type;
  1162. $arr['createid'] = $this->adminid;
  1163. $arr['createname'] = $this->adminname;
  1164. $arr['createdt'] = $this->rock->now;
  1165. $arr['explain'] = $explain;
  1166. $arr['valid'] = 1;
  1167. $gid = m('im_group')->insert($arr);
  1168. $this->db->insert('[Q]im_groupuser','gid,uid','select '.$gid.',id from [Q]admin where id in('.$receid.') and `status`=1', true);
  1169. $arr['id'] = $gid;
  1170. $arr['type'] = 'group';
  1171. return $arr;
  1172. }
  1173. /**
  1174. * 下载同步聊天记录到app本地
  1175. * $uid 对应用户,$maxid 从哪个最大的id,$minid从哪个最小Id
  1176. * 最多下载20天内的记录,每次下载30条
  1177. */
  1178. public function downrecord($uid, $maxid=0, $minid=999999999)
  1179. {
  1180. $whes = $this->rock->dbinstr('receuid', $uid);
  1181. $lastdt = date('Y-m-d 00:00:00', time()-20*24*3600);
  1182. $limit = 30;
  1183. $fields = 'optdt,zt,id,`type`,receid,cont,sendid,fileid';
  1184. $sql1 = "select $fields from `[Q]im_mess` where `id`> $maxid and $whes";
  1185. if($maxid==0){
  1186. $sql1.=' order by id desc';
  1187. }else{
  1188. $sql1.=' order by id asc';
  1189. }
  1190. $sql1.=' limit '.$limit.'';
  1191. $rows = $this->db->getall($sql1);
  1192. $nsaid = '0';
  1193. foreach($rows as $k=>$rs)$nsaid.=','.$rs['id'].'';
  1194. $olimie = $limit-$this->db->count;
  1195. if($olimie>0 && $minid>1){
  1196. $sql2 = "select $fields from `[Q]im_mess` where `id`< $minid and `optdt`>='$lastdt' and `id` not in($nsaid) and $whes order by id desc limit $olimie";
  1197. $rowss = $this->db->getall($sql2);
  1198. if($rowss)$rows = array_merge($rows, $rowss);
  1199. }
  1200. $suids = '0';
  1201. $dbs = m('im_messzt');
  1202. foreach($rows as $k=>$rs){
  1203. $suids.= ','.$rs['sendid'];
  1204. if($rs['type'] != 'user'){
  1205. $zt = 0;
  1206. if($dbs->rows("`mid`='".$rs['id']."' and `uid`='$uid'")==0)$zt=1;
  1207. $rows[$k]['zt'] = $zt;
  1208. }
  1209. if($rs['sendid']==$uid)$rows[$k]['zt'] = 1;
  1210. $id = (int)$rs['id'];
  1211. if($id>$maxid)$maxid = $id;
  1212. if($id<$minid)$minid = $id;
  1213. }
  1214. $rows = $this->ivaregarr($suids, $rows);
  1215. $rows = $this->replacefileid($rows);
  1216. $isdown = '0';
  1217. if(count($rows)==$limit)$isdown = '1'; //需要继续下载
  1218. $arr['rows'] = $rows;
  1219. $arr['maxid'] = $maxid;
  1220. $arr['minid'] = $minid;
  1221. $arr['isdown'] = $isdown;
  1222. return $arr;
  1223. }
  1224. /**
  1225. * 删除服务器上记录
  1226. */
  1227. public function clearrecord($type,$gid, $uid, $ids='',$day=0)
  1228. {
  1229. $whes = $this->rock->dbinstr('receuid', $uid);
  1230. $this->setallyd($type,$uid, $gid);
  1231. if(!isempt($type)){
  1232. if($type=='user'){
  1233. $where1 = "`type`='$type' and ((`receid`='$uid' and `sendid`='$gid') or (`receid`='$gid' and `sendid`='$uid')) and $whes";
  1234. }else{
  1235. $where1 = "`type`='$type' and `receid`='$gid' and $whes";
  1236. }
  1237. }else{
  1238. $where1 = $whes;
  1239. }
  1240. if($ids!='')$where1.=" and `id` in($ids)";
  1241. if($day>0){
  1242. $dts = date('Y-m-d H:i:s',time()-$day*24*3600);
  1243. $where1.=" and `optdt`< '$dts'";
  1244. }
  1245. $rows = $this->getall($where1, '`receuid`,`id`');
  1246. $xids = '0';
  1247. foreach($rows as $k=>$rs){
  1248. $sid = $rs['id'];
  1249. if($this->isempt($rs['receuid'])){
  1250. $xids.=','.$sid.'';
  1251. }else{
  1252. $ssid = '';
  1253. $uidsa = explode(',', $rs['receuid']);
  1254. foreach($uidsa as $suid){
  1255. if($suid != $uid){
  1256. $ssid.=','.$suid.'';
  1257. }
  1258. }
  1259. if($ssid==''){
  1260. $xids.=','.$sid.'';
  1261. }else{
  1262. $ssid = substr($ssid,1);
  1263. $this->update("`receuid`='$ssid'", $sid);
  1264. }
  1265. }
  1266. $this->hisobj->update("`cont`=''", "`type`='$type' and `uid`='$uid' and `messid`='$sid'");
  1267. }
  1268. if($xids!='0')$this->delete("`id` in($xids)");
  1269. if($ids=='' && $day==0)$this->delhistory($type,$gid, $uid);
  1270. }
  1271. /**
  1272. * 转发
  1273. */
  1274. public function forward($tuid, $type, $cont, $fid=0)
  1275. {
  1276. $uid = $this->adminid;
  1277. if($fid>0){
  1278. $frs = m('file')->getone($fid, '`filepath`,`filename`,`filesizecn`,`fileext`');
  1279. $msg = '文件不存在了';
  1280. if(!$frs)return $msg;
  1281. if(!file_exists($frs['filepath']))return $msg;
  1282. $cont = '';$jpgallext = '|jpg|png|gif|bmp|jpeg|';
  1283. if(contain($jpgallext,'|'.$frs['fileext'].'|')){
  1284. $cont = '[图片 '.$frs['filesizecn'].']';
  1285. }else{
  1286. $cont = '['.$frs['filename'].' '.$frs['filesizecn'].']';
  1287. }
  1288. $cont = $this->rock->jm->base64encode($cont);
  1289. }
  1290. $tuids = explode(',', $tuid);
  1291. foreach($tuids as $gid)$this->sendinfor($type, $uid, $gid, array(
  1292. 'optdt' => $this->rock->now,
  1293. 'cont' => $cont,
  1294. 'fileid'=> $fid
  1295. ));
  1296. return 'ok';
  1297. }
  1298. //会话管理的
  1299. public function createchat($name, $aid, $uids='', $na='', $optdt='', $iscjwx=false)
  1300. {
  1301. if($optdt=='')$optdt=$this->rock->now;
  1302. if($na=='')$na = $this->adminname;
  1303. if($uids=='')$uids = $aid;
  1304. $this->db->record('[Q]im_group', array(
  1305. 'type' => 1,
  1306. 'name' => $name,
  1307. 'createid' => $aid,
  1308. 'createname' => $na,
  1309. 'createdt' => $optdt,
  1310. 'valid' => '1'
  1311. ));
  1312. $gid = $this->db->insert_id();
  1313. $this->adduserchat($gid, $uids, false);
  1314. return $gid;
  1315. }
  1316. //邀请
  1317. public function adduserchat($gid, $uids, $isadd=false)
  1318. {
  1319. if(isempt($uids))return '';
  1320. $ids = '';
  1321. $uidss = explode(',', $uids);
  1322. $db = m('im_groupuser');
  1323. foreach($uidss as $aid){
  1324. if($db->rows("gid='$gid' and `uid`='$aid'")==0){
  1325. $db->insert(array('gid' => $gid,'uid' => $aid));
  1326. $ids .= ','.$aid.'';
  1327. }
  1328. }
  1329. if($ids!=''){
  1330. $ids = substr($ids,1);
  1331. $unaem = '';
  1332. $urows = m('admin')->getall('`id` in('.$ids.')');
  1333. foreach($urows as $k=>$rs)$unaem.=','.$rs['name'].'';
  1334. if($unaem!=''){
  1335. $this->addxitong($gid, ''.$this->adminname.'邀请“'.substr($unaem,1).'”加入本会话');
  1336. }
  1337. }
  1338. return $ids;
  1339. }
  1340. public function deluserchat($gid, $uids)
  1341. {
  1342. if(isempt($uids))return;
  1343. m('im_groupuser')->delete("`gid`='$gid' and `uid` in($uids)");
  1344. }
  1345. public function deletechat($gid)
  1346. {
  1347. m('im_group')->delete($gid);
  1348. m('im_groupuser')->delete("`gid`='$gid'");
  1349. m('im_messzt')->delete("`gid`='$gid'");
  1350. $this->delhistory('group',$gid, 0);
  1351. }
  1352. public function exitchat($gid, $aid)
  1353. {
  1354. $names = m('admin')->getmou('name', $aid);
  1355. $this->addxitong($gid, ''.$names.'退出本会话');
  1356. $dbs = m('im_groupuser');
  1357. $dbs->delete("`gid`='$gid' and `uid`='$aid'");
  1358. m('im_messzt')->delete("`gid`='$gid' and `uid`='$aid'");
  1359. if($dbs->rows('gid='.$gid.'')==0)m('im_group')->delete($gid);
  1360. $this->delhistory('group',$gid, $aid);
  1361. }
  1362. public function addxitong($gid, $cont, $fid=0)
  1363. {
  1364. $this->sendinfor('group', $this->adminid, $gid, array(
  1365. 'optdt' => $this->rock->now,
  1366. 'cont' => $this->rock->jm->base64encode($cont),
  1367. 'fileid'=> $fid
  1368. ));
  1369. }
  1370. //修改会话名称
  1371. public function editname($gid, $name)
  1372. {
  1373. m('im_group')->update("`name`='$name'",$gid);
  1374. $this->addxitong($gid, ''.$this->adminname.'将会话名称修改为“'.$name.'”');
  1375. }
  1376. //修改头像
  1377. public function editface($gid, $fileid)
  1378. {
  1379. $face= '';
  1380. if($fileid>0){
  1381. $frs = m('file')->getone($fileid);
  1382. if($frs)$face= $frs['thumbpath'];
  1383. }
  1384. m('im_group')->update("`face`='$face'",$gid);
  1385. }
  1386. //微信消息回调(弃用了)
  1387. public function getwxchat($arr)
  1388. {
  1389. $this->rock->debugs(json_encode($arr),'cccc');if(!isset($arr['MsgType']))return;
  1390. $MsgType = $arr['MsgType'];
  1391. $FromUserName = $arr['FromUserName'];
  1392. $user = $FromUserName;
  1393. $urs = m('admin')->getone("`user`='$FromUserName'",'id,name');
  1394. if(!$urs)return;
  1395. $sendid = $urs['id'];
  1396. $sendname = $urs['name'];
  1397. $this->adminid = $sendid;
  1398. $this->adminname= $sendname;
  1399. if($MsgType == 'event'){
  1400. $event = $arr['Event'];
  1401. if($event=='create_chat')m('weixin:chat')->addchat($sendid, $sendname,$arr);
  1402. if($event=='update_chat')m('weixin:chat')->updatechat($arr);
  1403. if($event=='quit_chat')m('weixin:chat')->quitchat($arr);
  1404. if($event=='subscribe')m('weixin:user')->subscribe($user,1);
  1405. if($event=='unsubscribe')m('weixin:user')->subscribe($user,4);
  1406. return;
  1407. }
  1408. if(!isset($arr['Type']))return;
  1409. $Type = $arr['Type'];
  1410. $gid = 0;
  1411. $optdt = date('Y-m-d H:i:s', $arr['CreateTime']);
  1412. $cont = '';
  1413. if($Type=='single' || $Type=='userid'){
  1414. $gid = (int)m('admin')->getmou('id', "`user`='".$arr['Id']."'");
  1415. $type= 'user';
  1416. }
  1417. if($Type=='group'){
  1418. $gid = m('weixin:chat')->getchatid($arr['Id'], $sendid, $sendname);
  1419. $type= 'group';
  1420. }
  1421. if($gid==0)return;
  1422. @$msgid = $arr['MsgId'];if(isempt($msgid))return;
  1423. if($this->rows("`msgid`='$msgid'")>0)return;
  1424. if($MsgType=='text'){
  1425. $cont = $arr['Content'];
  1426. }
  1427. if($MsgType=='location'){
  1428. $cont = '位置:'.$arr['Label'];
  1429. }
  1430. if($MsgType=='voice'){
  1431. $cont = '语音,请用微信收听';
  1432. if(isset($arr['MediaId']))$this->asynurl('asynrun','downwxmedia', array(
  1433. 'mediaid' => $arr['MediaId'],
  1434. 'msgid' => $msgid,
  1435. 'fileext' => 'amr',
  1436. 'adminid' => $sendid
  1437. ));
  1438. }
  1439. if($MsgType=='image'){
  1440. $cont = '[图片]';
  1441. $PicUrl = $this->rock->jm->encrypt($arr['PicUrl']);
  1442. $this->asynurl('asynrun','downwxpic', array(
  1443. 'picurl' => $PicUrl,
  1444. 'msgid' => $msgid,
  1445. 'adminid' => $sendid
  1446. ));
  1447. }
  1448. if($MsgType=='link'){
  1449. if(isempt($arr['Title']))$arr['Title']='链接';
  1450. $cont = '[A]'.$arr['Title'].'|'.$arr['Url'].'[/A]';
  1451. }
  1452. if($cont!='')$this->sendinfor($type,$sendid, $gid, array(
  1453. 'cont' => $this->rock->jm->base64encode($cont),
  1454. 'optdt' => $optdt,
  1455. 'msgid' => $msgid
  1456. ));
  1457. }
  1458. //下载微信上图片
  1459. public function downwximg($url, $msgid)
  1460. {
  1461. if($url=='' || $msgid=='')return;
  1462. $cont = c('curl')->getcurl($url);
  1463. $barr = c('down')->createimage($cont,'jpg','微信图片');
  1464. if($barr){
  1465. $fileid = $barr['id'];
  1466. $filesize = $barr['filesizecn'];
  1467. $mors = $this->getone("`msgid`='$msgid'",'id');
  1468. if($mors){
  1469. $id = $mors['id'];
  1470. $this->update(array('fileid' => $fileid), $id);
  1471. m('file')->addfile($fileid, 'im_mess', $id);
  1472. }
  1473. }
  1474. }
  1475. /**
  1476. * 定时未读的会话消息提醒推送到微信上
  1477. */
  1478. public function chatpushtowx($dt='')
  1479. {
  1480. if(getconfig('platdwnum'))return false;
  1481. if($dt=='')$dt = date('Y-m-d H:i:s', time()-5*60);
  1482. //$bowx = $this->installwx(0);
  1483. $bowxqy = $this->installwx(1);
  1484. if(!$bowxqy)return;
  1485. $rows = $this->db->getall("select * from `[Q]im_history` where `optdt`>='$dt' and `stotal`>0 and `type` in('user','group') order by `uid`,`optdt` asc");
  1486. $uarrs = array();
  1487. $gusrra = array();
  1488. foreach($rows as $k=>$rs){
  1489. $rson = false;
  1490. $key = $rs['type'].$rs['receid'];
  1491. $face = 'images/noface.png';
  1492. if($rs['type']=='group')$face = 'images/group.png';
  1493. if(!isset($gusrra[$key])){
  1494. if($rs['type']=='user'){
  1495. $rson = $this->db->getone('[Q]admin', $rs['receid'], 'name,face');
  1496. }else{
  1497. $rson = $this->db->getone('[Q]im_group', $rs['receid'], 'name,face');
  1498. }
  1499. }else{
  1500. $rson = $gusrra[$key];
  1501. }
  1502. if(!$rson)continue;
  1503. $gusrra[$key] = $rson;
  1504. $rs['name'] = $rson['name'];
  1505. $rs['face'] = $this->getface($rson['face'], URL.$face);
  1506. $uarrs[$rs['uid']][] = $rs;
  1507. }
  1508. $sendarr = array();
  1509. if($uarrs)foreach($uarrs as $uid=>$usend){
  1510. $cont = $tites = '';
  1511. $zshu = 0;
  1512. if($usend)foreach($usend as $k=>$rs){
  1513. $zshu++;
  1514. if($k>0)$cont.="\n";
  1515. $cont.=''.$rs['name'].':'.$this->rock->jm->base64decode($rs['cont']).' ('.substr($rs['optdt'],11,5).')';
  1516. }
  1517. if($zshu==0)continue;
  1518. if($zshu==1){
  1519. $title = '你有['.$rs['name'].']未读会话消息';
  1520. $url = ''.URL.'?m=chat&d=we&type='.$rs['type'].'&uid='.$rs['receid'].'';
  1521. }else{
  1522. $title = '你有'.$zshu.'个未读会话消息';
  1523. $url = ''.URL.'?d=we#list';
  1524. }
  1525. $wxarr = array(
  1526. 'title' => $title,
  1527. 'description' => $cont,
  1528. 'url' => $url,
  1529. 'uid' => $uid
  1530. );
  1531. //根据内容分组发送
  1532. $contkey = md5($cont);
  1533. $sendarr[$contkey][] = $wxarr;
  1534. }
  1535. $devagent = $this->optiondb->getval('weixinqy_devagent');
  1536. if(isempt($devagent))$devagent = '办公助手';
  1537. foreach($sendarr as $key=>$rowss){
  1538. $uids = '';
  1539. $wxarr= $rowss[0];
  1540. foreach($rowss as $k=>$rs){
  1541. $uids.=','.$rs['uid'].'';
  1542. }
  1543. //发送
  1544. if($uids!=''){
  1545. $uids = substr($uids, 1);
  1546. if($bowxqy){
  1547. $barr = m('weixinqy:index')->sendxiao($uids, 'REIM,REIM助手,'.$devagent.'', $wxarr);
  1548. m('log')->todolog('企业微信提醒', $barr);
  1549. }
  1550. }
  1551. }
  1552. }
  1553. /**
  1554. * 撤回消息功能
  1555. */
  1556. public function chehuimess($type, $gid, $id)
  1557. {
  1558. $chehui = (int)$this->optiondb->getval('reimchehuisystem',0);
  1559. if($chehui<=0)return '没有开启此功能';
  1560. $createid = m('im_group')->getmou('createid', $gid);
  1561. $rs = $this->getone('`id`='.$id.'');
  1562. if(!$rs)return '不存在';
  1563. if($createid != $this->adminid){
  1564. if($rs['sendid'] != $this->adminid)return '不是你发的';
  1565. $t3 = time()-strtotime($rs['optdt']);
  1566. if($t3>$chehui*60)return '已经超过'.$chehui.'分钟无法撤回';
  1567. }
  1568. $msg1= '<del style="color:gray">已撤回</del>';
  1569. $msg = $this->rock->jm->base64encode($msg1);
  1570. $msg2 = $this->rock->jm->base64encode($this->adminname.':');
  1571. $this->update("`cont`='$msg',`fileid`=0", $id);
  1572. $this->hisobj->update("`cont`='".$msg2.$msg."',`optdt`='{$this->rock->now}'", "`messid`='$id'");
  1573. $pusharr = array(
  1574. 'cont' => $msg,
  1575. 'type' => 'chehui',
  1576. 'messid' => $id,
  1577. );
  1578. $this->sendpush($this->adminid, $rs['receuid'], $pusharr);
  1579. $pusharr['atype'] = 'sendapp';
  1580. $this->sendpush($this->adminid, $rs['receuid'], $pusharr);
  1581. return array(
  1582. 'receid' => $rs['receuid'],
  1583. 'id' => $id,
  1584. 'msg' => $msg,
  1585. 'msg1' => $msg1,
  1586. );
  1587. }
  1588. }
粤ICP备19079148号