wxAuthorized.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import {
  2. wechatConfig,
  3. share
  4. } from '@/api/wx';
  5. import store from '../store';
  6. // 保存wx对象
  7. let weixinObj = require('jweixin-module');
  8. // 保存分享数据
  9. let shareData = '';
  10. // 保存注册返回appId数据
  11. let appId = '';
  12. //微信登录
  13. /**
  14. * @param {string} 当前页面地址信息
  15. */
  16. export function loginWinxin() {
  17. let pages, page, path;
  18. try {
  19. pages = getCurrentPages();
  20. // 获取跳转前页面
  21. page = pages[pages.length - 1];
  22. // 获取跳转前路由地址
  23. path = page.route;
  24. } catch (e) {
  25. console.log(e);
  26. }
  27. // 判断是否在登录页
  28. if (path != 'pages/redirect/redirect') {
  29. setTimeout((e) => {
  30. weixinObj.ready(() => {
  31. console.log('weixinObj.ready');
  32. // 调用
  33. try {
  34. console.log(shareData);
  35. console.log(path);
  36. weixinSq(shareData, path);
  37. } catch (e) {
  38. console.log(e);
  39. //TODO handle the exception
  40. }
  41. });
  42. weixinObj.error((e) => {
  43. console.log(e);
  44. })
  45. }, 10)
  46. }
  47. };
  48. // 微信授权登录
  49. function weixinSq(data, path) {
  50. // 微信授权后跳转页面
  51. try {
  52. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + '/#/pages/redirect/redirect');
  53. console.log(ul, "ul")
  54. // 打开微信授权页面
  55. let url =
  56. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  57. appId +
  58. '&redirect_uri=' +
  59. ul +
  60. '&response_type=code&scope=snsapi_userinfo&state=' +
  61. new Date().getTime() +
  62. '#wechat_redirect';
  63. console.log(url, "url")
  64. window.location.href = url;
  65. } catch (e) {
  66. console.log(e);
  67. }
  68. };
  69. // 微信注册
  70. export function weixindata(config) {
  71. // console.log(config);
  72. //注册微信信息
  73. let weixinObj = require('jweixin-module');
  74. // 保存微信对象到全局仓库
  75. store.commit('setWeiChatObj', weixinObj)
  76. console.log(window.location.href);
  77. console.log('当前注册url地址',window.location.href);
  78. wechatConfig({
  79. // url: 'pages/applic/location',
  80. url: store.state.baseURL+ store.state.urlFile+'/',
  81. })
  82. .then(({
  83. data
  84. }) => {
  85. // uni.showModal({
  86. // // url: 'http://yyred.igxys.com/index/',
  87. // title:'地址',
  88. // content: window.location.href
  89. // })
  90. // 保存appId
  91. store.commit('setWeiChatInfo', data)
  92. appId = data.appId;
  93. // 微信信息配置
  94. weixinObj.config({
  95. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  96. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  97. timestamp: data.timestamp, // 必填,生成签名的时间戳
  98. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  99. signature: data.signature, // 必填,签名,见附录1
  100. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  101. });
  102. weixinObj.ready((e) => {
  103. console.log('分享数据',shareData);
  104. if (shareData == '') {
  105. share({}).then(({
  106. data
  107. }) => {
  108. shareData = data.data
  109. shareLoad(config)
  110. });
  111. } else {
  112. shareLoad(config)
  113. }
  114. })
  115. })
  116. .catch(e => {
  117. console.log(e);
  118. });
  119. }
  120. // 加载分享数据
  121. function shareLoad(config) {
  122. let mess;
  123. if (config) {
  124. mess = {
  125. link: config.link, // 分享链接
  126. imgUrl: config.imgUrl,
  127. desc: config.desc,
  128. title: config.title,
  129. success: config.success,
  130. fail: config.fail||function (e) {
  131. console.log(e);
  132. }
  133. }
  134. } else {
  135. // console.log(window.location.href, 55)
  136. // let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  137. let url = window.location.href;
  138. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  139. mess = {
  140. title: shareData.title,
  141. link: url, // 分享链接
  142. imgUrl: shareData.img, // 分享图标
  143. desc: shareData.synopsis,
  144. success: function() {
  145. },
  146. fail:function (e) {
  147. console.log(e);
  148. }
  149. }
  150. }
  151. // 获取仓库数据
  152. // 分享好友
  153. // weixinObj.updateAppMessageShareData(mess);
  154. weixinObj.onMenuShareAppMessage(mess);// 即将废弃
  155. // 分享朋友圈
  156. // weixinObj.updateTimelineShareData(mess)
  157. weixinObj.onMenuShareTimeline(mess);// 即将废弃
  158. }
  159. // 隐藏功能按钮
  160. export function weixinhideMenu(config) {
  161. console.log(config)
  162. weixinObj.ready((e) => {
  163. weixinObj.hideMenuItems({
  164. menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  165. });
  166. })
  167. }
  168. // export function getLoca(config) {
  169. // console.log(config)
  170. // weixinObj.ready((e) => {
  171. // weixinObj.getLocation({
  172. // type: 'wgs84',
  173. // success: function (res) {
  174. // config.longitude = res.longitude;
  175. // config.latitude = res.latitude;
  176. // uni.showModal({
  177. // title: '提示',
  178. // content: res.longitude
  179. // })
  180. // console.log('当前位置的经度:' + res.longitude);
  181. // console.log('当前位置的纬度:' + res.latitude);
  182. // }
  183. // });
  184. // })
  185. // }
  186. export default {
  187. weixinObj,
  188. shareData,
  189. appId
  190. }