sa-item.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <template>
  2. <!-- 自定义slot -->
  3. <div class="c-item" :class="{br: br}" v-if="$slots.default && type != 'fast-btn'">
  4. <label class="c-label" v-if="name && name.length > 0">{{name}}:</label>
  5. <span v-else-if="name === undefined"></span>
  6. <label class="c-label" v-else></label>
  7. <span v-else></span>
  8. <slot></slot>
  9. </div>
  10. <!-- 普通input -->
  11. <div class="c-item" :class="{br: br}" v-else-if="type == 'text' || type == 'link'">
  12. <label class="c-label">{{name}}:</label>
  13. <el-input type="text" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-input>
  14. </div>
  15. <!-- 数字input -->
  16. <div class="c-item" :class="{br: br}" v-else-if="type == 'num'">
  17. <label class="c-label">{{name}}:</label>
  18. <el-input type="number" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-input>
  19. </div>
  20. <!-- 密码input -->
  21. <div class="c-item" :class="{br: br}" v-else-if="type == 'password'">
  22. <label class="c-label">{{name}}:</label>
  23. <el-input type="password" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-input>
  24. </div>
  25. <!-- 多行文本域 -->
  26. <div class="c-item" :class="{br: br}" v-else-if="type == 'textarea'">
  27. <label class="c-label">{{name}}:</label>
  28. <div style="display: inline-block;">
  29. <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 10}" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-input>
  30. </div>
  31. </div>
  32. <!-- 普通input - 列表 -->
  33. <div class="c-item" :class="{br: br}" v-else-if="type == 'text-list'">
  34. <label class="c-label">{{name}}:</label>
  35. <div class="c-item-mline">
  36. <div v-for="item in value_arr">
  37. <el-input v-model="item.value" @input="value_arr_change"></el-input>
  38. <el-link type="danger" class="del-rr" @click="value_arr_delete(item)">
  39. <i class="el-icon-close"></i>
  40. <small style="vertical-align: top;">删除</small>
  41. </el-link>
  42. </div>
  43. <el-link type="primary" @click="value_arr_push({value: ''})">[ + 添加 ]</el-link>
  44. <span class="c-remark" style="vertical-align: -5%;" v-if="remark">{{remark}}</span>
  45. </div>
  46. </div>
  47. <!-- 钱 money (单位 元) -->
  48. <div class="c-item" :class="{br: br}" v-else-if="type == 'money'">
  49. <label class="c-label">{{name}}:</label>
  50. <el-input type="text" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-input>
  51. <span>元</span>
  52. </div>
  53. <!-- 钱 price-f (单位 分) -->
  54. <div class="c-item" :class="{br: br}" v-else-if="type == 'money-f'">
  55. <label class="c-label">{{name}}:</label>
  56. <el-input type="text" v-model="valueReal" @input="$emit('input', $event * 100)" :placeholder="placeholder" :disabled="disabled"></el-input>
  57. <span>元</span>
  58. </div>
  59. <!-- img -->
  60. <div class="c-item" :class="{br: br}" v-else-if="type == 'img'">
  61. <label class="c-label">{{name}}:</label>
  62. <img :src="value" class="info-img" @click="sa.showImage(value, '400px', '400px')" v-if="!sa.isNull(value)">
  63. <el-link type="primary" @click="sa.uploadImage(src => {$emit('input', src); sa.ok2('上传成功');})">上传</el-link>
  64. </div>
  65. <!-- audio -->
  66. <div class="c-item" :class="{br: br}" v-else-if="type == 'audio'">
  67. <label class="c-label">{{name}}:</label>
  68. <el-link type="info" :href="value" target="_blank" v-if="!sa.isNull(value)">{{value}}</el-link>
  69. <el-link type="primary" @click="sa.uploadAudio(src => {$emit('input', src); sa.ok2('上传成功');})">上传</el-link>
  70. </div>
  71. <!-- video -->
  72. <div class="c-item" :class="{br: br}" v-else-if="type == 'video'">
  73. <label class="c-label">{{name}}:</label>
  74. <el-link type="info" :href="value" target="_blank" v-if="!sa.isNull(value)">{{value}}</el-link>
  75. <el-link type="primary" @click="sa.uploadVideo(src => {$emit('input', src); sa.ok2('上传成功');})">上传</el-link>
  76. </div>
  77. <!-- file -->
  78. <div class="c-item" :class="{br: br}" v-else-if="type == 'file'">
  79. <label class="c-label">{{name}}:</label>
  80. <el-link type="info" :href="value" target="_blank" v-if="!sa.isNull(value)">{{value}}</el-link>
  81. <el-link type="primary" @click="sa.uploadFile(src => {$emit('input', src); sa.ok2('上传成功');})">上传</el-link>
  82. </div>
  83. <!-- img-list -->
  84. <div class="c-item" :class="{br: br}" v-else-if="type == 'img-list'">
  85. <label class="c-label">{{name}}:</label>
  86. <div class="c-item-mline image-box">
  87. <div class="image-box-2" v-for="item in value_arr">
  88. <img :src="item.value" @click="sa.showImage(item.value, '500px', '400px')" />
  89. <p>
  90. <el-link @click="value_arr_delete(item)" style="color: #999;">
  91. <i class="el-icon-close" style="position: relative; top: 2px;"></i>
  92. 移除
  93. </el-link>
  94. </p>
  95. </div>
  96. <!-- 上传图集 -->
  97. <div class="image-box-2 up_img" @click="sa.uploadImageList(src => value_arr_push({value: src}))">
  98. <img src="../../static/img/up-icon.png">
  99. </div>
  100. </div>
  101. </div>
  102. <!-- audio-list、video-list、file-list、img-video-list -->
  103. <div class="c-item" :class="{br: br}" v-else-if="type == 'audio-list' || type == 'video-list' || type == 'file-list' || type == 'img-video-list'">
  104. <label class="c-label">{{name}}:</label>
  105. <div class="c-item-mline">
  106. <div v-for="item in value_arr">
  107. <el-link type="info" :href="item.value" target="_blank">{{item.value}}</el-link>
  108. <el-link type="danger" class="del-rr" @click="value_arr_delete(item)">
  109. <i class="el-icon-close"></i>
  110. <small style="vertical-align: top;">删除</small>
  111. </el-link>
  112. </div>
  113. <el-link type="primary" @click="sa.uploadAudioList(src => value_arr_push({value: src}))" v-if="type == 'audio-list'">上传</el-link>
  114. <el-link type="primary" @click="sa.uploadVideoList(src => value_arr_push({value: src}))" v-if="type == 'video-list'">上传</el-link>
  115. <el-link type="primary" @click="sa.uploadFileList(src => value_arr_push({value: src}))" v-if="type == 'file-list'">上传</el-link>
  116. <el-link type="primary" @click="sa.uploadImageList(src => value_arr_push({value: src}))" v-if="type == 'img-video-list'">上传图片</el-link>
  117. <el-link type="primary" @click="sa.uploadVideoList(src => value_arr_push({value: src}))" v-if="type == 'img-video-list'" style="margin-left: 7px;">上传视频</el-link>
  118. </div>
  119. </div>
  120. <!-- 富文本 richtext f -->
  121. <div class="c-item" style="margin-top: 10px;" :class="{br: br}" v-else-if="type == 'richtext' || type == 'f'">
  122. <label class="c-label">{{name}}:</label>
  123. <div class="editor-box c-item-mline">
  124. <div :id="'editor-' + editor_id"></div>
  125. </div>
  126. <div style="clear: both;"></div>
  127. </div>
  128. <!-- enum 枚举 -->
  129. <div class="c-item" :class="{br: br}" v-else-if="type == 'enum' || type == 'j' || type == 'switch'">
  130. <label class="c-label">{{name}}:</label>
  131. <el-radio-group v-if="jtype == 1 || jtype == 2" :class="{'s-radio-text': jtype == 2}" :value="value" @input="onInput">
  132. <el-radio label="" v-if="def">{{def}}</el-radio>
  133. <el-radio v-for="j in jvList" :key="j.key" :label="j.key">{{j.value}}</el-radio>
  134. </el-radio-group>
  135. <el-radio-group v-if="jtype == 3" :value="value" @input="onInput">
  136. <el-radio-button label="" v-if="def">{{def}}</el-radio-button>
  137. <el-radio-button v-for="j in jvList" :key="j.key" :label="j.key">{{j.value}}</el-radio-button>
  138. </el-radio-group>
  139. <el-select v-if="jtype == 4" :value="value" @input="onInput">
  140. <el-option label="" v-if="def" :value="def"></el-option>
  141. <el-option v-for="j in jvList" :key="j.key" :label="j.value" :value="j.key"></el-option>
  142. </el-select>
  143. </div>
  144. <!-- 日期选择器 -->
  145. <div class="c-item" :class="{br: br}" v-else-if="type == 'date'">
  146. <label class="c-label">{{name}}:</label>
  147. <el-date-picker type="date" value-format="yyyy-MM-dd" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-date-picker>
  148. </div>
  149. <!-- 日期时间选择器 -->
  150. <div class="c-item" :class="{br: br}" v-else-if="type == 'datetime'">
  151. <label class="c-label">{{name}}:</label>
  152. <el-date-picker type="datetime" value-format="yyyy-MM-dd HH:mm:ss" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-date-picker>
  153. </div>
  154. <!-- 时间选择器 -->
  155. <div class="c-item" :class="{br: br}" v-else-if="type == 'time'">
  156. <label class="c-label">{{name}}:</label>
  157. <el-time-picker value-format="HH:mm:ss" :value="value" @input="onInput" :placeholder="placeholder" :disabled="disabled"></el-time-picker>
  158. </div>
  159. <!-- 日期范围选择 -->
  160. <div class="c-item" :class="{br: br}" v-else-if="type == 'date-range'">
  161. <label class="c-label">{{name}}:</label>
  162. <el-date-picker
  163. type="daterange"
  164. range-separator="至"
  165. start-placeholder="开始日期"
  166. end-placeholder="结束日期"
  167. value-format="yyyy-MM-dd"
  168. :value="dateRangeValue"
  169. @input="dateRangeOnChange"
  170. :disabled="disabled"
  171. >
  172. </el-date-picker>
  173. </div>
  174. <!-- 滑块 -->
  175. <div class="c-item" :class="{br: br}" v-else-if="type == 'slider'">
  176. <label class="c-label">{{name}}:</label>
  177. <div style="display: inline-block; height: 0px; vertical-align: top; width: 250px;">
  178. <el-slider :value="value" @input="onInput" style="position: relative; top: -5px;" :disabled="disabled"></el-slider>
  179. </div>
  180. </div>
  181. <!-- 级联输入 -->
  182. <div class="c-item" :class="{br: br}" v-else-if="type == 'cascader'">
  183. <label class="c-label">{{name}}:</label>
  184. <el-cascader :value="value" @input="onInput" :options="options" :props="{expandTrigger: 'hover'}" :placeholder="placeholder" :disabled="disabled"></el-cascader>
  185. </div>
  186. <!-- 颜色输入 -->
  187. <div class="c-item" :class="{br: br}" style="height: 0px;" v-else-if="type == 'color'">
  188. <label class="c-label">{{name}}:</label>
  189. <el-color-picker :value="value" @input="onInput" :disabled="disabled"></el-color-picker>
  190. <span class="c-remark" style="vertical-align: top;">{{value}}</span>
  191. </div>
  192. <!-- 评分组件 -->
  193. <div class="c-item" :class="{br: br}" v-else-if="type == 'rate'">
  194. <label class="c-label">{{name}}:</label>
  195. <div style="display: inline-block;">
  196. <el-rate :value="value" @input="onInput" show-text :disabled="disabled"></el-rate>
  197. </div>
  198. </div>
  199. <!-- 快捷增删改查按钮 -->
  200. <div class="fast-btn" v-else-if="type == 'fast-btn'">
  201. <el-button type="primary" icon="el-icon-plus" @click="$parent.add()" v-if="showBtns.indexOf('add') != -1">新增</el-button>
  202. <el-button type="success" icon="el-icon-view" @click="$parent.getBySelect()" v-if="showBtns.indexOf('get') != -1">查看</el-button>
  203. <el-button type="danger" icon="el-icon-delete" @click="$parent.deleteByIds()" v-if="showBtns.indexOf('delete') != -1">删除</el-button>
  204. <el-button type="warning" icon="el-icon-download" @click="sa.exportExcel()" v-if="showBtns.indexOf('export') != -1">导出</el-button>
  205. <el-button type="info" icon="el-icon-refresh" @click="sa.f5()" v-if="showBtns.indexOf('reset') != -1">重置</el-button>
  206. <slot></slot>
  207. </div>
  208. <!-- 分页组件 -->
  209. <div class="page-box" v-else-if="type == 'page'">
  210. <el-pagination background
  211. layout="total, prev, pager, next, sizes, jumper"
  212. :current-page.sync="curr"
  213. :page-size.sync="size"
  214. :total="total"
  215. :page-sizes="sizes || [1, 10, 20, 30, 40, 50, 100]"
  216. @current-change="changePage()"
  217. @size-change="changePage()">
  218. </el-pagination>
  219. </div>
  220. </template>
  221. <script>
  222. module.exports = {
  223. // props: ['name', 'value'],
  224. props: {
  225. // text、num、
  226. type: {
  227. default: 'text'
  228. },
  229. // label提示文字
  230. name: {
  231. type: String
  232. },
  233. // 绑定的值
  234. value: {},
  235. // 备注
  236. remark: '',
  237. // 提示文字
  238. placeholder: {},
  239. // 是否禁用
  240. disabled: {},
  241. // 是否换行
  242. br: {
  243. type: Boolean,
  244. default: false
  245. },
  246. // 日期范围时的选择字段,调用方需要加 .sync 修饰符
  247. start: {}, end: {},
  248. // type=menu时,值列表 -- 形如:{1: '正常[green]', 2: '禁用[red]'}
  249. jv: {default: ''},
  250. // type=menu时,具体的枚举类型 -- 1=单选框,2=单选文字,3=单选按钮,4=单选下拉框
  251. jtype: {default: 1},
  252. // type=menu时,增加的默认项文字
  253. def: {},
  254. // 级联选择的数据列表
  255. options: {},
  256. // 快捷按钮显示列表,形如:add,get,delete,export,reset
  257. show: {},
  258. // 分页信息
  259. curr: {}, size: {}, total: {}, sizes: {}
  260. },
  261. data() {
  262. return {
  263. // 日期范围时的值
  264. dateRangeValue: [],
  265. // 快捷按钮显示按钮列表
  266. showBtns: [],
  267. // type=menu时,解析的值列表 -- 形如:[{key: 1, value: '正常', color: 'green'}]
  268. jvList: [],
  269. // type = img-list 时,解析的元素List
  270. value_arr: [],
  271. // 富文本编辑器id
  272. editor_id: '',
  273. // 富文本编辑器对象
  274. editor: null,
  275. // money-f 的底层字段
  276. valueReal: ''
  277. }
  278. },
  279. watch: {
  280. // 监听一些类型的 value 变动
  281. value: function(oldValue, newValue) {
  282. // img-list、audio-list、video-list、file-list、img-video-list
  283. if(this.type == 'img-list' || this.type == 'audio-list' || this.type == 'video-list' || this.type == 'file-list'
  284. || this.type == 'img-video-list') {
  285. this.value_to_arr(this.value);
  286. }
  287. if(this.type == 'text-list' && this.value_arr.length == 0) {
  288. this.value_to_arr(this.value);
  289. }
  290. // 如果是富文本
  291. // if(this.type == 'richtext' || this.type == 'f') {
  292. // if(this.editor) {
  293. // // this.editor.txt.html(newValue);
  294. // $('#editor-' + this.editor_id + " .w-e-text").html(newValue);
  295. // }
  296. // }
  297. },
  298. },
  299. methods: {
  300. // input值发生变化时触发
  301. onInput: function($event) {
  302. this.$emit('input', $event);
  303. },
  304. // 日期范围选择时触发
  305. dateRangeOnChange: function(value) {
  306. console.log(value);
  307. this.dateRangeValue = value;
  308. this.start = value[0];
  309. this.end = value[1];
  310. this.$emit('update:start', value[0]);
  311. this.$emit('update:end', value[1]);
  312. },
  313. // 刷新分页
  314. changePage: function() {
  315. this.$emit('update:curr', this.curr);
  316. this.$emit('update:size', this.size);
  317. this.$emit('change');
  318. },
  319. // 解析枚举
  320. parseJv: function() {
  321. for(let key in this.jv) {
  322. let value = this.jv[key];
  323. let color = '';
  324. //
  325. if(value.indexOf('[') != -1 && value.endsWith(']')) {
  326. let index = value.indexOf('[');
  327. color = value.substring(index + 1, value.length - 1);
  328. value = value.substring(0, index);
  329. // console.log(color + ' --- ' + value);
  330. }
  331. //
  332. if(isNaN(key) == false) {
  333. key = parseInt(key);
  334. }
  335. //
  336. this.jvList.push({
  337. key: key,
  338. value: value,
  339. color: color
  340. })
  341. }
  342. },
  343. // 解析 value 为 value_arr
  344. value_to_arr: function(value) {
  345. let arr = sa.isNull(value) ? [] : value.split(',');
  346. let value_arr = [];
  347. for (var i = 0; i < arr.length; i++) {
  348. if(arr[i] != '' && arr[i].trim() != '') {
  349. value_arr.push({value: arr[i]});
  350. }
  351. }
  352. this.value_arr = value_arr;
  353. },
  354. // value_arr 数组增加值
  355. value_arr_push: function(item) {
  356. this.value_arr.push(item);
  357. // this.value = this.value_arr.join(',');
  358. this.$emit('input', sa.getArrayField(this.value_arr, 'value').join(','));
  359. },
  360. // value_arr 数组删除值
  361. value_arr_delete: function(item) {
  362. sa.arrayDelete(this.value_arr, item);
  363. // this.value = this.value_arr.join(',');
  364. this.$emit('input', sa.getArrayField(this.value_arr, 'value').join(','));
  365. },
  366. // value_arr 更改值时触发
  367. value_arr_change: function() {
  368. this.$emit('input', sa.getArrayField(this.value_arr, 'value').join(','));
  369. },
  370. // 创建富文本编辑器
  371. create_editor: function(content) {
  372. var E = window.wangEditor;
  373. var editor = new E('#editor-' + this.editor_id);
  374. editor.config.menus = [
  375. 'head', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough', 'foreColor', 'backColor', 'link', 'list',
  376. 'justify', 'quote', 'emoticon', 'image', 'table', 'code', 'undo', 'redo' // 重复
  377. ]
  378. editor.config.debug = true; // debug模式
  379. // editor.config.uploadFileName = 'file'; // 图片流name
  380. editor.config.withCredentials = true; // 跨域携带cookie
  381. editor.config.uploadImgMaxSize = 100 * 1024 * 1024; // 图片大小最大100M
  382. // editor.config.uploadImgShowBase64 = true // 使用 base64 保存图片
  383. // 监听内容变动
  384. editor.config.onchange = function (newHtml) {
  385. // console.log("change 之后最新的 html", newHtml);
  386. this.$emit('input', newHtml);
  387. }.bind(this);
  388. // 重写上传图片的函数到OSS
  389. editor.config.customUploadImg = function(files, insert) {
  390. var file = files[0]; // 文件对象
  391. startUploadImage2(file, function(src) {
  392. insert(src);
  393. sa.msg('上传成功');
  394. });
  395. }
  396. editor.create(); // 创建
  397. editor.txt.html(content); // 为编辑器赋值
  398. this.editor = editor;
  399. // setTimeout(function() {
  400. // $('.editor-box').height($('.editor-box').height());
  401. // })
  402. },
  403. // 为编辑器赋值
  404. editorSet: function(value) {
  405. this.editor.txt.html(value);
  406. },
  407. valueSet(valueReal) {
  408. this.valueReal = valueReal;
  409. }
  410. },
  411. created() {
  412. // console.log(this.br);
  413. if(this.type == 'fast-btn') {
  414. this.showBtns = this.show.split(',');
  415. for (var i = 0; i < this.showBtns.length; i++) {
  416. this.showBtns[i] = this.showBtns[i].trim();
  417. }
  418. }
  419. // 如果是枚举
  420. if(this.type == 'enum' || this.type == 'j' || this.type == 'switch') {
  421. this.parseJv();
  422. }
  423. // 如果是 img-list 等
  424. if(this.type == 'img-list' || this.type == 'audio-list' || this.type == 'video-list' || this.type == 'file-list'
  425. || this.type == 'img-video-list' || this.type == 'text-list') {
  426. this.value_to_arr(this.value);
  427. }
  428. // 如果是富文本
  429. if(this.type == 'richtext' || this.type == 'f') {
  430. this.editor_id = sa.randomString(32);
  431. this.$nextTick(function() {
  432. this.create_editor(this.value);
  433. })
  434. }
  435. // 如果是 money-f
  436. if(this.type == 'money-f') {
  437. if(this.value) {
  438. this.valueReal = this.value / 100;
  439. }
  440. }
  441. }
  442. }
  443. </script>
  444. <style scoped>
  445. </style>