UserMessage.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use JPush\Client;
  5. require_once env('root_path').'vendor/jpush/jpush/autoload.php';
  6. // 会员消息
  7. class UserMessage extends Model
  8. {
  9. // ['$module'=>['$type'=>['title'=>'标题','msg'=>['$msg_key'=>'','$msg_key'=>'']]]
  10. static $type_desc = [
  11. 'video'=>[
  12. 1=>['title'=>'视频投稿','msg'=>['视频投稿成功请等待审核']],
  13. 2=>['title'=>'视频审核','msg'=>['恭喜您视频审核成功','抱歉啦视频审核失败~~']],
  14. 3=>['title'=>'视频评论回复','msg'=>['有会员评论了您的回复']],
  15. 4=>['title'=>'视频评论','msg'=>['有会员评论了您的视频']],
  16. 5=>['title'=>'追更','msg'=>['您追更的视频有更新']],
  17. ],// 视频
  18. 'article'=>[
  19. 1=>['title'=>'图文投稿','msg'=>['图文投稿成功请等待审核']],
  20. 2=>['title'=>'图文审核','msg'=>['恭喜您图文审核成功','抱歉啦图文审核失败~~']],
  21. 3=>['title'=>'图文评论回复','msg'=>['有会员评论了您的回复']],
  22. 4=>['title'=>'图文评论','msg'=>['有会员评论了您的图文']],
  23. ],//图文
  24. 'datum'=>[
  25. 1=>['title'=>'资料投稿','msg'=>['资料投稿成功请等待审核']],
  26. 2=>['title'=>'资料审核','msg'=>['恭喜您资料审核成功','抱歉啦资料审核失败~~']],
  27. 3=>['title'=>'资料评论回复','msg'=>['有会员评论了您的回复']],
  28. ],// 资料
  29. 'activity'=>[
  30. 1=>['title'=>'活动报名','msg'=>['恭喜您活动报名成功']],
  31. ],// 活动
  32. 'demand'=>[
  33. 1=>['title'=>'需求投稿','msg'=>['需求投稿成功请等待审核']],
  34. 2=>['title'=>'需求审核','msg'=>['恭喜您需求审核成功','抱歉啦需求审核失败~~']],
  35. 3=>['title'=>'需求评论回复','msg'=>['有会员评论了您的回复']],
  36. 4=>['title'=>'需求评论','msg'=>['有会员评论了您的需求']],
  37. ],// 需求
  38. 'forum'=>[
  39. 1=>['title'=>'问答投稿','msg'=>['问答投稿成功']],
  40. 2=>['title'=>'问答审核','msg'=>['恭喜您问答审核成功','抱歉啦问答审核失败']],
  41. 3=>['title'=>'回复的评论','msg'=>['有会员评论了您的回复']],
  42. 4=>['title'=>'评论的回复','msg'=>['有会员回复了您的评论']],
  43. 5=>['title'=>'问答回复','msg'=>['有会员回复了您的问题']],
  44. ],// 问答
  45. 'press'=>[
  46. 1=>['title'=>'评论回复','msg'=>['有会员回复了您的评论']],
  47. ],// 新闻
  48. 'mall'=>[
  49. ],// 商城
  50. 'feedback'=>[
  51. 1=>['title'=>'反馈内容回复','msg'=>['平台回复了您的反馈']],
  52. ],// 反馈
  53. 'user' =>[
  54. 1=>['title'=>'反馈内容回复','msg'=>['平台回复了您的反馈']],
  55. ],// 会员管理
  56. ];
  57. /**
  58. * 发送会员消息
  59. * @param $to_user 发送给谁
  60. * @param $module 模块
  61. * @param $type 模块类型
  62. * @param $msg_key 内容key[msg的key]
  63. * @param $from_user 哪个会员发送的
  64. * @param $rel_id 关联的id
  65. * @param string $content
  66. */
  67. public static function sendUserMessage($to_user,$module,$type,$msg_key,$from_user,$rel_id,$content = '')
  68. {
  69. $message = [
  70. 'user_id'=>$to_user,
  71. 'from_user'=>$from_user,
  72. 'content'=> $content ? $content : static::$type_desc[$module][$type]['msg'][$msg_key] ,
  73. 'relation_id'=>$rel_id,
  74. 'type_id'=>$type,
  75. 'module'=>$module,
  76. 'title'=>static::$type_desc[$module][$type]['title'],
  77. ];
  78. $from_name = User::where('id',$from_user)->value('name');
  79. switch ($module) {
  80. case 'video':
  81. if(in_array($type,[3,4]))Jpush($to_user,$from_name.$message['content'],$module,$type,$rel_id);// $message['jg_type'] = 1;
  82. break;
  83. case 'article':
  84. if(in_array($type,[3,4]))Jpush($to_user,$from_name.$message['content'],$module,$type,$rel_id);// $message['jg_type'] = 1;
  85. break;
  86. case 'press':
  87. if(in_array($type,[1])) Jpush($to_user,$from_name.$message['content'],$module,$type,$rel_id);// $message['jg_type'] = 1;
  88. break;
  89. case 'forum':
  90. $switch = PlatformSwitch::checkSwitch($to_user,$rel_id,1);
  91. if($switch && in_array($type,[3,4,5]))Jpush($to_user,$from_name.$message['content'],$module,$type,$rel_id);// $message['jg_type'] = 1;
  92. break;
  93. case 'feedback':
  94. if(in_array($type,[1]))Jpush($to_user,$from_name.$message['content'],$module,$type,$rel_id);// $message['jg_type'] = 1;
  95. break;
  96. }
  97. self::create($message);
  98. // 哪些模块中的某些类型需要极光推送
  99. }
  100. /**
  101. * 获取消息未读数量
  102. * @param $user_id 发送给谁
  103. * @param $module 模块
  104. * @param $type 模块类型
  105. * @param $msg_key 内容key[msg的key]
  106. */
  107. public static function getUnreadNum($user_id,$module,$type)
  108. {
  109. return static::where(['user_id'=>$user_id,'module'=>$module,'type'=>$type,'is_read'=>0])->count();
  110. }
  111. }