wechat.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var jweixin = require('./jweixin.js');
  2. var serverUrl = "https://mdyg.sdmingde.cn/api/h5/";
  3. export default {
  4. //判断是否在微信中
  5. isWechat: function() {
  6. var ua = window.navigator.userAgent.toLowerCase();
  7. if (ua.match(/micromessenger/i) == 'micromessenger') {
  8. // console.log('是微信客户端')
  9. return true;
  10. } else {
  11. // console.log('不是微信客户端')
  12. return false;
  13. }
  14. },
  15. initJssdk:function(callback){
  16. var surl = encodeURIComponent(window.location.href.split('#')[0]); // 当前地址
  17. uni.request({
  18. url: serverUrl + 'Oauth/jssdk',
  19. data: {url:surl},
  20. method: 'POST',
  21. header: {
  22. 'content-type': 'application/x-www-form-urlencoded'
  23. },
  24. success: function (res) {
  25. console.log(res)
  26. if(res.data.code == 0) {
  27. jweixin.config({
  28. debug: res.data.data.debug,
  29. appId: res.data.data.appid,
  30. timestamp: res.data.data.timestamp,
  31. nonceStr: res.data.data.noncestr,
  32. signature: res.data.data.signature,
  33. jsApiList: res.data.data.jsapilist,
  34. });
  35. //配置完成后,再执行分享等功能
  36. if (callback) {
  37. callback(res.data);
  38. }
  39. }
  40. },
  41. fail: function (err) {
  42. console.log(err);
  43. }
  44. })
  45. },
  46. //在需要定位页面调用
  47. location: function(callback) {
  48. if (!this.isWechat()) {
  49. console.log('不是微信客户端')
  50. return;
  51. }
  52. this.initJssdk(function(res) {
  53. jweixin.ready(function() {
  54. console.info('定位ready')
  55. jweixin.getLocation({
  56. type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
  57. success: function (res) {
  58. console.log(res);
  59. callback(res)
  60. },
  61. fail:function(res){
  62. console.log(res)
  63. },
  64. });
  65. });
  66. });
  67. }
  68. }