CommonController.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Admin\Controller;
  3. use Common\Controller\BaseController;
  4. use Common\Server\RegularServer;
  5. use Common\Common\Common;
  6. /**
  7. * 公共控制器
  8. */
  9. class CommonController extends BaseController {
  10. public function __construct() {
  11. parent::__construct();
  12. }
  13. /**
  14. * 生成验证码
  15. * @author: huangch
  16. */
  17. public function get_verify_code() {
  18. $config = array(
  19. 'fontSize' => 18, // 验证码字体大小
  20. 'length' => 4, // 验证码位数
  21. 'useNoise' => false, // 关闭验证码杂点
  22. 'imageW' => 120,
  23. 'imageH' => 50
  24. );
  25. $Verify = new \Think\Verify($config);
  26. $Verify->entry();
  27. }
  28. /**
  29. * 验证图片验证码
  30. * @param $code
  31. * @param string $id
  32. * @return bool
  33. * @author: huangch
  34. */
  35. public function check_verify($code, $id = ''){
  36. $verify = new \Think\Verify();
  37. return $verify->check($code, $id);
  38. }
  39. /**
  40. * 图片上传
  41. * @author: huangch
  42. */
  43. public function img_upload(){
  44. header('Content-Type:application/json; charset=utf-8');
  45. $Common = new Common();
  46. $img = I('post.file',null);
  47. $img = $Common->base64_upyun($img);
  48. $this->apiReturn(true,['url' => $img],'上传成功');
  49. }
  50. public function base64_upload($img) {
  51. $Common = new Common();
  52. $url = $Common->base64_upyun($img);
  53. return $url;
  54. }
  55. public function file_upload() {
  56. $Common = new Common();
  57. $url = $Common->upload();
  58. $url = $this->img_upyun($url);
  59. die(json_encode($url));
  60. }
  61. //上传单图到又拍云
  62. public function img_upyun($pic_url) {
  63. $upyun = new \Common\Common\util\UpYun(UPYUN_BUCKET, UPYUN_NAME, UPYUN_PWD);
  64. if (strpos($pic_url, ROOT_PATH) === false) {
  65. $path = ROOT_PATH . '/' . $pic_url;
  66. } else {
  67. $path = $pic_url;
  68. }
  69. try {
  70. $fh = fopen($path, 'rb');
  71. $img_str = end(explode('/', $path));
  72. $img_arr = explode('.', $img_str);
  73. $img_name = md5($path . time()); //重新命名图片名称
  74. $img_path = '/' . date("Y") .'/'. date("m") .'/'. date("d") . '/';
  75. $rsp = $upyun->writeFile('/' . $img_path . $img_name . '.' . end($img_arr), $fh, True); // 上传图片,自动创建目录
  76. fclose($fh);
  77. unlink($pic_url);
  78. $img = UPYUN_DOMAIN . $img_path . $img_name . '.' . end($img_arr);
  79. return $img;
  80. } catch (\Exception $e) {
  81. dump($e->getMessage());
  82. }
  83. }
  84. /**
  85. * 上传图片到又拍云并返回地址
  86. */
  87. public function upload_upyun(){
  88. if(IS_POST) {
  89. $img = I('post.img');
  90. if ('data:image' == substr($img, 0, 10)) {
  91. $Common = new \Common\Common\Common();
  92. $url = $Common->base64_upyun($img);
  93. $this->success($url);
  94. } else {
  95. $this->error('图片错误');
  96. }
  97. } else {
  98. $this->display();
  99. }
  100. }
  101. }