Qrcode.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Controllers\Wechat;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Ixudra\Curl\Facades\Curl;
  6. class Qrcode extends Controller
  7. {
  8. //
  9. #创建二维码
  10. public static function create($action_name, $value, $expire_seconds = 60)
  11. {
  12. $post = ['expire_seconds' => $expire_seconds];
  13. switch ($action_name) {
  14. case 'QR_SCENE':
  15. $scene_type = 'scene_id';
  16. $value = (int)$value;
  17. break;
  18. case 'QR_STR_SCENE':
  19. $scene_type = 'scene_str';
  20. $value = (string)$value;
  21. break;
  22. case 'QR_LIMIT_SCENE':
  23. $scene_type = 'scene_id';
  24. $value = (int)$value;
  25. break;
  26. case 'QR_LIMIT_STR_SCENE':
  27. $scene_type = 'scene_str';
  28. $value = (string)$value;
  29. break;
  30. }
  31. $post = json_encode(array_merge($post, [
  32. 'action_name' => $action_name,
  33. 'action_info' => [
  34. 'scene' => [
  35. $scene_type => $value
  36. ]
  37. ]
  38. ]));
  39. $http = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . Base::getAccessToken();
  40. $result = json_decode(Curl::to($http)->withData($post)->post());
  41. $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($result->ticket);
  42. return $url;
  43. }
  44. public function all($scene_str)
  45. {
  46. $action_name = 'QR_STR_SCENE';
  47. $result = Curl::to((self::create($action_name, $scene_str, 2592000)))->get();
  48. return response($result, 200, ['Content-Type' => 'image/png']);
  49. }
  50. public function from($from, $secret = null)
  51. {
  52. $param = [
  53. 'event' => 'secret',
  54. 'from' => $from
  55. ];
  56. if (isset($secret)) {
  57. $param['secret'] = $secret;
  58. }
  59. $action_name = 'QR_STR_SCENE';
  60. $scene_str = http_build_query($param);
  61. $result = Curl::to((self::create($action_name, $scene_str, 2592000)))->get();
  62. return response($result, 200, ['Content-Type' => 'image/png']);
  63. }
  64. /**
  65. * 永久二维码
  66. */
  67. public function perpetual($scene_str, $alias = '')
  68. {
  69. $action_name = 'QR_LIMIT_STR_SCENE';
  70. $result = Curl::to((self::create($action_name, $scene_str)))->get();
  71. parse_str($scene_str, $param);
  72. if (\DB::table('kdgx_fpdx_qrcode_alias')->where('key', $scene_str)->first()) {
  73. if ($alias) {
  74. \DB::table('kdgx_fpdx_qrcode_alias')->where('key', $scene_str)->update([
  75. 'alias' => $alias,
  76. 'updated_at' => date('Y-m-d H:i:s')
  77. ]);
  78. }
  79. } else {
  80. \DB::table('kdgx_fpdx_qrcode_alias')->insert([
  81. 'from' => @$param['from'],
  82. 'key' => $scene_str,
  83. 'alias' => $alias,
  84. 'created_at' => date('Y-m-d H:i:s'),
  85. 'updated_at' => date('Y-m-d H:i:s')
  86. ]);
  87. }
  88. return response($result, 200, ['Content-Type' => 'image/png']);
  89. }
  90. }