wxAuthorized.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // uni.showModal({
  110. // title: 'shareData',
  111. // content: JSON.justifySelf(shareData)
  112. // })
  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. // uni.showModal({
  142. // title: '确认',
  143. // content: '确认分享'
  144. // })
  145. let url = window.location.href;
  146. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  147. mess = {
  148. title: shareData.title,
  149. link: url, // 分享链接
  150. imgUrl: shareData.img, // 分享图标
  151. desc: shareData.synopsis,
  152. success: function() {
  153. },
  154. fail:function (e) {
  155. console.log(e);
  156. }
  157. }
  158. }
  159. // 获取仓库数据
  160. // 分享好友
  161. weixinObj.updateAppMessageShareData(mess);
  162. // weixinObj.onMenuShareAppMessage(mess);// 即将废弃
  163. // 分享朋友圈
  164. weixinObj.updateTimelineShareData(mess)
  165. // weixinObj.onMenuShareTimeline(mess);// 即将废弃
  166. }
  167. // 隐藏功能按钮
  168. export function weixinhideMenu(config) {
  169. console.log(config)
  170. weixinObj.ready((e) => {
  171. weixinObj.hideMenuItems({
  172. menuList: config, // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
  173. });
  174. })
  175. }
  176. // export function getLoca(config) {
  177. // console.log(config)
  178. // weixinObj.ready((e) => {
  179. // weixinObj.getLocation({
  180. // type: 'wgs84',
  181. // success: function (res) {
  182. // config.longitude = res.longitude;
  183. // config.latitude = res.latitude;
  184. // uni.showModal({
  185. // title: '提示',
  186. // content: res.longitude
  187. // })
  188. // console.log('当前位置的经度:' + res.longitude);
  189. // console.log('当前位置的纬度:' + res.latitude);
  190. // }
  191. // });
  192. // })
  193. // }
  194. export default {
  195. weixinObj,
  196. shareData,
  197. appId
  198. }