Message.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use GatewayClient\Gateway;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Model;
  8. use tools\Qxsms;
  9. class Message extends Model
  10. {
  11. public function add($type,$busId,$busType,$userId,$orgId,$context,$sms=0){
  12. $data = [
  13. 'type' => $type,
  14. 'bus_id' => $busId,
  15. 'bus_type' => $busType,
  16. 'user_id' => $userId,
  17. 'org_id' => $orgId,
  18. 'context' => $context,
  19. 'status' => 0,
  20. 'create_time' => date('Y-m-d H:i:s')
  21. ];
  22. $ret = Db::name('message')->insert($data);
  23. if($ret){
  24. try{
  25. // websocket推送
  26. Gateway::$registerAddress = config('app.gateway_register_ip');
  27. if(Gateway::isUidOnline($userId)){ // 在线发送消息
  28. // Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
  29. }
  30. if($sms == 1){
  31. // $this->sendsms($userId,$context);
  32. }else{
  33. //TODO:: 需要加队列执行耗时较多的极光推送和短信
  34. if($type == 2||$type == 1){
  35. // 短信推送
  36. // $this->sendsms($userId,$context);
  37. }
  38. if($type == 3 && $busType == 12){ //工资审批提醒
  39. // 短信推送
  40. // $this->sendsms($userId,$context);
  41. }
  42. }
  43. // 极光推送
  44. $extras = array(); // 额外参数
  45. // send_jpush([(string)$userId],$context,$extras);
  46. }catch (\Exception $e){
  47. trace('error:'.$e->getMessage());
  48. }
  49. }
  50. return $ret?true:false;
  51. }
  52. private function sendsms($userId,$content){
  53. $phone = Db::name('user')->where('id',$userId)->value('phone');
  54. $turn = Db::name('sms_config')->where('type',1)->value('value');
  55. if($phone && $turn){
  56. $qxsms = new Qxsms(config('app.sms_config'));
  57. $content = "【基建中心】".$content;
  58. $ret = $qxsms->send($phone,$content);
  59. return $ret;
  60. }
  61. return true;
  62. }
  63. public function lists($page,$size,$userId,$orgId,$type,$context,$status){
  64. $map[] = ['user_id','=',$userId];
  65. $map[] = ['org_id','=',$orgId];
  66. if($context != ''){
  67. $map[] = ['context','like','%'.$context.'%'];
  68. }
  69. if($type > 0){
  70. $map[] = ['type','=',$type];
  71. }
  72. if($status > 0){
  73. $map[] = ['status','=',$status];
  74. }
  75. $lists = $this
  76. ->where($map)
  77. ->page($page,$size)
  78. ->order('id desc')
  79. ->select();
  80. foreach ($lists as $k=>$v){
  81. if($v['bus_type'] == 2){
  82. $applyid = Db::name('apply_record')->where('del',0)->where('id',$v['bus_id'])->value('apply_id');
  83. $lists[$k]['bus_id'] = $applyid?$applyid:0;
  84. }
  85. }
  86. $total = $this->where($map)->count();
  87. $data = [
  88. 'total' => $total,
  89. 'list' => $lists?$lists->toArray():[]
  90. ];
  91. return $data;
  92. }
  93. // 消息未读数量
  94. public function selUnreadNum($userId,$orgId){
  95. $map[] = ['user_id','=',$userId];
  96. $map[] = ['org_id','=',$orgId];
  97. $map[] = ['status','=',0];
  98. $total = $this->where($map)->count();
  99. return $total;
  100. }
  101. // 标记为已读
  102. public function updataStatus($id,$userId){
  103. $ret = Db::name('message')->where('id',$id)->where('user_id',$userId)->where('status',0)->setField('status',1);
  104. if($ret){
  105. // websocket推送
  106. Gateway::$registerAddress = config('app.gateway_register_ip');
  107. if(Gateway::isUidOnline($userId)){ // 在线发送消息
  108. Gateway::sendToUid($userId, json_encode(['type'=>'msgcount','data'=>[],'msg'=>'']));
  109. }
  110. }
  111. }
  112. // 全部已读
  113. public function batcheUpdateStatus($type,$userId){
  114. $map[] = ['status','=',0];
  115. $map[] = ['user_id','=',$userId];
  116. if($type > 0){
  117. $map[] = ['type','=',$type];
  118. }
  119. Db::name('message')->where($map)->setField('status',1);
  120. if($type == 0){
  121. //工作动态报送未读的改成已读
  122. Db::name('work_news_receiver')->where('user_id',$userId)->where('is_read',0)->update([
  123. 'is_read' => 1,
  124. 'read_time' => date('Y-m-d H:i:')
  125. ]);
  126. }
  127. }
  128. // 删除
  129. public function batcheDelete($ids,$userId){
  130. Db::name('message')->where('id','in',$ids)->where('user_id',$userId)->delete();
  131. }
  132. // 系统消息通知列表
  133. public function selMessageByOrg($page,$size,$userId){
  134. $map[] = ['type','=',4];
  135. $map[] = ['user_id','=',$userId];
  136. $lists = $this
  137. ->where($map)
  138. ->page($page,$size)
  139. ->order('id desc')
  140. ->select();
  141. return $lists?$lists->toArray():[];
  142. }
  143. public function queryNewsList($userId,$orgId){
  144. $data = [
  145. [
  146. 'id' => 0,
  147. 'name' => '公告通知',
  148. 'cate' => 1,
  149. 'type' => 0,
  150. 'title' => '',
  151. 'nums' => 0
  152. ],
  153. [
  154. 'id' => 0,
  155. 'name' => '流程审批',
  156. 'cate' => 2,
  157. 'type' => 0,
  158. 'title' => '',
  159. 'nums' => 0
  160. ],
  161. [
  162. 'id' => 0,
  163. 'name' => '工作提醒',
  164. 'cate' => 3,
  165. 'type' => 0,
  166. 'title' => '',
  167. 'nums' => 0
  168. ],
  169. [
  170. 'id' => 0,
  171. 'name' => '工作汇报',
  172. 'cate' => 5,
  173. 'type' => 0,
  174. 'title' => '',
  175. 'nums' => 0
  176. ],
  177. [
  178. 'id' => 0,
  179. 'name' => '党建纪检',
  180. 'cate' => 6,
  181. 'type' => 0,
  182. 'title' => '',
  183. 'nums' => 0
  184. ],
  185. [
  186. 'id' => 0,
  187. 'name' => '系统消息',
  188. 'cate' => 4,
  189. 'type' => 0,
  190. 'title' => '',
  191. 'nums' => 0
  192. ],
  193. ];
  194. foreach ($data as $k=>$v){
  195. if($v['cate'] == 4){
  196. $org = 0;
  197. }else{
  198. $org = $orgId;
  199. }
  200. $info = Db::name('message')
  201. ->where('type',$v['cate'])
  202. ->where('org_id',$org)
  203. ->where('user_id',$userId)
  204. ->order('id desc')
  205. ->field('id,context as title,create_time')
  206. ->find();
  207. if($info){
  208. $data[$k]['title'] = $info['title'];
  209. $data[$k]['id'] = $info['id'];
  210. $data[$k]['title'] = $info['title'];
  211. $nums = Db::name('message')
  212. ->where('type',$v['cate'])
  213. ->where('org_id',$org)
  214. ->where('status',0)
  215. ->where('user_id',$userId)
  216. ->count();
  217. $data[$k]['nums'] = $nums;
  218. $data[$k]['type'] = $nums > 0 ? 1 : 0;
  219. }
  220. }
  221. return $data;
  222. }
  223. // 工作提醒列表
  224. public function selMessageWork($page,$size,$userId,$orgId){
  225. $map[] = ['type','=',3];
  226. $map[] = ['user_id','=',$userId];
  227. $map[] = ['org_id','=',$orgId];
  228. $lists = $this
  229. ->where($map)
  230. ->page($page,$size)
  231. ->order('id desc')
  232. ->select();
  233. $lists = $lists?$lists->toArray():[];
  234. foreach ($lists as $k=>$v){
  235. if($v['bus_type'] == 2){
  236. $lists[$k]['bus_id'] = Db::name('apply_record')->where('del',0)->where('id',$v['bus_id'])->value('apply_id');
  237. }
  238. }
  239. return $lists;
  240. }
  241. // 工作通知列表
  242. public function queryWorkNotice($page,$size,$userId,$orgId){
  243. $map[] = ['type','=',2];
  244. $map[] = ['user_id','=',$userId];
  245. $map[] = ['org_id','=',$orgId];
  246. $map[] = ['bus_type','=',2];
  247. $lists = Db::name('message')
  248. ->where($map)
  249. ->field('create_time as mcreate_time,type,bus_id')
  250. ->page($page,$size)
  251. ->order('id desc')
  252. ->select();
  253. $lists = $lists?$lists:[];
  254. foreach ($lists as $k=>$v){
  255. $info = Db::name('apply_record')
  256. ->alias('ar')
  257. ->join('apply a','a.id = ar.apply_id')
  258. ->join('user_info ui','ui.user_id = a.user_id')
  259. ->where('ar.id',$v['bus_id'])
  260. ->where('ar.del',0)
  261. ->field('ui.name as userName,a.create_time,a.apply_sn,ar.apply_id,ar.status,a.title as applyTitle,ar.type as arType,ar.id as arId')
  262. ->find();
  263. $lists[$k]['userName'] = $info['userName'];
  264. $lists[$k]['applySn'] = $info['apply_sn'];
  265. $lists[$k]['applyId'] = $info['apply_id'];
  266. $lists[$k]['status'] = $info['status'];
  267. $lists[$k]['applyTitle'] = $info['applyTitle'];
  268. $lists[$k]['arType'] = $info['arType'];
  269. $lists[$k]['createTime'] = $info['create_time'];
  270. $lists[$k]['arId'] = $info['arId'];
  271. $lists[$k]['workLogId'] = 0;
  272. $lists[$k]['contentOne'] = '';
  273. $lists[$k]['cate'] = 1;
  274. // if($info['arType'] == 2||$info['arType'] == 8){
  275. // $lists[$k]['cate'] = 1;
  276. // }else{
  277. // $lists[$k]['cate'] = 2;
  278. // }
  279. unset($lists[$k]['bus_id']);
  280. }
  281. return $lists;
  282. }
  283. // 工作汇报列表
  284. public function selMessageReport($page,$size,$userId,$orgId){
  285. $map[] = ['type','=',5];
  286. $map[] = ['user_id','=',$userId];
  287. $map[] = ['org_id','=',$orgId];
  288. $lists = $this
  289. ->where($map)
  290. ->page($page,$size)
  291. ->order('id desc')
  292. ->select();
  293. $lists = $lists?$lists->toArray():[];
  294. foreach ($lists as $k=>$v){
  295. $reportType = Db::name('work_report_receiver')
  296. ->alias('wrr')
  297. ->join('work_report wr','wr.id = wrr.work_report_id')
  298. ->where('wrr.id',$v['bus_id'])
  299. ->value('wr.work_type_id');
  300. $lists[$k]['report_type'] = $reportType;
  301. }
  302. return $lists;
  303. }
  304. }