| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace app\modules\client\models\v1;
- use app\models\QAndAQa;
- use app\models\QAndASetting;
- use app\utils\DeepSeek\DeepSeekAiTools;
- use Exception;
- use yii\base\Model;
- class QAndAForm extends Model
- {
- public $question;
- public $user;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['question', 'user'], 'required'],
- ];
- }
- public function save()
- {
- if (!$this->validate())
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0]
- ];
- try {
- $user = $this->user;
- $setting = QAndASetting::find()->where(['store_id' => get_store_id()])->one();
- if ($setting && $setting->q_integral > 0) {
- if ($user->integral < $setting->q_integral) {
- throw new Exception('积分不足, 请先充值后再提问!');
- }
- }
- try {
- $daan = DeepSeekAiTools::send($setting->key, $this->question);
- if ($daan['code'] != 0) {
- throw new Exception($daan['msg']);
- }
- } catch (\Throwable $e) {
- return [
- 'code' => 0,
- 'data' => [
- 'text' => '请求失败,请检查配置是否正确',
- 'time' => date("Y-m-d H:i:s"),
- ]
- ];
- }
-
- $time = time();
- $answer = $daan['data'];
- $q = new QAndAQa();
- $q->store_id = get_store_id();
- $q->user_id = $user->id;
- $q->question = $this->question;
- $q->answer = $answer;
- $q->created_at = $time;
- $q->answer_time = $time;
- $q->use_integral = $setting ? $setting->q_integral : 0;
- if ($q->save()) {
- return [
- 'code' => 0,
- 'data' => [
- 'text' => $answer,
- 'time' => date("Y-m-d H:i:s", $q->answer_time),
- ]
- ];
- } else {
- throw new Exception($q->getErrorSummary(false)[0]);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'data' => [
- 'text' => $e->getMessage(),
- 'time' => date("Y-m-d H:i:s"),
- ],
- ];
- }
- }
- }
|