Userposter.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 用户推荐
  7. */
  8. class Userposter extends Api
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = ['*'];
  12. //二维码海报
  13. private function mini_code($scene, $page_url, $introcode)
  14. {
  15. $value = config('wxMiniProgram');
  16. $appid = $value['appid'];
  17. $secret = $value['secret'];
  18. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
  19. $res = file_get_contents($url);
  20. $token = json_decode($res, true)['access_token'];
  21. $URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $token;
  22. $data = [
  23. 'scene' => 'introcode='.$introcode, //二维码传入参数
  24. 'page' => $page_url, //扫码后进入页面
  25. 'env_version' => 'trial', //要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
  26. 'width' => 800, //二维码的宽度,单位 px,最小 280px,最大 1280px
  27. 'auto_color' => false, //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
  28. 'is_hyaline' => false, //是否需要透明底色,为 true 时,生成透明底色的小程序
  29. 'check_path' => false,
  30. ];
  31. $json = json_encode($data); //数组加密
  32. //$result = $this->api_notice_increment($URL, $json); //用CURL 进行POST请求
  33. $result = curl_post($URL,$json);
  34. $path = ROOT_PATH . 'public/uploads/minicode'; //ROOT_PATH 我使用的是TP5框架
  35. $path2 = '/uploads/minicode';
  36. if (!file_exists($path)) { //判断目录是否存在
  37. mkdir($path, 0777, true);
  38. }
  39. $path = $path . '/' . $scene . '.png'; //最后要写入的目录及文件名
  40. $path2 = $path2 . '/' . $scene . '.png'; //最后要写入的目录及文件名
  41. file_put_contents($path, $result);
  42. return $path2;
  43. }
  44. //生成视频分享海报
  45. public function shareposter() {
  46. $inviteimage = $this->mini_code($this->auth->id,'pages/index/index',$this->auth->introcode);
  47. $data = [
  48. [
  49. "left"=> "100px",
  50. "top"=> "515px",
  51. "type"=> "img",
  52. "width"=> "170px",
  53. "height"=> "170px",
  54. "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  55. ],
  56. [
  57. "left"=> "158px",
  58. "top"=> "490px",
  59. "type"=> "nickname",
  60. "width"=> "166px",
  61. "height"=> "38px",
  62. "size"=> "13px",
  63. "color"=> "#2cb47d",
  64. "content" => $this->auth->introcode,
  65. ],
  66. ];
  67. $data = json_encode($data, 320);
  68. $poster = [
  69. 'id' => 1,
  70. 'title' => '测试',
  71. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  72. 'bg_image' => '/assets/img/posteruserbg.png',
  73. 'data' => $data,
  74. 'status' => 'normal',
  75. 'weigh' => 0,
  76. 'createtime' => 1653993709,
  77. 'updatetime' => 1653994259,
  78. ];
  79. $image = new \addons\poster\library\Image();
  80. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  81. if (!$imgurl) {
  82. $this->error('生成海报出错');
  83. }
  84. $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  85. // echo '<html><body><img src="'.$imgurl.'"></body></html>';
  86. $this->success('', $imgurl);
  87. }
  88. }