wechat.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. },
  54. async initialWechatConfig(){
  55. const url = window.location.href.split("#")[0]
  56. const result = await asyncRequest.config({ url })
  57. const { appid, noncestr, signature, timestamp } = result.data;
  58. wx.config({
  59. appId:appid,
  60. timestamp,
  61. nonceStr: noncestr,
  62. signature,
  63. jsApiList:['chooseImage'],
  64. openTagList:['wx-open-subscribe']
  65. })
  66. wx.ready(() => {
  67. this.isReady = true;
  68. })
  69. wx.error(function(err){ console.log(err,'---') })
  70. },
  71. async requestUserinfo() {
  72. const code = getParameterByName("code")
  73. this.loading = true
  74. const result = await asyncRequest.userinfo({ code })
  75. this.loading = false
  76. if (result.data === null) {
  77. this.isError = true;
  78. this.message = result.message;
  79. }
  80. const { wxinfo, isLogin, userinfo } = (result.data || {})
  81. this.isBound = Number(isLogin) === 1;
  82. this.openid = wxinfo.openid;
  83. if (!this.isBound) return;
  84. this.jumpToSystem(userinfo.token)
  85. },
  86. jumpToSystem(token = "") {
  87. const encJson = CryptoJS.AES.encrypt(JSON.stringify({ token }), "key123")
  88. const encData = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encJson))
  89. window.location.href = this.back + "/#/accept/?t=" + encData
  90. },
  91. async getTemplate(){
  92. const result = await asyncRequest.template()
  93. console.log(result)
  94. }
  95. }
  96. };
  97. </script>