Browse Source

微信jssdk引入并demo使用 地图导航功能

ming 4 years ago
parent
commit
ead2ca9419
5 changed files with 83 additions and 85 deletions
  1. 2 1
      pages/index/index.vue
  2. 25 8
      pages/siteManage/siteManage.vue
  3. 2 2
      util/api.js
  4. 0 74
      util/auth.js
  5. 54 0
      util/jssdk.js

+ 2 - 1
pages/index/index.vue

@@ -340,7 +340,8 @@
 				return context;
 			},
 			async init() {
-				var appId = "wx4eab2e3b5531d58b";
+				var appId = "wx4eab2e3b5531d58b";
+				// var appId = "wx8bba7ec467b61efa"; //ming测试公众号
 				var params = {};
 				var CODE = this.getUrlCode();
 				var code = CODE['code'];

+ 25 - 8
pages/siteManage/siteManage.vue

@@ -48,8 +48,14 @@
 	</view>
 
 </template>
-<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
+<!-- <script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script> -->
 <script>
+
+</script>
+<script>
+	//引入js sdk的封装
+	// import * as jwx from '../../util/jssdk.js'
+
 	export default {
 		data() {
 			return {
@@ -59,7 +65,8 @@
 
 				siteName: '',
 				siteList: [],
-				siteListRes: 0,
+				siteListRes: 0
+
 			};
 		},
 
@@ -104,10 +111,11 @@
 
 			// 打开导航
 			mapLocation(item) {
-				
-				//h5地图导航
+				
+				//h5 地图导航
 				window.location.href =  'http://apis.map.qq.com/uri/v1/marker?marker=coord:'+parseFloat(item.latitude)+','+parseFloat(item.longitude)+'' 
-
+
+				//app导航
 				// uni.openLocation({
 				// 	latitude: parseFloat(item.latitude),
 				// 	longitude: parseFloat(item.longitude),
@@ -115,11 +123,20 @@
 				// 		// console.log('success');
 				// 	}
 				// });
+
+				// 微信公众号导航
+				// jwx.configWeiXin(jweixin => {
+				// 	let shareInfo = {
+				// 		latitude: parseFloat(item.latitude), // 纬度,浮点数,范围为90 ~ -90
+				// 		longitude: parseFloat(item.longitude), // 经度,浮点数,范围为180 ~ -180。
+				// 		name: item.siteName, // 位置名
+				// 		address: item.address, // 地址详情说明
+				// 		scale: 15, // 地图缩放级别,整型值,范围从1~28。默认为最大
+				// 	};
+				// 	jweixin.openLocation(shareInfo);
+				// });
 			},
 
-
-
-
 			async setDelSite(ming = {}) {
 				const res = await this.$myRequest({
 					url: 'SiteManagement/setDelSite',

+ 2 - 2
util/api.js

@@ -1,5 +1,5 @@
-const BASE_URL = 'https://iot.usky.cn/USKYOF/USKYOF.php/Home/'
-// const BASE_URL='https://qhome.usky.cn/USKYOF/USKYOF.php/Home/'
+// const BASE_URL = 'https://iot.usky.cn/USKYOF/USKYOF.php/Home/'
+const BASE_URL='https://qhome.usky.cn/USKYOF/USKYOF.php/Home/'
 // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
 let ajaxTimes = 0;
 

+ 0 - 74
util/auth.js

@@ -1,74 +0,0 @@
-
-// start
-	getUrlCode() {
-		var url = location.search
-		this.winUrl = url
-		var theRequest = new Object()
-		if (url.indexOf("?") != -1) {
-			var str = url.substr(1)
-			var strs = str.split("&")
-			for (var i = 0; i < strs.length; i++) {
-				theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
-			}
-		}
-		return theRequest
-	},
-	 getUrlPrams(key) {
-		var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
-		var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
-		var context = "";
-		if (r != null) {
-			context = r[2];
-		}
-		return context;
-	},
-
-export const authInit = (options) => {
-
-	var appId = "wx4eab2e3b5531d58b";
-	var params = {};
-	var CODE = getUrlCode();
-	var code = CODE['code'];
-	// alert("CODE="+code);
-	if (code) {
-	 // console.log("打印参数",JSON.stringify(params));
-	 params.code=code;
-	 // alert(code);
-	 // alert(JSON.stringify(params));
-		   
-	 //使用code请求服务器接口,换取用户用户openID
-	 const res = await this.$myRequest({
-	  url: 'Com/getPageAuthorization',
-	  data:params
-	 })
-	 // alert(res.data.flag);
-	 if(res.data.flag==3000){
-	  alert("授权失败,请重新登录");
-	  uni.navigateTo({
-	   url: '/pages/login/login',
-	  });
-	 }
-	 if(res.data.flag==3002){
-	  alert("授权失败,请重新登录");
-	  uni.navigateTo({
-	   url: '/pages/login/login',
-	  });
-	 }
-	 return "ok";
-	}
-	// alert("授权开始");
-	var cs = {};
-	var curl=window.location.href;
-	cs.curl=curl;
-	// alert("当前页面路径"+curl);
-	const res1 = await this.$myRequest({
-	 url: 'Com/getAuthorizationUrl',
-	 data:cs
-	})
-	// alert(res1.data);
-	var turl=res1.data.turl;
-	// alert(turl)
-	window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4eab2e3b5531d58b&redirect_uri='+turl+'&response_type=code&scope=snsapi_base&state=abc123#wechat_redirect';
-		
-
-}

+ 54 - 0
util/jssdk.js

@@ -0,0 +1,54 @@
+// jwx.js
+//#ifdef H5
+const jweixin = require('jweixin-module')
+//#endif
+export function configWeiXin(callback) {
+	const url = window.location.href.split('#')[0];
+	uni.request({
+		url: 'https://s.yxin.tech/sign.php', //仅为示例,并非真实接口地址。
+		header: {
+			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
+		},
+		data:{"url":url},
+		method: 'GET',
+		success: (res) => {
+			console.log('请求的签名的参数')
+			console.log(res)
+			
+			let apiList = [ // 可能需要用到的能力 需要啥就写啥。多写也没有坏处
+				// 'scanQRCode',
+				'openLocation'
+			];
+			let info = {
+				debug: true, // 调试,发布的时候改为false
+				appId: res.data.appid,
+				nonceStr: res.data.nonceStr,
+				timestamp: parseInt(res.data.timestamp),
+				signature: res.data.sha_str,
+				jsApiList: apiList
+			};
+						
+			jweixin.config(info);
+			jweixin.error(err => {
+				console.log('config fail:', err);
+				return
+			});
+			
+			jweixin.ready(res => {
+				if (callback) callback(jweixin);
+			});
+			
+			
+			//end
+			
+			
+		}
+	});
+	
+	
+	
+	
+	
+	
+	
+}