Docs.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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('知识库不存在或已被删除!');
  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 ($row['role_' . $checkType] == 'member') {
  44. if (!DB::table('docs_users')->where('bookid', $bookid)->where('username', $user['username'])->exists()) {
  45. return Base::retError('知识库仅对成员开放!', -1002);
  46. }
  47. } elseif ($row['role_' . $checkType] == 'private') {
  48. if ($row['username'] != $user['username']) {
  49. return Base::retError('知识库仅对作者开放!', -1003);
  50. }
  51. }
  52. //
  53. return Base::retSuccess('success');
  54. }
  55. /**
  56. * 通知正在编辑的成员
  57. *
  58. * @param integer $sid 章节ID
  59. * @param array $bodyArray body参数
  60. */
  61. public static function notice($sid, $bodyArray = [])
  62. {
  63. $user = Users::auth();
  64. $array = Base::json2array(Cache::get("docs::" . $sid));
  65. if ($array) {
  66. foreach ($array as $uname => $vbody) {
  67. if (intval($vbody['indate']) + 20 < time()) {
  68. unset($array[$uname]);
  69. }
  70. }
  71. }
  72. $pushLists = [];
  73. foreach ($array AS $tuser) {
  74. $uLists = Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where('username', $tuser['username'])->get());
  75. foreach ($uLists AS $item) {
  76. if ($item['username'] == $user['username']) {
  77. continue;
  78. }
  79. $pushLists[] = [
  80. 'fd' => $item['fd'],
  81. 'msg' => [
  82. 'messageType' => 'docs',
  83. 'body' => array_merge([
  84. 'sid' => $sid,
  85. 'nickname' => $user['nickname'] ?: $user['username'],
  86. 'time' => time(),
  87. ], $bodyArray)
  88. ]
  89. ];
  90. }
  91. }
  92. $pushTask = new PushTask($pushLists);
  93. Task::deliver($pushTask);
  94. }
  95. }