其他分享
首页 > 其他分享> > 使用记录16_微信中长按图片识别二维码

使用记录16_微信中长按图片识别二维码

作者:互联网

转载自 https://blog.csdn.net/haibo19981/article/details/80452490

1.微信官网预览图片的说明
wx.previewImage(Object object)         
                        默认值       是否必填
current     string            urls 的第一张    否    当前显示图片的链接    
urls     Array.<string>                    是    需要预览的图片链接列表    
success     function                        否    接口调用成功的回调函数    
fail     function                        否    接口调用失败的回调函数    
complete function                        否    接口调用结束的回调函数(调用成功、失败都会执行)


wx.previewImage({  
  urls: this.data.scene.split(',')  // 需要预览的图片http链接  使用split把字符串转数组。不然会报错  
})  


2.请求广告图服务器给出的API        
URL:https://www.mysite.net/common/game/ads?app_name=run        
参数:app_name 指每个游戏唯一标识,英文数字下划线
返回:
{
    "code":0,
    "msg":"\u6210\u529f", //成功
    "data":{
        "list":[
            {
            "id":"2",
            "appName":"2048youxiwang",
            "image":"http:\/\/static.mysite.cn\/wxgame\/ad\/201805\/nxeRBdr5Q8AdM55s.jpg", //弹出图片的链接
            "title":"",
            "url":"",
            "weight":"50", //权重(当有多个同类可选择时,按权重随机)
            "icon":"http:\/\/static.mysite.cn\/wxgame\/ad\/201805\/DkfGWsRfxfAHGPB7.png", //按钮的icon
            "status":"0",
            "createdAt":"1525778538",
            "updatedAt":"1525778538"
            }
        ]
    }
}


3.客户端实例
这里以我的项目2048为例。
3.1.登陆微信公众平台注册https://www.mysite.net
在“设置”/“开发设置”页面中,设置服务器域名如下
request合法域名:https://www.mysite.net
若不设置,在微信开发者工具中会报错。


3.2.获得广告图信息
Start.js------------
var common = require('Common');
cc.Class({
    extends: cc.Component,
    properties: {
    },
    onLoad: function(){
        this.httpRequestAds();
        this.scheduleOnce(function(){
            this.setMoreGameBtnImage();
        }, 1); //延迟1秒
    },
    
    httpRequestAds: function() { //获得广告图信息
        //cc.log('发送请求');
        var urlStr = 'https://www.mysite.net/common/game/ads?app_name=2048youxiwang';
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
                //console.log('收到数据', xhr);
                var res = JSON.parse(xhr.response);
                common.adsList = [];
                if(res.data && res.data.list){
                    //console.log("list!!!");
                    for(var i = 0; i < res.data.list.length; i++){
                        common.adsList.push({"iconUrl":res.data.list[i].icon, "imageUrl":res.data.list[i].image, "weight":res.data.list[i].weight});
                    }
                }
            }
        };
        xhr.open('GET', urlStr, true);
        xhr.send();
    },
    
    setMoreGameBtnImage: function(){ //设置更多游戏图片
        var data = common.getAdsInfo();
        if(data && data.imageUrl != "" && data.iconUrl != ""){
            var btnSprite = this.node.getChildByName("moreGameBtn").getComponent(cc.Sprite);
            common.createImage(btnSprite, data.iconUrl);
            this.adsImageUrl = data.imageUrl;
            this.scheduleOnce(function(){
                this.node.getChildByName("moreGameBtn").active = true;
                this.node.getChildByName("moreGameBtn").runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.1, 1.1), cc.scaleTo(0.2, 0.9), cc.scaleTo(0.1, 1), cc.delayTime(1))));
            }, 1); //从后台下载图片替换到按钮上会延迟大概1秒
        }else{
            this.node.getChildByName("moreGameBtn").active = false;
        }
    },
    
    onMoreGameBtn: function(){
        //console.log("点击更多游戏按钮");
        if(this.adsImageUrl != ""){
            wx.previewImage({
                current: this.adsImageUrl,  
                urls: [this.adsImageUrl]
            });  
        }
    },
});


Common.js------------
module.exports = {
    adsList: [], //广告图列表(其中元素{iconUrl,imageUrl,weight})
    
    getAdsInfo: function(){ //获得广告图信息
        var ret = null;
        if(this.adsList.length == 0){ //0
        }else if(this.adsList.length == 1){ //只有1张
            ret = {"iconUrl":this.adsList[0].iconUrl, "imageUrl":this.adsList[0].imageUrl};
        }else{ //有多张
            var weightSum = 0; //权重和
            for(var i = 0; i < this.adsList.length; i++){
                weightSum += parseInt(this.adsList[i].weight);
            }
            var rate = []; //几率
            for(var i = 0; i < this.adsList.length; i++){ 
                rate.push(parseInt(this.adsList[i].weight) / weightSum);
            }
            var checkNum = []; //所有广告图的几率范围 0-20%-40%-...-100%
            checkNum.push(0);
            for(var i = 0; i < rate.length; i++){ //初始化
                checkNum.push(0);
            }
            for(var i = 1; i < checkNum.length; i++){ //累加几率
                for(var j = 1; j < i + 1 && j < checkNum.length; j++){ 
                    checkNum[i] += rate[j - 1];
                }
            }
            var num = Math.random();
            for(var i = 0; i < checkNum.length - 1; i++){ 
                if(num >= checkNum[i] && num < checkNum[i + 1]){
                    ret = {"iconUrl":this.adsList[i].iconUrl, "imageUrl":this.adsList[i].imageUrl};
                    break;
                }
            }
        }
        return ret;
    },
    
    createImage: function(sprite, url){ //加载图片
        let image = wx.createImage();
        image.onload = function () {
            let texture = new cc.Texture2D();
            texture.initWithElement(image);
            texture.handleLoadedTexture();
            sprite.spriteFrame = new cc.SpriteFrame(texture);
        };
        image.src = url;
    },
};

标签:function,16,cc,微信,adsList,checkNum,二维码,var,data
来源: https://blog.csdn.net/cui6864520fei000/article/details/89225544