wechat.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div id="app" v-cloak v-loading="loading">
  3. <!-- 未绑定账号 -->
  4. <div v-if="!isError && !isBound && !loading && template && isReady">
  5. <Binding :openid="openid" :template="template" @bind="jumpToSystem" />
  6. </div>
  7. <!-- 已绑定账号 -->
  8. <div
  9. v-if="!isError && isBound && !loading"
  10. v-loading="true"
  11. style="height:100vh"
  12. :element-loading-text="` 正在登录至 ${name} 系统...`"
  13. />
  14. <!-- 登陆时出错 -->
  15. <UnusualState v-if="isError && !loading" :message="message" />
  16. </div>
  17. </template>
  18. <script>
  19. import asyncRequest from "@/api"
  20. import { getParameterByName } from "@/utils/auth"
  21. import UnusualState from "@/components/unusual/index.vue"
  22. import Binding from "@/components/binding/index.vue"
  23. import CryptoJS from "crypto-js"
  24. export default {
  25. name: 'report',
  26. components: { UnusualState, Binding },
  27. data() {
  28. return {
  29. loading: false,
  30. isBound: false,
  31. isReady:false,
  32. back:"",
  33. name:"",
  34. openid: "",
  35. isError: "",
  36. message: "",
  37. template:""
  38. }
  39. },
  40. async mounted() {
  41. this.back = getParameterByName("back")
  42. this.name = getParameterByName("name")
  43. await this.requestUserinfo()
  44. await this.initialTemplate()
  45. this.initialWechatConfig()
  46. },
  47. methods: {
  48. async initialTemplate(){
  49. this.loading = true
  50. const result = await asyncRequest.template();
  51. this.loading = false;
  52. this.template = result.data.map(({priTmplId}) => priTmplId).join(",")
  53. console.log(this.template,'-----')
  54. },
  55. async initialWechatConfig(){
  56. const url = window.location.href.split("#")[0]
  57. const result = await asyncRequest.config({ url })
  58. const { appid, noncestr, signature, timestamp } = result.data;
  59. wx.config({
  60. appId:appid,
  61. timestamp,
  62. nonceStr: noncestr,
  63. signature,
  64. jsApiList:['chooseImage'],
  65. openTagList:['wx-open-subscribe']
  66. })
  67. wx.ready(() => {
  68. this.isReady = true;
  69. })
  70. wx.error(function(err){ console.log(err,'---') })
  71. },
  72. async requestUserinfo() {
  73. const code = getParameterByName("code")
  74. this.loading = true
  75. const result = await asyncRequest.userinfo({ code })
  76. this.loading = false
  77. if (result.data === null) {
  78. this.isError = true;
  79. this.message = result.message;
  80. }
  81. const { wxinfo, isLogin, userinfo } = (result.data || {})
  82. this.isBound = Number(isLogin) === 1;
  83. this.openid = wxinfo.openid;
  84. const isMobile = /Mobi|Android|iPhone/i.test(navigator.userAgent);
  85. //未绑定且非移动端-直接跳转至登录页
  86. if(!this.isBound && !isMobile){
  87. this.$alert('请使用手机版微信绑定账号', '正在跳转至登录页...', {
  88. type:'warning',
  89. callback: () => {
  90. const back = getParameterByName("back")
  91. window.location.href = back + "/?from=authorization"
  92. }
  93. });
  94. return
  95. }
  96. if (!this.isBound) return;
  97. this.jumpToSystem(userinfo.token)
  98. },
  99. jumpToSystem(token = "") {
  100. const encJson = CryptoJS.AES.encrypt(JSON.stringify({ token }), "key123")
  101. const encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson))
  102. window.location.href = this.back + "/#/accept?t=" + encData
  103. },
  104. async getTemplate(){
  105. const result = await asyncRequest.template()
  106. console.log(result)
  107. }
  108. }
  109. };
  110. </script>