Question.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\controller\api;
  3. use laytp\controller\Api;
  4. use laytp\library\Random;
  5. use think\facade\Db;
  6. use app\service\ConfServiceFacade;
  7. use app\service\MemberServiceFacade;
  8. use app\service\api\UserServiceFacade;
  9. use app\service\BillServiceFacade;
  10. use app\service\api\ImageCensorServiceFacade;
  11. use app\service\api\MiniappServiceFacade;//微信小程序服务
  12. /**
  13. * AI相关
  14. * @ApiWeigh (90)
  15. */
  16. class Question extends Api
  17. {
  18. protected function _initialize()
  19. {
  20. $this->model = new \app\model\Question();
  21. }
  22. public $noNeedLogin = [
  23. "list"
  24. ];
  25. // 使用记录
  26. public function list()
  27. {
  28. global $_GPC;
  29. $order = ['id' => 'desc'];
  30. $data = $this->model->where('uniacid','=',$_GPC['uniacid'])->order($order)->select()->toArray();
  31. if(!$data){
  32. return $this->error('数据获取失败');
  33. }
  34. return $this->success('数据获取成功', $data);
  35. }
  36. public function add()
  37. {
  38. global $_GPC;
  39. $isValidity = 0;
  40. $post = $this->request->post();
  41. $question = $this->request->post('question','');
  42. $answer = $this->request->post('answer','');
  43. $platform = $this->request->header('platform','H5');
  44. $conf = ConfServiceFacade::groupGet('system.config', 0);
  45. $type = $this->request->post('type','gpt35');
  46. $loginUserInfo = UserServiceFacade::getUserInfo();
  47. if(!$question){
  48. return $this->error('记录失败,请上传问题',2);
  49. }
  50. if(!$answer){
  51. return $this->error('记录失败,请上传回答',2);
  52. }
  53. if($type =='gpt35'){
  54. $coin = ConfServiceFacade::get('system.plan.unlock_gpt3',1);
  55. }else{
  56. $coin = ConfServiceFacade::get('system.plan.lock_gpt4',0);
  57. }
  58. $checkMember = MemberServiceFacade::check($loginUserInfo,$type,$coin,$conf);
  59. if(isset($checkMember['status']) && !$checkMember['status']){
  60. return $this->error($checkMember['msg'],2);
  61. }
  62. if($platform == 'wxMiniProgram'){
  63. $check = MiniappServiceFacade::msgSecCheck($answer,$loginUserInfo['openid_miniapp']);
  64. if(!$check){
  65. return $this->error('失败,文字内容安全检测不通过!');
  66. }
  67. }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
  68. if(!empty($conf) && !empty($conf['is_h5_filter']) && $conf['is_h5_filter'] == 1){
  69. $check = ImageCensorServiceFacade::textCensorUserDefined($answer);
  70. if(!empty($check['conclusion']) && $check['conclusion'] == '不合规' ){
  71. return $this->error('失败,文字内容安全检测不通过!');
  72. }
  73. }
  74. }
  75. if($coin >0){
  76. $checkMember = MemberServiceFacade::cash($loginUserInfo,$type,$coin,$conf);
  77. if(isset($checkMember['status']) && !$checkMember['status']){
  78. return $this->error($checkMember['msg'],2);
  79. }
  80. }
  81. $a = $this->model->save(['uid'=>$loginUserInfo['id'],'question'=>$question,'answer'=>$answer,'uniacid'=>$_GPC['uniacid'] ]);
  82. if(!$a){
  83. return $this->error('数据保存失败',2);
  84. }
  85. return $this->success('数据获取成功', $a);
  86. }
  87. // 使用记录
  88. public function my()
  89. {
  90. global $_GPC;
  91. $loginUserInfo = UserServiceFacade::getUserInfo();
  92. $order = ['id' => 'desc'];
  93. $where = ['uid' => $loginUserInfo['id'],'uniacid'=>$_GPC['uniacid']];
  94. $limit = $this->request->param('limit', 10);
  95. $data = $this->model->order($order)->where($where)->paginate($limit)->toArray();
  96. if(!$data){
  97. return $this->error('数据获取失败');
  98. }
  99. return $this->success('数据获取成功', $data);
  100. }
  101. public function del(){
  102. global $_GPC;
  103. $loginUserInfo = UserServiceFacade::getUserInfo();
  104. $id = $this->request->post('id');
  105. if (!$id) {
  106. return $this->error('参数id不能为空',2);
  107. }
  108. $jobUid = $this->model->where('id',$id)->value('uid');
  109. if($jobUid != $loginUserInfo['id']){
  110. return $this->error('您没有删除的权限');
  111. }
  112. try{
  113. $delRes = $this->model->destroy($id);
  114. if (!$delRes) {
  115. return $this->error('删除失败');
  116. }
  117. return $this->success('删除成功');
  118. }catch (\Exception $e){
  119. return $this->exceptionError($e);
  120. }
  121. }
  122. }