短码生成算法及url二维码生成

时间:2022-11-17 07:30:09

懒得整理直接把接口贴上去了。

	public function action_test() {
        $callback   = isset( $_GET[ 'callback' ] ) ? $_GET[ 'callback' ] : 'callback';
        $content = Arr::get($_GET, 'content', '');
        // 获取会员
        $member_id  = 0;
        $memberinfo = member_Member::getMemberInfo();
        if (is_array($memberinfo) && $memberinfo['code'] == 200) {
            $member_id = (int)$memberinfo['data']['member_id'];
        }
        if ($member_id == 0) {
            $data = array(
                'code' => 30001,
                'msg'  => '未登录',
                'data' => array()
            );
        } else if (empty($content)){
				$data = array(
					'code' => 30002,
					'msg'  => '内容不能为空',
					'data' => array()
				);
		}else {
		    //生成短字符串
		    $chars = array(
				"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5",
				"6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
			);
			$url_str = "http://www.guugle.com.cn/".time().rand(0000,9999);
			$hash = md5($url_str);
            $code = '';
			for($i=0;$i<5;$i++){
				$temp = substr($hash, $i*6, 6);
				$num = base_convert($temp, 16, 10).'<br/>';
				$code .= $chars[$num%62];
			}
        	// 检测生成的短字符是否存在
			$res = DB::query(Database::SELECT,"SELECT code FROM `tmp_qixijie` WHERE binary code = '".$code."'")
						->execute()
						->as_array();
            if (count($res)) {
                //存在的话打乱短码
				$code = str_shuffle($code);
            }
			 $idata = array(
				 'member_id'   => $member_id,
				 'content'    => $content,
				 'code'    => $code,
				 'gmt_created' => date('Y-m-d H:i:s', time())
			 );
			DB::insert('tmp_test', array_keys($idata))
					 ->values(array_values($idata))
					 ->execute();
            $url = 'http://www.guugle.com.cn?c='.$code;
              //二维码生成
             $PNG_WEB_DIR = '/data/.../imgcode/';
             include("/data/.../qrlib.php");
             $ecc = 'H';     // L-smallest, M, Q, H-best
             $size = 6; // 1-50
             $file = $code.time().'.png';
             $filename = $PNG_WEB_DIR.$file;
             QRcode::png($url, $filename, $ecc, $size, 2);
             chmod($filename, 0640);
             $rdata = array(
                      'member_id'  => $member_id,
                      'content'    => $content,
                      'url'        => $url,
                      'img'        => $file
              );
			$data = array(
				'code' => 200,
				'msg'  => '留言成功',
				'data' => $rdata
			);
        }
        echo $callback.'('.json_encode($data).')';
        exit;
    }

二维码生成采用插件

下载地址:http://phpqrcode.sourceforge.net