Docs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Module;
  3. use App\Tasks\PushTask;
  4. use Cache;
  5. use DB;
  6. use Hhxsv5\LaravelS\Swoole\Task\Task;
  7. /**
  8. * Class Docs
  9. * @package App\Module
  10. */
  11. class Docs
  12. {
  13. /**
  14. * 检验是否有阅读或修改权限
  15. * @param $bookid
  16. * @param string $checkType edit|view
  17. * @return array|mixed
  18. */
  19. public static function checkRole($bookid, $checkType = 'edit')
  20. {
  21. $row = Base::DBC2A(DB::table('docs_book')->where('id', $bookid)->first());
  22. if (empty($row)) {
  23. return Base::retError('知识库不存在或已被删除!', -1000);
  24. }
  25. $userE = Users::authE();
  26. if (Base::isError($userE)) {
  27. $user = [];
  28. } else {
  29. $user = $userE['data'];
  30. }
  31. $checkType = $checkType == 'edit' ? 'edit' : 'view';
  32. if ($checkType == 'edit') {
  33. if (empty($user)) {
  34. return $userE;
  35. }
  36. } else {
  37. if ($row['role_view'] != 'all') {
  38. if (empty($user)) {
  39. return Base::retError('知识库仅对会员开放,请登录后再试!', -1001);
  40. }
  41. }
  42. }
  43. if ($user['username'] == $row['username']) {
  44. return Base::retSuccess('success');
  45. }
  46. //
  47. if ($row['role_' . $checkType] == 'member') {
  48. if (!DB::table('docs_users')->where('bookid', $bookid)->where('username', $user['username'])->exists()) {
  49. return Base::retError('知识库仅对成员开放!', $checkType == 'edit' && $row['role_look'] == 'reg' ? 1002 : -1002);
  50. }
  51. } elseif ($row['role_' . $checkType] == 'private') {
  52. if ($row['username'] != $user['username']) {
  53. return Base::retError('知识库仅对作者开放!', $checkType == 'edit' && $row['role_look'] == 'reg' ? 1003 : -1003);
  54. }
  55. }
  56. //
  57. return Base::retSuccess('success');
  58. }
  59. /**
  60. * 通知正在编辑的成员
  61. *
  62. * @param integer $sid 章节ID
  63. * @param array $bodyArray body参数
  64. */
  65. public static function notice($sid, $bodyArray = [])
  66. {
  67. $user = Users::auth();
  68. $array = Base::json2array(Cache::get("docs::" . $sid));
  69. if ($array) {
  70. foreach ($array as $uname => $vbody) {
  71. if (intval($vbody['indate']) + 20 < time()) {
  72. unset($array[$uname]);
  73. }
  74. }
  75. }
  76. $pushLists = [];
  77. if ($array) {
  78. foreach ($array AS $tuser) {
  79. $uLists = Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where('username', $tuser['username'])->get());
  80. foreach ($uLists AS $item) {
  81. if ($item['username'] == $user['username']) {
  82. continue;
  83. }
  84. $pushLists[] = [
  85. 'fd' => $item['fd'],
  86. 'msg' => [
  87. 'messageType' => 'docs',
  88. 'body' => array_merge([
  89. 'sid' => $sid,
  90. 'nickname' => $user['nickname'] ?: $user['username'],
  91. 'time' => time(),
  92. ], $bodyArray)
  93. ]
  94. ];
  95. }
  96. }
  97. }
  98. $pushTask = new PushTask($pushLists);
  99. Task::deliver($pushTask);
  100. }
  101. }