wxAuth.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <div>
  3. <van-loading style="position: absolute;top: 10%;left: 50%;transform: translateX(-50%);" size="24px">请在微信端打开...</van-loading>
  4. </div>
  5. </template>
  6. <script>
  7. import {
  8. getWxApiToken,
  9. getUSerInfo,
  10. getCourseCodeDetail
  11. } from "../api";
  12. import { isWxEnv } from "../utils";
  13. import {mapState,mapGetters} from 'vuex'
  14. export default {
  15. name: '',
  16. components: {},
  17. props: [],
  18. data () {
  19. return {
  20. pid:null,
  21. code:null,
  22. }
  23. },
  24. created () {
  25. console.log(this.$route.query)
  26. this.init()
  27. },
  28. computed:{
  29. ...mapGetters(['wxid'])
  30. },
  31. methods: {
  32. // 根据wxid获取微信个人数据
  33. getUerInfo(wxid) {
  34. getWxApiToken(wxid).then(token => {
  35. getUSerInfo(token).then(res => {
  36. this.user_info = JSON.parse(localStorage.getItem("wx_user_info"));
  37. if (this.$route.query && this.$route.query.pid&&!this.user_info.marketable) {
  38. localStorage.setItem("pid", this.$route.query.pid);
  39. this.$router.replace(`/courseLogin?pid=${this.$route.query.pid}`);
  40. }else if(this.$route.query && this.$route.query.code){
  41. let codeArr = this.$route.query.code.split('-')
  42. this.$router.replace(`/course/courseDeail/${codeArr[1]}?code=${codeArr[0]}`);
  43. // getCourseCodeDetail(this.$route.query.code).then(res=>{
  44. // if(res != null){
  45. // this.$router.replace(`/course/courseDeail/${res.subjectId}?code=${this.$route.query.code}`);
  46. // }else{
  47. // this.$toast.fail("课程码无效")
  48. // this.$router.replace(`/courseHome`);
  49. // }
  50. // })
  51. }else{
  52. this.$router.replace(`/courseHome`);
  53. }
  54. });
  55. });
  56. },
  57. // 微信环境判断
  58. wxAuth(type,data){
  59. isWxEnv(type,data).then(()=>{
  60. this.$router.replace(`/courseHome`);
  61. }).catch(()=>{
  62. this.getUerInfo(this.wxid)
  63. })
  64. },
  65. //初始化判断
  66. init(){
  67. if(this.$route.query && this.$route.query.pid){
  68. this.wxAuth('pid',this.$route.query.pid)
  69. }else if(this.$route.query && this.$route.query.code){
  70. this.wxAuth('code',this.$route.query.code)
  71. }else{
  72. this.wxAuth()
  73. }
  74. }
  75. },
  76. }
  77. </script>
  78. <style scoped lang='scss'>
  79. </style>