wxAuthorized.js 5.1 KB

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