海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Agent;
  5. use App\Models\Order;
  6. use App\Models\User;
  7. use App\Common\OrderStatus;
  8. use EasyWeChat\Factory;
  9. use Illuminate\Support\Facades\Storage;
  10. class VerificationController extends Controller
  11. {
  12. //核销订单
  13. public function verify()
  14. {
  15. $input_verify_code = request()->input('verify_code'); //订单ID
  16. $code_arr = explode('-', $input_verify_code);
  17. if (count($code_arr) != 2) {
  18. return $this->error('参数错误');
  19. }
  20. list($id, $verify_code) = $code_arr;
  21. $order = Order::with('agentProduct:id,verifier')
  22. ->where(['agent_id' => $this->agent_id, 'verify_code' => $verify_code])
  23. ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID])
  24. ->find($id);
  25. if (!$order) {
  26. return $this->error('订单不存在或订单状态不允许核销');
  27. }
  28. $user = User::firstWhere(['id' => $this->user_id, 'status' => 1]);
  29. if (!$user || $user->is_verify !=1 || $user->id != $order->agentProduct->verifier) {
  30. return $this->error('对不起,你没有核销权限,请联系管理员');
  31. }
  32. $order->status = OrderStatus::SUCCESS;
  33. $order->save();
  34. return $this->success();
  35. }
  36. public function qrcode()
  37. {
  38. $id = request()->input('id'); //订单ID
  39. $order = Order::where(['agent_id' => $this->agent_id, 'user_id' => $this->user_id])->find($id);
  40. if (!$order) {
  41. return $this->error('订单不存在!');
  42. } else if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::REFUSED_REFUND])) {
  43. return $this->error('当前订单状态不允许核销!');
  44. } else if (!$order->verify_code) {
  45. $order->verify_code = uniqid();
  46. $order->save();
  47. }
  48. $verify_code = $order->id . '-' . $order->verify_code;
  49. $agent = Agent::find($this->agent_id);
  50. $config = [
  51. 'app_id' => $agent->appid,
  52. 'secret' => $agent->appsecret,
  53. ];
  54. $app = Factory::miniProgram($config);
  55. $response = $app->app_code->getUnlimit($verify_code, ['path' => 'pages/index/index']);
  56. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  57. $filename = $response->saveAs(storage_path('app/public/verify_code'), $verify_code);
  58. }
  59. $prefix = Storage::disk('public')->url('verify_code/');
  60. return $this->success(['qrcode' => $prefix . $filename]);
  61. }
  62. }