wxAuthorized.js 6.2 KB

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