فهرست منبع

feat: 优化小程序

xiansin 2 سال پیش
والد
کامیت
ea150f67fe

+ 10 - 4
mini/App.vue

xqd
@@ -5,16 +5,22 @@ export default {
   },
   async onLaunch(options) {
     console.log('App Launch')
+    const whitelist = [
+      '/pages/login',
+      '/pages/forget'
+    ]
     const path = options.path ? '/' + options.path : '/pages/index'
     if (this.$api.user.isLogin()) {
       await this.$api.user.info().then(res => {
         this.$store.dispatch('user/info', res.data)
       })
     } else {
-      Cache.set('path', path)
-      uni.reLaunch({
-        url: '/pages/index'
-      })
+      if (whitelist.indexOf(path) === -1) {
+        Cache.set('path', path)
+        uni.reLaunch({
+          url: '/pages/index'
+        })
+      }
     }
   },
   onShow() {

+ 8 - 1
mini/api/setting.js

xqd
@@ -12,7 +12,14 @@ export async function loginContact() {
   )
 }
 
+export async function indexBanner() {
+  return request.get(
+    'setting/index/banner'
+  )
+}
+
 export default {
   loginBanner,
-  loginContact
+  loginContact,
+  indexBanner
 }

+ 6 - 2
mini/components/Index/Logged.vue

xqd xqd
@@ -2,7 +2,7 @@
   <view class="index">
     <view class="logo-black logo" />
 
-    <swiper-box type="login" height="560rpx" />
+    <swiper-box type="index" height="560rpx" @click="handleClick" />
 
     <view class="btn-box  main-left cross-center">
       <u-button
@@ -41,7 +41,11 @@ export default {
       userInfo: seate => seate.user.info
     })
   },
-  methods: {}
+  methods: {
+    handleClick(data) {
+      this.$u.route({ url: `/pages/product/detail?id=${data.product_id}` })
+    }
+  }
 }
 </script>
 

+ 8 - 1
mini/components/SwiperBox/index.vue

xqd xqd xqd
@@ -20,6 +20,7 @@
       indicator-mode="dot"
       :indicator-style="{bottom: '24rpx'}"
       @change="handleChange"
+      @click="handleClick"
     >
       <view
         slot="indicator"
@@ -61,6 +62,7 @@ export default {
     return {
       loading: true,
       list: [],
+      original: [],
       currentNum: 0
     }
   },
@@ -74,13 +76,18 @@ export default {
       this.currentNum = e.current
     },
     getSwiper() {
-      const method = this.type === 'login' ? 'loginBanner' : ''
+      const method = this.type === 'login' ? 'loginBanner' : 'indexBanner'
       this.$api.setting[method]().then(res => {
         this.loading = false
+        this.original = res.data
         res.data.forEach(obj => {
           this.list.push(obj.image)
         })
       })
+    },
+    handleClick(index) {
+      const data = this.original[index]
+      this.$emit('click', data)
     }
   }
 }

+ 9 - 19
mini/pages/index.vue

xqd xqd
@@ -1,17 +1,20 @@
 <template>
-  <page-loading class="index-page" :loading="loading">
-    <index-logged v-if="isLogin" />
-    <index-login v-else />
-  </page-loading>
+  <view class="index-page">
+    <template v-if="isLogin">
+      <index-logged />
+    </template>
+    <template v-else>
+      <index-login />
+    </template>
+  </view>
 </template>
 
 <script>
 import IndexLogged from '../components/Index/Logged'
 import IndexLogin from '../components/Index/Login'
