Chatgpt.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\controller;
  3. use app\model\chatgpt\ChatgptRecharge;
  4. use app\model\chatgpt\ChatgptOrder;
  5. use app\model\chatgpt\UserChatgptUseRecord;
  6. use app\model\User;
  7. use app\model\wxpay\Wxpay;
  8. use think\facade\Db;
  9. use think\facade\Request;
  10. /**
  11. * chatgpt
  12. */
  13. class Chatgpt extends CommonController
  14. {
  15. /**
  16. * 检测计数
  17. * 833456167
  18. */
  19. public function getAnswerByChatgpt()
  20. {
  21. $uid = Request::param('uid') ?? '';
  22. $question = Request::param('question') ?? '';
  23. if( ! $uid || ! $question) {
  24. return $this->_json_error('请求错误!');
  25. }
  26. $user = User::find($uid);
  27. if( ! $user) {
  28. return $this->_json_succ('请先注册后使用!');
  29. }
  30. if($user->chatgpt_num <= 0) {
  31. return $this->_json_succ('请支付使用!' , 1007);
  32. }
  33. $answer = Request::param('answer') ?? '';
  34. /**
  35. * 启用事务
  36. */
  37. Db::startTrans();
  38. try {
  39. /**
  40. * 同步使用记录
  41. */
  42. UserChatgptUseRecord::create(
  43. [
  44. 'uid' => $uid ,
  45. 'question' => $question,
  46. 'answer' => $answer ,
  47. 'createtime' => time()
  48. ]
  49. );
  50. /**
  51. * 更新使用次数
  52. */
  53. $user->chatgpt_num -= 1;
  54. $user->save();
  55. Db::commit();
  56. }catch(\Exception $e) {
  57. Db::rollback();
  58. return $this->_json_error(
  59. $e->getMessage()
  60. );
  61. }
  62. //$answer = $this->requestChatgpt($question);
  63. return $this->_json_succ(
  64. [
  65. 'question' => $question,
  66. 'answer' => $answer
  67. ]
  68. );
  69. }
  70. /**
  71. * chatgpt 请求
  72. * http://3.15.12.98:5000/chat?content=XXX
  73. * 3.5.12.98:8009
  74. * http://3.15.12.98:5001/chatstream
  75. */
  76. protected function requestChatgpt($question)
  77. {
  78. $url = "http://3.15.12.98:5000/chat?content=".$question;
  79. $result = file_get_contents($url);
  80. return $result;
  81. }
  82. /**
  83. * chatgpt充值次数列表
  84. */
  85. public function chatgptNumList()
  86. {
  87. return $this->_json_succ(
  88. ChatgptRecharge::field('id as recharge_id , name , money , num')->where('status' , 1)->select()->toArray()
  89. );
  90. }
  91. /**
  92. * 下单
  93. */
  94. public function generateOrder()
  95. {
  96. $params = Request::only(
  97. [
  98. 'uid' , 'recharge_id'
  99. ]
  100. );
  101. $user = User::find($params['uid'] ?? '');
  102. if( ! $user) {
  103. return $this->_json_error('未注册用户,请先注册!');
  104. }
  105. $chatgptRecharge = ChatgptRecharge::find($params['recharge_id'] ?? '');
  106. if( ! $chatgptRecharge) {
  107. return $this->_json_error('充值信息有误!');
  108. }
  109. $orderNo = $this->createOrderNum();
  110. $params['order_no'] = $orderNo;
  111. $params['pay_price'] = $chatgptRecharge->money;
  112. $params['createtime'] = time();
  113. $result = ChatgptOrder::create(
  114. $params
  115. );
  116. if( ! $result) {
  117. return $this->_json_error('订单创建失败!');
  118. }
  119. $result = (new Wxpay())->createOrder($orderNo , $chatgptRecharge->money);
  120. $order = $params;
  121. $order['num'] = $chatgptRecharge->num;
  122. $order['createtime'] = date('Y-m-d H:i');
  123. return $this->_json_succ(
  124. array_merge($result , $order)
  125. );
  126. }
  127. /**
  128. * 订单详情
  129. */
  130. public function orderDetail()
  131. {
  132. $orderNo = Request::param('order_no') ?? '';
  133. $order = ChatgptOrder::field('order_no , pay_time , createtime , pay_price , recharge_id , order_status')->where('order_no' , $orderNo)->find()->toArray();
  134. if( ! $order) {
  135. return $this->_json_error('未查到相关订单!');
  136. }
  137. $chatgptRecharge = ChatgptRecharge::find($order['recharge_id'])->toArray();
  138. $order['num'] = $chatgptRecharge['num'];
  139. return $this->_json_succ(
  140. $order
  141. );
  142. }
  143. /**
  144. * 用户信息
  145. */
  146. public function user()
  147. {
  148. return $this->_json_succ(
  149. User::field('chatgpt_num')->find(Request::param('uid') ?? '') ?? []
  150. );
  151. }
  152. /**
  153. * 绑定邀请码
  154. */
  155. public function inviteCode()
  156. {
  157. $params = Request::only(
  158. [
  159. 'order_no' , 'invite_code'
  160. ]
  161. );
  162. if( ! isset($params['invite_code'])) {
  163. return $this->_json_error('参数错误!');
  164. }
  165. $order = ChatgptOrder::where('order_no' , $params['order_no'])->find();
  166. if( ! $order) {
  167. return $this->_json_error('未查到相关订单!');
  168. }
  169. $order->invite_code = $params['invite_code'];
  170. $order->save();
  171. return $this->_json_succ();
  172. }
  173. /**
  174. * 问答历史记录
  175. */
  176. public function historyList()
  177. {
  178. $uid = Request::param('uid') ?? '';
  179. $page = Request::param('page') ?? 1;
  180. $pageSize = 50;
  181. $limit = ($page - 1) * $pageSize;
  182. return $this->_json_succ(
  183. UserChatgptUseRecord::field('uid , question , answer , createtime')->where('uid' , $uid)->limit($limit , $pageSize)->select()->toArray()
  184. );
  185. }
  186. }