sa-item.vue 17 KB

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