浏览代码

封装弹窗组件default和toast类型

seimin 3 年之前
父节点
当前提交
d5c26ca806
共有 15 个文件被更改,包括 427 次插入55 次删除
  1. 4 0
      .prettierrc
  2. 18 0
      .vscode/settings.json
  3. 35 27
      App.vue
  4. 145 0
      components/seiminModel/seiminModel.vue
  5. 16 4
      main.js
  6. 28 0
      package-lock.json
  7. 19 0
      package.json
  8. 19 23
      pages/index/index.vue
  9. 0 0
      readme.md
  10. 18 0
      request/config.js
  11. 6 0
      request/demo.js
  12. 39 0
      request/index.js
  13. 31 0
      store/index.js
  14. 25 0
      store/modules/demo.js
  15. 24 1
      uni.scss

+ 4 - 0
.prettierrc

@@ -0,0 +1,4 @@
1
+{
2
+  "semi": true,
3
+  "endOfLine": "auto"
4
+}

+ 18 - 0
.vscode/settings.json

@@ -0,0 +1,18 @@
1
+{
2
+  "editor.formatOnSave": true,
3
+  "[vue]": {
4
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
5
+  },
6
+  "[html]": {
7
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
8
+  },
9
+  "[javascript]": {
10
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
11
+  },
12
+  "[typescript]": {
13
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
14
+  },
15
+  "[jsonc]": {
16
+    "editor.defaultFormatter": "esbenp.prettier-vscode"
17
+  }
18
+}

+ 35 - 27
App.vue

@@ -1,32 +1,40 @@
1 1
 <script>
2
-	export default {
3
-		onLaunch: function() {
4
-			console.warn('当前组件仅支持 uni_modules 目录结构 ,请升级 HBuilderX 到 3.1.0 版本以上!')
5
-			console.log('App Launch')
6
-		},
7
-		onShow: function() {
8
-			console.log('App Show')
9
-		},
10
-		onHide: function() {
11
-			console.log('App Hide')
12
-		}
13
-	}
2
+import { mapActions } from "vuex";
3
+export default {
4
+  methods: {
5
+    ...mapActions("demo", ["getData"]),
6
+  },
7
+  onLaunch: function () {
8
+    console.log("App Launch");
9
+  },
10
+  onShow: function () {
11
+    console.log("App Show");
12
+  },
13
+  onHide: function () {
14
+    console.log("App Hide");
15
+  },
16
+  mounted() {
17
+    this.getData().then((res) => {
18
+      console.log(res);
19
+    });
20
+  },
21
+};
14 22
 </script>
15 23
 
16 24
 <style lang="scss">
17
-	/*每个页面公共css */
18
-	@import '@/uni_modules/uni-scss/index.scss';
19
-	/* #ifndef APP-NVUE */
20
-	@import '@/static/customicons.css';
21
-	// 设置整个项目的背景色
22
-	page {
23
-		background-color: #f5f5f5;
24
-	}
25
-
26
-	/* #endif */
27
-	.example-info {
28
-		font-size: 14px;
29
-		color: #333;
30
-		padding: 10px;
31
-	}
25
+/*每个页面公共css */
26
+// @import "@/uni_modules/uni-scss/index.scss";
27
+// 设置整个项目的样式
28
+page {
29
+  background-color: #f9fafb;
30
+  * {
31
+    box-sizing: border-box;
32
+  }
33
+  .red {
34
+    color: $textColorRed;
35
+  }
36
+  .green {
37
+    color: $defaultColor;
38
+  }
39
+}
32 40
 </style>

+ 145 - 0
components/seiminModel/seiminModel.vue

