UNI-APP development micro channel public number (H5) JSSDK using

Recent developments in the use, needs to run on public number. The need to achieve to obtain latitude and longitude positioning function.

REFERENCE module micro-channel mode using js-sdk

Reference Methods: https: //ask.dcloud.net.cn/article/35380

github:https://github.com/zhetengbiji/jweixin-module

  1. NPM installation (NPM will not use do not use this way)
     

    npm install jweixin-module --save  

     

  2. Download use

Download: https://unpkg.com/[email protected]/out/index.js

 

use

var jweixin = require('jweixin-module')  
jweixin.ready(function(){  
    // TODO  
});  

Two places, the use of methods like the above put it simple. But really if used together, on the tragedy. Especially rookies.

Forum DCloud official website, there are examples of sharing http://ask.dcloud.net.cn/article/36007.

 

I am here to be an example of positioning interfaces.

Micro-letter first look at the document. Clear general processes. https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

 

1、commonDirectory, create a file, the name iswechat.js。内容如下:

  . 1  // Import Request from './request'; // network package author himself request class, may be used directly uni.request   
  2  Import {
   . 3      POST
   . 4 } from './wxRequest' ;
   . 5  var jweixin the require = ( ' Module1-jweixin ' );
   . 6  
  . 7 Export default {
   . 8      // determines whether the micro-channel   
  . 9      isWechat: function () {
 10          var UA = window.navigator.userAgent.toLowerCase ();
 . 11          iF (ua.match (/ micromessenger / I) == 'micromessenger' ) {
 12 is              // the console.log ( 'micro channel clients')
13 is              return  to true ;
 14          } the else {
 15              // the console.log ( 'not WeChat client') 
16              return  to false ;
 . 17          }
 18 is      },
 . 19      // initialize sdk configuration   
20 is      initJssdkShare: function (the callback, URL) {
 21 is          // signing server, used interchangeably uni.request. See document signature algorithm   
22 is          POST (
 23 is              'https://fbyc.microchainsoft.cn/index/wechat/getSignPackage' ,
 24              {
 25                  URL: URL
 26 is             },
 27             function(res) {
 28                 // console.log(res)
 29                 if (res.data) {
 30                     jweixin.config({
 31                         debug: true,
 32                         appId: res.data.appId,
 33                         timestamp: res.data.timestamp,
 34                         nonceStr: res.data.nonceStr,
 35                         signature: res.data.signature,
 36                         jsApiList: [
 37                             'checkJsApi',
 38                             'onMenuShareTimeline' ,
 39                              'onMenuShareAppMessage' ,
 40                              'getLocation'
 41 is                          ]
 42 is                      });
 43 is                      // After the configuration, and then perform other functions sharing   
44 is                      IF (the callback) {
 45                          the callback (res.data);
 46 is                      }
 47                  }
 48          });
 49      },
 50      initJssdk: function (the callback) {
 51 is         post('https://fbyc.microchainsoft.cn/index/wechat/getSignPackage',{},
 52             function (res) {
 53                 if(res.data){
 54                     jweixin.config({
 55                         debug: true,
 56                         appId: res.data.appId,
 57                         timestamp: res.data.timestamp,
 58                         nonceStr: res.data.nonceStr,
 59                         signature: res.data.signature,
 60                         jsApiList: [
 61                             'checkJsApi',
62 is                              'getLocation'
 63 is                          ]
 64                      });
 65                      // After the configuration, and then perform other functions sharing   
66                      IF (the callback) {
 67                          the callback (res.data);
 68                      }
 69                  }
 70              })
 71 is      },
 72      // in need to customize the page above the call to share   
73      report this content share: function (the Data, url) {
 74          url = url? url: window.location.href;
 75          IF (! the this.isWechat ()) {
 76              return ;
 77          }
 78          // every reinitialization configuration can share   
79          the this .initJssdkShare ( function (signData) {
 80              jweixin.ready ( function () {
 81                  var shareData = {
 82                      title: Data && data.title? data.title: signData.site_name,
 83                      desc: Data && data.desc? data.desc: signData.site_description,
 84                      Link: URL,
 85                      for imgUrl: Data data.img &&?data.img: signData.site_logo,
 86                      Success: function (RES) {
 87                          // user clicks the share callback, where statistics can, for example, gold or the like to send share   
88                          // POST ( '/ API / Member / Share '); 
89                      },
 90                      Cancel: function (RES) {}
 91 is                  };
 92                  // share the interface to a friend   
93                  jweixin.onMenuShareAppMessage (shareData);
 94                  // Share circle of friends interfaces   
95                  jweixin.onMenuShareTimeline (shareData);
 96              });
 97         }, URL);
 98      },
 99      // in the need to locate the page call 
100      LOCATION: function (the callback) {
 101          IF (! The this .isWechat ()) {
 102              the console.log ( 'not WeChat client' )
 103              return ;
 104          }
 105          console.info ( 'targeting' )
 106          the this .initJssdk ( function (RES) {
 107              jweixin.ready ( function () {
 108                  console.info ( 'positioned READY' )
 109                 jweixin.getLocation ({
 110                      type: 'gcj02', // default wgs84 gps coordinates, if the coordinates to be returned directly to Mars openLocation used, can be passed 'gcj02' 
111                      Success: function (RES) {
 112                          // Console .log (RES); 
113                          the callback (RES)
 114                      },
 115                      Fail: function (RES) {
 1 16                          the console.log (RES)
 117                      },
 1 18                      // Complete: function (RES) { 
119                      //      the console.log (RES ) 
120                     // }
121                 });
122             });
123         });
124     }
125 }
View Code

2, main.js load the file

1 // #ifdef H5  
2 import wechat from './common/util/wechat'
3 if(wechat.isWechat()){
4     Vue.prototype.$wechat =wechat;
5 }
6 // #endif  

3, used in the page

. 1              // #ifdef the H5 
2              // Obtain latitude and longitude 
. 3              IF ( the this . && $ WeChat the this . Wechat.isWechat $ ()) {
 . 4                   the this . Wechat.location $ ( function (RES) {
 . 5                       the console.log (RES)
 . 6                      // the let latitude = res.latitude; // latitude, floating-point, in the range of -90 ~ 90 
. 7                      @ the let longitude = res.longitude; // longitude, floating-point, in the range of 180 to -180. 
8                      // TODO 
. 9                      the let Latitude = 31.14979 ;
 10                      the let longitude = 121.12426 ; 
 . 11                     
12                      // latitude and longitude, analysis area, the user is prompted 
13 is                   });
 14              }
 15              // #endif

 

Back-end server, you can refer to: https://my.oschina.net/superkangning/blog/368043

Guess you like

Origin www.cnblogs.com/shen55/p/11077677.html