zoukankan      html  css  js  c++  java
  • 微信选择图片、上传图片、下载图片、扫一扫接口调用源码

    1.添加微信js引用:

    <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

    2.Html示例代码

    <div style="margin: 200px 0px 0px 200px;">
    <button id="chooseImage" class="btn btn_primary">照相一下</button>
    <br />
    <button id="uploadImage">上传相片</button>
    <br />
    <button id="downloadImage">下载相片</button>
    <br />
    <button id="scanQRCode">扫一扫</button>
    </div>

    3.微信接口调用配置-通过config接口注入权限验证配置

    所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用)。

    wx.config({
    debug: true,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印

    appId: '{$appId}',// 必填,公众号的唯一标识

    timestamp: '{$timestamp}',// 必填,生成签名的时间戳

    nonceStr: '{$nonceStr}',// 必填,生成签名的随机串

    signature: '{$signature}',// 必填,签名

    jsApiList: [  // 必填,需要使用的js接口列表
    'checkJsApi',
    'onMenuShareTimeline',
    'onMenuShareAppMessage',
    'onMenuShareQQ',
    'onMenuShareWeibo',
    'hideMenuItems',
    'showMenuItems',
    'hideAllNonBaseMenuItem',
    'showAllNonBaseMenuItem',
    'translateVoice',
    'startRecord',
    'stopRecord',
    'onRecordEnd',
    'playVoice',
    'pauseVoice',
    'stopVoice',
    'uploadVoice',
    'downloadVoice',
    'chooseImage',
    'previewImage',
    'uploadImage',
    'downloadImage',
    'getNetworkType',
    'openLocation',
    'getLocation',
    'hideOptionMenu',
    'showOptionMenu',
    'closeWindow',
    'scanQRCode',
    'chooseWXPay',
    'openProductSpecificView',
    'addCard',
    'chooseCard',
    'openCard'
    ]
    });

    // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。

    wx.ready(function() {
    var images = {
    localId: [],
    serverId: []
    };

    4.选择图片

    document.querySelector('#chooseImage').onclick = function() {
    wx.chooseImage({
    success: function(res) {
    images.localId = res.localIds;
    alert('已选择 ' + res.localIds.length + ' 张图片');
    $.each(images.localId, function(key, val) {
    $("#Img").append("<div><img width='150' height='150' src=" + images.localId[key] + " /></div>");
    });
    }
    });
    };

    5.上传图片

    document.querySelector('#uploadImage').onclick = function() {
    if (images.localId.length == 0) {
    alert('请先使用 chooseImage 接口选择图片');
    return;
    }
    var i = 0,
    length = images.localId.length;
    images.serverId = [];

    function upload() {
    wx.uploadImage({
    localId: images.localId[i],
    success: function(res) {
    i++;
    //alert('已上传:' + i + '/' + length);

    images.serverId.push(res.serverId);
    alert(images.serverId);
    if (i < length) {
    upload();
    }
    },
    fail: function(res) {
    alert(JSON.stringify(res));
    }
    });
    }
    upload();
    };

    6.下载图片

    document.querySelector('#downloadImage').onclick = function() {
    if (images.serverId.length === 0) {
    alert('请先使用 uploadImage 上传图片');
    return;
    }
    var i = 0,
    length = images.serverId.length;
    images.localId = [];

    function download() {
    wx.downloadImage({
    serverId: images.serverId[i],
    success: function(res) {
    i++;
    alert('已下载:' + i + '/' + length);
    images.localId.push(res.localId);
    if (i < length) {
    download();
    }
    }
    });
    }
    download();
    };

    7.扫一扫

    document.querySelector('#scanQRCode').onclick = function() {
    wx.scanQRCode({
    needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
    scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
    success: function(res) {
    var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
    alert(result);
    }
    });
    };

  • 相关阅读:
    vue iview 导入excel文件(upload)
    iview table 中用根据不同状态改变颜色(用render)
    vue项目中导出excel表格数据
    iview table 中 render 时间格式化
    微信浏览器的部分特效更接近浏览器IE 9
    ABP 拦截器不工作
    vue + webpack 添加的引用不编译成ES6的问题
    把vue组件添加到body下
    vue+vuex+router实现阻止浏览器回退
    webpack使用vue-moment-libs 在PC微信浏览器下显示空白
  • 原文地址:https://www.cnblogs.com/wang150601/p/6377279.html
Copyright © 2011-2022 走看看