sa-item.vue 18 KB

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