reimModel.php 51 KB

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