解决H5在微信浏览器中保存联系人问题

利用QRCode.js 生成二维码。 可离线生成,

主要是通过获取 DOM 的标签,再通过 HTML5 Canvas 绘制而成,不依赖任何库

效果图

上代码!!!!

<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>手机名片生成器</title>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no" />
  <meta name="format-detection" content="telephone=no" />
  <link rel="stylesheet" href="./css/normalize.min.css" />
  <script type="text/javascript" src="./js/zepto.min.js"></script>
  <script type="text/javascript" src="./js/qrcode.js"></script>
  <style type="text/css">
#mytable {
	width: 100%;
}

#mytable, #mytable td {
	border: 1px solid #ccc;
}

#mytable input, #mytable select {
	height: 40px;
	border: none;
	width: 100%;
	-webkit-appearance: none;
	box-sizing: border-box;
	outline: none;
}

.title {
	text-align: right;
	padding-right: 5px;
}

#qrcode {
	text-align: center;
	width: 100%;
	overflow: hidden;
	padding: 20px 0;
}

#tips {
	position: fixed;
	bottom: -40%;
	left: 50%;
	display: inline-block;
	padding: 10px;
	border-radius: 10px;
	background-color: rgba(0,0,0,.8);
	color: #fff;
	text-align: center;
}
  </style>
 </head>
 <body>
  <table id="mytable">
   <tbody>
    <tr>
     <td colspan="4">
      <div style="height: 60px; line-height: 60px; text-align: center; font-size: 24px;">
       手机名片生成器
      </div></td>
    </tr>
    <tr>
     <td width="25%" class="title">姓氏</td>
     <td width="25%"><input type="text" id="fname" placeholder="姓氏" maxlength="4" /></td>
     <td width="25%" class="title">名字</td>
     <td width="25%"><input type="text" id="name" placeholder="名字" maxlength="4" /></td>
    </tr>
    <tr>
     <td class="title">职称</td>
     <td colspan="3"><input type="text" id="title" placeholder="职称" maxlength="10" /></td>
    </tr>
    <tr>
     <td class="title">移动电话</td>
     <td colspan="3"><input type="tel" id="cell" placeholder="xxx-xxxx-xxxx" maxlength="13" /></td>
    </tr>
    <tr>
     <td class="title">家庭电话</td>
     <td colspan="3"><input type="tel" id="home" placeholder="xxx-xxxxxxxx" maxlength="13" /></td>
    </tr>
    <tr>
     <td class="title">家庭住址</td>
     <td colspan="3"><input type="text" id="adr" placeholder="家庭住址" maxlength="20" /></td>
    </tr>
    <tr>
     <td class="title">公司名称</td>
     <td colspan="3"><input type="text" id="org" placeholder="公司名称" maxlength="20" /></td>
    </tr>
    <tr>
     <td class="title">个人网站</td>
     <td colspan="3"><input type="url" id="url" placeholder="http://www.xxx.com" maxlength="25" /></td>
    </tr>
    <tr>
     <td class="title">电子邮箱</td>
     <td colspan="3"><input type="email" id="email" placeholder="xxxx@xxx.com" maxlength="25" /></td>
    </tr>
    <tr>
     <td class="title">生日</td>
     <td colspan="3"><input type="date" id="bday" placeholder="yyyy-mm-dd" /></td>
    </tr>
    <tr>
     <td><select id="radius"><option value="0">直角</option><option value="0.25">小圆角</option><option value="0.5">大圆角</option></select></td>
     <td><select id="fill"><option value="#000">黑色块</option><option value="#002C7D">蓝色块</option><option value="#AE4141">红色块</option><option value="#FF79E1">粉色块</option><option value="#7CFF57">绿色块</option></select></td>
     <td><select id="mode"><option value="0">无签名</option><option value="1">签名1</option><option value="2">签名2</option></select></td>
     <td><select id="fontcolor"><option value="#000">黑签名</option><option value="#002C7D">蓝签名</option><option value="#AE4141">红签名</option><option value="#FF79E1">粉签名</option><option value="#7CFF57">绿签名</option></select></td>
    </tr>
    <tr>
     <td colspan="4"><input type="button" onclick="createvcf()" value="生成名片二维码" /></td>
    </tr>
   </tbody>
  </table>
  <div id="qrcode">
   <div style="box-sizing: border-box; padding: 0 20px; width: 100%; text-align: left;">
    本工具可将你的信息生成一张二维码,你可以按住生成的二维码将其保存至手机然后通过微信或者其他通讯工具发给其他人,其他用户扫描你的二维码之后可自动将你的信息保存至你的
    <span style="color: red;">手机通讯录</span>。注意是
    <strong style="color: red;">手机通讯录</strong>哦~
    <br />使用过程中完全不占你流量,不窃取你任何信息,打开本页面后断开手机网络依旧可以
    <span style="color: red;">免流量离线生成</span>二维码。
   </div>
  </div>
  <div id="tips"></div>
  <script type="text/javascript">
 function createvcf() {
    var a, 
    b = $("#fname").val().replaceAll(" ", ""),	//姓氏
    c = $("#name").val().replaceAll(" ", ""),	//名字
    d = $("#title").val().replaceAll(" ", ""),	//职称
    e = $("#adr").val().replaceAll(" ", ""),	//家庭住址
    f = $("#org").val().replaceAll(" ", ""),	//公司名称	
    g = $("#cell").val().replaceAll(" ", ""),	//移动电话
    h = $("#home").val().replaceAll(" ", ""),	//家庭电话
    i = $("#url").val().replaceAll(" ", ""),	//个人网站
    j = $("#email").val().replaceAll(" ", ""),	//电子邮箱
    k = $("#bday").val().replaceAll(" ", "");	//生日
    b ? c ? g || h ? (a = "BEGIN:VCARD", a += "\r\nN:" + b + ";" + c + ";;;", a += "\r\nFN: " + c + "  " + b, d && (a += "\r\nTITLE:" + d), e && (a += "\r\nADR;HOME:;;" + e + ";;;;"), f && (a += "\r\nORG:" + f), g && (a += "\r\nTEL;CELL,VOICE:" + g), h && (a += "\r\nTEL;HOME,VOICE:" + h), i && (a += "\r\nURL;WORK:" + i), j && (a += "\r\nEMAIL;INTERNET,HOME:" + j), k && (a += "\r\nBDAY:" + k), a += "\r\nEND:VCARD", $("#qrcode").empty().qrcode({
        render: "image",
        ecLevel: "0" == $("#mode").val() ? "L": "H",
        size: 300,
        background: "#fff",
        fill: $("#fill").val(),
        radius: $("#radius").val(),
        mode: 1 * $("#mode").val(),
        fontcolor: $("#fontcolor").val(),
        label: b + c,
        text: a
    }), 
    //提示框
    tips("名片生成成功"), window.scrollTo(0, 1e3)) : tips("必须输入一个电话") : tips("请填写您的名字") : tips("请填写您的姓氏")
}
String.prototype.replaceAll = function(a, b) {
    return this.replace(new RegExp(a.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g, "\\$1"), "ig"), b)
};
var _tipstime, tips = function(a) {
    _tipstime && clearTimeout(_tipstime);
    var b = $("#tips").text(a);
    b.css({
        transform: "translateX(" + -b.width() / 2 + "px)",
        bottom: "20%"
    }),
    _tipstime = setTimeout(function() {
        b.empty().css("bottom", "-40%")
    },
    1500)
};
  </script>
 </body>
