Skip to content

Drupal 通讯录页面添加二维码

🏷️ Drupal

在通讯录的 View 上添加二维码,使用手机扫描二维码自动新建联系人。

本来想在 View 上新建一个 CustomText 列,使用别的网站的 api 直接生成二维码的。

但是中文有乱码问题,CustomText 列中又不能写 js 代码,只好新建一个 view 的模板文件,在模板文件中添加相应的代码。

同时使用 jquery-qrcode 插件,使用 canvas 生成二维码图片。

views-view-table--contacts.tpl.php 的代码如下:

点击查看代码
php
<?php
/**
 * @file
 * Views-view-table.tpl.php.
 */

/**
 * Template to display a view as a table.
 *
 * Fields Available.
 * - $title : The title of this group of rows.  May be empty.
 * - $header: An array of header labels keyed by field id.
 * - $caption: The caption for this table. May be empty.
 * - $header_classes: An array of header classes keyed by field id.
 * - $fields: An array of CSS IDs to use for each field id.
 * - $classes: A class or classes to apply to the table, based on settings.
 * - $row_classes: An array of classes to apply to each row, indexed by row
 *   number. This matches the index in $rows.
 * - $rows: An array of row items. Each row is an array of content.
 *   $rows are keyed by row number, fields within rows are keyed by field ID.
 * - $field_classes: An array of classes to apply to each field, indexed by
 *   field id, then row number. This matches the index in $rows.
 *
 * @ingroup views_templates
 */
?>
<script type="text/javascript" src="/sites/all/themes/color_glass/js/qrcode/qrcode.js"></script>
<script type="text/javascript" src="/sites/all/themes/color_glass/js/qrcode/jquery.qrcode.js"></script>
<script type="text/javascript">
var _countBits = function(_c) {
    var cnt = 0;
    while(_c > 0) {
        cnt++;
        _c = _c >>> 1;
    }
    return cnt;
};
function UnicodeToUtf8Bytes2(code) {
        if ((code == null) || (code < 0) ||
        (code > (Math.pow(2, 31) -1))) {
        return ["?".charCodeAt(0)];
    }
    if (code < 0x80) {
        return 
;
    }
    var arr = ;
    while ((code >>> 6) > 0) {
        arr.push(0x80 | (code & 0x3F));
        code = code >>> 6;
    }
    if ((arr.length + 2 + (_countBits(code))) > 8) {
        arr.push(0x80 | code);
        code = 0;
    }
    var pre = 0x80;
    for (var i = 0; i < arr.length; i++) {
      pre |= (0x80 >>> (i + 1));
    }
    arr.push(pre | code);
    return arr.reverse();
}

QR8bitByte.prototype.getLength = function(buffer) {
  var len = 0;
  for (var i = 0; i < this.data.length; i++) {
    var bytes = UnicodeToUtf8Bytes2(this.data.charCodeAt(i));
    len += bytes.length;
  }
  return len;
};

QR8bitByte.prototype.write = function(buffer) {
  for (var i = 0; i < this.data.length; i++) {
    var bytes = UnicodeToUtf8Bytes2(this.data.charCodeAt(i));
    for (var x = 0; x < bytes.length; x++) {
      buffer.put(bytes[x], 8);
    }
  }
};
</script>

<?php if ($responsive): ?>
<div class="table-responsive">
<?php endif; ?>
<table <?php if ($classes) : print 'class="' . $classes . '" '; endif; ?><?php print $attributes; ?>>
   <?php if (!empty($title) || !empty($caption)) : ?>
     <caption><?php print $caption . $title; ?></caption>
  <?php endif; ?>
  <?php if (!empty($header)) : ?>
    <thead>
      <tr>
        <?php foreach ($header as $field => $label): ?>
          <th <?php if ($header_classes[$field]) : print 'class="' . $header_classes[$field] . '" '; endif; ?>>
            <?php print $label; ?>
          </th>
        <?php endforeach; ?>
        <th>联系人二维码</th>
      </tr>
    </thead>
  <?php endif; ?>
  <tbody>
    <?php foreach ($rows as $row_count => $row): ?>
      <tr <?php if ($row_classes[$row_count]) : print 'class="' . implode(' ', $row_classes[$row_count]) . '"';  endif; ?>>
        <?php foreach ($row as $field => $content): ?>
          <td <?php if ($field_classes[$field][$row_count]) : print 'class="' . $field_classes[$field][$row_count] . '" '; endif; ?><?php print drupal_attributes($field_attributes[$field][$row_count]); ?>>
            <?php print $content; ?>
          </td>
        <?php endforeach; ?>
        <td>
          <div <?php print 'id="qr_' . $row["name"] .'"'; ?>></div>
          <script>
          jQuery(function(){
            jQuery(<?php print '"#qr_' . $row["name"] .'"'; ?>).qrcode(
              {
                correctLevel:QRErrorCorrectLevel.M,
                text:"BEGIN:VCARD\r\nVERSION:3.0\r\nN:<?php print $row["name"]; ?>;\r\nTEL;TYPE=WORD,VOICE,PREF,MSG:<?php print $row["field_mobile_phone"]; ?>\r\nEMAIL;TYPE=PREF,INTERNET:<?php print $row["field_email"]; ?>\r\nEND:VCARD"
              }
            );
          });
          </script>
        </td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>
<?php if ($responsive): ?>
  </div>
<?php endif; ?>