From a72c68ce87731d3837176329ffe663a9361ffa60 Mon Sep 17 00:00:00 2001 From: sfmind <130201237@qq.com> Date: Wed, 23 Nov 2022 00:20:35 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BE=AE=E8=B0=83=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yd-text-price/yd-text-price.vue | 31 +++++++++++++++---- yudao-ui-app/pages/index/index.vue | 2 +- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/yudao-ui-app/components/yd-text-price/yd-text-price.vue b/yudao-ui-app/components/yd-text-price/yd-text-price.vue index fab033972..be32b13de 100644 --- a/yudao-ui-app/components/yd-text-price/yd-text-price.vue +++ b/yudao-ui-app/components/yd-text-price/yd-text-price.vue @@ -1,6 +1,6 @@ <template> <view> - <text v-for="(item, index) in textArray" :key="index" :style="{ 'font-size': (index === 1 ? integerSize : size) + 'px', 'color': color }"> + <text v-for="(item, index) in textArray" :key="index" :style="{ fontSize: (index === 1 ? integerSize : size) + 'px', color, textDecoration }"> {{ item }} </text> </view> @@ -8,15 +8,20 @@ <script> /** - * 此组件简单的显示特定样式的(人名币)价格数字 + * 此组件简单的显示(驼峰式)的价格 */ export default { name: 'yd-text-price', components: {}, props: { + //货币符号 + symbol: { + type: String, + default: '¥' + }, price: { type: [String, Number], - default: '0.00' + default: '' }, color: { type: String, @@ -30,14 +35,28 @@ export default { //整形部分字体大小可单独定义 intSize: { type: [String, Number], - default: 15 + default: '' + }, + //文字装饰,下划线,中划线等 + decoration: { + type: String, + default: 'none' + } + }, + data() { + return { + textDecoration: this.decoration } }, computed: { textArray() { - let array = ['¥'] + let array = [] + if (this.price === '' || this.price === undefined) { + return [] + } + array.push(this.symbol) if (!/^\d+(\.\d+)?$/.test(this.price)) { - console.error('组件<custom-text-price :text="???" 此处参数应为金额数字') + console.error('组件<yd-text-price :text="???" 此处参数应为金额数字') } else { let arr = parseFloat(this.price).toFixed(2).split('.') array.push(arr[0]) diff --git a/yudao-ui-app/pages/index/index.vue b/yudao-ui-app/pages/index/index.vue index 137de3b22..202a7e72a 100644 --- a/yudao-ui-app/pages/index/index.vue +++ b/yudao-ui-app/pages/index/index.vue @@ -28,7 +28,7 @@ <!--商品展示栏--> <yd-product-box :product-list="productList" :title="'每日上新'" show-type="normal"></yd-product-box> <yd-product-box :product-list="productList" :title="'热卖商品'" show-type="half"></yd-product-box> - <yd-product-more :product-list="productList" more-status="moreStatus"></yd-product-more> + <yd-product-more :product-list="productList" :more-status="moreStatus"></yd-product-more> <u-gap height="5px"></u-gap> </view> From 49fe926cb86bd3714d7d12a160212a0d5ae7440b Mon Sep 17 00:00:00 2001 From: sfmind <130201237@qq.com> Date: Wed, 23 Nov 2022 00:21:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?uni-app=E8=B0=83=E6=95=B4vuex=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-ui-app/store/getters.js | 6 ++ yudao-ui-app/store/index.js | 86 ++---------------------- yudao-ui-app/store/mudules/user.js | 101 +++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 81 deletions(-) create mode 100644 yudao-ui-app/store/getters.js create mode 100644 yudao-ui-app/store/mudules/user.js diff --git a/yudao-ui-app/store/getters.js b/yudao-ui-app/store/getters.js new file mode 100644 index 000000000..720a48620 --- /dev/null +++ b/yudao-ui-app/store/getters.js @@ -0,0 +1,6 @@ +const getters = { + accessToken: state => state.user.accessToken, + userInfo: state => state.user.userInfo, + hasLogin: state => !!state.user.accessToken +} +export default getters diff --git a/yudao-ui-app/store/index.js b/yudao-ui-app/store/index.js index 9eed31c90..e46138835 100644 --- a/yudao-ui-app/store/index.js +++ b/yudao-ui-app/store/index.js @@ -1,91 +1,15 @@ import Vue from 'vue' import Vuex from 'vuex' -import { logout } from '@/api/auth' -import { getUserInfo } from '@/api/user' -import { passwordLogin, smsLogin, weixinMiniAppLogin } from '@/api/auth' - -const AccessTokenKey = 'ACCESS_TOKEN' -const RefreshTokenKey = 'REFRESH_TOKEN' +import user from './mudules/user' +import getters from './getters' Vue.use(Vuex) // vue的插件机制 // Vuex.Store 构造器选项 const store = new Vuex.Store({ - state: { - accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌 - refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌 - userInfo: {} + modules: { + user }, - getters: { - accessToken: state => state.accessToken, - refreshToken: state => state.refreshToken, - userInfo: state => state.userInfo, - hasLogin: state => !!state.accessToken - }, - mutations: { - // 更新 state 的通用方法 - SET_STATE_ATTR(state, param) { - if (param instanceof Array) { - for (let item of param) { - state[item.key] = item.val - } - } else { - state[param.key] = param.val - } - }, - // 更新令牌 - SET_TOKEN(state, data) { - // 设置令牌 - const { accessToken, refreshToken } = data - state.accessToken = accessToken - state.refreshToken = refreshToken - uni.setStorageSync(AccessTokenKey, accessToken) - uni.setStorageSync(RefreshTokenKey, refreshToken) - - // 加载用户信息 - this.dispatch('ObtainUserInfo') - }, - // 更新用户信息 - SET_USER_INFO(state, data) { - state.userInfo = data - }, - // 清空令牌 和 用户信息 - CLEAR_LOGIN_INFO(state) { - uni.removeStorageSync(AccessTokenKey) - uni.removeStorageSync(RefreshTokenKey) - state.accessToken = '' - state.refreshToken = '' - state.userInfo = {} - } - }, - actions: { - //账号登录 - Login({ state, commit }, { type, data }) { - if (type === 0) { - return passwordLogin(data).then(res => { - commit('SET_TOKEN', res.data) - }) - } else if (type === 1) { - return smsLogin(data).then(res => { - commit('SET_TOKEN', res.data) - }) - } else { - return weixinMiniAppLogin(data).then(res => { - commit('SET_TOKEN', res.data) - }) - } - }, - // 退出登录 - async Logout({ state, commit }) { - commit('CLEAR_LOGIN_INFO') - await logout() - }, - // 获得用户基本信息 - async ObtainUserInfo({ state, commit }) { - const res = await getUserInfo() - commit('SET_USER_INFO', res.data) - } - } + getters }) - export default store diff --git a/yudao-ui-app/store/mudules/user.js b/yudao-ui-app/store/mudules/user.js new file mode 100644 index 000000000..5784dfda7 --- /dev/null +++ b/yudao-ui-app/store/mudules/user.js @@ -0,0 +1,101 @@ +import { getUserInfo } from '@/api/user' +import { passwordLogin, smsLogin, weixinMiniAppLogin, logout } from '@/api/auth' + +const AccessTokenKey = 'ACCESS_TOKEN' +const RefreshTokenKey = 'REFRESH_TOKEN' + +const user = { + state: { + accessToken: uni.getStorageSync(AccessTokenKey), // 访问令牌 + refreshToken: uni.getStorageSync(RefreshTokenKey), // 刷新令牌 + userInfo: {} + }, + mutations: { + // 更新 state 的通用方法 + SET_STATE_ATTR(state, param) { + if (param instanceof Array) { + for (let item of param) { + state[item.key] = item.val + } + } else { + state[param.key] = param.val + } + }, + // 更新令牌 + SET_TOKEN(state, data) { + // 设置令牌 + const { accessToken, refreshToken } = data + state.accessToken = accessToken + state.refreshToken = refreshToken + uni.setStorageSync(AccessTokenKey, accessToken) + uni.setStorageSync(RefreshTokenKey, refreshToken) + + // 加载用户信息 + this.dispatch('ObtainUserInfo') + }, + // 更新用户信息 + SET_USER_INFO(state, data) { + state.userInfo = data + }, + // 清空令牌 和 用户信息 + CLEAR_LOGIN_INFO(state) { + uni.removeStorageSync(AccessTokenKey) + uni.removeStorageSync(RefreshTokenKey) + state.accessToken = '' + state.refreshToken = '' + state.userInfo = {} + } + }, + actions: { + //账号登录 + Login({ state, commit }, { type, data }) { + if (type === 0) { + return passwordLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + } else if (type === 1) { + return smsLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + } else { + return weixinMiniAppLogin(data) + .then(res => { + commit('SET_TOKEN', res.data) + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + } + }, + // 退出登录 + Logout({ state, commit }) { + return logout() + .then(res => { + return Promise.resolve(res) + }) + .catch(err => { + return Promise.reject(err) + }) + .finally(() => { + commit('CLEAR_LOGIN_INFO') + }) + }, + // 获得用户基本信息 + async ObtainUserInfo({ state, commit }) { + const res = await getUserInfo() + commit('SET_USER_INFO', res.data) + } + } +} +export default user