张延森 пре 4 година
родитељ
комит
50a821506d

+ 6
- 0
config/index.js Прегледај датотеку

@@ -1,3 +1,5 @@
1
+const path = require('path')
2
+
1 3
 const config = {
2 4
   projectName: 'myApp',
3 5
   date: '2020-8-7',
@@ -59,6 +61,10 @@ const config = {
59 61
         }
60 62
       }
61 63
     }
64
+  },
65
+  alias: {
66
+    '@/components': path.resolve(__dirname, '..', 'src/components'),
67
+    '@/util': path.resolve(__dirname, '..', 'src/util')
62 68
   }
63 69
 }
64 70
 

+ 48
- 1
src/app.js Прегледај датотеку

@@ -1,4 +1,7 @@
1
+import Taro from '@tarojs/taro'
1 2
 import Vue from 'vue'
3
+import BasicLayout from '@/components/Layout/Basic'
4
+import { vueAuthPlugin } from '@/util/auth-plugin'
2 5
 import store from './store/index'
3 6
 
4 7
 import './app.scss'
@@ -10,13 +13,57 @@ import './iconfont.scss'
10 13
 import ToolClass from './util/PublicMethod/index'
11 14
 Vue.prototype.ToolClass = ToolClass
12 15
 
16
+Vue.use(vueAuthPlugin)
17
+Vue.component('BasicLayout', BasicLayout)
18
+
13 19
 const App = new Vue({
14 20
   store,
15
-  onShow (options) {
21
+  onLaunch (options) {
22
+    this.login(options)
16 23
   },
17 24
   render(h) {
18 25
     // this.$slots.default 是将要会渲染的页面
19 26
     return h('block', this.$slots.default)
27
+  },
28
+  methods: {
29
+    login(options) {
30
+      Taro.login({
31
+        success (res) {
32
+          if (res.code) {
33
+            store.dispatch(
34
+              'user/MainSignIn',
35
+              { queryData: { code: res.code } }
36
+            ).then((res) => { // 获取用户信息
37
+              store.commit(
38
+                'user/EditUserInfo',
39
+                { name: 'PersonId', value: res.data.data.person.personId }
40
+              )
41
+
42
+              store.commit(
43
+                'user/EditUserInfo',
44
+                { name: 'OpenId', value: res.data.data.person.openId }
45
+              )
46
+              
47
+              store.commit(
48
+                'user/EditUserInfo',
49
+                { name: 'SessionKey', value: res.data.data.person.sessionKey }
50
+              )
51
+              
52
+              store.commit(
53
+                'user/EditUserInfo',
54
+                { name: 'Phone', value: res.data.data.person.phone }
55
+              )
56
+            })
57
+          } else {
58
+            Taro.showToast({
59
+              title: '初始化失败, 请退出重试',
60
+              icon: 'none',
61
+              duration: 3000
62
+            })
63
+          }
64
+        }
65
+      })
66
+    }
20 67
   }
21 68
 })
22 69
 

+ 25
- 0
src/components/Layout/Basic/index.vue Прегледај датотеку

@@ -0,0 +1,25 @@
1
+<template>
2
+  <block>
3
+    <block v-if="user.PersonId">
4
+      <slot v-bind:user="user"></slot>
5
+    </block>
6
+    <Loading v-else />
7
+  </block>
8
+</template>
9
+
10
+<script>
11
+import { mapState } from 'vuex'
12
+import Loading from '@/components/Loading'
13
+
14
+export default {
15
+  name: 'BasicLayout',
16
+  components: {
17
+    Loading
18
+  },
19
+  computed: {
20
+    ...mapState({
21
+      user: s => s.user.UserInfo || {}
22
+    })
23
+  }
24
+}
25
+</script>

+ 3
- 0
src/components/Loading/index.vue Прегледај датотеку

@@ -0,0 +1,3 @@
1
+<template>
2
+  <view style="text-align: center; margin-top: 20vh">Loading</view>
3
+</template>

+ 2
- 13
src/pages/HuiAiXin/index.vue Прегледај датотеку

@@ -89,19 +89,8 @@ export default {
89 89
     BannerSwiper
90 90
   },
91 91
   created () {
92
-    let _that = this
93
-    wx.login({
94
-      success (res) {
95
-        _that.MainSignIn({ queryData: { code: res.code } }).then((res) => { // 获取用户信息
96
-          _that.EditUserInfo({ name: 'PersonId', value: res.data.data.person.personId })
97
-          _that.EditUserInfo({ name: 'OpenId', value: res.data.data.person.openId })
98
-          _that.EditUserInfo({ name: 'SessionKey', value: res.data.data.extraInfo.sessionKey })
99
-          if (res.data.data.person.phone !== undefined && res.data.data.person.phone !== null) {
100
-            _that.EditUserInfo({ name: 'Phone', value: res.data.data.person.phone })
101
-          }
102
-          _that.Init()
103
-        })
104
-      }
92
+    this.$authed(() => {
93
+      this.Init()
105 94
     })
106 95
   },
107 96
   methods: {

+ 50
- 59
src/pages/HuiShengHuo/index.vue Прегледај датотеку

@@ -1,64 +1,66 @@
1 1
 <template>
2
-  <view class="page HuiShengHuo">
3
-    <ScrollY :IsRefreshing="IsRefreshing" @Refresh="Refresh">
2
+  <BasicLayout>
3
+    <view class="page HuiShengHuo">
4
+      <ScrollY :IsRefreshing="IsRefreshing" @Refresh="Refresh">
4 5
 
5
-      <!-- banner -->
6
-      <view class="Banner">
7
-        <view>
8
-          <BannerSwiper :List="IndexBanner"></BannerSwiper>
6
+        <!-- banner -->
7
+        <view class="Banner">
8
+          <view>
9
+            <BannerSwiper :List="IndexBanner"></BannerSwiper>
10
+          </view>
9 11
         </view>
10
-      </view>
11 12
 
12
-      <!-- 活动照片墙 -->
13
-      <view class="PhotoList">
14
-        <view class="flex-h">
15
-          <view class="flex-item">
16
-            <image mode="heightFix" src="https://cjgw.oss-cn-hangzhou.aliyuncs.com/ygcxxfb/image/huodongzhaopianqiang.png"></image>
13
+        <!-- 活动照片墙 -->
14
+        <view class="PhotoList">
15
+          <view class="flex-h">
16
+            <view class="flex-item">
17
+              <image mode="heightFix" src="https://cjgw.oss-cn-hangzhou.aliyuncs.com/ygcxxfb/image/huodongzhaopianqiang.png"></image>
18
+            </view>
19
+            <navigator url="./PhotoList/index" hover-class="other-navigator-hover" class="More">更多 <text class="iconfont iconjiantouright"></text></navigator>
17 20
           </view>
18
-          <navigator url="./PhotoList/index" hover-class="other-navigator-hover" class="More">更多 <text class="iconfont iconjiantouright"></text></navigator>
19
-        </view>
20
-        <view class="ListContainer" v-if="PhotoList.length">
21
-          <scroll-view scroll-x="true">
22
-            <view class="List">
23
-              <view v-for="(item, index) in PhotoList" :key="index">
24
-                <view>
25
-                  <image mode="aspectFill" class="centerLabel" :src="ToolClass.DemoImg()"></image>
21
+          <view class="ListContainer" v-if="PhotoList.length">
22
+            <scroll-view scroll-x="true">
23
+              <view class="List">
24
+                <view v-for="(item, index) in PhotoList" :key="index">
25
+                  <view>
26
+                    <image mode="aspectFill" class="centerLabel" :src="ToolClass.DemoImg()"></image>
27
+                  </view>
26 28
                 </view>
27 29
               </view>
28
-            </view>
29
-          </scroll-view>
30
+            </scroll-view>
31
+          </view>
30 32
         </view>
31
-      </view>
32 33
 
33
-      <!-- 精彩活动 -->
34
-      <view class="ActivityList">
35
-        <view class="flex-h">
36
-          <view class="flex-item">
37
-            <image mode="heightFix" src="https://cjgw.oss-cn-hangzhou.aliyuncs.com/ygcxxfb/image/jingcaihuodong.png"></image>
34
+        <!-- 精彩活动 -->
35
+        <view class="ActivityList">
36
+          <view class="flex-h">
37
+            <view class="flex-item">
38
+              <image mode="heightFix" src="https://cjgw.oss-cn-hangzhou.aliyuncs.com/ygcxxfb/image/jingcaihuodong.png"></image>
39
+            </view>
40
+            <navigator url="./ActivityList/index" hover-class="other-navigator-hover" class="More">更多 <text class="iconfont iconjiantouright"></text></navigator>
38 41
           </view>
39
-          <navigator url="./ActivityList/index" hover-class="other-navigator-hover" class="More">更多 <text class="iconfont iconjiantouright"></text></navigator>
40
-        </view>
41
-        <view class="List" v-if="ActivityList.length">
42
-          <view class="Item" v-for="(item, index) in ActivityList" :key="index">
43
-            <navigator class="Img" :url="`./ActivityList/ActivityDetail/index?id=${item.activityId}`" hover-class="other-navigator-hover">
44
-              <image mode="aspectFill" class="centerLabel" :src="item.thumb"></image>
45
-            </navigator>
46
-            <view class="Info">
47
-              <text>{{item.name}}</text>
48
-              <view class="flex-h">
49
-                <text class="flex-item">参与截止时间:{{ToolClass.DateFormat(new Date(item.enrollEnd).getTime())}}</text>
50
-                <navigator :url="`./ActivityList/ActivityDetail/index?id=${item.activityId}`" hover-class="other-navigator-hover" class="Btn" v-if="item.isEnroll">立即报名</navigator>
51
-                <navigator :url="`./ActivityList/ActivityDetail/index?id=${item.activityId}`" hover-class="other-navigator-hover" class="Btn" v-if="item.isVote">立即投票</navigator>
52
-                <view v-if="new Date(item.endDate).getTime() <= Date.now()">已结束</view>
42
+          <view class="List" v-if="ActivityList.length">
43
+            <view class="Item" v-for="(item, index) in ActivityList" :key="index">
44
+              <navigator class="Img" :url="`./ActivityList/ActivityDetail/index?id=${item.activityId}`" hover-class="other-navigator-hover">
45
+                <image mode="aspectFill" class="centerLabel" :src="item.thumb"></image>
46
+              </navigator>
47
+              <view class="Info">
48
+                <text>{{item.name}}</text>
49
+                <view class="flex-h">
50
+                  <text class="flex-item">参与截止时间:{{ToolClass.DateFormat(new Date(item.enrollEnd).getTime())}}</text>
51
+                  <navigator :url="`./ActivityList/ActivityDetail/index?id=${item.activityId}`" hover-class="other-navigator-hover" class="Btn" v-if="item.isEnroll">立即报名</navigator>
52
+                  <navigator :url="`./ActivityList/ActivityDetail/index?id=${item.activityId}`" hover-class="other-navigator-hover" class="Btn" v-if="item.isVote">立即投票</navigator>
53
+                  <view v-if="new Date(item.endDate).getTime() <= Date.now()">已结束</view>
54
+                </view>
53 55
               </view>
54 56
             </view>
57
+            <view style="width: 100%; height: 10px;"></view>
55 58
           </view>
56
-          <view style="width: 100%; height: 10px;"></view>
57 59
         </view>
58
-      </view>
59 60
 
60
-    </ScrollY>
61
-  </view>
61
+      </ScrollY>
62
+    </view>
63
+  </BasicLayout>
62 64
 </template>
63 65
 
64 66
 <script>
@@ -90,19 +92,8 @@ export default {
90 92
     BannerSwiper
91 93
   },
92 94
   created () {
93
-    let _that = this
94
-    wx.login({
95
-      success (res) {
96
-        _that.MainSignIn({ queryData: { code: res.code } }).then((res) => { // 获取用户信息
97
-          _that.EditUserInfo({ name: 'PersonId', value: res.data.data.person.personId })
98
-          _that.EditUserInfo({ name: 'OpenId', value: res.data.data.person.openId })
99
-          _that.EditUserInfo({ name: 'SessionKey', value: res.data.data.extraInfo.sessionKey })
100
-          if (res.data.data.person.phone !== undefined && res.data.data.person.phone !== null) {
101
-            _that.EditUserInfo({ name: 'Phone', value: res.data.data.person.phone })
102
-          }
103
-          _that.Init()
104
-        })
105
-      }
95
+    this.$authed(() => {
96
+      this.Init()
106 97
     })
107 98
   },
108 99
   methods: {

+ 3
- 0
src/store/index.js Прегледај датотеку

@@ -1,8 +1,11 @@
1 1
 import Vue from 'vue'
2 2
 import Vuex from 'vuex'
3
+import { storeAuthPlugin } from '@/util/auth-plugin'
4
+
3 5
 Vue.use(Vuex)
4 6
 
5 7
 const store = new Vuex.Store({
8
+  plugins: [storeAuthPlugin],
6 9
   state: {
7 10
   },
8 11
   mutations: {

+ 18
- 16
src/util/PublicMethod/index.js Прегледај датотеку

@@ -44,24 +44,26 @@ const ToolClass = {
44 44
       ...(config.data || {}),
45 45
       header: { ...Header },
46 46
       success: (res) => {
47
-        if (res.header['X-Authorization-Jwt'] !== undefined) { // 更新本地存储token
48
-          wx.setStorageSync('token', res.header['X-Authorization-Jwt'])
47
+        const token = res.header['X-Authorization-Jwt'] || res.header['X-Authorization-JWT']
48
+        if (token) { // 更新本地存储token
49
+          wx.setStorageSync('token', token)
49 50
         }
50 51
         if (res.data.code - 0 === 1001) { // token失效
51
-          wx.login({ // 微信获取code
52
-            success (subRes) {
53
-              wx.request({ // 登录获取token
54
-                url: `${SERVER_API}/login?code=${subRes.code}`,
55
-                method: `post`,
56
-                success: (cRes) => {
57
-                  if (cRes.header['X-Authorization-Jwt'] !== undefined) { // 更新本地存储token
58
-                    wx.setStorageSync('token', cRes.header['X-Authorization-Jwt'])
59
-                    _that.WxRequest(config) // 获得token之后重新请求接口
60
-                  }
61
-                }
62
-              })
63
-            }
64
-          })
52
+          // wx.login({ // 微信获取code
53
+          //   success (subRes) {
54
+          //     wx.request({ // 登录获取token
55
+          //       url: `${SERVER_API}/login?code=${subRes.code}`,
56
+          //       method: `post`,
57
+          //       success: (cRes) => {
58
+          //         const token = cRes.header['X-Authorization-Jwt'] || cRes.header['X-Authorization-JWT']
59
+          //         if (token) { // 更新本地存储token
60
+          //           wx.setStorageSync('token', token)
61
+          //           _that.WxRequest(config) // 获得token之后重新请求接口
62
+          //         }
63
+          //       }
64
+          //     })
65
+          //   }
66
+          // })
65 67
         }
66 68
         if (config.success !== undefined && res.data.code - 0 === 1000) {
67 69
           config.success(res)

+ 28
- 0
src/util/auth-plugin.js Прегледај датотеку

@@ -0,0 +1,28 @@
1
+import observe from './observe'
2
+
3
+const monitor = observe()
4
+
5
+// 只执行一次
6
+let done = false
7
+
8
+// VUEX 插件
9
+export function storeAuthPlugin(store) {
10
+  store.subscribe((mutation, state) => {
11
+    if (!done && mutation.type === 'user/EditUserInfo') {
12
+      monitor.notify()
13
+      done = true
14
+    }
15
+  })
16
+}
17
+
18
+// VUE 插件
19
+export function vueAuthPlugin(Vue, options) {
20
+  // 添加实例 $authed 方法
21
+  Vue.prototype.$authed = function(next) {
22
+    if (done) {
23
+      next()
24
+    } else {
25
+      monitor.subscribe(next)
26
+    }
27
+  }
28
+}

+ 22
- 0
src/util/observe.js Прегледај датотеку

@@ -0,0 +1,22 @@
1
+
2
+export default function observe() {
3
+  const listeners = []
4
+
5
+  function notify(...args) {
6
+    listeners.forEach(listener => listener(...args))
7
+  }
8
+
9
+  function subscribe (listener) {
10
+    listeners.push(listener)
11
+
12
+    return function unsubscribe() {
13
+      const index = listeners.indexOf(listener)
14
+      listeners.splice(index, 1)
15
+    }
16
+  }
17
+
18
+  return {
19
+    notify,
20
+    subscribe
21
+  }
22
+}