Logon.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <script>
  2. import wx from 'weixin-js-sdk';
  3. import {
  4. SY
  5. } from '@/api/index.js'
  6. import {
  7. HTTP
  8. } from '@/api/http.js'
  9. import {
  10. setUserInfo, //存储用户信息
  11. setToken //储存用户token
  12. } from "@/utils/auth.js";
  13. import {
  14. jieqiFn,
  15. BASE_URL,
  16. CDX,
  17. appId
  18. } from '@/config'
  19. export default {
  20. onLaunch: function() {
  21. console.log('App Launch')
  22. },
  23. onShow: function() {
  24. console.log('App Show')
  25. },
  26. onHide: function() {
  27. console.log('App Hide')
  28. },
  29. data() {
  30. return {
  31. page: "",
  32. code: "",
  33. data: undefined,
  34. url: undefined
  35. };
  36. },
  37. methods: {
  38. // 获取链接上带的参数
  39. getQueryString(name) {
  40. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  41. var r = window.location.search.substr(1).match(reg);
  42. console.log(window.location.search, '我是链接里携带的参数');
  43. if (r != null) {
  44. return unescape(r[2]);
  45. }
  46. return null;
  47. },
  48. infff(data) {
  49. console.log('url', this.url, data)
  50. /* eslint-disable */
  51. let lat = this;
  52. wx.ready(() => { //需在用户可能点击分享按钮前就先调用
  53. wx.updateTimelineShareData({
  54. title: '《阳光少年·二十四节气养生文化夏令营》活动火热进行中', // 分享标题
  55. link: lat.url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  56. imgUrl: data.img_url, // 分享图标
  57. success: () => {
  58. // 设置成功
  59. console.log('1 设置成功')
  60. wx.updateAppMessageShareData({
  61. title: '《阳光少年·二十四节气养生文化夏令营》活动火热进行中', // 分享标题
  62. // 您的好友“张三”和阳光少年“王五”送您一个花神红包,请点击领取
  63. desc: '您的好友“' + data.user_name +
  64. '”和阳光少年“' + data.receive_user_name +
  65. '”送您一个花神红包,请点击领取', // 分享描述
  66. link: lat.url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  67. imgUrl: data.img_url, // 分享图标
  68. success: function() {
  69. uni.setStorageSync("fenXiang", "true")
  70. // 设置成功
  71. console.log('2 设置成功')
  72. let state = lat.getQueryString("state") || "";
  73. console.log('state', state)
  74. let strings = state.split("-");
  75. console.log('strings', strings)
  76. if (strings.length > 2) {
  77. let string = strings[0];
  78. let string1 = strings[1];
  79. let share_user_Id = undefined;
  80. let bFxren = undefined;
  81. if (string && string.indexOf('sid_') > -1) {
  82. share_user_Id = string.split('sid_')[1];
  83. }
  84. if (string1 && string1.indexOf('bFxen_') > -1) {
  85. bFxren = string1.split('_')[1];
  86. }
  87. // this.$router.push({path:'/redict',share_user_Id:share_user_Id,beifenxiang_id:bFxren})
  88. uni.navigateTo({
  89. url: '/pages/luoDi/luoDi?share_user_Id=' +
  90. share_user_Id +
  91. '&beifenxiang_id=' + bFxren
  92. })
  93. }
  94. },
  95. fail: (e) => {
  96. console.log('2', e)
  97. }
  98. })
  99. },
  100. fail: (e) => {
  101. console.log('1', e)
  102. }
  103. })
  104. })
  105. wx.error(function(res) {
  106. // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
  107. console.log(res)
  108. })
  109. },
  110. // 解析参数
  111. getQueryParams(url) {
  112. const params = new URLSearchParams(new URL(url).search);
  113. const obj = {};
  114. for (const [key, value] of params.entries()) {
  115. obj[key] = value;
  116. }
  117. return obj;
  118. }
  119. },
  120. created() {
  121. if(CDX == ""){//没有重定向就直接去首页
  122. uni.reLaunch({
  123. url:"/pages/index/index"
  124. })
  125. }
  126. // console.log("记录了吗啊")
  127. if (CDX != "") { //有重定向
  128. uni.removeStorageSync("fenXiang")
  129. this.code = this.getQueryString("code") || ""; //授权登录信息
  130. let lat = this;
  131. if (window.location.href.indexOf('share_user_id') > -1) {
  132. let wx_url =
  133. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${CDX}?page=null&response_type=code&scope=snsapi_userinfo&state=sid_`+
  134. this.getQueryString("share_user_id") + "-bFxen_" + this.getQueryString("bfxren") +
  135. "-#wechat_redirect";
  136. window.location.href = wx_url; // 跳转路径 移动端
  137. }
  138. if (this.code) {
  139. let state = lat.getQueryString("state") || "";
  140. console.log('state', state)
  141. let strings = state.split("-");
  142. console.log('strings', strings)
  143. let share_user_Id = undefined;
  144. let bFxren = undefined;
  145. if (strings.length > 2) {
  146. let string = strings[0];
  147. let string1 = strings[1];
  148. // let share_user_Id = undefined;
  149. // let bFxren = undefined;
  150. if (string && string.indexOf('sid_') > -1) {
  151. share_user_Id = string.split('sid_')[1];
  152. }
  153. if (string1 && string1.indexOf('bFxen_') > -1) {
  154. bFxren = string1.split('_')[1];
  155. }
  156. // this.$router.push({path:'/redict',share_user_Id:share_user_Id,beifenxiang_id:bFxren})
  157. uni.navigateTo({
  158. url: '/pages/luoDi/luoDi?share_user_Id=' +
  159. share_user_Id +
  160. '&beifenxiang_id=' + bFxren
  161. })
  162. }
  163. // 登录逻辑
  164. SY.login({
  165. code: lat.code,
  166. share_user_id: share_user_Id,
  167. relation_id: bFxren
  168. }).then((res) => {
  169. console.log('登录信息', res, lat.code);
  170. if (res.code == 200) {
  171. setToken(res.data.app_token)
  172. uni.setStorageSync('open_id', res.data.open_id);
  173. console.log('用户id', res.data.user_id)
  174. lat.url = window.location.href.split("#")[0] + '?page=shareUrl&&share_user_id=' + res
  175. .data
  176. .user_id + "&&bfxren=";
  177. let chae = undefined;
  178. SY.shareData().then(res => {
  179. chae = res.data;
  180. console.log('SY.shareData()_1', res)
  181. lat.url = lat.url + res.data.user_flora_id;
  182. // console.log('SY.shareData()', lat.data)
  183. console.log('lat.url', lat.url)
  184. HTTP.SY.getWxConfig({
  185. url: window.location.href.split("#")[0]
  186. })
  187. .then((res) => {
  188. var result = res.data
  189. // eslint-disable-next-line
  190. if (!wx) {
  191. console.log('请升级当前微信版本')
  192. }
  193. wx.checkJsApi({
  194. jsApiList: ['updateTimelineShareData',
  195. 'updateAppMessageShareData',
  196. 'onMenuShareTimeline',
  197. 'onMenuShareAppMessage'
  198. ], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  199. success: function(res) {
  200. console.log('res', res)
  201. }
  202. });
  203. wx.config({
  204. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
  205. appId: result.appId, // 必填,公众号的唯一标识
  206. timestamp: result.timestamp, // 必填,生成签名的时间戳
  207. nonceStr: result.nonceStr, // 必填,生成签名的随机串
  208. signature: result.signature, // 必填,签名
  209. jsApiList: ['updateTimelineShareData',
  210. 'updateAppMessageShareData',
  211. 'onMenuShareTimeline',
  212. 'onMenuShareAppMessage'
  213. ] // 必填,需要使用的JS接口列表
  214. })
  215. setTimeout((res) => {
  216. console.log("需要使用的JS接口列表:ok", res)
  217. lat.infff(chae)
  218. }, 500)
  219. })
  220. })
  221. }
  222. })
  223. } else {
  224. let wx_url =
  225. `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${CDX}?page=null&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
  226. window.location.href = wx_url; // 跳转路径 移动端
  227. }
  228. }
  229. }
  230. }
  231. </script>
  232. <style lang='scss'>
  233. page {
  234. background-color: #fff;
  235. }
  236. /*每个页面公共css */
  237. </style>