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

84 lines
2.6 KiB

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