@@ -0,0 +1,145 @@
1
+<!--
2
+ * @Author: 廖明明
3
+ * @Date: 2022-04-01 17:11:19
4
+ * @LastEditors: 廖明明
5
+ * @LastEditTime: 2022-04-02 17:03:14
6
+ * @Description: 自定义弹窗组件
7
+-->
8
+<template>
9
+  <view class="seiminModel seiminModel_mask" v-if="opts.isVisible">
10
+    <view
11
+      class="seiminModel_container animate__animated animate__fadeIn animate__faster"
12
+    >
13
+      <view class="seiminModel_header">{{ opts.title }}</view>
14
+      <view class="seiminModel_content" v-html="opts.content"></view>
15
+      <view class="seiminModel_footer">
16
+        <view
17
+          class="seiminModel_footer__btn"
18
+          v-for="(btn, i) in opts.btns"
19
+          :style="{
20
+            flex: btn.flex,
21
+            color: btn.textColor,
22
+          }"
23
+          @click="btn.click($event)"
24
+          :key="i"
25
+          >{{ btn.name }}</view
26
+        >
27
+      </view>
28
+    </view>
29
+  </view>
30
+</template>
31
+
32
+<script>
33
+export default {
34
+  name: "seiminModel",
35
+  data() {
36
+    return {
37
+      // 配置项
38
+      opts: {},
39
+    };
40
+  },
41
+  methods: {
42
+    // 显示弹窗
43
+    show(args = {}) {
44
+      // 默认配置项
45
+      let defaultOptions = {
46
+        skin: "default", //弹窗风格(default|toast|)
47
+        isVisible: false, //是否显示弹窗
48
+        title: "提示", //标题
49
+        content: "", //内容
50
+        btns: [
51
+          { name: "取消", textColor: "#666", flex: 1, click: this.close },
52
+          { name: "确定", textColor: "#666", flex: 1, click: this.close },
53
+        ], //弹窗按钮
54
+      };
55
+      // 根据弹窗风格修改默认配置项
56
+      switch (args.skin) {
57
+        case "toast":
58
+          defaultOptions.btns = [
59
+            {
60
+              name: "知道了",
61
+              textColor: "#49b856",
62
+              flex: 1,
63
+              click: this.close,
64
+            },
65
+          ];
66
+          break;
67
+      }
68
+      // 合并配置
69
+      this.opts = Object.assign({}, defaultOptions, args, {
70
+        isVisible: true,
71
+      });
72
+    },
73
+    // 关闭弹窗
74
+    close() {
75
+      this.opts.isVisible = false;
76
+    },
77
+  },
78
+};
79
+</script>
80
+
81
+<style lang="scss" scoped>
82
+.seiminModel {
83
+  font-size: 36rpx;
84
+  color: #000;
85
+  line-height: 50rpx;
86
+  text-align: center;
87
+  &.seiminModel_mask {
88
+    background-color: rgba(0, 0, 0, 0.5);
89
+    position: fixed;
90
+    top: 0;
91
+    right: 0;
92
+    bottom: 0;
93
+    left: 0;
94
+    margin: auto;
95
+    @include flex(center, center);
96
+    .seiminModel_container {
97
+      width: 560rpx;
98
+      border-radius: 8rpx;
99
+      background-color: #fff;
100
+      display: flex;
101
+      flex-direction: column;
102
+      align-content: center;
103
+      .seiminModel_header {
104
+        height: 100rpx;
105
+        @include flex(center, center);
106
+      }
107
+      .seiminModel_content {
108
+        flex: 1;
109
+        background-color: #f9fafb;
110
+        margin: 0 36rpx 25rpx;
111
+        border: 1px solid #e5e9ed;
112
+        color: #333;
113
+        padding: 76rpx 24rpx;
114
+        @include numbersAndLettersNoWrap;
115
+        .red {
116
+          color: $textColorRed;
117
+        }
118
+        .green {
119
+          color: $defaultColor;
120
+        }
121
+      }
122
+      .seiminModel_footer {
123
+        border-top: 1px solid #e5e5e5;
124
+        height: 100rpx;
125
+        color: #666;
126
+        @include flex(center, center);
127
+        .seiminModel_footer__btn {
128
+          height: 100%;
129
+          @include flex(center, center);
130
+          position: relative;
131
+          &::after {
132
+            content: "";
133
+            height: 86rpx;
134
+            position: absolute;
135
+            width: 1px;
136
+            bottom: 0;
137
+            right: 0;
138
+            background-color: #dde1e5;
139
+          }
140
+        }
141
+      }
142
+    }
143
+  }
144
+}
145
+</style>

+ 16 - 4
main.js

@@ -1,20 +1,32 @@
1
-
2 1
 // #ifndef VUE3
3 2
 import Vue from 'vue'
4 3
 import App from './App'
4
+// 引入vuex配置
5
+import store from './store'
6
+// 引入自定义全局组件
7
+import seiminModel from './components/seiminModel/seiminModel.vue'
8
+Vue.component('seiminModel', seiminModel)
9
+// 引入request配置
10
+import {
11
+  request
12
+} from './request';
13
+Vue.prototype.$request = request
5 14
 
6 15
 Vue.config.productionTip = false
7 16
 
8 17
 App.mpType = 'app'
9 18
 
10 19
 const app = new Vue({
11
-    ...App
20
+  store,
21
+  ...App
12 22
 })
13 23
 app.$mount()
14 24
 // #endif
15 25
 
16 26
 // #ifdef VUE3
