张保健 3 anni fa
commit
2e8ce8f5df
1 ha cambiato i file con 205 aggiunte e 0 eliminazioni
  1. 205 0
      App.vue

+ 205 - 0
App.vue Vedi File

@@ -0,0 +1,205 @@
1
+<script>
2
+import { checkLogin } from './libs/login';
3
+import { HTTP_REQUEST_URL } from './config/app';
4
+import { getShopConfig, silenceAuth } from '@/api/public';
5
+import Auth from './libs/wechat.js';
6
+import Routine from './libs/routine.js';
7
+export default {
8
+	globalData: {
9
+		spid: 0,
10
+		code: 0,
11
+		isLogin: false,
12
+		userInfo: {},
13
+		MyMenus: [],
14
+		globalData: false,
15
+		isIframe: false,
16
+		tabbarShow: true,
17
+		isWsOpen:false,
18
+	},
19
+	onLaunch: function(option) {
20
+		let that = this;
21
+		// #ifdef MP
22
+		if (HTTP_REQUEST_URL == '') {
23
+			console.error(
24
+				"请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret"
25
+			);
26
+			return false;
27
+		}
28
+		if (option.query.hasOwnProperty('scene')) {
29
+			switch (option.scene) {
30
+				//扫描小程序码
31
+				case 1047:
32
+					let val = that.$util.getUrlParams(decodeURIComponent(option.query.scene));
33
+					that.globalData.code = val.pid === undefined ? val : val.pid;
34
+					break;
35
+				//长按图片识别小程序码
36
+				case 1048:
37
+					that.globalData.code = option.query.scene;
38
+					break;
39
+				//手机相册选取小程序码
40
+				case 1049:
41
+					that.globalData.code = option.query.scene;
42
+					break;
43
+				//直接进入小程序
44
+				case 1001:
45
+					that.globalData.spid = option.query.scene;
46
+					break;
47
+			}
48
+		}
49
+		// #endif
50
+		getShopConfig().then(res => {
51
+			this.$store.commit('SETPHONESTATUS', res.data.status);
52
+		});
53
+		// 获取导航高度;
54
+		uni.getSystemInfo({
55
+			success: function(res) {
56
+				that.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 91;
57
+			}
58
+		});
59
+
60
+		// #ifdef H5
61
+        if (option.query.hasOwnProperty('type') && option.query.type=="crmebDiy") {
62
+			this.globalData.isIframe = true;
63
+		} else {
64
+			this.globalData.isIframe = false;
65
+		}
66
+		// try {
67
+		// 	// 静默授权code
68
+		// 	var snsapiCode = uni.getStorageSync('snsapiCode');
69
+		// } catch (e) {}
70
+		let snsapiBase = 'snsapi_base';
71
+		let urlData = location.pathname + location.search;
72
+		// if (snsapiCode) {
73
+		// 	return
74
+		// } else {
75
+		if (!that.$store.getters.isLogin && Auth.isWeixin()) {
76
+			const { code, state, scope } = option.query;
77
+			if (code && location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
78
+				// 存储静默授权code
79
+				uni.setStorageSync('snsapiCode', code);
80
+				let spread = that.globalData.spid ? that.globalData.spid : '';
81
+				silenceAuth({
82
+					code: code,
83
+					spread: that.$Cache.get('spread'),
84
+					spid: that.globalData.code
85
+				})
86
+					.then(res => {
87
+						uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url)));
88
+						if (res.data.key !== undefined && res.data.key) {
89
+							this.$Cache.set('snsapiKey', res.data.key);
90
+						} else {
91
+							let time = res.data.expires_time - this.$Cache.time();
92
+							this.$store.commit('LOGIN', {
93
+								token: res.data.token,
94
+								time: time
95
+							});
96
+							this.$store.commit('SETUID', res.data.userInfo.uid);
97
+							this.$store.commit('UPDATE_USERINFO', res.data.userInfo);
98
+							location.href = decodeURIComponent(decodeURIComponent(option.query.back_url));
99
+						}
100
+					})
101
+					.catch(res => {
102
+						this.$util.Tips({
103
+							title: error
104
+						});
105
+					});
106
+			} else {
107
+				if (!this.$Cache.has('snsapiKey')) {
108
+					if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) {
109
+						Auth.oAuth(snsapiBase, urlData);
110
+					}
111
+				}
112
+			}
113
+		} else {
114
+			if(option.query.back_url){
115
+				// alert(uni.getStorageSync('snsapiCode'))
116
+				// alert(uni.getStorageSync('snRouter'))
117
+				location.href = uni.getStorageSync('snRouter')
118
+			}
119
+		}
120
+		// }
121
+
122
+		// #endif
123
+		// #ifdef MP
124
+		// 小程序静默授权
125
+		console.log(this.$store.getters.isLogin, 'this.$store');
126
+		if (!this.$store.getters.isLogin) {
127
+			Routine.getCode()
128
+				.then(code => {
129
+					this.silenceAuth(code);
130
+				})
131
+				.catch(res => {
132
+					uni.hideLoading();
133
+				});
134
+		}
135
+		// #endif
136
+	},
137
+	mounted() {},
138
+	methods: {
139
+		// 小程序静默授权
140
+		silenceAuth(code) {
141
+			let that = this;
142
+			let spread = that.globalData.spid ? that.globalData.spid : '';
143
+			silenceAuth({
144
+				code: code,
145
+				spread_spid: spread,
146
+				spread_code: that.globalData.code
147
+			})
148
+				.then(res => {
149
+					if (res.data.token !== undefined && res.data.token) {
150
+						uni.hideLoading();
151
+						let time = res.data.expires_time - this.$Cache.time();
152
+						that.$store.commit('LOGIN', {
153
+							token: res.data.token,
154
+							time: time
155
+						});
156
+						that.$store.commit('SETUID', res.data.userInfo.uid);
157
+						that.$store.commit('UPDATE_USERINFO', res.data.userInfo);
158
+					}
159
+				})
160
+				.catch(res => {
161
+					console.log(res);
162
+				});
163
+		}
164
+	},
165
+	onHide: function() {
166
+		//console.log('App Hide')
167
+	}
168
+};
169
+</script>
170
+
171
+<style>
172
+@import url("@/plugin/emoji-awesome/css/google.min.css");
173
+@import url('@/plugin/animate/animate.min.css');
174
+@import 'static/css/base.css';
175
+@import 'static/iconfont/iconfont.css';
176
+@import 'static/css/guildford.css';
177
+@import 'static/css/style.scss';
178
+
179
+view {
180
+	box-sizing: border-box;
181
+}
182
+
183
+.bg-color-red {
184
+	background-color: #e93323 !important;
185
+}
186
+
187
+.syspadding {
188
+	padding-top: var(--status-bar-height);
189
+}
190
+
191
+.flex {
192
+	display: flex;
193
+}
194
+
195
+.uni-scroll-view::-webkit-scrollbar {
196
+	/* 隐藏滚动条,但依旧具备可以滚动的功能 */
197
+	display: none;
198
+}
199
+
200
+::-webkit-scrollbar {
201
+	width: 0;
202
+	height: 0;
203
+	color: transparent;
204
+}
205
+</style>