Browse Source

feat: 优化小程序绑定上级,分享没有记录BUG

xiansin 2 years ago
parent
commit
c98a13f119

+ 18 - 1
mini/App.vue

xqd xqd
@@ -1,5 +1,5 @@
 <script>
-
+import Cache from './utils/cache'
 export default {
   globalData: {
     isLogin: false
@@ -9,9 +9,26 @@ export default {
     console.log('App Launch')
     const path = options.path ? '/' + options.path : '/pages/index/index'
     const query = options.query
+    // 直接传递user_id
+    if (typeof query.user_id !== 'undefined' && query.user_id) {
+      Cache.set('parent_id', query.user_id)
+    }
+    // 微信小程序 对应的二维码是 scene_code
+    if (typeof query.scene_code !== 'undefined' && query.scene_code) {
+      Cache.set('parent_id', query.scene_code)
+    }
     if (this.$api.user.isLogin()) {
       await this.$api.user.info().then(res => {
         this.$store.dispatch('user/info', res.data)
+        // 绑定上级
+        const parentId = Cache.get('parent_id')
+        if (parentId && !res.data.info.parent_id) {
+          this.$api.user.bind(parentId).then(res => {
+            console.log('-->bind parent success')
+            this.$store.dispatch('user/info', res.data)
+            Cache.remove('parent_id')
+          })
+        }
       })
     } else {
       uni.reLaunch({

+ 2 - 1
mini/api/episode.js

xqd xqd
@@ -45,7 +45,7 @@ export function list(params) {
 
 export function shared(id) {
   return request.post(
-    `episode/shared/${id}`
+    `episode/${id}/shared`
   )
 }
 
@@ -77,6 +77,7 @@ export default {
   detail,
   trace,
   list,
+  shared,
   search,
   vipFree,
   listBuyNum

+ 1 - 0
mini/api/user/index.js

xqd
@@ -41,6 +41,7 @@ export async function login() {
           { code: loginRes.code, anonymousCode: loginRes.anonymousCode }
         ).then(res => {
           Cache.set('isLogin', 0)
+          const parentId = Cache.get('parent_id')
           resolve(res)
         }).catch(err => {
           Cache.set('isLogin', 0)

+ 1 - 1
mini/components/Recharge/index.vue

xqd
@@ -148,7 +148,7 @@ export default {
     },
     handleRecharge() {
       // IOS 不允许购买
-      // #ifdef MP-TOUTIAO | MP-WEIXIN
+      // #ifdef MP-TOUTIAO
       if (!this.$util.checkOS()) return
       // #endif
       const item = this.combos[this.rechargeActive]

+ 8 - 8
mini/pages/episode/play.vue

xqd xqd
@@ -339,8 +339,9 @@ export default {
       })
     },
     // 分享
-    handleShared(id, list_id) {
-      this.$api.episode.shared(id, list_id).then(res => {
+    handleShared(id) {
+      console.log('-->handleShared success')
+      this.$api.episode.shared(id).then(res => {
         this.episode.share_count += 1
       })
     },
@@ -418,17 +419,16 @@ export default {
     }
     let options = {
       title: '',
-      path: `/pages/episode/play?id=${this.id}`
+      path: `/pages/episode/play?id=${this.id}&user_id=${this.userInfo.user_id}`
     }
     if (this.episode) {
+      // 没有success 回调 只要点击分享了就默认成功
+      this.handleShared(this.id)
       options = {
         title: this.episode.name,
-        path: `/pages/episode/play?id=${this.id}`,
+        path: `/pages/episode/play?id=${this.id}&user_id=${this.userInfo.user_id}`,
         imageUrl: this.episode.cover_img,
-        desc: this.episode.name,
-        success: res => {
-          this.handleShared(this.id, this.currentEpisode.id)
-        }
+        desc: this.episode.name
       }
     }
     return options

+ 0 - 5
mini/pages/index/index.vue

xqd
@@ -80,11 +80,6 @@ export default {
   onLoad(options) {
     console.log('-->index data', options)
     this.isLogin && this.getHomeColumn()
-    this.scene_code = typeof options.user_id !== 'undefined' ? options.scene : ''
-    this.parent_id = typeof options.user_id !== 'undefined' ? options.user_id : 0
-    if ((this.parent_id || this.scene_code) && !this.userInfo.parent_id) {
-      this.isLogin && this.bindParent()
-    }
   },
   methods: {
     handleSearch() {

+ 11 - 0
mini/pages/login.vue

xqd xqd
@@ -10,6 +10,8 @@
 </template>
 
 <script>
+import Cache from '../utils/cache'
+
 export default {
   components: {},
 
@@ -28,6 +30,15 @@ export default {
         const { token, user_info } = res.data
         await this.$store.dispatch('user/token', token)
         await this.$store.dispatch('user/info', user_info)
+        // 绑定上级
+        const parentId = Cache.get('parent_id')
+        if (parentId && !user_info.info.parent_id) {
+          this.$api.user.bind(parentId).then(res => {
+            console.log('-->bind parent success')
+            this.$store.dispatch('user/info', res.data)
+            Cache.remove('parent_id')
+          })
+        }
         this.relaunch()
       }).catch(async() => {
         await this.$store.dispatch('user/clear')

+ 1 - 1
mini/pages/member/index.vue

xqd
@@ -73,7 +73,7 @@ export default {
       // this.modal.show = true
     },
     handleBuy() {
-      // #ifdef MP-TOUTIAO | MP-WEIXIN
+      // #ifdef MP-TOUTIAO
       if (!this.$util.checkOS()) return
       // #endif
       const item = this.settings[this.activeIndex]

+ 1 - 0
server/routes/api.php

xqd
@@ -192,6 +192,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api)
             $api->get('vip/free', 'EpisodeController@vipFree'); // banner
             $api->get('{id}/detail', 'EpisodeController@detail'); // 详情
             $api->get('/list/{listId}/buyNum', 'EpisodeController@listBuyNum'); // 详情
+            $api->post('{id}/shared', 'EpisodeController@shared'); // 分享
         });
 
     });