17
-import { createSSRApp } from 'vue'
27
+import {
28
+  createSSRApp
29
+} from 'vue'
18 30
 import App from './App.vue'
19 31
 export function createApp() {
20 32
   const app = createSSRApp(App)
@@ -22,4 +34,4 @@ export function createApp() {
22 34
     app
23 35
   }
24 36
 }
25
-// #endif
37
+// #endif

+ 28 - 0
package-lock.json

@@ -0,0 +1,28 @@
1
+{
2
+  "name": "zy-pda",
3
+  "version": "1.0.0",
4
+  "lockfileVersion": 2,
5
+  "requires": true,
6
+  "packages": {
7
+    "": {
8
+      "name": "zy-pda",
9
+      "version": "1.0.0",
10
+      "license": "ISC",
11
+      "dependencies": {
12
+        "animate.css": "^4.1.1"
13
+      }
14
+    },
15
+    "node_modules/animate.css": {
16
+      "version": "4.1.1",
17
+      "resolved": "https://registry.npmmirror.com/animate.css/-/animate.css-4.1.1.tgz",
18
+      "integrity": "sha512-+mRmCTv6SbCmtYJCN4faJMNFVNN5EuCTTprDTAo7YzIGji2KADmakjVA3+8mVDkZ2Bf09vayB35lSQIex2+QaQ=="
19
+    }
20
+  },
21
+  "dependencies": {
22
+    "animate.css": {
23
+      "version": "4.1.1",
24
+      "resolved": "https://registry.npmmirror.com/animate.css/-/animate.css-4.1.1.tgz",
25
+      "integrity": "sha512-+mRmCTv6SbCmtYJCN4faJMNFVNN5EuCTTprDTAo7YzIGji2KADmakjVA3+8mVDkZ2Bf09vayB35lSQIex2+QaQ=="
26
+    }
27
+  }
28
+}

+ 19 - 0
package.json

@@ -0,0 +1,19 @@
1
+{
2
+  "name": "zy-pda",
3
+  "version": "1.0.0",
4
+  "description": "",
5
+  "main": "main.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "repository": {
10
+    "type": "git",
11
+    "url": "http://git.dashitech.com/seimin/zy-pda.git"
12
+  },
13
+  "keywords": [],
14
+  "author": "",
15
+  "license": "ISC",
16
+  "dependencies": {
17
+    "animate.css": "^4.1.1"
18
+  }
19
+}

+ 19 - 23
pages/index/index.vue

@@ -1,29 +1,25 @@
1 1
 <template>
2
-	<view class="container">
3
-		
4
-		<view class="intro">本项目已包含uni ui组件,无需import和注册,可直接使用。在代码区键入字母u,即可通过代码助手列出所有可用组件。光标置于组件名称处按F1,即可查看组件文档。</view>
5
-		<text class="intro">详见:</text>
6
-		<uni-link :href="href" :text="href"></uni-link>
7
-	</view>
2
+  <view class="container">
3
+    这是测试的一行文字
4
+    <button type="default" @click="clickHandler()">点击弹窗</button>
5
+    <seiminModel ref="seiminModel"></seiminModel>
6
+  </view>
8 7
 </template>
9 8
 
10 9
 <script>
11
-	export default {
12
-		data() {
13
-			return {
14
-				href: 'https://uniapp.dcloud.io/component/README?id=uniui'
15
-			}
16
-		},
17
-		methods: {
18
-
19
-		}
20
-	}
10
+export default {
11
+  data() {
12
+    return {};
13
+  },
14
+  methods: {
15
+    clickHandler() {
16
+      this.$refs.seiminModel.show({
17
+        skin: "toast",
18
+        content: "你还好吗?",
19
+      });
20
+    },
21
+  },
22
+};
21 23
 </script>
22 24
 
23
-<style>
24
-	.container {
25
-		padding: 20px;
26
-		font-size: 14px;
27
-		line-height: 24px;
28
-	}
29
-</style>
25
+<style></style>

+ 0 - 0
readme.md


+ 18 - 0
request/config.js

@@ -0,0 +1,18 @@
1
+/*
2
+ * @Author: 廖明明
3
+ * @Date: 2022-03-31 15:12:00
4
+ * @LastEditors: 廖明明
5
+ * @LastEditTime: 2022-04-02 14:29:15
6
+ * @Description: 后端服务配置文件
7
+ */
8
+//配置项
9
+const config = {
10
+  baseUrl: "", // 后端服务地址
11
+};
12
+// 后端服务地址
13
+const baseUrls = {
14
+  development: "http://itsmpoc.dashitech.com/service", //开发环境
15
+  production: "http://itsmpoc.dashitech.com/service", //生产环境
16
+};
17
+config.baseUrl = baseUrls[process.env.NODE_ENV];
18
+export default config;

