Code.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | 获取小程序二维码
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace Wechat_applet\Src;
  8. use Dever;
  9. use Token\Lib\Wechat;
  10. class Code
  11. {
  12. public function get($send = array())
  13. {
  14. $project = Dever::input('project');
  15. $param['path'] = Dever::input('path', 'pages/index/index');
  16. $param['param'] = Dever::input('send');
  17. $filename = Dever::input('filename');
  18. $param['width'] = Dever::input('width', 200);
  19. $param['is_hyaline'] = false;
  20. #正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
  21. $param['env_version'] = 'release';
  22. $update = Dever::input('update', 2);
  23. if ($send) {
  24. if (isset($send['project'])) {
  25. $project = $send['project'];
  26. }
  27. if (isset($send['path'])) {
  28. $param['path'] = $send['path'];
  29. }
  30. if (isset($send['send'])) {
  31. $param['param'] = $send['send'];
  32. }
  33. if (isset($send['width'])) {
  34. $param['width'] = $send['width'];
  35. }
  36. if (isset($send['env'])) {
  37. $param['env_version'] = $send['env'];
  38. }
  39. if (isset($send['filename'])) {
  40. $filename = $send['filename'];
  41. }
  42. if (isset($send['update'])) {
  43. $update = $send['update'];
  44. }
  45. }
  46. if (!$filename) {
  47. $filename = md5($param['param']);
  48. }
  49. $where['project_id'] = $project;
  50. $where['filename'] = $filename;
  51. $where['path'] = $param['path'];
  52. $where['send'] = $param['param'];
  53. $code = Dever::db('wechat_applet/code')->find($where);
  54. if (!$code) {
  55. $update = 1;
  56. } elseif ($update == 2) {
  57. return $code['file'] . '?v=' . time();
  58. }
  59. if ($update == 1) {
  60. $wechat = new Wechat($project, 'wechat_applet');
  61. $data = $wechat->curl('code', $param, true);
  62. if ($data) {
  63. $upload = Dever::load('upload/save')->copy('content,' . $data, 1, false, '', 'png', $filename);
  64. if (isset($upload['url'])) {
  65. $where['file'] = $upload['url'];
  66. $where['width'] = $param['width'];
  67. $where['env'] = $param['env_version'];
  68. if ($code) {
  69. $where['where_id'] = $code['id'];
  70. Dever::db('wechat_applet/code')->update($where);
  71. } else {
  72. Dever::db('wechat_applet/code')->insert($where);
  73. }
  74. return $upload['url'] . '?v=' . time();
  75. }
  76. }
  77. }
  78. return false;
  79. /*
  80. $file = Dever::pathDay('upload/applet/code', false) . $project . '_' . md5($param['path'] . '_' . $param['param'] . '_' . $param['width']) . '.jpg';
  81. if (!is_file($file) || $update == 1) {
  82. $wechat = new Wechat($project, 'wechat_applet');
  83. $data = $wechat->curl('code', $param, true);
  84. if ($data) {
  85. file_put_contents($file, $data);
  86. }
  87. }
  88. //$host = str_replace(DEVER_PROJECT . '/' . DEVER_APP_NAME . '/', '', Dever::config('host')->base);
  89. $host = Dever::config('host')->host;
  90. if (strstr($file, '/share/dc/data/')) {
  91. return $host . str_replace('/share/dc/data/', '', $file);
  92. }
  93. return $host . str_replace(DEVER_PROJECT_PATH, '', $file);
  94. $file = Dever::pic($file);
  95. return $file;
  96. */
  97. }
  98. }