jazz
2023-12-04 bb2b0836b12254eee6bad1886f3d5606ab58aa6b
提交 | 用户 | age
3ac5f2 1 <template>
J 2     <div id="app">
3         <transition name="fade" mode="out-in">
4             <!-- 页面,isRouterAlive 刷新用 -->
5             <router-view v-if="isRouterAlive"/>
6         </transition>
7
8         <!-- Toast 提示 -->
9         <transition name="fade">
10             <div class="toast-mask flex flex-center" :class="{'type-toast': toastType=='toast', 'type-loading': toastType=='loading'}" v-if="toastShow">
11                 <!-- 提示 -->
12                 <div class="toast" v-html="toastText" v-if="toastType=='toast'"></div>
13                 <!-- loading -->
14                 <div class="toast" v-if="toastType=='loading'">
15                     <div class="ball-rotate"><div></div></div>
16                 </div>
17             </div>
18         </transition>
19     </div>
20 </template>
21
22 <script>
23
24 // 入口参数
25 // 这里注释入口参数
26
27 import Login from '@/utils/jun_login.js'
28 import wxsign from '@/utils/wxsign.js'
29 import Config from './config'
30
31 export default {
32     name: 'App',
33
34     // 安装组件
35     components: {
36
37     },
38
39     // 监听路由
40     watch: {
41         // $route(to, from){
42
43         // }
44     },
45
46     // 子组件injection
47     // -用于共享app methods
48     provide(){
49         return {
50             reload: this.reload, // 刷新
51             toast: this.toast, // toast
52             // 修复input导致ios微信浏览器收起键盘时,不回滚
53             inputBlur: ()=>{
54                 let u = navigator.userAgent,
55                 app = navigator.appVersion;
56                 let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
57                 if(isIOS){
58                     setTimeout(() => {
59                         const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0
60                         window.scrollTo(0, Math.max(scrollHeight - 1, 0))
61                     }, 200)
62                 }
63             },
64             noop(){},
65         }
66     },
67     data () {
68         return {
69             // 刷新用
70             isRouterAlive: true,
71
72             toastText: '',
73             toastShow: false,
74             toastType: 'toast',
75         }
76     },
77     mounted () {
78         console.log('app amounted')
79
80         // 避免刷新导致code重复使用
81         let code = this.getQueryString('code') // 微信回调code
82         let cover = this.getQueryString('cover') // 是否封面入口
83         let local_code = this.getLocalStorage('code') // 本地缓存code
84         let isRule = this.getQueryString('rule')==1 // 规则
85         let isCoupon = this.getQueryString('coupon')==1 // 优惠券
86
87         // 以下情况需要重定向到长链
88         // 1. 链接带有code,但code已经用过
89         // 2. 非cover入口
90         // 3. 正式环境
91         // 4. 非规则单页
92         // 4. 非优惠券单页
93         // console.dir(Config.createCodeUrl())
94
95         if(Config.isWxLoginType){
96             if (code && local_code == code || (!isCoupon && !isRule && !cover && !code && !Config.ismock && !Config.istest)) {
97                 // 该微信code已使用,重新跳转长链
98                 Login.toLongUrl()
99                 return
100             } else {
101                 this.removeLocalStorage('code')
102             }
103         }
104
105         // 刷新固定首页
106         if (location.hash && location.hash!='#/') {
107             this.$router.replace({name: 'root'})
108         }
109
110         // 暴露到全局,jun_httpEvent.js 调用
111         window.appLoading = this.loading.bind(this)
112         window.appHideLoading = this.hideLoading.bind(this)
113         window.appToast = this.toast.bind(this)
114
115         // this.$refs.audio.play()
116         if(Config.isWxLoginType) this.wxinit()
117     },
118     methods: {
119         // 微信初始化
120         wxinit () {
121             wxsign.main()
122
123             // 测试用
124             // let time = new Date('2020/10/11 00:00:00').getTime()
125             // if ( Date.now() < time ) {
126             //     alert('该活动未结束')
127             // }
128
129             wx.ready(()=>{
130                 console.log('ready')
131
132                 let shareImg = sessionStorage.getItem('shareImg');
133                 let shareTitle = sessionStorage.getItem('shareTitle');
134                 let shareDesc = sessionStorage.getItem('shareDesc');
135
136                 // 分享链接
137                 let url = location.origin + location.pathname + '?cover=1'
138                 // 带channelNo
139                 let we_session = this.getLocalStorage('we_session')
140                 if (we_session && we_session.we_session) {
141                     url += '&channelNo=' + we_session.we_session
142                 }
143                 console.log(url)
144
145                 // 分享
146                 // wx.updateAppMessageShareData({
147                 //     title: shareTitle, // 分享标题
148                 //     desc: shareDesc, // 分享描述
149                 //     link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
150                 //     imgUrl: shareImg, // 分享图标
151                 //     success: function () {
152                 //         // 设置成功
153                 //     }
154                 // })
155                 // wx.updateTimelineShareData({
156                 //     title: shareTitle, // 分享标题
157                 //     link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
158                 //     imgUrl: shareImg, // 分享图标
159                 //     success: function () {
160                 //         // 设置成功
161                 //     }
162                 // })
163
164                 wx.onMenuShareTimeline({
165                     title: shareTitle, // 分享标题
166                     link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
167                     imgUrl: shareImg, // 分享图标
168                     success: function () {
169                         // 用户点击了分享后执行的回调函数
170                         // alert(123)
171                     }
172                 })
173
174                 wx.onMenuShareAppMessage({
175                     title: shareTitle, // 分享标题
176                     desc: shareDesc, // 分享描述
177                     link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
178                     imgUrl: shareImg, // 分享图标
179                     type: 'link', // 分享类型,music、video或link,不填默认为link
180                     // dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
181                     success: function () {
182                         // 用户点击了分享后执行的回调函数
183                         // alert(123)
184                     }
185                 });
186             })
187         },
188
189         // 刷新
190         reload(){
191             this.isRouterAlive = false
192             this.$nextTick(()=>{
193                 this.isRouterAlive = true
194             })
195         },
196
197         // toast
198         toast (e, type) {
199             type = type || 'toast'
200             this.toastText = e
201             this.toastShow = true
202             this.toastType = type
203
204             if (type == 'toast') {
205                 clearTimeout(timer)
206                 timer = setTimeout(()=>{
207                     this.hideToast()
208                 }, 2000)
209             }
210         },
211         // 显示loading
212         loading () {
213             this.toast('', 'loading')
214         },
215         // 隐藏toast
216         hideToast(){
217             this.toastShow = false
218         },
219         // toast loading 收起
220         hideLoading(){
221             this.hideToast()
222         },
223     }
224 }
225 </script>
226
227 <style>
228 @import './assets/css/common.css';
229 .toast-mask{
230     position: fixed;
231     z-index: 2000;
232     top: 0;
233     left: 0;
234     right: 0;
235     bottom: 0;
236     transition: all .5s;
237 }
238 .toast-mask.type-toast{
239     pointer-events: none;
240 }
241 .toast{
242     text-align: center;
243     border-radius: 6px;
244     color:#FFF;
245     background: rgba(17, 17, 17, 0.9);
246     line-height: 1.6;
247     padding: 30px;
248     max-width: 400px;
249     font-size: 24px;
250 }
251
252 @keyframes ball-rotate {
253   0% {transform: rotate(0deg) scale(1); }
254
255   50% {transform: rotate(180deg) scale(0.6);}
256
257   100% {transform: rotate(360deg) scale(1);}
258 }
259 .ball-rotate{position: relative;width: 200px;height: 140px;}
260 .ball-rotate > div {
261     background-color: #fff;
262     width: 30px;
263     height: 30px;
264     border-radius: 100%;
265     margin: 4px;
266     animation-fill-mode: both;
267     position: absolute;
268     top: 50%;
269     left: 50%;
270     margin: -16px 0 0 -16px;
271 }
272 .ball-rotate > div:first-child {
273     animation: ball-rotate 1s 0s cubic-bezier(.7, -.13, .22, .86) infinite;
274 }
275 .ball-rotate > div:before, .ball-rotate > div:after {
276     background-color: #fff;
277     width: 30px;
278     height: 30px;
279     border-radius: 100%;
280     margin: 4px;
281     content: ""!important;
282     position: absolute;
283     opacity: .8;
284 }
285 .ball-rotate > div:before {top: 0px;left: -56px}
286 .ball-rotate > div:after {top: 0px;left: 50px}
287 </style>