123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Http\Requests\Api\PostsRequests\AskRequest;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use App\Wen\Utils\Utils;
- use App\Models\WxOrder;
- use App\Models\User\WxUser;
- use App\Http\Controllers\Api\Traits\PayTrait;
- use App\Wen\Utils\Settings;
- use App\Wen\Utils\UserUtils;
- use App\Models\WxSlideshow;
- class AskController extends BaseController
- {
- use PayTrait;
- //发起一个咨询
- public function createNew(AskRequest $request){
- // 验证
- $request->validate('createnew');
- //
- $question = trim($request->question);
- if(mb_strlen($question) > 300){
- return $this->fail(200016,[],'咨询内容不能超过300字');
- }
- //
- $images = trim($request->images);
- $images = trim($images,',');
- if(count(explode(',',$images)) > 9){
- return $this->fail(200016,[],'咨询图片最大9张');
- }
- //验证答主
- if($request->uid == $request->blogger_user_id){
- return $this->fail(200016,[],'不能咨询自己');
- }
- $blogger = DB::table('blogger')->where('user_id',$request->blogger_user_id)->first();
- if(empty($blogger)){
- return $this->fail(200016,[],'此答主没有经过认证');
- }
- if($blogger->status != 1){
- return $this->fail(200016,[],'此答主没有经过认证');
- }
- //
- $data = [
- 'user_id' => $request->uid,
- 'blogger_user_id' => $request->blogger_user_id,
- 'question' => $question,
- 'images' => $images,
- 'createtime' => time(),
- 'ask_price' => $blogger->ask_price,
- 'sit_price' => $blogger->sit_price,
- 'ask_minute' => $blogger->ask_minute,
- 'is_hidden' => $request->is_hidden,
- 'is_public' => $request->is_public,
- ];
- //0元的直接已付
- $need_pay = 1;
- if($blogger->ask_price == 0){
- $data['status'] = 10;
- $data['paytime'] = time();
- $need_pay = 0;
- }
- $order_id = DB::table('ask_order')->insertGetId($data);
- return $this->success(['order_id'=>$order_id,'need_pay'=>$need_pay]);
- }
- //评价此次咨询
- public function evaluate(AskRequest $request){
- // 验证
- $request->validate('evaluate');
- //
- $eva_content = trim($request->eva_content);
- if(mb_strlen($eva_content) > 100){
- return $this->fail(200016,[],'咨询内容不能超过100字');
- }
- //
- $score = $request->eva_score;
- if($score < 0){
- $score = 0;
- }
- if($score > 5){
- $score = 5;
- }
- //
- $order_id = _empty_default_($request->order_id,0);
- $order = DB::table('ask_order')->where('id',$order_id)->where('user_id',$request->uid)->where('status','>',0)->first();
- if(empty($order)){
- return $this->fail(200016,[],'没有找到该咨询');
- }
- if($order->status != 20){
- return $this->fail(200016,[],'咨询还没有结束');
- }
- if(!empty($order->eva_content)){
- return $this->fail(200016,[],'已经评价过了');
- }
- //
- $update = [
- 'eva_score' => $request->eva_score,
- 'eva_content' => $request->eva_content,
- ];
- DB::table('ask_order')->where('id',$order_id)->update($update);
- return $this->success();
- }
- //结束此次咨询,用户或答主都可操作
- public function finish(Request $request){
- //
- $order_id = _empty_default_($request->order_id,0);
- $uid = $request->uid;
- $order = DB::table('ask_order')->where('id',$order_id)->where('status','>',0)
- // ->where('user_id',$request->uid)
- ->where(function($query) use ($uid) {
- $query->orWhere('user_id', $uid)
- ->orWhere('blogger_user_id', $uid);
- })
- ->first();
- if(empty($order)){
- return $this->fail(200016,[],'没有找到该咨询');
- }
- if($order->status == 20){
- return $this->fail(200016,[],'咨询已经结束了');
- }
- //
- $update = [
- 'status' => 20,
- 'finish_time' => time(),
- ];
- DB::table('ask_order')->where('id',$order_id)->update($update);
- return $this->success();
- }
- //旁听
- public function sit(Request $request){
- $order_id = _empty_default_($request->order_id,0);
- $order = DB::table('ask_order')->where('id',$order_id)->first();
- if(empty($order)){
- return $this->fail(200016,[],'没有找到该咨询');
- }
- if($order->status != 20){
- return $this->fail(200016,[],'咨询还没有结束');
- }
- if($order->is_public != 1){
- return $this->fail(200016,[],'咨询没有公开');
- }
- if($order->user_id == $request->uid){
- return $this->fail(200016,[],'不需要旁听自己咨询的内容');
- }
- if($order->blogger_user_id == $request->uid){
- return $this->fail(200016,[],'不需要旁听自己回答的内容');
- }
- //是否需要支付
- $need_pay = 1;
- //检查旁听订单
- $sit_order_id = 0;
- $sit_order = DB::table('ask_sit_order')->where('order_id',$order_id)->where('sit_user_id',$request->uid)->first();
- if($sit_order){
- $sit_order_id = $sit_order->id;
- if($sit_order->status == 10){
- return $this->fail(200016,[],'已经旁听过了');
- }else{
- //去支付即可
- }
- }else{
- $data = [
- 'order_id' => $order_id,
- 'sit_user_id' => $request->uid,
- 'createtime' => time(),
- 'sit_price' => $order->sit_price,
- ];
- if($order->sit_price == 0){
- $need_pay = 0; //零元不需要支付
- $data['status'] = 10;
- $data['paytime'] = time();
- }
- $sit_order_id = DB::table('ask_sit_order')->insertGetId($data);
- }
- return $this->success(['sit_order_id'=>$sit_order_id,'need_pay'=>$need_pay]);
- }
- //付费咨询订单,余额支付+拉起
- public function payAskOrder(Request $request)
- {
- $order_id = _empty_default_($request->order_id,0);
- $uid = $request->uid;
- $order = DB::table('ask_order')->where('id',$order_id)->where('user_id',$request->uid)->first();
- if(empty($order)){
- return $this->fail(200016,[],'没有找到该咨询');
- }
- if($order->status != 0){
- return $this->fail(200016,[],'该咨询已经支付过了');
- }
- //使用余额支付
- if($request->pay_type == 'balance'){
- //检查支付密码
- $rs = $this->check_paycode($uid,$request->paycode);
- if($rs !== true){
- return $rs;
- }
- //检查余额
- $balance = UserUtils::user_balance($uid);
- if($balance <= $order->ask_price){
- return $this->fail(200012);
- }
- DB::beginTransaction();
- //余额支付
- $pay_res = UserUtils::update_user_financial($uid, 101, $order->ask_price, '您花费了¥'.$order->ask_price.'余额,付费咨询');
- if(!$pay_res){
- DB::rollBack();
- return $this->fail(200012);
- }
- //直接修改订单状态,支付完成
- $rs1 = DB::table('ask_order')->where('id',$order_id)->update(['status'=>10,'paytime'=>time()]);
- if(!$rs1){
- DB::rollBack();
- return $this->fail([],200,'支付失败');
- }
- DB::commit();
- return $this->success([],200,'余额支付成功');
- }
- //拉起三方支付
- $body = '付费咨询';
- $data['body'] = '付费咨询';
- $total_fee = $order->ask_price;
- $orderSn = 'A' . Utils::getSn('A');
- $parame = serialize($data);
- // 创建订单
- $this->createOrder($uid,101,$body,$total_fee,$total_fee,$orderSn,$parame,'ask_order',$order_id);
- $openid = WxUser::where('id', $uid)->value('weixin_openid');
- $appid = Settings::get('app_id');
- $mch_id = Settings::get('mch_id');
- $key = Settings::get('mch_secret');
- $out_trade_no = $orderSn;
- if(_empty_($openid)){
- return $this->fail(200043, [
- 'title' => '未绑定微信',
- 'content' => '还没有获取到您的小程序openId,无法拉起支付',
- 'confirmText' => '去绑定',
- 'target_type' => 6,
- 'target_id' => '/pagesA/mine/editmine/accountbind'
- ], '未绑定微信');
- }
- return $this->payHandler($uid, $request->pay_type, $request->platform, $total_fee, $data['body'], $out_trade_no, 1);
- }
- //付费旁听订单,余额支付+拉起
- public function paySitOrder(Request $request)
- {
- $order_id = _empty_default_($request->order_id,0);
- $uid = $request->uid;
- $order = DB::table('ask_sit_order')->where('id',$order_id)->where('sit_user_id',$request->uid)->first();
- if(empty($order)){
- return $this->fail(200016,[],'没有找到该旁听');
- }
- if($order->status != 0){
- return $this->fail(200016,[],'该旁听已经支付过了');
- }
- //使用余额支付
- if($request->pay_type == 'balance'){
- //检查支付密码
- $rs = $this->check_paycode($uid,$request->paycode);
- if($rs != true){
- return $rs;
- }
- //检查余额
- $balance = UserUtils::user_balance($uid);
- if($balance <= $order->sit_price){
- return $this->fail(200012);
- }
- DB::beginTransaction();
- //余额支付
- $pay_res = UserUtils::update_user_financial($uid, 102, $order->sit_price, '您花费了¥'.$order->sit_price.'余额,付费旁听');
- if(!$pay_res){
- DB::rollBack();
- return $this->fail(200012);
- }
- //直接修改订单状态,支付完成
- $rs1 = DB::table('ask_order')->where('id',$order_id)->update(['status'=>10,'paytime'=>time()]);
- if(!$rs1){
- DB::rollBack();
- return $this->fail([],200,'支付失败');
- }
- DB::commit();
- return $this->success([],200,'余额支付成功');
- }
- //拉起三方支付
- $body = '付费旁听';
- $data['body'] = '付费旁听';
- $total_fee = $order->sit_price;
- $orderSn = 'S' . Utils::getSn('S');
- $parame = serialize($data);
- // 创建订单
- $this->createOrder($uid,102,$body,$total_fee,$total_fee,$orderSn,$parame,'ask_sit_order',$order_id);
- $openid = WxUser::where('id', $uid)->value('weixin_openid');
- $appid = Settings::get('app_id');
- $mch_id = Settings::get('mch_id');
- $key = Settings::get('mch_secret');
- $out_trade_no = $orderSn;
- if(_empty_($openid)){
- return $this->fail(200043, [
- 'title' => '未绑定微信',
- 'content' => '还没有获取到您的小程序openId,无法拉起支付',
- 'confirmText' => '去绑定',
- 'target_type' => 6,
- 'target_id' => '/pagesA/mine/editmine/accountbind'
- ], '未绑定微信');
- }
- return $this->payHandler($uid, $request->pay_type, $request->platform, $total_fee, $data['body'], $out_trade_no, 1);
- }
- //检查支付密码
- private function check_paycode($uid, $paycode){
- //验证支付密码
- if(_empty_($paycode)){
- return $this->fail(200004, [], '请先输入支付密码');
- }
- if(is_array($paycode) && str_replace(',', '', implode(',', $paycode)) !== get_user_meta($uid, 'paycode', 's')){
- return $this->fail(200043, [
- 'title' => '支付密码错误',
- 'content' => '已忘记,前往修改?',
- 'confirmText' => '去修改',
- 'target_type' => 6,
- 'target_id' => '/pagesA/mine/paycode/forget'
- ], '支付密码错误');
- }
- if(is_string($paycode) && $paycode !== get_user_meta($uid, 'paycode', 's')){
- return $this->fail(200004, [], '支付密码不匹配');
- }
- return true;
- }
- /**
- * 创建支付订单
- * $order_type = 101 咨询订单
- * $order_type = 102 旁听订单
- */
- private function createOrder($user_id,$order_type,$order_information,$order_price,$order_pay_price,$order_number,$parame='',$table_name,$table_id)
- {
- $orderModel = new WxOrder();
- $orderModel->user_id = $user_id;
- $orderModel->order_type = $order_type;
- $orderModel->order_information = $order_information;
- $orderModel->order_price = $order_price;
- $orderModel->order_pay_price = $order_pay_price;
- $orderModel->order_number = $order_number;
- $orderModel->parame = $parame;
- $orderModel->table_name = $table_name;
- $orderModel->table_id = $table_id;
- $orderModel->save();
- return $orderModel;
- }
- //首页轮播
- public function banner(Request $request){
- $ids = Settings::get('ask_index_banner', '', true);
- $ids = '30,32,33,34,35,36,37';
- $list = WxSlideshow::wherein('id', explode(',', $ids))->orderBy(DB::raw('FIND_IN_SET(id, "' . $ids . '"' . ')'))
- ->get(['id', 'poster', 'target_id', 'slideshow_type']);
- return $this->success($list);
- }
- //某个提问的旁听详情
- //我购买的旁听列表
- //我发起的咨询
- //所有问答列表
- //某答主的评价列表
- public function bloggerEvaList(Request $request){
- //分页
- $page = $request->page ?? 1;
- $limit = $request->limit ?? 10;
- $offset = ($page - 1) * $limit;
- $list = DB::table('ask_order')
- ->leftJoin('wx_user','wx_user.id','=','ask_order.user_id')
- ->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')
- ->where('ask_order.blogger_user_id',$request->user_id)
- ->where('ask_order.eva_time','>',0)
- ->where('ask_order.status',20)
- ->orderBy('ask_order.eva_time','desc')
- ->offset($offset)->limit($limit)
- ->get();
- $list = json_decode(json_encode($list),true);
- foreach($list as &$val){
- if($val['is_hidden'] == 1){
- $val['user_avatar'] = 'https://img.yiyoujiayuan.cn/2025/03/20/0/b59ce3b31611a3bd82199c3183208a10.jpg';//匿名头像
- $val['user_name'] = '匿名用户';
- }
- }
- return $this->success($list);
- }
- //
- //
- //
- //
- //
- //
- //
- //
- }
|