wxAuthorized.js 6.0 KB

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