wxAuthorized.js 6.0 KB

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