-import PageLoading from '../components/PageLoading'
 export default {
   name: 'Index',
-  components: { PageLoading, IndexLogged, IndexLogin },
+  components: { IndexLogged, IndexLogin },
   data() {
     return {
       isLogin: false,
@@ -20,22 +23,9 @@ export default {
   },
   computed: {},
   methods: {
-    async authorize() {
-      this.$loading('小程序授权中...')
-      await this.$api.user.authorize().then(async res => {
-        this.$hideLoading()
-        await this.$store.dispatch('user/authorize')
-      })
-    }
   },
   async onShow() {
-    if (!this.$api.user.isAuthorize()) {
-      await this.authorize()
-    }
     this.isLogin = this.$api.user.isLogin()
-    setTimeout(() => {
-      this.loading = false
-    }, 500)
   }
 }
 </script>

+ 1 - 9
mini/store/modules/user.js

xqd xqd
@@ -1,9 +1,8 @@
-import { getToken, setToken, getAuthorize, setAuthorize } from '@/utils/auth'
+import { getToken, setToken } from '@/utils/auth'
 import Cache from '@/utils/cache'
 const getDefaultState = () => {
   return {
     token: getToken(),
-    authorize: getAuthorize(),
     info: Cache.get('userInfo')
   }
 }
@@ -18,17 +17,10 @@ const mutations = {
   SET_INFO: (state, info) => {
     Cache.set('userInfo', info)
     state.info = info
-  },
-  SET_AUTHORIZE: (state) => {
-    setAuthorize()
-    state.authorize = true
   }
 }
 
 const actions = {
-  authorize({ commit }) {
-    commit('SET_AUTHORIZE')
-  },
   token({ commit }, token) {
     commit('SET_TOKEN', token)
   },

+ 0 - 13
mini/utils/auth.js

xqd xqd
@@ -1,7 +1,6 @@
 import Cache from './cache'
 
 const TokenKey = 'auth_token'
-const AuthorizeKey = 'authorize'
 
 export function getToken() {
   return Cache.get(TokenKey)
@@ -14,15 +13,3 @@ export function setToken(token) {
 export function removeToken() {
   return Cache.remove(TokenKey)
 }
-
-export function getAuthorize() {
-  return Cache.get(AuthorizeKey)
-}
-
-export function setAuthorize() {
-  return Cache.set(AuthorizeKey, true)
-}
-
-export function removeAuthorize() {
-  return Cache.remove(AuthorizeKey)
-}

+ 5 - 0
mini/utils/request/responseInterceptors.js

xqd xqd
@@ -2,6 +2,7 @@
  * 响应拦截
  * @param {Object} http
  */
+const { removeToken } = require('../auth')
 module.exports = vm => {
   uni.$u.http.interceptors.response.use(
     response => {
@@ -25,6 +26,10 @@ module.exports = vm => {
       // 401 登录超时 402 需要登录
       if (typeof error.data.status_code !== 'undefined') {
         if (error.data.status_code === 401 || error.data.status_code === 402) {
+          removeToken('')
+          uni.reLaunch({
+            url: '/pages/index'
+          })
           return Promise.resolve()
         }
       }

+ 29 - 32
server/app/Http/Controllers/V1/AuthController.php

xqd xqd xqd xqd xqd
@@ -38,9 +38,7 @@ class AuthController extends Controller
                 return $this->error('账号或密码错误');
             }
 
-            $app = Factory::miniProgram(config('wechat.mini_program.default'));
-            $data = $app->auth->session($req['code']);
-            $user = User::where('open_id',$data['openid'])->first();
+            $user = $this->checkUser();
             $user->account_id = $account->id;
             $user->save();
 
@@ -57,30 +55,6 @@ class AuthController extends Controller
         }
     }
 
-    public function wechatMiniCode(Request $request): JsonResponse
-    {
-        try {
-            $req = $request->post();
-            $this->validate($request, [
-                'code'      => 'required',
-            ]);
-            $app = Factory::miniProgram(config('wechat.mini_program.default'));
-            $data = $app->auth->session($req['code']);
-            $user = User::where('open_id',$data['openid'])->first();
-            if(!$user){
-                $user = new User();
-                $user->open_id = $data['openid'];
-            }
-
-            $user->remember_token = $data['session_key'];
-            $user->save();
-
-            return $this->success();
-        }catch (\Exception $ex){
-            return $this->error($ex->getMessage());
-        }
-    }
-
     public function wechatMiniPhone(Request $request)
     {
         try{
@@ -90,10 +64,13 @@ class AuthController extends Controller
                 'iv'            => 'required',
             ]);
             $app = Factory::miniProgram(config('wechat.mini_program.default'));
-            $data = $app->auth->session($req['code']);
-            $user = User::where('open_id',$data['openid'])->first();
+            $user = $this->checkUser();
 
-            $decryptedData = $app->encryptor->decryptData($data['session_key'], $req['iv'], $req['encryptedData']);
+            $decryptedData = $app->encryptor->decryptData(
+                $user->remember_token,
+                $req['iv'],
+                $req['encryptedData']
+            );
             $account = Account::where('account', $decryptedData['phoneNumber'])
                 ->where('status', 1)
                 ->first();
@@ -101,8 +78,6 @@ class AuthController extends Controller
                 return $this->error('没有找到相关账号');
             }
 
-            $user = User::where('id', $user['id'])->first();
-            $user->remember_token = $data['session_key'];
             if($user->mobile != $decryptedData['phoneNumber']) {
                 $user->mobile = $decryptedData['phoneNumber'];
                 $user->account_id = $account->id;
@@ -123,5 +98,27 @@ class AuthController extends Controller
         }
     }
 
+    /**
+     * @return User|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
+     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
+     */
+    private function checkUser()
+    {
+        $req = \request()->post();
+
+        $app = Factory::miniProgram(config('wechat.mini_program.default'));
+        $data = $app->auth->session($req['code']);
+        $user = User::where('open_id', $data['openid'])->first();
+        if(!$user){
+            $user = new User();
+            $user->open_id = $data['openid'];
+        }
+
+        $user->remember_token = $data['session_key'];
+        $user->save();
+
+        return $user;
+    }
+
 
 }

+ 11 - 0
server/app/Http/Controllers/V1/SettingController.php

xqd xqd
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\V1;
 
 use App\Models\Banner;
 use App\Models\Contact;
+use App\Models\ProductHot;
 use App\Models\User;
 use Cache;
 use EasyWeChat\Factory;
@@ -33,4 +34,14 @@ class SettingController extends Controller
 
         return $this->success($lists);
     }
+
+    public function indexBanner(): JsonResponse
+    {
+        $lists = ProductHot::select(['cover_img as image','product_id'])
+            ->where('type',\user()->account->type)
+            ->where('is_opened' ,1)
+            ->orderByDesc('sort')
+            ->get();
+        return $this->success($lists);
+    }
 }

+ 1 - 0
server/app/Http/Controllers/V1/UserController.php

xqd
@@ -9,6 +9,7 @@ use App\Models\UserInfo;
 use App\Services\Api\SortService;
 use Carbon\Carbon;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Validator;
 
 class UserController extends Controller

+ 1 - 0
server/routes/api.php

xqd
@@ -82,6 +82,7 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\V1'], function ($api)
         // 相关设置
         $api->group(['prefix' => 'setting'],function ($api){
             /* @var Dingo\Api\Routing\Router $api*/
+            $api->get('index/banner', 'SettingController@indexBanner'); // banner
         });
 
         // 产品