sa-item.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. <!-- 钱 money (单位 元) -->
  33. <div class="c-item" :class="{br: br}" v-else-if="type == 'money'">
  34. <label class="c-label">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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">{{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="total"
  198. :page-sizes="sizes || [1, 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. // 是否换行
  223. br: {
  224. type: Boolean,
  225. default: false
  226. },
  227. // 日期范围时的选择字段,调用方需要加 .sync 修饰符
  228. start: {}, end: {},
  229. // type=menu时,值列表 -- 形如:{1: '正常[green]', 2: '禁用[red]'}
  230. jv: {default: ''},
  231. // type=menu时,具体的枚举类型 -- 1=单选框,2=单选文字,3=单选按钮,4=单选下拉框
  232. jtype: {default: 1},
  233. // type=menu时,增加的默认项文字
  234. def: {},
  235. // 级联选择的数据列表
  236. options: {},
  237. // 快捷按钮显示列表,形如:add,get,delete,export,reset
  238. show: {},
  239. // 分页信息
  240. curr: {}, size: {}, total: {}, sizes: {}
  241. },
  242. data() {
  243. return {
  244. // 日期范围时的值
  245. dateRangeValue: [],
  246. // 快捷按钮显示按钮列表
  247. showBtns: [],
  248. // type=menu时,解析的值列表 -- 形如:[{key: 1, value: '正常', color: 'green'}]
  249. jvList: [],
  250. // type = img-list 时,解析的元素List
  251. value_arr: [],
  252. // 富文本编辑器id
  253. editor_id: '',
  254. // 富文本编辑器对象
  255. editor: null,
  256. // money-f 的底层字段
  257. valueReal: ''
  258. }
  259. },
  260. watch: {
  261. // 监听一些类型的 value 变动
  262. value: function(oldValue, newValue) {
  263. // img-list、audio-list、video-list、file-list、img-video-list
  264. if(this.type == 'img-list' || this.type == 'audio-list' || this.type == 'video-list' || this.type == 'file-list' || this.type == 'img-video-list') {
  265. this.value_to_arr(this.value);
  266. }
  267. // 如果是富文本
  268. // if(this.type == 'richtext' || this.type == 'f') {
  269. // if(this.editor) {
  270. // // this.editor.txt.html(newValue);
  271. // $('#editor-' + this.editor_id + " .w-e-text").html(newValue);
  272. // }
  273. // }
  274. },
  275. },
  276. methods: {
  277. // input值发生变化时触发
  278. onInput: function($event) {
  279. this.$emit('input', $event);
  280. },
  281. // 日期范围选择时触发
  282. dateRangeOnChange: function(value) {
  283. console.log(value);
  284. this.dateRangeValue = value;
  285. this.start = value[0];
  286. this.end = value[1];
  287. this.$emit('update:start', value[0]);
  288. this.$emit('update:end', value[1]);
  289. },
  290. // 刷新分页
  291. changePage: function() {
  292. this.$emit('update:curr', this.curr);
  293. this.$emit('update:size', this.size);
  294. this.$emit('change');
  295. },
  296. // 解析枚举
  297. parseJv: function() {
  298. for(let key in this.jv) {
  299. let value = this.jv[key];
  300. let color = '';
  301. //
  302. if(value.indexOf('[') != -1 && value.endsWith(']')) {
  303. let index = value.indexOf('[');
  304. color = value.substring(index + 1, value.length - 1);
  305. value = value.substring(0, index);
  306. // console.log(color + ' --- ' + value);
  307. }
  308. //
  309. if(isNaN(key) == false) {
  310. key = parseInt(key);
  311. }
  312. //
  313. this.jvList.push({
  314. key: key,
  315. value: value,
  316. color: color
  317. })
  318. }
  319. },
  320. // 解析 value 为 value_arr
  321. value_to_arr: function(value) {
  322. this.value_arr = sa.isNull(value) ? [] : value.split(',');
  323. for (var i = 0; i < this.value_arr.length; i++) {
  324. if(this.value_arr[i] == '' || this.value_arr[i].trim() == '') {
  325. sa.arrayDelete(this.value_arr, this.value_arr[i]);
  326. i--;
  327. }
  328. }
  329. },
  330. // value_arr 数组增加值
  331. value_arr_push: function(item) {
  332. this.value_arr.push(item);
  333. // this.value = this.value_arr.join(',');
  334. this.$emit('input', this.value_arr.join(','));
  335. },
  336. // value_arr 数组删除值
  337. value_arr_delete: function(item) {
  338. sa.arrayDelete(this.value_arr, item);
  339. // this.value = this.value_arr.join(',');
  340. this.$emit('input', this.value_arr.join(','));
  341. },
  342. // 创建富文本编辑器
  343. create_editor: function(content) {
  344. var E = window.wangEditor;
  345. var editor = new E('#editor-' + this.editor_id);
  346. editor.config.menus = [
  347. 'head', 'fontSize', 'fontName', 'italic', 'underline', 'strikeThrough', 'foreColor', 'backColor', 'link', 'list',
  348. 'justify', 'quote', 'emoticon', 'image', 'table', 'code', 'undo', 'redo' // 重复
  349. ]
  350. editor.config.debug = true; // debug模式
  351. // editor.config.uploadFileName = 'file'; // 图片流name
  352. editor.config.withCredentials = true; // 跨域携带cookie
  353. editor.config.uploadImgMaxSize = 100 * 1024 * 1024; // 图片大小最大100M
  354. // editor.config.uploadImgShowBase64 = true // 使用 base64 保存图片
  355. // 监听内容变动
  356. editor.config.onchange = function (newHtml) {
  357. // console.log("change 之后最新的 html", newHtml);
  358. this.$emit('input', newHtml);
  359. }.bind(this);
  360. // 重写上传图片的函数到OSS
  361. editor.config.customUploadImg = function(files, insert) {
  362. var file = files[0]; // 文件对象
  363. startUploadImage2(file, function(src) {
  364. insert(src);
  365. sa.msg('上传成功');
  366. });
  367. }
  368. editor.create(); // 创建
  369. editor.txt.html(content); // 为编辑器赋值
  370. this.editor = editor;
  371. // setTimeout(function() {
  372. // $('.editor-box').height($('.editor-box').height());
  373. // })
  374. },
  375. // 为编辑器赋值
  376. editorSet: function(value) {
  377. this.editor.txt.html(value);
  378. },
  379. valueSet(valueReal) {
  380. this.valueReal = valueReal;
  381. }
  382. },
  383. created() {
  384. // console.log(this.br);
  385. if(this.type == 'fast-btn') {
  386. this.showBtns = this.show.split(',');
  387. for (var i = 0; i < this.showBtns.length; i++) {
  388. this.showBtns[i] = this.showBtns[i].trim();
  389. }
  390. }
  391. // 如果是枚举
  392. if(this.type == 'enum' || this.type == 'j' || this.type == 'switch') {
  393. this.parseJv();
  394. }
  395. // 如果是 img-list 等
  396. if(this.type == 'img-list' || this.type == 'audio-list' || this.type == 'video-list' || this.type == 'file-list' || this.type == 'img-video-list') {
  397. this.value_to_arr(this.value);
  398. }
  399. // 如果是富文本
  400. if(this.type == 'richtext' || this.type == 'f') {
  401. this.editor_id = sa.randomString(32);
  402. this.$nextTick(function() {
  403. this.create_editor(this.value);
  404. })
  405. }
  406. // 如果是 money-f
  407. if(this.type == 'money-f') {
  408. if(this.value) {
  409. this.valueReal = this.value / 100;
  410. }
  411. }
  412. }
  413. }
  414. </script>
  415. <style scoped>
  416. </style>