wxAuthorized.js 5.2 KB

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