wxAuthorized.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 function loginWinxin() {
  26. console.log('1');
  27. let pages, page, path;
  28. try {
  29. pages = getCurrentPages();
  30. // 获取跳转前页面
  31. page = pages[pages.length - 1];
  32. // 获取跳转前路由地址
  33. path = page.route;
  34. } catch (e) {
  35. console.log(e);
  36. //TODO handle the exception
  37. }
  38. // 判断是否在登录页
  39. if (path != 'pages/redirect/redirect') {
  40. log.addLog('开始注册微信')
  41. weixindata().then(() => {
  42. console.log('2');
  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. // 打开微信授权页面
  61. let url =
  62. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  63. appId +
  64. '&redirect_uri=' +
  65. ul +
  66. '&response_type=code&scope=snsapi_userinfo&state=' +
  67. new Date().getTime() +
  68. '#wechat_redirect';
  69. window.location.href = url;
  70. } catch (e) {
  71. console.log(e);
  72. //TODO handle the exception
  73. }
  74. };
  75. // 微信注册
  76. export function weixindata(config) {
  77. let url;
  78. try {
  79. if (router.mode === "history") {
  80. // 在ios中时候注册为微信刚进入时候的页面
  81. if (window.entryUrl === '') {
  82. window.entryUrl = location.href.split('#')[0]
  83. }
  84. url = isAndroid() ? location.href.split('#')[0] : window.entryUrl;
  85. }
  86. if (router.mode === "hash") {
  87. url = location.href.split('#')[0];
  88. }
  89. log.addLog('注册开始', url)
  90. console.log('开始注册', url);
  91. } catch (e) {
  92. console.log('错误', e);
  93. }
  94. return new Promise((ok, error) => {
  95. try {
  96. //注册微信信息
  97. wechatConfig({
  98. url
  99. })
  100. .then(({
  101. data
  102. }) => {
  103. try {
  104. // 保存appId
  105. appId = data.appId
  106. // 微信信息配置
  107. weixinObj.config({
  108. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  109. appId: data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
  110. timestamp: data.timestamp, // 必填,生成签名的时间戳
  111. nonceStr: data.nonceStr, // 必填,生成签名的随机串
  112. signature: data.signature, // 必填,签名,见附录1
  113. jsApiList: data.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  114. });
  115. weixinObj.ready((e) => {
  116. log.addLog("注册完毕", data)
  117. console.log('注册完毕');
  118. ok(data)
  119. })
  120. } catch (e) {
  121. console.log(e)
  122. }
  123. })
  124. .catch(e => {
  125. error(e);
  126. console.log(e);
  127. });
  128. } catch (e) {
  129. console.log(e);
  130. //TODO handle the exception
  131. }
  132. })
  133. }
  134. // 判断分享调用方法
  135. export function shareLoad(config) {
  136. console.log('开始调用分享')
  137. try {
  138. weixindata().then((e) => {
  139. // 判断有无自定义数据
  140. if (config) {
  141. shareFun(config)
  142. } else {
  143. // 判断是否已经缓存了默认数据
  144. if (shareData) {
  145. shareFun()
  146. } else {
  147. // 请求获取默认数据
  148. share({}).then(({
  149. data
  150. }) => {
  151. shareData = data.data
  152. shareFun()
  153. });
  154. }
  155. }
  156. })
  157. } catch (e) {
  158. console.log('报错', e)
  159. //TODO handle the exception
  160. }
  161. }
  162. // 配置分享数据
  163. function shareFun(config) {
  164. try {
  165. console.log('再付分享内容', config);
  166. // uni.showModal({
  167. // title: '链接',
  168. // content: config.link,
  169. // showCancel: false,
  170. // cancelText: '',
  171. // confirmText: '',
  172. // success: res => {},
  173. // fail: () => {},
  174. // complete: () => {}
  175. // });
  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. // uni.showModal({
  215. // title:"提示",
  216. // content:JSON.stringify(mess)
  217. // })
  218. console.log(mess,'data')
  219. // 获取仓库数据
  220. // 分享好友
  221. weixinObj.updateAppMessageShareData(mess);
  222. // weixinObj.onMenuShareAppMessage(mess);// 即将废弃
  223. // 分享朋友圈
  224. weixinObj.updateTimelineShareData(mess)
  225. // weixinObj.onMenuShareTimeline(mess);// 即将废弃
  226. } catch (e) {
  227. console.log(e);
  228. //TODO handle the exception
  229. }
  230. }
  231. // 保存路由对象
  232. export function setRouter(route) {
  233. router = route
  234. }
  235. export default {
  236. weixinObj,
  237. shareData,
  238. appId,
  239. setRouter,
  240. shareLoad
  241. }