wxAuthorized.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {
  2. wechatConfig,
  3. share
  4. } from '@/api/wx';
  5. // 加载日志
  6. import * as log from './log.js'
  7. import {
  8. isAndroid
  9. } from './platform.js'
  10. import store from '../store';
  11. // 保存wx对象
  12. import weixinObj from "@/plugin/jweixin-module/index.js";
  13. console.log(weixinObj,'初始化对象');
  14. // 保存分享数据
  15. let shareData = '';
  16. // 保存注册返回appId数据
  17. let appId = '';
  18. //保存路由对象
  19. let router = '';
  20. //微信登录
  21. /**
  22. * @param {string} 当前页面地址信息
  23. */
  24. export async function loginWinxin() {
  25. let pages, page, path;
  26. if (!router) {
  27. router = await setRouter();
  28. }
  29. try {
  30. pages = getCurrentPages();
  31. // 获取跳转前页面
  32. page = pages[pages.length - 1];
  33. // 获取跳转前路由地址
  34. path = page.route;
  35. } catch (e) {
  36. console.log(e,'获取页面');
  37. //TODO handle the exception
  38. }
  39. // 判断是否在登录页
  40. if (path != 'pages/redirect/redirect') {
  41. log.addLog('开始注册微信')
  42. weixindata().then(() => {
  43. // 调用
  44. try {
  45. weixinSq();
  46. } catch (e) {
  47. console.log(e,'错误3');
  48. //TODO handle the exception
  49. }
  50. }).catch((re)=>{
  51. clog
  52. });
  53. }
  54. };
  55. // 微信授权登录
  56. function weixinSq() {
  57. // 微信授权后跳转页面
  58. try {
  59. // 判断是否真实路由模式
  60. const type = router.mode === "history" ? '' : '/#'
  61. let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + type + '/pages/redirect/redirect');
  62. // 打开微信授权页面
  63. let url =
  64. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  65. appId +
  66. '&redirect_uri=' +
  67. ul +
  68. '&response_type=code&scope=snsapi_userinfo&state=' +
  69. new Date().getTime() +
  70. '#wechat_redirect';
  71. window.location.href = url;
  72. } catch (e) {
  73. console.log(e,'错误');
  74. //TODO handle the exception
  75. }
  76. };
  77. // 微信注册
  78. export async function weixindata(config) {
  79. let url;
  80. try {
  81. // 判断是否存在router
  82. if (!router) {
  83. router = await setRouter();
  84. }
  85. if (router.mode === "history") {
  86. // 在ios中时候注册为微信刚进入时候的页面
  87. if (!window.entryUrl) {
  88. window.entryUrl = location.href.split('#')[0]
  89. }
  90. url = isAndroid() ? location.href.split('#')[0] : window.entryUrl;
  91. }
  92. if (router.mode === "hash") {
  93. url = location.href.split('#')[0];
  94. }
  95. log.addLog('注册开始', url)
  96. console.log('开始注册', url);
  97. } catch (e) {
  98. console.log('错误', e);
  99. }
  100. return new Promise((ok, error) => {
  101. try {
  102. //注册微信信息
  103. wechatConfig({
  104. url
  105. })
  106. .then(({
  107. data
  108. }) => {
  109. try {
  110. // 保存appId
  111. appId = data.appId
  112. // 微信信息配置
  113. weixinObj.config({
  114. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  115. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  116. timestamp: data.timestamp, // 必填,生成签名的时间戳
  117. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  118. signature: data.signature, // 必填,签名,见附录1
  119. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  120. });
  121. weixinObj.ready((e) => {
  122. log.addLog("注册完毕", data)
  123. console.log('注册完毕');
  124. store.commit('setWeiChatObj', weixinObj)
  125. ok(weixinObj)
  126. })
  127. } catch (e) {
  128. console.log(e,'错误')
  129. }
  130. })
  131. .catch(e => {
  132. error(e);
  133. console.log(e);
  134. });
  135. } catch (e) {
  136. console.log(e);
  137. //TODO handle the exception
  138. }
  139. })
  140. }
  141. // 判断分享调用方法
  142. export function shareLoad(config) {
  143. console.log('开始调用分享')
  144. try {
  145. weixindata().then((e) => {
  146. // 判断有无自定义数据
  147. if (config) {
  148. shareFun(config)
  149. } else {
  150. // 判断是否已经缓存了默认数据
  151. if (shareData) {
  152. shareFun()
  153. } else {
  154. // 请求获取默认数据
  155. share({}).then(({
  156. data
  157. }) => {
  158. shareData = data.data
  159. shareFun()
  160. });
  161. }
  162. }
  163. })
  164. } catch (e) {
  165. console.log('报错', e)
  166. //TODO handle the exception
  167. }
  168. }
  169. // 配置分享数据
  170. function shareFun(config) {
  171. try {
  172. console.log('再付分享内容', config);
  173. let mess;
  174. if (config) {
  175. mess = {
  176. link: config.link, // 分享链接
  177. imgUrl: config.imgUrl,
  178. desc: config.desc,
  179. title: config.title,
  180. success: config.success || function(e) {
  181. console.log(e);
  182. },
  183. fail: config.fail || function(e) {
  184. console.log(e);
  185. }
  186. }
  187. } else {
  188. const userInfo = uni.getStorageSync('userInfo')
  189. console.log(userInfo);
  190. const url = window.location.href + '?spread=' + userInfo.uid;
  191. url = url.replace(/[\?,&]{0,1}from=singlemessage/g, '');
  192. mess = {
  193. title: shareData.title,
  194. link: url, // 分享链接
  195. imgUrl: shareData.img, // 分享图标
  196. desc: shareData.synopsis,
  197. success: function() {
  198. // uni.showModal({
  199. // title: '分享',
  200. // content: '分享成功',
  201. // showCancel: false,
  202. // success: res => {
  203. // console.log('分享成功回调接口');
  204. // },
  205. // fail: () => {},
  206. // complete: () => {}
  207. // });
  208. }
  209. }
  210. }
  211. // 获取仓库数据
  212. // 分享好友
  213. weixinObj.updateAppMessageShareData(mess);
  214. // 分享朋友圈
  215. weixinObj.updateTimelineShareData(mess)
  216. } catch (e) {
  217. console.log(e);
  218. //TODO handle the exception
  219. }
  220. }
  221. // 保存路由对象
  222. export function setRouter(route) {
  223. return new Promise((ok, err) => {
  224. router = getApp().$router;
  225. console.log(router, '开始数据');
  226. if (!router) {
  227. const set = setInterval(() => {
  228. router = getApp().$router;
  229. console.log(router, '返回数据');
  230. if (router) {
  231. console.log(router, '结束');
  232. clearInterval(set)
  233. ok(router)
  234. }
  235. }, 100);
  236. } else {
  237. console.log(router, '成功');
  238. ok(router)
  239. }
  240. })
  241. }
  242. export default {
  243. weixinObj,
  244. shareData,
  245. appId,
  246. setRouter,
  247. shareLoad
  248. }