mali-applet-ui 0.0.39 → 0.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
  <input
3
3
  class="ml-amount-input"
4
4
  v-model="inpVal"
5
- type="text"
5
+ type="digit"
6
6
  :class="[{'is-right': align === 'right', 'is-border': border}, className]"
7
7
  autocomplete="off"
8
8
  :placeholder="placeholder"
@@ -31,9 +31,9 @@ export default {
31
31
  }
32
32
  },
33
33
  methods: {
34
- changeEvent () {
35
- this.$emit('input', this.selectVal)
36
- this.$emit('change', { value: this.selectVal })
34
+ changeEvent (value) {
35
+ this.$emit('input', value)
36
+ this.$emit('change', { value })
37
37
  }
38
38
  }
39
39
  }
@@ -31,9 +31,9 @@ export default {
31
31
  }
32
32
  },
33
33
  methods: {
34
- changeEvent () {
35
- this.$emit('input', this.selectVal)
36
- this.$emit('change', { value: this.selectVal })
34
+ changeEvent (value) {
35
+ this.$emit('input', value)
36
+ this.$emit('change', { value })
37
37
  }
38
38
  }
39
39
  }
@@ -32,8 +32,8 @@ export default {
32
32
  }
33
33
  },
34
34
  methods: {
35
- changeEvent () {
36
- const [startDate, endDate] = this.selectDates
35
+ changeEvent (value) {
36
+ const [startDate, endDate] = value
37
37
  this.$emit('update:startDate', startDate)
38
38
  this.$emit('update:endDate', endDate)
39
39
  this.$emit('change', { startDate, endDate })
@@ -1,7 +1,14 @@
1
1
  <template>
2
2
  <view class="ml-form" :class="[className || '']">
3
3
  <uni-forms :key="fKey" ref="uniForm" :modelValue="data" :rules="formRules" :label-position="vertical ? 'top' : 'left'">
4
- <slot></slot>
4
+ <slot>
5
+ <view v-for="(item, index) in items" :key="index">
6
+ <ml-form-group v-if="item.children && item.children.length">
7
+ <ml-form-item v-for="(cItem, cIndex) in item.children" :key="cIndex" :title="cItem.title" :field="cItem.field" :itemRender="cItem.itemRender" @modelvalue="updateModel"></ml-form-item>
8
+ </ml-form-group>
9
+ <ml-form-item v-else :title="item.title" :field="item.field" :itemRender="item.itemRender" @modelvalue="updateModel"></ml-form-item>
10
+ </view>
11
+ </slot>
5
12
  </uni-forms>
6
13
  </view>
7
14
  </template>
@@ -15,9 +22,11 @@ export default {
15
22
  virtualHost: true
16
23
  },
17
24
  props: {
25
+ value: Object,
18
26
  data: Object,
19
27
  items: Array,
20
28
  rules: Object,
29
+ readonly: Boolean,
21
30
  vertical: Boolean,
22
31
  labelWidth: String,
23
32
  className: String
@@ -51,8 +60,8 @@ export default {
51
60
  })
52
61
  },
53
62
  methods: {
54
- matchComponent (item, name) {
55
- return item.itemRender && item.itemRender.name === name
63
+ getProp (item, prop, defultValue) {
64
+ return (item.itemRender.props ? item.itemRender.props[prop] : null) || defultValue
56
65
  },
57
66
  validate () {
58
67
  return this.$refs.uniForm.validate().then(() => {
@@ -67,6 +76,9 @@ export default {
67
76
  }).catch(() => {
68
77
  return {}
69
78
  })
79
+ },
80
+ updateModel ({ field, value}) {
81
+ this.$emit('input', { ...this.value, [field]: value })
70
82
  }
71
83
  }
72
84
  }
@@ -1,13 +1,21 @@
1
1
  <template>
2
- <uni-forms-item
3
- ref="uniFormItem"
4
- :class="className"
5
- :label="title"
6
- :name="field"
7
- :label-width="itemLabelWidth"
8
- :label-position="itemVertical">
9
- <slot></slot>
10
- </uni-forms-item>
2
+ <view class="ml-form-item">
3
+ <uni-forms-item
4
+ ref="uniFormItem"
5
+ :class="className"
6
+ :label="title"
7
+ :name="field"
8
+ :label-width="itemLabelWidth"
9
+ :label-position="itemVertical">
10
+ <slot>
11
+ <ml-input v-if="matchComponent('MlInput')" :value="itemValue" border @change="modelEvent"></ml-input>
12
+ <ml-date-picker v-else-if="matchComponent('MlDatePicker')" :value="itemValue" border @change="modelEvent"></ml-date-picker>
13
+ <ml-select-picker v-else-if="matchComponent('MlSelectPicker')" :value="itemValue" :options="renderOpts.options" border @change="modelEvent"></ml-select-picker>
14
+ <ml-textarea v-else-if="matchComponent('MlTextarea')" :value="itemValue" :rows="renderOpts.rows || 3" border @change="modelEvent"></ml-textarea>
15
+ <view v-else>{{ itemValue }}</view>
16
+ </slot>
17
+ </uni-forms-item>
18
+ </view>
11
19
  </template>
12
20
 
13
21
  <script>
@@ -24,6 +32,7 @@ export default {
24
32
  default: null
25
33
  },
26
34
  labelWidth: String,
35
+ itemRender: Object,
27
36
  className: String
28
37
  },
29
38
  inject: {
@@ -33,6 +42,15 @@ export default {
33
42
  }
34
43
  },
35
44
  computed: {
45
+ itemValue () {
46
+ return this.formData[this.field]
47
+ },
48
+ formData () {
49
+ return (this.form ? this.form.value : {}) || {}
50
+ },
51
+ renderOpts () {
52
+ return this.itemRender || {}
53
+ },
36
54
  itemVertical () {
37
55
  let vertical = this.vertical
38
56
  if (!vertical) {
@@ -64,7 +82,13 @@ export default {
64
82
  this.$nextTick(() => {
65
83
  this.$refs.uniFormItem.localLabelPos = this.itemVertical
66
84
  })
85
+ },
86
+ matchComponent (name) {
87
+ return this.renderOpts.name === name
88
+ },
89
+ modelEvent (params) {
90
+ this.$emit('modelvalue', { field: this.field, ...params })
67
91
  }
68
92
  }
69
93
  }
70
- </script>
94
+ </script>
@@ -1,7 +1,7 @@
1
1
  <template>
2
2
  <view class="ml-input" :class="[className || '', {'is-right': align === 'right', 'is-border': border, 'is-prefix': prefixIcon || type === 'search', 'is-suffix': suffixIcon, 'is-disabled': disabled}]">
3
3
  <view v-if="prefixIcon || type === 'search'" class="ml-input-prefix-icon">
4
- <ml-icon v-if="prefixIcon" :class="prefixIcon"></ml-icon>
4
+ <ml-icon v-if="prefixIcon" :className="prefixIcon"></ml-icon>
5
5
  <ml-icon v-else name="search"></ml-icon>
6
6
  </view>
7
7
  <input
@@ -13,7 +13,7 @@
13
13
  @input="inputEvent"
14
14
  @confirm="confirmEvent"/>
15
15
  <view v-if="suffixIcon" class="ml-input-suffix-icon">
16
- <ml-icon :class="suffixIcon"></ml-icon>
16
+ <ml-icon :className="suffixIcon"></ml-icon>
17
17
  </view>
18
18
  </view>
19
19
  </template>
@@ -2,7 +2,7 @@
2
2
  <input
3
3
  class="ml-number-input"
4
4
  v-model="inpVal"
5
- type="text"
5
+ :type="type === 'integer' ? 'number' : 'digit'"
6
6
  :class="[className || '', {'is-right': align === 'right', 'is-border': border}]"
7
7
  autocomplete="off"
8
8
  :placeholder="placeholder"
@@ -21,6 +21,7 @@ export default {
21
21
  value: String,
22
22
  align: String,
23
23
  border: Boolean,
24
+ type: String,
24
25
  maxLength: {
25
26
  type: Number,
26
27
  default: 12
@@ -67,11 +67,12 @@ export default {
67
67
  },
68
68
  methods: {
69
69
  inputEvent () {
70
- if (this.maxLength && this.inpVal && this.inpVal.length > this.maxLength) {
71
- this.$emit('input', this.inpVal.slice(0, this.maxLength))
72
- } else {
73
- this.$emit('input', this.inpVal)
70
+ let value = this.inpVal
71
+ if (this.maxLength && value &&value.length > this.maxLength) {
72
+ value = value.slice(0, this.maxLength)
74
73
  }
74
+ this.$emit('input', value)
75
+ this.$emit('change', { value })
75
76
  }
76
77
  }
77
78
  }
@@ -0,0 +1,377 @@
1
+ <template>
2
+ <view class="ml-upload" :class="className">
3
+ <view v-if="readonly">
4
+ <view v-if="mediatype === 'image'" class="ml-upload-image-list">
5
+ <view class="ml-upload-file-item" v-for="(item, index) in value" :key="index">
6
+ <image class="ml-upload-file-img" mode="aspectFill" :src="getUrl(item)" @click="previewEvent(item, index)"></image>
7
+ </view>
8
+ </view>
9
+ <view v-else class="ml-upload-file-list">
10
+ <view class="ml-upload-file-item" v-for="(item, index) in value" :key="index">
11
+ <view class="ml-upload-file-name" @click="downloadEvent(item)">{{ item[nameField] }}</view>
12
+ </view>
13
+ </view>
14
+ </view>
15
+ <uni-file-picker
16
+ v-else
17
+ v-model="selectUniFileList"
18
+ :limit="limit"
19
+ file-mediatype="image"
20
+ :image-styles="imageStyles"
21
+ :auto-upload="false"
22
+ @select="selectFile"
23
+ @delete="removeEvent">
24
+ </uni-file-picker>
25
+ </view>
26
+ </template>
27
+
28
+ <script>
29
+ import XEUtils from 'xe-utils'
30
+ import globalStore from '../../config/store'
31
+
32
+ export default {
33
+ name: 'MlUpload',
34
+ options: {
35
+ virtualHost: true
36
+ },
37
+ props: {
38
+ value: {
39
+ type: Array,
40
+ default: () => ([])
41
+ },
42
+ urlList: {
43
+ type: Array,
44
+ default: () => ([])
45
+ },
46
+ simpleValue: {
47
+ type: Boolean,
48
+ default: false
49
+ },
50
+ readonly: {
51
+ type: Boolean,
52
+ default: false
53
+ },
54
+ limit: {
55
+ type: Number,
56
+ default: 9
57
+ },
58
+ className: String,
59
+ mediatype: {
60
+ type: String,
61
+ default: 'all'
62
+ },
63
+ params: {
64
+ type: Object,
65
+ default: () => ({})
66
+ },
67
+ fileProps: {
68
+ type: Object,
69
+ default: () => ({})
70
+ },
71
+ getFileUrl: Function,
72
+ uploadFile: Function,
73
+ removeFile: Function
74
+ },
75
+ data () {
76
+ return {
77
+ fileList: [],
78
+ selectUniFileList: [],
79
+ imageStyles: {
80
+ // "height": 60, // 边框高度
81
+ // "width": 60, // 边框宽度
82
+ // "border":{ // 如果为 Boolean 值,可以控制边框显示与否
83
+ // "color":"transparent", // 边框颜色
84
+ // "width":"1px", // 边框宽度
85
+ // "style":"solid", // 边框样式
86
+ // }
87
+ },
88
+ imgTypes: ['png', 'jpg']
89
+ }
90
+ },
91
+ computed: {
92
+ imageList () {
93
+ return this.value ? this.value.filter(item => this.imgTypes.includes(item[this.typeField])) : []
94
+ },
95
+ fieldMaps () {
96
+ return { ...globalStore.upload.fileProps, ...this.fileProps }
97
+ },
98
+ urlField () {
99
+ return this.fieldMaps.fileUrl || 'fileUrl'
100
+ },
101
+ typeField () {
102
+ return this.fieldMaps.fileType || 'fileType'
103
+ },
104
+ nameField () {
105
+ return this.fieldMaps.fileName || 'fileName'
106
+ }
107
+ },
108
+ watch: {
109
+ value (val) {
110
+ if (!this.isUpdate) {
111
+ this.isUpdate = true
112
+ this.loadList()
113
+ }
114
+ },
115
+ urlList () {
116
+ this.loadList()
117
+ }
118
+ },
119
+ created () {
120
+ this.loadList()
121
+ },
122
+ methods: {
123
+ loadList () {
124
+ if (this.simpleValue) {
125
+ this.fileList = this.urlList.map(url => {
126
+ const name = this.getUrlName(url)
127
+ return {
128
+ id: null,
129
+ [this.nameField]: name,
130
+ [this.typeField]: this.getSuffix(name),
131
+ [this.urlField]: url,
132
+ tempPath: url
133
+ }
134
+ })
135
+ this.selectUniFileList = this.urlList.map(url => {
136
+ const name = this.getUrlName(url)
137
+ return {
138
+ name: name,
139
+ extname: this.getSuffix(name),
140
+ url: url
141
+ }
142
+ })
143
+ this.isUpdate = true
144
+ this.$emit('input', this.fileList)
145
+ } else {
146
+ this.fileList = this.value.map(item => {
147
+ item.tempPath = item[this.urlField]
148
+ return item
149
+ })
150
+ this.selectUniFileList = this.value.map(item => {
151
+ return {
152
+ name: item[this.nameField],
153
+ extname: item[this.typeField],
154
+ url: item[this.urlField]
155
+ }
156
+ })
157
+ }
158
+ },
159
+ updateModel () {
160
+ this.$emit('update:urlList', this.fileList.map(item => item[this.urlField]))
161
+ this.$emit('input', this.fileList)
162
+ },
163
+ async selectFile (e) {
164
+ const { tempFiles } = e
165
+ uni.showLoading({
166
+ title: "上传中..."
167
+ });
168
+ const uploadMethod = this.uploadFile ? this.uploadFile : (globalStore.upload ? globalStore.upload.uploadFile : null)
169
+ Promise.all(
170
+ tempFiles.map(file => {
171
+ const fileSuffix = this.getSuffix(file.name)
172
+ if (uploadMethod) {
173
+ return Promise.resolve(
174
+ uploadMethod({
175
+ file,
176
+ params: this.params
177
+ })
178
+ ).then(res => {
179
+ const item = {
180
+ ...res,
181
+ [this.nameField]: res[this.nameField] || file.name,
182
+ [this.typeField]: res[this.typeField] || fileSuffix,
183
+ tempPath: file.path
184
+ }
185
+ this.selectUniFileList.push(file)
186
+ this.fileList.push(item)
187
+ this.isUpdate = true
188
+ this.updateModel()
189
+ })
190
+ } else {
191
+ const item = {
192
+ [this.nameField]: file.name,
193
+ [this.typeField]: file.extname,
194
+ [this.urlField]: file.path,
195
+ tempPath: file.path
196
+ }
197
+ this.selectUniFileList.push(file)
198
+ this.fileList.push(item)
199
+ this.isUpdate = true
200
+ this.updateModel()
201
+ }
202
+ })
203
+ ).then(res => {
204
+ uni.hideLoading();
205
+ }).catch(() => {
206
+ console.error(e)
207
+ uni.hideLoading();
208
+ })
209
+ },
210
+ removeEvent (e) {
211
+ const { tempFilePath } = e
212
+ const item = this.fileList.find(item => item.tempPath === tempFilePath)
213
+ this.selectUniFileList = this.selectUniFileList.filter(item => item.url !== tempFilePath)
214
+ this.fileList = this.fileList.filter(item => item.tempPath !== tempFilePath)
215
+ const removeMethod = this.removeFile ? this.removeFile : (globalStore.upload ? globalStore.upload.removeFile : null)
216
+ if (item && !item.id) {
217
+ if (removeMethod) {
218
+ removeMethod({
219
+ item,
220
+ params: this.params
221
+ })
222
+ }
223
+ }
224
+ this.isUpdate = true
225
+ this.updateModel()
226
+ },
227
+ getUrl (item) {
228
+ const getFileMethod = this.getFileUrl ? this.getFileUrl : (globalStore.upload ? globalStore.upload.getFileUrl : null)
229
+ if (getFileMethod) {
230
+ return getFileMethod({
231
+ item,
232
+ params: this.params
233
+ })
234
+ }
235
+ return item[this.urlField]
236
+ },
237
+ getUrlName (url) {
238
+ const urlRest = XEUtils.parseUrl(url)
239
+ return urlRest.pathname.split('/').slice(-1)[0] || ''
240
+ },
241
+ getSuffix (filename) {
242
+ const pos = filename.lastIndexOf('.')
243
+ let suffix = ''
244
+ if (pos !== -1) {
245
+ suffix = filename.substring(pos + 1)
246
+ }
247
+ return suffix
248
+ },
249
+ previewEvent (item, index) {
250
+ if (this.previewStatus) {
251
+ return
252
+ }
253
+ this.previewStatus = true
254
+ Promise.all(
255
+ this.value.map(item => {
256
+ return new Promise((resolve, reject) => {
257
+ uni.downloadFile({
258
+ url: this.getUrl(item),
259
+ success: (downRes) => {
260
+ resolve({
261
+ url: downRes.tempFilePath
262
+ })
263
+ },
264
+ fail () {
265
+ resolve({
266
+ url: ''
267
+ })
268
+ }
269
+ })
270
+ })
271
+ })
272
+ ).then(res => {
273
+ if (typeof wx === 'object') {
274
+ wx.previewMedia({
275
+ current: index,
276
+ sources: res
277
+ })
278
+ }
279
+ this.previewStatus = false
280
+ })
281
+ },
282
+ previewImg (item) {
283
+ uni.downloadFile({
284
+ url: this.getUrl(item),
285
+ success: (downRes) => {
286
+ uni.previewImage({
287
+ urls: [
288
+ downRes.tempFilePath
289
+ ],
290
+ longPressActions: {
291
+ itemList: ['保存图片']
292
+ }
293
+ })
294
+ }
295
+ })
296
+ },
297
+ downloadEvent (item) {
298
+ uni.downloadFile({
299
+ url: this.getUrl(item),
300
+ success: (downRes) => {
301
+ // 仅支持类型
302
+ if (['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'].includes(item[this.typeField])) {
303
+ uni.openDocument({
304
+ filePath: downRes.tempFilePath,
305
+ showMenu: true,
306
+ success (saveRes) {
307
+ },
308
+ fail () {
309
+ uni.showModal({
310
+ content: '打开失败',
311
+ showCancel: false
312
+ });
313
+ }
314
+ })
315
+ } else if (['png', 'jpg'].includes(item[this.typeField])) {
316
+ this.previewImg(item)
317
+ } else {
318
+ uni.saveFile({
319
+ tempFilePath: downRes.tempFilePath,
320
+ success: function(res) {
321
+ uni.showToast({
322
+ icon: 'none',
323
+ mask: true,
324
+ title: '文件已保存:' + res.savedFilePath, //保存路径
325
+ duration: 3000,
326
+ })
327
+ }
328
+ })
329
+
330
+ }
331
+ },
332
+ fail () {
333
+ uni.showModal({
334
+ content: '下载失败',
335
+ showCancel: false
336
+ });
337
+ }
338
+ })
339
+ }
340
+ }
341
+ }
342
+ </script>
343
+
344
+ <style lang="scss">
345
+ .ml-upload{
346
+ margin: 20rpx 0;
347
+ background: #fff;
348
+ }
349
+ .ml-upload-image-list {
350
+ .ml-upload-file-item {
351
+ display: inline-block;
352
+ width: 210rpx;
353
+ height: 210rpx;
354
+ padding: 0 10rpx;
355
+ }
356
+ .ml-upload-file-img {
357
+ width: 100%;
358
+ height: 100%;
359
+ }
360
+ }
361
+ .ml-upload-file-list {
362
+ display: block;
363
+ .ml-upload-file-item {
364
+ border: 1rpx solid rgba(39, 42, 48, 0.0200);
365
+ background-color: rgba(39, 42, 48, 0.0200);
366
+ margin: 10rpx 0;
367
+ border-radius: 8rpx;
368
+ padding: 20rpx;
369
+ }
370
+ .ml-upload-file-name {
371
+ width: 600rpx;
372
+ overflow: hidden;
373
+ text-overflow: ellipsis;
374
+ white-space: nowrap;
375
+ }
376
+ }
377
+ </style>
@@ -0,0 +1,9 @@
1
+ import globalStore from './store'
2
+ import XEUtils from 'xe-utils'
3
+
4
+ function config (options) {
5
+ XEUtils.merge(globalStore, options)
6
+ return globalStore
7
+ }
8
+
9
+ export default config
@@ -0,0 +1,22 @@
1
+ const globalStore = {
2
+ // 上传组件
3
+ upload: {
4
+ // fileProps: {
5
+ // fileUrl: 'fileUrl', // 文件地址
6
+ // fileType: 'fileType', // 文件类型
7
+ // fileName: 'fileName' // 文件名
8
+ // },
9
+ // 获取附件缩略图方法
10
+ getThumbnailFileUrl: null,
11
+ // 获取附件路径方法
12
+ getFileUrl: null,
13
+ // 上传附件方法
14
+ uploadFile: null,
15
+ // 下载附件方法
16
+ downloadFile: null,
17
+ // 删除附件方法
18
+ removeFile: null
19
+ }
20
+ }
21
+
22
+ export default globalStore
package/index.js CHANGED
@@ -1,6 +1,13 @@
1
- function install (Vue) {
1
+ import config from './config'
2
+ import * as MaliUtils from './utils'
3
+
4
+ const MaliUI = {
5
+ config,
6
+ MaliUtils
7
+ }
8
+
9
+ export {
10
+ MaliUtils
2
11
  }
3
12
 
4
- export default {
5
- install
6
- }
13
+ export default MaliUI
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "mali-applet-ui",
3
- "version": "0.0.39",
3
+ "version": "0.0.42",
4
4
  "description": "码里云小程序组件库",
5
5
  "files": [
6
6
  "components",
7
+ "config",
7
8
  "utils",
8
9
  "iconfont.css",
9
10
  "index.js",
package/utils/date.js ADDED
@@ -0,0 +1,19 @@
1
+ import XEUtils from 'xe-utils'
2
+
3
+ /**
4
+ * 格式化日期
5
+ * @param {*} date
6
+ * @param {*} format
7
+ * @returns
8
+ */
9
+ export function toDateString (date, format = 'yyyy-MM-dd HH:mm:ss') {
10
+ return XEUtils.toDateString(date, format.replace('YYYY', 'yyyy').replace('DD', 'dd'))
11
+ }
12
+
13
+ /**
14
+ * 字符串转日期
15
+ * @param {*} value
16
+ */
17
+ export function toStringDate (value) {
18
+ return XEUtils.toStringDate(value)
19
+ }
package/utils/file.js ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 下载文件
3
+ * @param {*} url
4
+ * @param {*} name
5
+ */
6
+ export function saveFileByUrl (url, name) {
7
+ uni.downloadFile({
8
+ url,
9
+ success: (downRes) => {
10
+ uni.saveFile({
11
+ tempFilePath: downRes.tempFilePath,
12
+ success (res) {
13
+ uni.showModal({
14
+ title: '保存成功',
15
+ content: '路径:' + res.savedFilePath
16
+ })
17
+ }
18
+ })
19
+ }
20
+ })
21
+ }
package/utils/index.js CHANGED
@@ -1,157 +1,3 @@
1
- import XEUtils from 'xe-utils'
2
-
3
- /**
4
- * 格式化日期
5
- * @param {*} date
6
- * @param {*} format
7
- * @returns
8
- */
9
- export function toDateString (date, format = 'yyyy-MM-dd HH:mm:ss') {
10
- return XEUtils.toDateString(date, format.replace('YYYY', 'yyyy').replace('DD', 'dd'))
11
- }
12
-
13
- /**
14
- * 字符串转日期
15
- * @param {*} value
16
- */
17
- export function toStringDate (value) {
18
- return XEUtils.toStringDate(value)
19
- }
20
-
21
- /**
22
- * 金额转成中文
23
- * @param {*} money
24
- * @returns
25
- */
26
- export function toNumMoneyToChinese (money) {
27
- const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] // 汉字的数字
28
- const cnIntRadice = ['', '拾', '佰', '仟'] // 基本单位
29
- const cnIntUnits = ['', '万', '亿', '兆'] // 对应整数部分扩展单位
30
- const cnDecUnits = ['角', '分', '毫', '厘'] // 对应小数部分单位
31
- const cnInteger = '整' // 整数金额时后面跟的字符
32
- const cnIntLast = '元' // 整型完以后的单位
33
- const maxNum = 999999999999999 // 最大处理的数字
34
- let IntegerNum // 金额整数部分
35
- let DecimalNum // 金额小数部分
36
- let ChineseStr = '' // 输出的中文金额字符串
37
- let parts // 分离金额后用的数组,预定义
38
- let Symbol = '' // 正负值标记
39
- if (money === '') {
40
- return ''
41
- }
42
- money = parseFloat(money)
43
- if (money >= maxNum) {
44
- return ''
45
- }
46
- if (money === 0) {
47
- ChineseStr = cnNums[0] + cnIntLast + cnInteger
48
- return ChineseStr
49
- }
50
- if (money < 0) {
51
- money = -money
52
- Symbol = '负 '
53
- }
54
- money = money.toString() // 转换为字符串
55
- if (money.indexOf('.') === -1) {
56
- IntegerNum = money
57
- DecimalNum = ''
58
- } else {
59
- parts = money.split('.')
60
- IntegerNum = parts[0]
61
- DecimalNum = parts[1].substr(0, 4)
62
- }
63
- if (parseInt(IntegerNum, 10) > 0) { // 获取整型部分转换
64
- let zeroCount = 0
65
- const IntLen = IntegerNum.length
66
- for (let i = 0; i < IntLen; i++) {
67
- const n = IntegerNum.substr(i, 1)
68
- const p = IntLen - i - 1
69
- const q = p / 4
70
- const m = p % 4
71
- if (n === '0') {
72
- zeroCount++
73
- } else {
74
- if (zeroCount > 0) {
75
- ChineseStr += cnNums[0]
76
- }
77
- zeroCount = 0 // 归零
78
- ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
79
- }
80
- if (m === 0 && zeroCount < 4) {
81
- ChineseStr += cnIntUnits[q]
82
- }
83
- }
84
- ChineseStr += cnIntLast
85
- // 整型部分处理完毕
86
- }
87
- if (DecimalNum !== '') { // 小数部分
88
- const decLen = DecimalNum.length
89
- for (let i = 0; i < decLen; i++) {
90
- const n = DecimalNum.substr(i, 1)
91
- if (n !== '0') {
92
- ChineseStr += cnNums[Number(n)] + cnDecUnits[i]
93
- }
94
- }
95
- }
96
- if (ChineseStr === '') {
97
- ChineseStr += cnNums[0] + cnIntLast + cnInteger
98
- } else if (DecimalNum === '') {
99
- ChineseStr += cnInteger
100
- }
101
- ChineseStr = Symbol + ChineseStr
102
-
103
- return ChineseStr
104
- }
105
-
106
- /**
107
- * 格式化为金额
108
- * @param {*} value
109
- * @param {*} fixed
110
- */
111
- export function toAmountString (value, fixed = 2) {
112
- return XEUtils.commafy(XEUtils.round(value, fixed), { digits: fixed })
113
- }
114
-
115
- /**
116
- * 格式化为万元单位
117
- * @param {*} value
118
- * @returns
119
- */
120
- export function toWanAmountString (value, fixed = 2) {
121
- return XEUtils.commafy(XEUtils.floor(value ? value / 10000 : 0, fixed), { digits: fixed })
122
- }
123
-
124
- /**
125
- * 四舍五入
126
- */
127
- export function round (num, fixed) {
128
- return XEUtils.round(num, fixed)
129
- }
130
-
131
- /**
132
- * 加法
133
- */
134
- export function add (num1, num2) {
135
- return XEUtils.add(num1, num2)
136
- }
137
-
138
- /**
139
- * 减法
140
- */
141
- export function subtract (num1, num2) {
142
- return XEUtils.subtract(num1, num2)
143
- }
144
-
145
- /**
146
- * 乘法
147
- */
148
- export function multiply (num1, num2) {
149
- return XEUtils.multiply(num1, num2)
150
- }
151
-
152
- /**
153
- * 除法
154
- */
155
- export function divide (num1, num2) {
156
- return XEUtils.divide(num1, num2)
157
- }
1
+ export * from './date'
2
+ export * from './number'
3
+ export * from './file'
@@ -0,0 +1,139 @@
1
+ import XEUtils from 'xe-utils'
2
+
3
+ /**
4
+ * 金额转成中文
5
+ * @param {*} money
6
+ * @returns
7
+ */
8
+ export function toNumMoneyToChinese (money) {
9
+ const cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] // 汉字的数字
10
+ const cnIntRadice = ['', '拾', '佰', '仟'] // 基本单位
11
+ const cnIntUnits = ['', '万', '亿', '兆'] // 对应整数部分扩展单位
12
+ const cnDecUnits = ['角', '分', '毫', '厘'] // 对应小数部分单位
13
+ const cnInteger = '整' // 整数金额时后面跟的字符
14
+ const cnIntLast = '元' // 整型完以后的单位
15
+ const maxNum = 999999999999999 // 最大处理的数字
16
+ let IntegerNum // 金额整数部分
17
+ let DecimalNum // 金额小数部分
18
+ let ChineseStr = '' // 输出的中文金额字符串
19
+ let parts // 分离金额后用的数组,预定义
20
+ let Symbol = '' // 正负值标记
21
+ if (money === '') {
22
+ return ''
23
+ }
24
+ money = parseFloat(money)
25
+ if (money >= maxNum) {
26
+ return ''
27
+ }
28
+ if (money === 0) {
29
+ ChineseStr = cnNums[0] + cnIntLast + cnInteger
30
+ return ChineseStr
31
+ }
32
+ if (money < 0) {
33
+ money = -money
34
+ Symbol = '负 '
35
+ }
36
+ money = money.toString() // 转换为字符串
37
+ if (money.indexOf('.') === -1) {
38
+ IntegerNum = money
39
+ DecimalNum = ''
40
+ } else {
41
+ parts = money.split('.')
42
+ IntegerNum = parts[0]
43
+ DecimalNum = parts[1].substr(0, 4)
44
+ }
45
+ if (parseInt(IntegerNum, 10) > 0) { // 获取整型部分转换
46
+ let zeroCount = 0
47
+ const IntLen = IntegerNum.length
48
+ for (let i = 0; i < IntLen; i++) {
49
+ const n = IntegerNum.substr(i, 1)
50
+ const p = IntLen - i - 1
51
+ const q = p / 4
52
+ const m = p % 4
53
+ if (n === '0') {
54
+ zeroCount++
55
+ } else {
56
+ if (zeroCount > 0) {
57
+ ChineseStr += cnNums[0]
58
+ }
59
+ zeroCount = 0 // 归零
60
+ ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
61
+ }
62
+ if (m === 0 && zeroCount < 4) {
63
+ ChineseStr += cnIntUnits[q]
64
+ }
65
+ }
66
+ ChineseStr += cnIntLast
67
+ // 整型部分处理完毕
68
+ }
69
+ if (DecimalNum !== '') { // 小数部分
70
+ const decLen = DecimalNum.length
71
+ for (let i = 0; i < decLen; i++) {
72
+ const n = DecimalNum.substr(i, 1)
73
+ if (n !== '0') {
74
+ ChineseStr += cnNums[Number(n)] + cnDecUnits[i]
75
+ }
76
+ }
77
+ }
78
+ if (ChineseStr === '') {
79
+ ChineseStr += cnNums[0] + cnIntLast + cnInteger
80
+ } else if (DecimalNum === '') {
81
+ ChineseStr += cnInteger
82
+ }
83
+ ChineseStr = Symbol + ChineseStr
84
+
85
+ return ChineseStr
86
+ }
87
+
88
+ /**
89
+ * 格式化为金额
90
+ * @param {*} value
91
+ * @param {*} fixed
92
+ */
93
+ export function toAmountString (value, fixed = 2) {
94
+ return XEUtils.commafy(XEUtils.round(value, fixed), { digits: fixed })
95
+ }
96
+
97
+ /**
98
+ * 格式化为万元单位
99
+ * @param {*} value
100
+ * @returns
101
+ */
102
+ export function toWanAmountString (value, fixed = 2) {
103
+ return XEUtils.commafy(XEUtils.floor(value ? value / 10000 : 0, fixed), { digits: fixed })
104
+ }
105
+
106
+ /**
107
+ * 四舍五入
108
+ */
109
+ export function round (num, fixed) {
110
+ return XEUtils.round(num, fixed)
111
+ }
112
+
113
+ /**
114
+ * 加法
115
+ */
116
+ export function add (num1, num2) {
117
+ return XEUtils.add(num1, num2)
118
+ }
119
+
120
+ /**
121
+ * 减法
122
+ */
123
+ export function subtract (num1, num2) {
124
+ return XEUtils.subtract(num1, num2)
125
+ }
126
+
127
+ /**
128
+ * 乘法
129
+ */
130
+ export function multiply (num1, num2) {
131
+ return XEUtils.multiply(num1, num2)
132
+ }
133
+
134
+ /**
135
+ * 除法
136
+ */
137
+ export function divide (num1, num2) {
138
+ return XEUtils.divide(num1, num2)
139
+ }