123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace Admin\Controller;
- use Common\Controller\BaseController;
- use Common\Server\RegularServer;
- use Common\Common\Common;
- /**
- * 公共控制器
- */
- class CommonController extends BaseController {
- public function __construct() {
- parent::__construct();
- }
- /**
- * 生成验证码
- * @author: huangch
- */
- public function get_verify_code() {
- $config = array(
- 'fontSize' => 18, // 验证码字体大小
- 'length' => 4, // 验证码位数
- 'useNoise' => false, // 关闭验证码杂点
- 'imageW' => 120,
- 'imageH' => 50
- );
- $Verify = new \Think\Verify($config);
- $Verify->entry();
- }
- /**
- * 验证图片验证码
- * @param $code
- * @param string $id
- * @return bool
- * @author: huangch
- */
- public function check_verify($code, $id = ''){
- $verify = new \Think\Verify();
- return $verify->check($code, $id);
- }
- /**
- * 图片上传
- * @author: huangch
- */
- public function img_upload(){
- header('Content-Type:application/json; charset=utf-8');
- $Common = new Common();
- $img = I('post.file',null);
- $img = $Common->base64_upyun($img);
- $this->apiReturn(true,['url' => $img],'上传成功');
- }
- public function base64_upload($img) {
- $Common = new Common();
- $url = $Common->base64_upyun($img);
- return $url;
- }
- public function file_upload() {
- $Common = new Common();
- $url = $Common->upload();
- $url = $this->img_upyun($url);
- die(json_encode($url));
- }
- //上传单图到又拍云
- public function img_upyun($pic_url) {
- $upyun = new \Common\Common\util\UpYun(UPYUN_BUCKET, UPYUN_NAME, UPYUN_PWD);
- if (strpos($pic_url, ROOT_PATH) === false) {
- $path = ROOT_PATH . '/' . $pic_url;
- } else {
- $path = $pic_url;
- }
- try {
- $fh = fopen($path, 'rb');
- $img_str = end(explode('/', $path));
- $img_arr = explode('.', $img_str);
- $img_name = md5($path . time()); //重新命名图片名称
- $img_path = '/' . date("Y") .'/'. date("m") .'/'. date("d") . '/';
- $rsp = $upyun->writeFile('/' . $img_path . $img_name . '.' . end($img_arr), $fh, True); // 上传图片,自动创建目录
- fclose($fh);
- unlink($pic_url);
- $img = UPYUN_DOMAIN . $img_path . $img_name . '.' . end($img_arr);
- return $img;
- } catch (\Exception $e) {
- dump($e->getMessage());
- }
- }
- /**
- * 上传图片到又拍云并返回地址
- */
- public function upload_upyun(){
- if(IS_POST) {
- $img = I('post.img');
- if ('data:image' == substr($img, 0, 10)) {
- $Common = new \Common\Common\Common();
- $url = $Common->base64_upyun($img);
- $this->success($url);
- } else {
- $this->error('图片错误');
- }
- } else {
- $this->display();
- }
- }
- }
|