wxAuthorized.js 5.6 KB

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