wxAuthorized.js 5.1 KB

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