海南旅游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.

77 lines
2.4 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. //生成核销二维码
  37. public function qrcode()
  38. {
  39. $id = request()->input('id'); //订单ID
  40. $order = Order::where(['agent_id' => $this->agent_id, 'user_id' => $this->user_id])->find($id);
  41. if (!$order) {
  42. return $this->error('订单不存在!');
  43. } else if (!in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])) {
  44. return $this->error('当前订单状态不允许核销!');
  45. } else if (!$order->verify_code) {
  46. $order->verify_code = uniqid();
  47. $order->save();
  48. }
  49. $verify_code = $order->id . '-' . $order->verify_code;
  50. $agent = Agent::find($this->agent_id);
  51. $config = [
  52. 'app_id' => $agent->appid,
  53. 'secret' => $agent->appsecret,
  54. ];
  55. $app = Factory::miniProgram($config);
  56. $response = $app->app_code->getUnlimit($verify_code, ['path' => 'pages/verification/index']);
  57. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  58. $filename = $response->saveAs(storage_path('app/public/verify_code'), $verify_code);
  59. }
  60. $prefix = Storage::disk('public')->url('verify_code/');
  61. return $this->success(['qrcode' => $prefix . $filename]);
  62. }
  63. }