AskController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. class AskController extends BaseController
  13. {
  14. use PayTrait;
  15. //发起一个咨询
  16. public function createNew(AskRequest $request){
  17. // 验证
  18. $request->validate('createnew');
  19. //
  20. $question = trim($request->question);
  21. if(mb_strlen($question) > 300){
  22. return $this->fail(200016,[],'咨询内容不能超过300字');
  23. }
  24. //
  25. $images = trim($request->images);
  26. $images = trim($images,',');
  27. if(count(explode(',',$images)) > 9){
  28. return $this->fail(200016,[],'咨询图片最大9张');
  29. }
  30. //验证答主
  31. if($request->uid == $request->blogger_user_id){
  32. return $this->fail(200016,[],'不能咨询自己');
  33. }
  34. $blogger = DB::table('blogger')->where('user_id',$request->blogger_user_id)->first();
  35. if(empty($blogger)){
  36. return $this->fail(200016,[],'此答主没有经过认证');
  37. }
  38. if($blogger->status != 1){
  39. return $this->fail(200016,[],'此答主没有经过认证');
  40. }
  41. //
  42. $data = [
  43. 'user_id' => $request->uid,
  44. 'blogger_user_id' => $request->blogger_user_id,
  45. 'question' => $question,
  46. 'images' => $images,
  47. 'createtime' => time(),
  48. 'ask_price' => $blogger->ask_price,
  49. 'sit_price' => $blogger->sit_price,
  50. 'ask_minute' => $blogger->ask_minute,
  51. 'is_hidden' => $request->is_hidden,
  52. 'is_public' => $request->is_public,
  53. ];
  54. DB::table('ask_order')->insert($data);
  55. return $this->success();
  56. }
  57. //评价此次咨询
  58. public function evaluate(AskRequest $request){
  59. // 验证
  60. $request->validate('evaluate');
  61. //
  62. $eva_content = trim($request->eva_content);
  63. if(mb_strlen($eva_content) > 100){
  64. return $this->fail(200016,[],'咨询内容不能超过100字');
  65. }
  66. //
  67. $score = $request->eva_score;
  68. if($score < 0){
  69. $score = 0;
  70. }
  71. if($score > 5){
  72. $score = 5;
  73. }
  74. //
  75. $order_id = _empty_default_($request->order_id,0);
  76. $order = DB::table('ask_order')->where('id',$order_id)->where('user_id',$request->uid)->where('status','>',0)->first();
  77. if(empty($order)){
  78. return $this->fail(200016,[],'没有找到该咨询');
  79. }
  80. if($order->status != 20){
  81. return $this->fail(200016,[],'咨询还没有结束');
  82. }
  83. if(!empty($order->eva_content)){
  84. return $this->fail(200016,[],'已经评价过了');
  85. }
  86. //
  87. $update = [
  88. 'eva_score' => $request->eva_score,
  89. 'eva_content' => $request->eva_content,
  90. ];
  91. DB::table('ask_order')->where('id',$order_id)->update($update);
  92. return $this->success();
  93. }
  94. //结束此次咨询
  95. //旁听
  96. public function sit(Request $request){
  97. $order_id = _empty_default_($request->order_id,0);
  98. $order = DB::table('ask_order')->where('id',$order_id)->first();
  99. if(empty($order)){
  100. return $this->fail(200016,[],'没有找到该咨询');
  101. }
  102. if($order->status != 20){
  103. return $this->fail(200016,[],'咨询还没有结束');
  104. }
  105. if($order->is_public != 1){
  106. return $this->fail(200016,[],'咨询没有公开');
  107. }
  108. if($order->user_id == $request->uid){
  109. return $this->fail(200016,[],'不需要旁听自己咨询的内容');
  110. }
  111. if($order->blogger_user_id == $request->uid){
  112. return $this->fail(200016,[],'不需要旁听自己回答的内容');
  113. }
  114. //是否需要支付
  115. $need_pay = 1;
  116. //检查旁听订单
  117. $sit_order = DB::table('ask_sit_order')->where('order_id',$order_id)->where('sit_user_id',$request->uid)->first();
  118. if($sit_order){
  119. if($sit_order->status == 10){
  120. return $this->fail(200016,[],'已经旁听过了');
  121. }else{
  122. //去支付即可
  123. }
  124. }else{
  125. $data = [
  126. 'order_id' => $order_id,
  127. 'sit_user_id' => $request->uid,
  128. 'createtime' => time(),
  129. 'sit_price' => $order->sit_price,
  130. ];
  131. if($order->sit_price == 0){
  132. $need_pay = 0; //零元不需要支付
  133. $data['status'] = 10;
  134. $data['paytime'] = time();
  135. }
  136. DB::table('ask_sit_order')->insert($data);
  137. }
  138. if($need_pay == 1){
  139. return $this->success('支付拉起'); //支付拉起
  140. }else{
  141. return $this->success();
  142. }
  143. }
  144. //付费咨询拉起订单
  145. public function payAskOrder(Request $request)
  146. {
  147. $order_id = _empty_default_($request->order_id,0);
  148. $uid = $request->uid;
  149. $order = DB::table('ask_order')->where('id',$order_id)->where('user_id',$request->uid)->first();
  150. if(empty($order)){
  151. return $this->fail(200016,[],'没有找到该咨询');
  152. }
  153. if($order->status != 0){
  154. return $this->fail(200016,[],'该咨询已经支付过了');
  155. }
  156. //使用余额支付
  157. if($request->pay_type == 'balance'){
  158. //检查支付密码
  159. $this->check_paycode($uid,$request->paycode);
  160. //检查余额
  161. $balance = UserUtils::user_balance($uid);
  162. if($balance <= $order->ask_price]){
  163. return $this->fail(200012);
  164. }
  165. DB::beginTransaction();
  166. //余额支付
  167. $pay_res = UserUtils::update_user_financial($uid, 101, $order->ask_price, '您花费了¥'.$order->ask_price.'余额,付费咨询');
  168. if(!$pay_res){
  169. DB::rollBack();
  170. return $this->fail(200012);
  171. }
  172. //直接修改订单状态,支付完成
  173. $rs1 = DB::table('ask_order')->where('id',$order_id)->update(['status'=>10,'paytime'=>time()]);
  174. if(!$rs1){
  175. DB::rollBack();
  176. return $this->fail([],200,'支付失败');
  177. }
  178. DB::commit();
  179. return $this->success();
  180. }
  181. //拉起三方支付
  182. $body = '付费咨询';
  183. $data['body'] = '付费咨询';
  184. $total_fee = $order->ask_price;
  185. $orderSn = 'A' . Utils::getSn('A');
  186. $parame = serialize($data);
  187. // 创建订单
  188. $this->createOrder($uid,101,$body,$total_fee,$total_fee,$orderSn,$parame,'ask_order',$order_id);
  189. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  190. $appid = Settings::get('app_id');
  191. $mch_id = Settings::get('mch_id');
  192. $key = Settings::get('mch_secret');
  193. $out_trade_no = $orderSn;
  194. if(_empty_($openid)){
  195. return $this->fail(200043, [
  196. 'title' => '未绑定微信',
  197. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  198. 'confirmText' => '去绑定',
  199. 'target_type' => 6,
  200. 'target_id' => '/pagesA/mine/editmine/accountbind'
  201. ], '未绑定微信');
  202. }
  203. return $this->payHandler($uid, 'wxpay', 'mini', $total_fee, $data['body'], $out_trade_no, 1);
  204. }
  205. //付费咨询拉起订单
  206. public function paySitOrder(Request $request)
  207. {
  208. $order_id = _empty_default_($request->order_id,0);
  209. $uid = $request->uid;
  210. $order = DB::table('ask_sit_order')->where('id',$order_id)->where('sit_user_id',$request->uid)->first();
  211. if(empty($order)){
  212. return $this->fail(200016,[],'没有找到该旁听');
  213. }
  214. if($order->status != 0){
  215. return $this->fail(200016,[],'该旁听已经支付过了');
  216. }
  217. //使用余额支付
  218. if($request->pay_type == 'balance'){
  219. //检查支付密码
  220. $this->check_paycode($uid,$request->paycode);
  221. //检查余额
  222. $balance = UserUtils::user_balance($uid);
  223. if($balance <= $order->sit_price]){
  224. return $this->fail(200012);
  225. }
  226. DB::beginTransaction();
  227. //余额支付
  228. $pay_res = UserUtils::update_user_financial($uid, 102, $order->sit_price, '您花费了¥'.$order->sit_price.'余额,付费旁听');
  229. if(!$pay_res){
  230. DB::rollBack();
  231. return $this->fail(200012);
  232. }
  233. //直接修改订单状态,支付完成
  234. $rs1 = DB::table('ask_order')->where('id',$order_id)->update(['status'=>10,'paytime'=>time()]);
  235. if(!$rs1){
  236. DB::rollBack();
  237. return $this->fail([],200,'支付失败');
  238. }
  239. DB::commit();
  240. return $this->success();
  241. }
  242. //拉起三方支付
  243. $body = '付费旁听';
  244. $data['body'] = '付费旁听';
  245. $total_fee = $order->sit_price;
  246. $orderSn = 'S' . Utils::getSn('S');
  247. $parame = serialize($data);
  248. // 创建订单
  249. $this->createOrder($uid,102,$body,$total_fee,$total_fee,$orderSn,$parame,'sit_order',$order_id);
  250. $openid = WxUser::where('id', $uid)->value('weixin_openid');
  251. $appid = Settings::get('app_id');
  252. $mch_id = Settings::get('mch_id');
  253. $key = Settings::get('mch_secret');
  254. $out_trade_no = $orderSn;
  255. if(_empty_($openid)){
  256. return $this->fail(200043, [
  257. 'title' => '未绑定微信',
  258. 'content' => '还没有获取到您的小程序openId,无法拉起支付',
  259. 'confirmText' => '去绑定',
  260. 'target_type' => 6,
  261. 'target_id' => '/pagesA/mine/editmine/accountbind'
  262. ], '未绑定微信');
  263. }
  264. return $this->payHandler($uid, 'wxpay', 'mini', $total_fee, $data['body'], $out_trade_no, 1);
  265. }
  266. //检查支付密码
  267. private function check_paycode($uid, $paycode){
  268. //验证支付密码
  269. if(_empty_($paycode)){
  270. return $this->fail(200004, [], '请先输入支付密码');
  271. }
  272. if(is_array($paycode) && str_replace(',', '', implode(',', $paycode)) !== get_user_meta($uid, 'paycode', 's')){
  273. return $this->fail(200043, [
  274. 'title' => '支付密码错误',
  275. 'content' => '已忘记,前往修改?',
  276. 'confirmText' => '去修改',
  277. 'target_type' => 6,
  278. 'target_id' => '/pagesA/mine/paycode/forget'
  279. ], '支付密码错误');
  280. }
  281. if(is_string($paycode) && $paycode !== get_user_meta($uid, 'paycode', 's')){
  282. return $this->fail(200004, [], '支付密码不匹配');
  283. }
  284. return true;
  285. }
  286. /**
  287. * 创建支付订单
  288. * $order_type = 101 咨询订单
  289. * $order_type = 102 旁听订单
  290. */
  291. private function createOrder($user_id,$order_type,$order_information,$order_price,$order_pay_price,$order_number,$parame='',$table_name,$table_id)
  292. {
  293. $orderModel = new WxOrder();
  294. $orderModel->user_id = $user_id;
  295. $orderModel->order_type = $order_type;
  296. $orderModel->order_information = $order_information;
  297. $orderModel->order_price = $order_price;
  298. $orderModel->order_pay_price = $order_pay_price;
  299. $orderModel->order_number = $order_number;
  300. $orderModel->parame = $parame;
  301. $orderModel->table_name = $table_name;
  302. $orderModel->table_id = $table_id;
  303. $orderModel->save();
  304. return $orderModel;
  305. }
  306. //答主的主页
  307. //咨询首页
  308. //首页轮播
  309. //推荐答主列表
  310. //某个提问的旁听详情
  311. //我购买的旁听列表
  312. //我发起的咨询
  313. //所有问答列表
  314. //答主的评价列表
  315. //
  316. //
  317. //
  318. //
  319. //
  320. //
  321. //
  322. //
  323. }