AskController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Requests\Api\PostsRequests\AskRequest;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Wen\Utils\Utils;
  7. use App\Models\WxOrder;
  8. use App\Models\User\WxUser;
  9. use App\Http\Controllers\Api\Traits\PayTrait;
  10. use App\Wen\Utils\Settings;
  11. use App\Wen\Utils\UserUtils;
  12. use App\Models\WxSlideshow;
  13. class AskController extends BaseController
  14. {
  15. use PayTrait;
  16. //发起一个咨询
  17. public function createNew(AskRequest $request){
  18. // 验证
  19. $request->validate('createnew');
  20. //
  21. $question = trim($request->question);
  22. if(mb_strlen($question) > 300){
  23. return $this->fail(200016,[],'咨询内容不能超过300字');
  24. }
  25. //
  26. $images = trim($request->images);
  27. $images = trim($images,',');
  28. if(count(explode(',',$images)) > 9){
  29. return $this->fail(200016,[],'咨询图片最大9张');
  30. }
  31. //验证答主
  32. if($request->uid == $request->blogger_user_id){
  33. return $this->fail(200016,[],'不能咨询自己');
  34. }
  35. $blogger = DB::table('blogger')->where('user_id',$request->blogger_user_id)->first();
  36. if(empty($blogger)){
  37. return $this->fail(200016,[],'此答主没有经过认证');
  38. }
  39. if($blogger->status != 1){
  40. return $this->fail(200016,[],'此答主没有经过认证');
  41. }
  42. //
  43. $data = [
  44. 'user_id' => $request->uid,
  45. 'blogger_user_id' => $request->blogger_user_id,
  46. 'question' => $question,
  47. 'images' => $images,
  48. 'createtime' => time(),
  49. 'ask_price' => $blogger->ask_price,
  50. 'sit_price' => $blogger->sit_price,
  51. 'ask_minute' => $blogger->ask_minute,
  52. 'is_hidden' => $request->is_hidden,
  53. 'is_public' => $request->is_public,
  54. ];
  55. //0元的直接已付
  56. $need_pay = 1;
  57. if($blogger->ask_price == 0){
  58. $data['status'] = 10;
  59. $data['paytime'] = time();
  60. $need_pay = 0;
  61. }
  62. $order_id = DB::table('ask_order')->insertGetId($data);
  63. return $this->success(['order_id'=>$order_id,'need_pay'=>$need_pay]);
  64. }
  65. //评价此次咨询
  66. public function evaluate(AskRequest $request){
  67. // 验证
  68. $request->validate('evaluate');
  69. //
  70. $eva_content = trim($request->eva_content);
  71. if(mb_strlen($eva_content) > 100){
  72. return $this->fail(200016,[],'咨询内容不能超过100字');
  73. }
  74. //
  75. $score = $request->eva_score;
  76. if($score < 0){
  77. $score = 0;
  78. }
  79. if($score > 5){
  80. $score = 5;
  81. }
  82. //
  83. $order_id = _empty_default_($request->order_id,0);
  84. $order = DB::table('ask_order')->where('id',$order_id)->where('user_id',$request->uid)->where('status','>',0)->first();
  85. if(empty($order)){
  86. return $this->fail(200016,[],'没有找到该咨询');
  87. }
  88. if($order->status != 20){
  89. return $this->fail(200016,[],'咨询还没有结束');
  90. }
  91. if(!empty($order->eva_content)){
  92. return $this->fail(200016,[],'已经评价过了');
  93. }
  94. //
  95. $update = [
  96. 'eva_score' => $request->eva_score,
  97. 'eva_content' => $request->eva_content,
  98. ];
  99. DB::table('ask_order')->where('id',$order_id)->update($update);
  100. return $this->success();
  101. }
  102. //结束此次咨询,用户或答主都可操作
  103. public function finish(Request $request){
  104. //
  105. $order_id = _empty_default_($request->order_id,0);
  106. $uid = $request->uid;
  107. $order = DB::table('ask_order')->where('id',$order_id)->where('status','>',0)
  108. // ->where('user_id',$request->uid)
  109. ->where(function($query) use ($uid) {
  110. $query->orWhere('user_id', $uid)
  111. ->orWhere('blogger_user_id', $uid);
  112. })
  113. ->first();
  114. if(empty($order)){
  115. return $this->fail(200016,[],'没有找到该咨询');
  116. }
  117. if($order->status == 20){
  118. return $this->fail(200016,[],'咨询已经结束了');
  119. }
  120. //
  121. $update = [
  122. 'status' => 20,
  123. 'finish_time' => time(),
  124. ];
  125. DB::table('ask_order')->where('id',$order_id)->update($update);
  126. return $this->success();
  127. }
  128. //旁听
  129. public function sit(Request $request){
  130. $order_id = _empty_default_($request->order_id,0);
  131. $order = DB::table('ask_order')->where('id',$order_id)->first();
  132. if(empty($order)){
  133. return $this->fail(200016,[],'没有找到该咨询');
  134. }
  135. if($order->status != 20){
  136. return $this->fail(200016,[],'咨询还没有结束');
  137. }
  138. if($order->is_public != 1){
  139. return $this->fail(200016,[],'咨询没有公开');
  140. }
  141. if($order->user_id == $request->uid){
  142. return $this->fail(200016,[],'不需要旁听自己咨询的内容');
  143. }
  144. if($order->blogger_user_id == $request->uid){
  145. return $this->fail(200016,[],'不需要旁听自己回答的内容');
  146. }
  147. //是否需要支付
  148. $need_pay = 1;
  149. //检查旁听订单
  150. $sit_order_id = 0;
  151. $sit_order = DB::table('ask_sit_order')->where('order_id',$order_id)->where('sit_user_id',$request->uid)->first();
  152. if($sit_order){
  153. $sit_order_id = $sit_order->id;
  154. if($sit_order->status == 10){
  155. return $this->fail(200016,[],'已经旁听过了');
  156. }else{
  157. //去支付即可
  158. }
  159. }else{
  160. $data = [
  161. 'order_id' => $order_id,
  162. 'sit_user_id' => $request->uid,
  163. 'createtime' => time(),
  164. 'sit_price' => $order->sit_price,
  165. ];
  166. if($order->sit_price == 0){
  167. $need_pay = 0; //零元不需要支付
  168. $data['status'] = 10;
  169. $data['paytime'] = time();
  170. }
  171. $sit_order_id = DB::table('ask_sit_order')->insertGetId($data);
  172. }
  173. return $this->success(['sit_order_id'=>$sit_order_id,'need_pay'=>$need_pay]);
  174. }
  175. //付费咨询订单,余额支付+拉起
  176. public function payAskOrder(Request $request)
  177. {
  178. $order_id = _empty_default_($request->order_id,0);
  179. $uid = $request->uid;
  180. $order = DB::table('ask_order')->where('id',$order_id)->where('user_id',$request->uid)->first();
  181. if(empty($order)){
  182. return $this->fail(200016,[],'没有找到该咨询');
  183. }
  184. if($order->status != 0){
  185. return $this->fail(200016,[],'该咨询已经支付过了');
  186. }
  187. //使用余额支付
  188. if($request->pay_type == 'balance'){
  189. //检查支付密码
  190. $rs = $this->check_paycode($uid,$request->paycode);
  191. if($rs !== true){
  192. return $rs;
  193. }
  194. //检查余额
  195. $balance = UserUtils::user_balance($uid);
  196. if($balance <= $order->ask_price){
  197. return $this->fail(200012);
  198. }
  199. DB::beginTransaction();
  200. //余额支付
  201. $pay_res = UserUtils::update_user_financial($uid, 101, $order->ask_price, '您花费了¥'.$order->ask_price.'余额,付费咨询');
  202. if(!$pay_res){
  203. DB::rollBack();
  204. return $this->fail(200012);
  205. }
  206. //直接修改订单状态,支付完成
  207. $rs1 = DB::table('ask_order')->where('id',$order_id)->update(['status'=>10,'paytime'=>time()]);
  208. if(!$rs1){
  209. DB::rollBack();
  210. return $this->fail([],200,'支付失败');
  211. }
  212. DB::commit();
  213. return $this->success([],200,'余额支付成功');
  214. }
  215. //拉起三方支付
  216. $body = '付费咨询';
  217. $data['body'] = '付费咨询';
  218. $total_fee = $order->ask_price;
  219. $orderSn = 'A' . Utils::getSn('A');
  220. $parame = serialize($data);
  221. // 创建订单
  222. $this->createOrder($uid,101,$body,$total_fee,$total_fee,$orderSn,$parame,'ask_order',$order_id);
  223. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  224. $appid = Settings::get('app_id');
  225. $mch_id = Settings::get('mch_id');
  226. $key = Settings::get('mch_secret');
  227. $out_trade_no = $orderSn;
  228. if(_empty_($openid)){
  229. return $this->fail(200043, [
  230. 'title' => '未绑定微信',
  231. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  232. 'confirmText' => '去绑定',
  233. 'target_type' => 6,
  234. 'target_id' => '/pagesA/mine/editmine/accountbind'
  235. ], '未绑定微信');
  236. }
  237. return $this->payHandler($uid, $request->pay_type, $request->platform, $total_fee, $data['body'], $out_trade_no, 1);
  238. }
  239. //付费旁听订单,余额支付+拉起
  240. public function paySitOrder(Request $request)
  241. {
  242. $order_id = _empty_default_($request->order_id,0);
  243. $uid = $request->uid;
  244. $order = DB::table('ask_sit_order')->where('id',$order_id)->where('sit_user_id',$request->uid)->first();
  245. if(empty($order)){
  246. return $this->fail(200016,[],'没有找到该旁听');
  247. }
  248. if($order->status != 0){
  249. return $this->fail(200016,[],'该旁听已经支付过了');
  250. }
  251. //使用余额支付
  252. if($request->pay_type == 'balance'){
  253. //检查支付密码
  254. $rs = $this->check_paycode($uid,$request->paycode);
  255. if($rs != true){
  256. return $rs;
  257. }
  258. //检查余额
  259. $balance = UserUtils::user_balance($uid);
  260. if($balance <= $order->sit_price){
  261. return $this->fail(200012);
  262. }
  263. DB::beginTransaction();
  264. //余额支付
  265. $pay_res = UserUtils::update_user_financial($uid, 102, $order->sit_price, '您花费了¥'.$order->sit_price.'余额,付费旁听');
  266. if(!$pay_res){
  267. DB::rollBack();
  268. return $this->fail(200012);
  269. }
  270. //直接修改订单状态,支付完成
  271. $rs1 = DB::table('ask_order')->where('id',$order_id)->update(['status'=>10,'paytime'=>time()]);
  272. if(!$rs1){
  273. DB::rollBack();
  274. return $this->fail([],200,'支付失败');
  275. }
  276. DB::commit();
  277. return $this->success([],200,'余额支付成功');
  278. }
  279. //拉起三方支付
  280. $body = '付费旁听';
  281. $data['body'] = '付费旁听';
  282. $total_fee = $order->sit_price;
  283. $orderSn = 'S' . Utils::getSn('S');
  284. $parame = serialize($data);
  285. // 创建订单
  286. $this->createOrder($uid,102,$body,$total_fee,$total_fee,$orderSn,$parame,'ask_sit_order',$order_id);
  287. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  288. $appid = Settings::get('app_id');
  289. $mch_id = Settings::get('mch_id');
  290. $key = Settings::get('mch_secret');
  291. $out_trade_no = $orderSn;
  292. if(_empty_($openid)){
  293. return $this->fail(200043, [
  294. 'title' => '未绑定微信',
  295. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  296. 'confirmText' => '去绑定',
  297. 'target_type' => 6,
  298. 'target_id' => '/pagesA/mine/editmine/accountbind'
  299. ], '未绑定微信');
  300. }
  301. return $this->payHandler($uid, $request->pay_type, $request->platform, $total_fee, $data['body'], $out_trade_no, 1);
  302. }
  303. //检查支付密码
  304. private function check_paycode($uid, $paycode){
  305. //验证支付密码
  306. if(_empty_($paycode)){
  307. return $this->fail(200004, [], '请先输入支付密码');
  308. }
  309. if(is_array($paycode) && str_replace(',', '', implode(',', $paycode)) !== get_user_meta($uid, 'paycode', 's')){
  310. return $this->fail(200043, [
  311. 'title' => '支付密码错误',
  312. 'content' => '已忘记,前往修改?',
  313. 'confirmText' => '去修改',
  314. 'target_type' => 6,
  315. 'target_id' => '/pagesA/mine/paycode/forget'
  316. ], '支付密码错误');
  317. }
  318. if(is_string($paycode) && $paycode !== get_user_meta($uid, 'paycode', 's')){
  319. return $this->fail(200004, [], '支付密码不匹配');
  320. }
  321. return true;
  322. }
  323. /**
  324. * 创建支付订单
  325. * $order_type = 101 咨询订单
  326. * $order_type = 102 旁听订单
  327. */
  328. private function createOrder($user_id,$order_type,$order_information,$order_price,$order_pay_price,$order_number,$parame='',$table_name,$table_id)
  329. {
  330. $orderModel = new WxOrder();
  331. $orderModel->user_id = $user_id;
  332. $orderModel->order_type = $order_type;
  333. $orderModel->order_information = $order_information;
  334. $orderModel->order_price = $order_price;
  335. $orderModel->order_pay_price = $order_pay_price;
  336. $orderModel->order_number = $order_number;
  337. $orderModel->parame = $parame;
  338. $orderModel->table_name = $table_name;
  339. $orderModel->table_id = $table_id;
  340. $orderModel->save();
  341. return $orderModel;
  342. }
  343. //首页轮播
  344. public function banner(Request $request){
  345. $ids = Settings::get('ask_index_banner', '', true);
  346. $ids = '30,32,33,34,35,36,37';
  347. $list = WxSlideshow::wherein('id', explode(',', $ids))->orderBy(DB::raw('FIND_IN_SET(id, "' . $ids . '"' . ')'))
  348. ->get(['id', 'poster', 'target_id', 'slideshow_type']);
  349. return $this->success($list);
  350. }
  351. //某个提问的旁听详情
  352. //我购买的旁听列表
  353. //我发起的咨询
  354. //所有问答列表
  355. //某答主的评价列表
  356. public function bloggerEvaList(Request $request){
  357. //分页
  358. $page = $request->page ?? 1;
  359. $limit = $request->limit ?? 10;
  360. $offset = ($page - 1) * $limit;
  361. $list = DB::table('ask_order')
  362. ->leftJoin('wx_user','wx_user.id','=','ask_order.user_id')
  363. ->select('ask_order.id','ask_order.eva_time','ask_order.eva_score','ask_order.eva_content','ask_order.is_hidden','wx_user.user_avatar','wx_user.user_name')
  364. ->where('ask_order.blogger_user_id',$request->user_id)
  365. ->where('ask_order.eva_time','>',0)
  366. ->where('ask_order.status',20)
  367. ->orderBy('ask_order.eva_time','desc')
  368. ->offset($offset)->limit($limit)
  369. ->get();
  370. $list = json_decode(json_encode($list),true);
  371. foreach($list as &$val){
  372. if($val['is_hidden'] == 1){
  373. $val['user_avatar'] = 'https://img.yiyoujiayuan.cn/2025/03/20/0/b59ce3b31611a3bd82199c3183208a10.jpg';//匿名头像
  374. $val['user_name'] = '匿名用户';
  375. }
  376. }
  377. return $this->success($list);
  378. }
  379. //
  380. //
  381. //
  382. //
  383. //
  384. //
  385. //
  386. //
  387. }