第一步下载sdk
2.前台视图需要的js代码
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script><script charset="utf-8" src="http://map.qq.com/api/js?v=2.exp"></script><script>// 添加收货地址定位function setWechatJSSDK(res){wx.config({debug: false,appId: res.appId,timestamp: res.timestamp,nonceStr: res.nonceStr,signature: res.signature,jsApiList: ['getLocation','scanQRCode']});wx.ready(function () {// 点击获取地理位置$('.iconfont').click(function(){wx.getLocation({type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'success: function (res) {var location = res.latitude+','+res.longitude;var url = 'http://apis.map.qq.com/ws/geocoder/v1/?'var data={location: location,key: 'SQGBZ-QV7RG-EPTQC-IZTVB-ID44J-YMFTA',output: "jsonp"};$.ajax({type:'get',dataType:'jsonp',data:data,url:url,success:function(json){alert(json.result.address);//这里是替换input的内容$('#address').html(json.result.address);// 补充弹框消失效果},error : function(err){alert("服务端错误,请刷新浏览器后重试")}});}});})$('.getqrcode').click(function(){wx.scanQRCode({needResult: 1,desc: 'scanQRCode desc',success: function (res) {var id = res.resultStr.split('=')[1];alert(JSON.stringify(res));}});})});}function loadWechatJSSDK() {$.get("{:U('addon/WeiuidDeit/Mobile/getlocation')}",{url:location.href}, function (res) {setWechatJSSDK(res);});}setTimeout(loadWechatJSSDK, 500);
3.控制器需要的代码
// 获取地址public function getlocation(){$wechatInfo = D('Mp')->find(3);$options = array('token' => $wechatInfo['valid_token'],'encodingaeskey' => $wechatInfo['encodingaeskey'],'appid' => $wechatInfo['appid'],'appsecret' => $wechatInfo['appsecret']);$wechatObj = new Wechat($options);$data = $wechatObj->getJsSign(I('get.url'));$this->ajaxReturn($data);}
