From bc834ce893fa2300f7aceefdc7920c69d97c7f5a Mon Sep 17 00:00:00 2001 From: dingyongliang Date: Thu, 23 Nov 2023 16:46:42 +0800 Subject: [PATCH] * add discovery category. --- module/ai/control.php | 65 +++++++++++++++++++++++++++------ module/ai/model.php | 85 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 131 insertions(+), 19 deletions(-) diff --git a/module/ai/control.php b/module/ai/control.php index 1483ab5a5d..5695c789c9 100644 --- a/module/ai/control.php +++ b/module/ai/control.php @@ -135,6 +135,16 @@ class ai extends control $this->display(); } + /** + * Mini program square. + * + * @param string $category + * @param int $recTotal + * @param int $recPerPage + * @param int $pageID + * @access public + * @return void + */ public function square($category = 'discovery', $recTotal = 0, $recPerPage = 15, $pageID = 1) { $this->app->loadClass('pager', true); @@ -148,7 +158,8 @@ class ai extends control } else if($category === 'discovery') { - + $popularIDs = $this->ai->getPopularMiniProgramIDs($pager); + $miniPrograms = $this->ai->getMiniProgramsByID($popularIDs); } else if($category === 'latest') { @@ -161,13 +172,21 @@ class ai extends control $this->view->collectedIDs = empty($collectedIDs) ? $this->ai->getCollectedMiniProgramIDs($this->app->user->id) : $collectedIDs; $this->view->category = $category; - $this->view->categoryList = array_merge($this->lang->ai->miniPrograms->squareCategories, $this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategory()); + $this->view->categoryList = array_merge($this->lang->ai->miniPrograms->squareCategories, $this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategories()); $this->view->pager = $pager; $this->view->miniPrograms = $miniPrograms ?: array(); $this->view->title = $this->lang->ai->miniPrograms->common; $this->display(); } + /** + * Collect mini program by appid. + * + * @param string $appID + * @param string $delete + * @access public + * @return void + */ public function collectMiniProgram($appID, $delete = 'false') { $this->ai->collectMiniProgram($this->app->user->id, $appID, $delete); @@ -185,7 +204,7 @@ class ai extends control $this->app->loadClass('pager', true); $pager = new pager($recTotal, $recPerPage, $pageID); $order = common::appendOrder($orderBy); - $categoryList = array_merge($this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategory()); + $categoryList = array_merge($this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategories()); $programs = $this->ai->getMiniPrograms($category, $status, $order, $pager); foreach($programs as $program) @@ -208,14 +227,20 @@ class ai extends control $this->display(); } + /** + * Edit mini program category. + * + * @access public + * @return void + */ public function editMiniProgramCategory() { if(!empty($_POST)) { - $this->ai->updateCustomCategory(); + $this->ai->updateCustomCategories(); } - $this->view->categoryList = $this->ai->getCustomCategory(); + $this->view->categoryList = $this->ai->getCustomCategories(); $this->view->title = $this->lang->ai->miniPrograms->common; $this->display(); } @@ -250,6 +275,13 @@ class ai extends control $this->sendError(dao::getError()); } + /** + * Test mini program. + * + * @param string $appID + * @access public + * @return void + */ public function testMiniProgram($appID) { if(!empty($_POST)) @@ -306,17 +338,24 @@ class ai extends control $this->view->iconName = 'writinghand'; $this->view->iconTheme = 7; } - $this->view->categoryList = $this->ai->getCustomCategory(); + $this->view->categoryList = $this->ai->getCustomCategories(); $this->view->title = $this->lang->ai->miniPrograms->common; $this->display(); } + /** + * Mini program view. + * + * @param string $id + * @access public + * @return void + */ public function miniProgramView($id) { $miniProgram = $this->ai->getMiniProgramByID($id); $this->view->miniProgram = $miniProgram; - $this->view->categoryList = array_merge($this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategory()); + $this->view->categoryList = array_merge($this->lang->ai->miniPrograms->categoryList, $this->ai->getCustomCategories()); $this->view->preAndNext = $this->loadModel('common')->getPreAndNextObject('miniProgram', $id); $this->view->actions = $this->loadModel('action')->getList('miniProgram', $id); $this->view->fields = $this->ai->getMiniProgramFields($id); @@ -760,7 +799,8 @@ class ai extends control $output = array('result' => 'fail', 'message' => sprintf($this->lang->ai->execute->failFormat, $e->getMessage())); /* Audition shall quit on such exception. */ - if(isset($_SESSION['auditPrompt']) && time() - $_SESSION['auditPrompt']['time'] < 10 * 60) { + if(isset($_SESSION['auditPrompt']) && time() - $_SESSION['auditPrompt']['time'] < 10 * 60) + { $output['locate'] = $this->inlink('promptAudit', "promptID=$promptId&objectId=$objectId&exit=true"); } return $this->send($output); @@ -808,14 +848,16 @@ class ai extends control */ public function promptAudit($promptId, $objectId, $exit = false) { - if(!empty($exit)) { + if(!empty($exit)) + { unset($_SESSION['auditPrompt']); return $this->send(array('result' => 'success', 'locate' => $this->inlink('promptview', "promptID=$promptId") . '#app=admin')); } $prompt = $this->ai->getPromptByID($promptId); - if($_POST) { + if($_POST) + { $data = fixer::input('post')->get(); $originalPrompt = clone $prompt; @@ -906,7 +948,8 @@ class ai extends control { $data = fixer::input('post')->get(); $method = $data->method; - switch ($method) { + switch ($method) + { case 'create': $this->ai->createRoleTemplate($data->role, $data->characterization); break; diff --git a/module/ai/model.php b/module/ai/model.php index bc125619ad..84a26dd594 100644 --- a/module/ai/model.php +++ b/module/ai/model.php @@ -162,7 +162,8 @@ class aiModel extends model $logFile = $app->getLogRoot() . 'saas.' . date('Ymd') . '.log.php'; if(!file_exists($logFile)) file_put_contents($logFile, ''); $fh = @fopen($logFile, 'a'); - if($fh) { + if($fh) + { fwrite($fh, date('Ymd H:i:s') . ": " . $app->getURI() . ' AI Request' . "\n"); fwrite($fh, "postData: " . print_r($data, true) . "\n"); fwrite($fh, "results:" . print_r($result, true) . "\n"); @@ -286,7 +287,8 @@ class aiModel extends model { function json_last_error_msg() { - switch (json_last_error()) { + switch (json_last_error()) + { case JSON_ERROR_DEPTH: return 'Maximum stack depth exceeded'; case JSON_ERROR_STATE_MISMATCH: @@ -309,10 +311,27 @@ class aiModel extends model return $response; } + /** + * Get popular mini programIDs. + * + * @param pager $pager + * @access public + * @return array + */ + public function getPopularMiniProgramIDs($pager = null) + { + $paris = $this->dao->select('appID, COUNT(*) AS count')->from(TABLE_MINIPROGRAMSTAR) + ->groupBy('appID') + ->orderBy('count_desc') + ->page($pager) + ->fetchPairs(); + return array_keys($paris); + } + /** * Get mini programs by appid. * - * @param array $id + * @param array $ids * @access public * @return object */ @@ -339,6 +358,14 @@ class aiModel extends model ->fetch(); } + /** + * Get collected mini programIDs + * + * @param string $userID + * @param pager $pager + * @access public + * @return array + */ public function getCollectedMiniProgramIDs($userID, $pager = null) { $programs = $this->dao->select('*') @@ -350,6 +377,15 @@ class aiModel extends model return array_keys($programs); } + /** + * Collect mini program. + * + * @param string $userID + * @param string $appID + * @param string $delete + * @access public + * @return bool + */ public function collectMiniProgram($userID, $appID, $delete = 'false') { if($delete === 'true') @@ -373,7 +409,13 @@ class aiModel extends model return !dao::isError(); } - public function getCustomCategory() + /** + * Get custom categories. + * + * @access public + * @return array + */ + public function getCustomCategories() { return $this->dao->select('`key`,`value`') ->from(TABLE_CONFIG) @@ -382,7 +424,13 @@ class aiModel extends model ->fetchPairs(); } - public function updateCustomCategory() + /** + * Update custom categories. + * + * @access public + * @return void + */ + public function updateCustomCategories() { $data = array_filter($_POST); if(empty($data)) return; @@ -435,6 +483,10 @@ class aiModel extends model /** * Get not deleted mini programs. * + * @param string $category + * @param string $status + * @param string $order + * @param pager $pager * @access public * @return array */ @@ -454,6 +506,13 @@ class aiModel extends model return $progarms; } + /** + * Get mini program fields by appid. + * + * @param string $appID + * @access public + * @return array + */ public function getMiniProgramFields($appID) { return $this->dao->select('*') @@ -513,7 +572,8 @@ class aiModel extends model */ public function canPublishMiniProgram($program) { - $isNotEmpty = function ($str) { + $isNotEmpty = function ($str) + { return isset($str) && strlen(strval($str)) > 0; }; @@ -570,6 +630,13 @@ class aiModel extends model return $appID; } + /** + * Save mini program fields. + * + * @param string $appID + * @access public + * @return void + */ public function saveMiniProgramFields($appID) { $data = fixer::input('post')->get(); @@ -647,7 +714,8 @@ class aiModel extends model if(empty($response)) return false; /* Extract text response choices. */ - if(isset($response->choices) && count($response->choices) > 0) { + if(isset($response->choices) && count($response->choices) > 0) + { $texts = array(); foreach($response->choices as $choice) $texts[] = $choice->text; return $texts; @@ -1103,7 +1171,8 @@ class aiModel extends model $supplement = ''; $supplementTypes = array(); - foreach($sources as $source) { + foreach($sources as $source) + { $objectName = $source[0]; $objectKey = $source[1];