</html>

如果出现乱码拼接的vcard传到 utf16to8函数中

function utf16to8(str) {
      var out, i, len, c;
      out = "";
      len = str.length;
      for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
          out += str.charAt(i);
        } else if (c > 0x07FF) {
          out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
          out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
          out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
          out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
          out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
      }
      return out;
    }

 

 

所需要的工具和源代码,点击这里

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在UniApp实现在微信浏览器进行H5绑定微信的功能,你可以按照以下步骤进行操作: 1. 在微信开放平台上创建一个应用,获取到AppID。 2. 在UniApp项目的`manifest.json`文件,添加微信登录的权限配置。在`uni-app`字段下添加以下代码: ```json "wx": { "appid": "你的AppID", "scope": "snsapi_login", "state": "uniapp", "redirect_uri": "http://your-domain.com/auth" } ``` 将其的`appid`替换为你在微信开放平台上获取到的AppID。`redirect_uri`是用户登录后重定向的URL,需要替换为你自己的URL。 3. 在需要进行微信绑定的页面,引入UniApp提供的wx组件,示例代码如下: ```html <template> <view> <button @click="bindWechat">绑定微信</button> </view> </template> <script> import { uniLogin } from '@/common/utils/wx' export default { methods: { async bindWechat() { try { const res = await uniLogin() // 处理绑定微信成功后的逻辑 } catch (error) { // 处理绑定微信失败的逻辑 } } } } </script> ``` 4. 在项目创建一个`common/utils/wx.js`文件,实现微信登录的方法。可以使用`uni.login`和`uni.getUserInfo`进行登录操作,示例代码如下: ```js export function uniLogin() { return new Promise((resolve, reject) => { uni.login({ provider: 'weixin', success: (res) => { uni.getUserInfo({ provider: 'weixin', success: (infoRes) => { resolve({ code: res.code, userInfo: infoRes.userInfo }) }, fail: () => { reject(new Error('获取用户信息失败')) } }) }, fail: () => { reject(new Error('微信登录失败')) } }) }) } ``` 这里使用了`uni.login`获取登录凭证,再通过`uni.getUserInfo`获取用户信息。你可以根据需要进行适当的修改。 以上是在UniApp实现在微信浏览器进行H5绑定微信的步骤,希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值