+ 6 - 0
request/demo.js

@@ -0,0 +1,6 @@
1
+import { request } from "./index.js";
2
+// 测试接口
3
+export const reqGetData = () =>
4
+  request({
5
+    url: "/auth/getSysName",
6
+  });

+ 39 - 0
request/index.js

@@ -0,0 +1,39 @@
1
+/*
2
+ * @Author: 廖明明
3
+ * @Date: 2022-03-30 15:54:48
4
+ * @LastEditors: 廖明明
5
+ * @LastEditTime: 2022-04-02 14:27:53
6
+ * @Description:后端服务入口文件
7
+ */
8
+import config from "./config.js";
9
+/**
10
+ * @description:封装请求函数
11
+ * @param {string} url 接口地址,以”/“开头
12
+ * @param {object} data 请求参数
13
+ * @param {string} method 请求类型
14
+ * @param {object} header 请求头
15
+ * @return {promise}
16
+ * @author: 廖明明
17
+ */
18
+export function request({ url = "", data = {}, method = "GET", header = {} }) {
19
+  return new Promise((resolve) => {
20
+    uni.request({
21
+      method,
22
+      url: config.baseUrl + url,
23
+      data,
24
+      header,
25
+      dataType: "json",
26
+      success(res) {
27
+        console.log(res);
28
+        resolve(res.data);
29
+      },
30
+      fail(err) {
31
+        console.log(err);
32
+        uni.showToast({
33
+          icon: "none",
34
+          title: "请求失败!",
35
+        });
36
+      },
37
+    });
38
+  });
39
+}

+ 31 - 0
store/index.js

@@ -0,0 +1,31 @@
1
+/*
2
+ * @Author: 廖明明
3
+ * @Date: 2022-03-31 16:36:19
4
+ * @LastEditors: 廖明明
5
+ * @LastEditTime: 2022-04-01 17:45:03
6
+ * @Description:vuex入口文件
7
+ */
8
+import Vue from "vue";
9
+import Vuex from "vuex";
10
+
11
+import demo from "@/store/modules/demo";
12
+
13
+Vue.use(Vuex);
14
+
15
+const myPlugin = (store) => {
16
+  // 当 store 初始化后调用
17
+  let vuexStorage = uni.getStorageSync("vuexStorage");
18
+  if (vuexStorage) {
19
+    store.replaceState(vuexStorage);
20
+  }
21
+  store.subscribe((mutation, state) => {
22
+    uni.setStorageSync("vuexStorage", state);
23
+  });
24
+};
25
+
26
+export default new Vuex.Store({
27
+  modules: {
28
+    demo,
29
+  },
30
+  plugins: [myPlugin],
31
+});

+ 25 - 0
store/modules/demo.js

@@ -0,0 +1,25 @@
1
+import { reqGetData } from "@/request/demo.js";
2
+const state = {
3
+  dataList: [],
4
+};
5
+const getters = {};
6
+const mutations = {
7
+  // 获取三级导航
8
+  getData(state, dataList) {
9
+    state.dataList = dataList;
10
+  },
11
+};
12
+const actions = {
13
+  async getData({ commit, state }) {
14
+    console.log(state);
15
+    let result = await reqGetData();
16
+    commit("getData", result.data);
17
+  },
18
+};
19
+export default {
20
+  namespaced: true,
21
+  state,
22
+  getters,
23
+  mutations,
24
+  actions,
25
+};

+ 24 - 1
uni.scss

@@ -1 +1,24 @@
1
-@import '@/uni_modules/uni-scss/variables.scss';
1
+@import "@/uni_modules/uni-scss/variables.scss";
2
+@import "animate.css/animate.css";
3
+// 主题色
4
+$defaultColor: #49b856;
5
+// 红色
6
+$textColorRed: #ff3b53;
7
+//数字字母不换行
8
+@mixin numbersAndLettersNoWrap {
9
+  word-break: break-all;
10
+}
11
+//flex布局
12
+// $justifyContent 主轴对齐方式
13
+// $alignItem 交叉轴对齐方式
14
+// $flexDirection 方向
15
+@mixin flex(
16
+  $justifyContent: flex-start,
17
+  $alignItem: stretch,
18
+  $flexDirection: row
19
+) {
20
+  display: flex;
21
+  justify-content: $justifyContent;
22
+  align-items: $alignItem;
23
+  flex-direction: $flexDirection;
24
+}