wxAuthorized.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // uni.showModal({
  72. // title:"微信注册config",
  73. // content:JSON.stringify(config)
  74. // })
  75. // console.log(config);
  76. //注册微信信息
  77. let weixinObj = require('jweixin-module');
  78. // 保存微信对象到全局仓库
  79. store.commit('setWeiChatObj', weixinObj)
  80. console.log(window.location.href);
  81. console.log('当前注册url地址',window.location.href);
  82. wechatConfig({
  83. // url: 'pages/applic/location',
  84. url: store.state.baseURL+ store.state.urlFile+'/',
  85. })
  86. .then(({
  87. data
  88. }) => {
  89. // uni.showModal({
  90. // // url: 'http://yyred.igxys.com/index/',
  91. // title:'地址',
  92. // content: window.location.href
  93. // })
  94. // 保存appId
  95. store.commit('setWeiChatInfo', data)
  96. appId = data.appId;
  97. // 微信信息配置
  98. weixinObj.config({
  99. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  100. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  101. timestamp: data.timestamp, // 必填,生成签名的时间戳
  102. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  103. signature: data.signature, // 必填,签名,见附录1
  104. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  105. });
  106. weixinObj.ready((e) => {
  107. console.log('分享数据',shareData);
  108. if (shareData == '') {
  109. share({}).then(({
  110. data
  111. }) => {
  112. shareData = data.data
  113. shareLoad(config)
  114. });
  115. } else {
  116. shareLoad(config)
  117. }
  118. })
  119. })
  120. .catch(e => {
  121. console.log(e);
  122. });
  123. }
  124. // 加载分享数据
  125. function shareLoad(config) {
  126. let mess;
  127. if (config) {
  128. mess = {
  129. link: config.link, // 分享链接
  130. imgUrl: config.imgUrl,
  131. desc: config.desc,
  132. title: config.title,
  133. success: config.success,
  134. fail: config.fail||function (e) {
  135. console.log(e);
  136. }
  137. }
  138. } else {
  139. // console.log(window.location.href, 55)
  140. // let url = window.location.href + '?spread=' + store.state.userInfo.uid;
  141. let url = window.location.href;
  142. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  143. mess = {
  144. title: shareData.title,
  145. link: url, // 分享链接
  146. imgUrl: shareData.img, // 分享图标
  147. desc: shareData.synopsis,
  148. success: function() {
  149. },
  150. fail:function (e) {
  151. console.log(e);
  152. }
  153. }
  154. }
  155. // 获取仓库数据
  156. // 分享好友
  157. // weixinObj.updateAppMessageShareData(mess);
  158. weixinObj.onMenuShareAppMessage(mess);// 即将废弃
  159. // 分享朋友圈
  160. // weixinObj.updateTimelineShareData(mess)
  161. weixinObj.onMenuShareTimeline(mess);// 即将废弃
  162. }
  163. // 隐藏功能按钮
  164. export function weixinhideMenu(config) {
  165. console.log(config)
  166. weixinObj.ready((e) => {
  167. weixinObj.hideMenuItems({
  168. menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  169. });
  170. })
  171. }
  172. // export function getLoca(config) {
  173. // console.log(config)
  174. // weixinObj.ready((e) => {
  175. // weixinObj.getLocation({
  176. // type: 'wgs84',
  177. // success: function (res) {
  178. // config.longitude = res.longitude;
  179. // config.latitude = res.latitude;
  180. // uni.showModal({
  181. // title: '提示',
  182. // content: res.longitude
  183. // })
  184. // console.log('当前位置的经度:' + res.longitude);
  185. // console.log('当前位置的纬度:' + res.latitude);
  186. // }
  187. // });
  188. // })
  189. // }
  190. export default {
  191. weixinObj,
  192. shareData,
  193. appId
  194. }