zentaobiz.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * 获取知识库在 ZAI 的 key。
  4. * Get key of knowledge base in ZAI.
  5. *
  6. * @access public
  7. * @param string|int $collection
  8. * @return string
  9. */
  10. public function getCollectionKey($collection)
  11. {
  12. return $this->loadExtension('zentaobiz')->getCollectionKey($collection);
  13. }
  14. /**
  15. * 判断用户是否可以查看对象。
  16. * Check if user can view object.
  17. *
  18. * @access public
  19. * @param string $objectType
  20. * @param int $objectID
  21. * @param array|null $attrs
  22. * @return bool
  23. */
  24. public function canViewObject($objectType, $objectID, $attrs = null)
  25. {
  26. if($this->app->user->admin) return true;
  27. $knowledgeLibID = $attrs && isset($attrs['knowledgeLib']) ? $attrs['knowledgeLib'] : 0;
  28. if($objectType === 'knowledge' || $knowledgeLibID)
  29. {
  30. if(empty($knowledgeLibID) && $objectType === 'knowledge')
  31. {
  32. $knowledge = $this->loadModel('ai')->getKnowledgeItemByID($objectID);
  33. if(!$knowledge || $knowledge->deleted) return false;
  34. $knowledgeLibID = $knowledge->lib;
  35. }
  36. $knowledgeLib = $knowledgeLibID ? $this->loadModel('ai')->getKnowledgeLibByID($knowledgeLibID) : null;
  37. $canViewKnowledgeLib = $knowledgeLib ? $this->loadModel('ai')->checkKnowledgeLibPriv($knowledgeLib) : false;
  38. if(!$canViewKnowledgeLib || $objectType === 'knowledge' || $objectType === 'file') return $canViewKnowledgeLib;
  39. }
  40. if($objectType === 'plan')
  41. {
  42. $product = $attrs && isset($attrs['product']) ? $attrs['product'] : 0;
  43. if(empty($product))
  44. {
  45. $plan = $this->loadModel('productplan')->getByID($objectID);
  46. if(!$plan) return false;
  47. $product = $plan->product;
  48. }
  49. return strpos(',' . $this->app->user->view->products . ',', ",$product,") !== false;
  50. }
  51. if($objectType === 'release' || $objectType === 'ticket' || $objectType === 'issue')
  52. {
  53. $release = $this->loadModel($objectType)->getByID($objectID);
  54. if(!$release) return false;
  55. if($release->product && strpos(',' . $this->app->user->view->products . ',', ",$release->product,") !== false) return true;
  56. if($release->project)
  57. {
  58. $projectIDList = explode(',', $release->project);
  59. foreach($projectIDList as $projectID)
  60. {
  61. if(strpos(',' . $this->app->user->view->projects . ',', ",$projectID,") === false) return false;
  62. }
  63. return true;
  64. }
  65. return false;
  66. }
  67. if($objectType === 'risk' || $objectType === 'opportunity')
  68. {
  69. $risk = $this->loadModel($objectType)->getByID($objectID);
  70. if(!$risk) return false;
  71. if($risk->project && strpos(',' . $this->app->user->view->projects . ',', ",$risk->product,") !== false) return true;
  72. return false;
  73. }
  74. return parent::canViewObject($objectType, $objectID, $attrs);
  75. }
  76. /**
  77. * 创建聊天。
  78. * Create chat.
  79. *
  80. * @access public
  81. * @param string $title
  82. * @param array|string $prompts
  83. * @param array $settings
  84. * @param array $customData
  85. * @param array $references
  86. * @return array|false
  87. */
  88. public function createChat($title, $prompts, $settings = [], $customData = [], $references = [])
  89. {
  90. return $this->loadExtension('zentaobiz')->createChat($title, $prompts, $settings, $customData, $references);
  91. }
  92. /**
  93. * 准备消息。
  94. * Prepare messages for posting to a chat.
  95. *
  96. * @access public
  97. * @param string|object|array $messages
  98. * @return array
  99. */
  100. public function prepareMessages($messages)
  101. {
  102. return $this->loadExtension('zentaobiz')->prepareMessages($messages);
  103. }
  104. /**
  105. * 发送消息到聊天。
  106. * Send message to chat.
  107. *
  108. * @access public
  109. * @param string $chatID
  110. * @param string|object|array $message
  111. * @param array $tools
  112. * @param callable|Collator|null $onSaveMessage
  113. * @param callable|Collator|null $onCallTool
  114. * @param string|null $bgid
  115. * @return array|false
  116. */
  117. public function postMessage($chatID, $message, $tools = [], $onSaveMessage = null, $onCallTool = null, $bgid = null)
  118. {
  119. return $this->loadExtension('zentaobiz')->postMessage($chatID, $message, $tools, $onSaveMessage, $onCallTool, $bgid);
  120. }
  121. /**
  122. * 获取聊天。
  123. * Get chat.
  124. *
  125. * @access public
  126. * @param string $chatID
  127. * @return array|false
  128. */
  129. public function getChat($chatID)
  130. {
  131. return $this->loadExtension('zentaobiz')->getChat($chatID);
  132. }
  133. /**
  134. * 更新聊天。
  135. * Update chat.
  136. *
  137. * @access public
  138. * @param string $chatID
  139. * @param array $data
  140. * @return array|false
  141. */
  142. public function updateChat($chatID, $data)
  143. {
  144. return $this->loadExtension('zentaobiz')->updateChat($chatID, $data);
  145. }
  146. /**
  147. * 更新聊天标题。
  148. * Update chat title.
  149. *
  150. * @access public
  151. * @param string $chatID
  152. * @param string $title
  153. * @return array|false
  154. */
  155. public function updateChatTitle($chatID, $title)
  156. {
  157. return $this->loadExtension('zentaobiz')->updateChatTitle($chatID, $title);
  158. }
  159. /**
  160. * 生成聊天标题。
  161. * Generate chat title.
  162. *
  163. * @access public
  164. * @param string $chatID
  165. * @return array|false
  166. */
  167. public function generateChatTitle($chatID)
  168. {
  169. return $this->loadExtension('zentaobiz')->generateChatTitle($chatID);
  170. }