addSheet.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <div class="addSheet-wrap">
  3. <div class="wrap-top">
  4. <ul class="form-ul">
  5. <li style="margin-right:30px;">
  6. <el-input
  7. v-model="sheetForm.name"
  8. placeholder="请输入表格名称"
  9. clearable
  10. style="width:240px">
  11. </el-input>
  12. </li>
  13. <li>
  14. <el-cascader
  15. v-model="sheetForm.classify"
  16. :options="classifyArr"
  17. style="width:240px;"
  18. :props="{
  19. label: 'ExcelClassifyName',
  20. value: 'ExcelClassifyId',
  21. children: 'Children',
  22. emitPath: false,
  23. checkStrictly: true
  24. }"
  25. clearable
  26. placeholder="请选择表格分类"
  27. />
  28. </li>
  29. </ul>
  30. <div v-if="updateTime" style="color:#999999;margin-right: 30px;">最近保存时间:{{ updateTime }}</div>
  31. <div>
  32. <el-button type="primary" size="medium" @click="saveSheetHandle" v-if="hasPermission">保存</el-button>
  33. <el-button type="primary" size="medium" plain @click="backHandle">返回</el-button>
  34. </div>
  35. </div>
  36. <div class="main">
  37. <Sheet ref="sheetRef" :limit="{disabled:false}" :option="this.option" v-if="sheetInit" @updated="autoSaveFun"
  38. :sheetInfo="{
  39. ExcelInfoId: sheetForm.infoId,
  40. ExcelName: sheetForm.name,
  41. ExcelClassifyId: sheetForm.classify,
  42. Source: sheetForm.source
  43. }" />
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. import * as sheetInterface from '@/api/modules/sheetApi.js';
  49. import Sheet from './components/SheetExcel.vue';
  50. import { getSheetImage } from './common/option';
  51. export default {
  52. components: { Sheet },
  53. data() {
  54. return {
  55. sheetId: this.$route.query.id || '',
  56. isCanEdit:false,
  57. sheetForm: {},
  58. classifyArr: [],
  59. sheetInit:false,
  60. option:{},
  61. updateTime:'',
  62. sheetButton:'',
  63. cancelAutoSave:false
  64. }
  65. },
  66. beforeRouteEnter(to, from, next) {
  67. if(to.query.id){
  68. to.matched[1].name=`编辑表格`
  69. }else{
  70. to.matched[1].name='添加表格'
  71. }
  72. next()
  73. },
  74. beforeRouteLeave(to,from,next){
  75. if(to.path!='/addsheet'){
  76. this.markFinishStatus()
  77. }
  78. next()
  79. },
  80. watch:{
  81. sheetForm:{
  82. handler(newVal){
  83. console.log(this.sheetInit,this.sheetId);
  84. if(this.sheetId && this.sheetInit) this.autoSaveFun()
  85. },
  86. deep:true
  87. }
  88. },
  89. computed:{
  90. hasPermission(){
  91. // console.log(this.sheetButton,'sheetButton');
  92. return this.sheetButton?
  93. this.permissionBtn.isShowBtn('etaTablePermission','etaTable_excel_save')&&this.sheetButton.OpButton:
  94. this.permissionBtn.isShowBtn('etaTablePermission','etaTable_excel_save')
  95. }
  96. },
  97. methods: {
  98. backHandle() {
  99. // window.close();
  100. this.$router.back()
  101. },
  102. /* 获取分类 */
  103. getClassify() {
  104. sheetInterface.excelClassifyOne().then(res => {
  105. if(res.Ret !==200) return
  106. this.classifyArr = res.Data.AllNodes || [];
  107. })
  108. },
  109. /* 获取表格详情 */
  110. async getDetail() {
  111. if(!this.sheetId){
  112. // 初始化数据
  113. this.option={data:[{}]}
  114. this.sheetInit=true
  115. return
  116. }
  117. const res = await sheetInterface.sheetDetail({
  118. ExcelInfoId: Number(this.sheetId)
  119. })
  120. if(res.Ret !== 200) return
  121. this.isCanEdit = res.Data.CanEdit
  122. if(!res.Data.CanEdit){
  123. this.$message.warning(`${res.Data.Editor}正在编辑中`)
  124. setTimeout(()=>{
  125. this.backHandle()
  126. },1000)
  127. return
  128. }
  129. const { ExcelInfoId,ExcelName,ExcelClassifyId,ExcelType,ModifyTime,Content,Source,Button} = res.Data;
  130. this.sheetButton=Button
  131. this.sheetForm = {
  132. infoId:ExcelInfoId,
  133. name: ExcelName,
  134. classify: ExcelClassifyId,
  135. sheetType: ExcelType,
  136. source:Source
  137. }
  138. this.updateTime = this.$moment(ModifyTime).format('YYYY-MM-DD HH:mm:ss')
  139. this.option={
  140. data: [{
  141. ...JSON.parse(Content),
  142. scrollTop: 0,
  143. scrollLeft: 0
  144. }]
  145. }
  146. this.$nextTick(()=>{
  147. this.sheetInit=true
  148. })
  149. },
  150. autoSaveFun:_.debounce(async function(){
  151. // cancelAutoSave -- 由于是延时自动保存 防止用户在聚焦文本框时点击保存,文本框失焦后又发起自动保存的情况
  152. if(!this.sheetId && this.sheetInit || this.cancelAutoSave) return
  153. const { name,classify,infoId } = this.sheetForm;
  154. luckysheet.exitEditMode()
  155. let data = luckysheet.getAllSheets()[0];
  156. if(!name || !classify) return this.$message.warning(name ? '请选择表格分类' : '请输入表格名称')
  157. if(!data.celldata.length) return this.$message.warning('请输入表格内容');
  158. let params={
  159. ExcelInfoId:infoId,
  160. ExcelName: name,
  161. ExcelClassifyId: classify,
  162. ExcelImage: "",
  163. Content: JSON.stringify(data)
  164. }
  165. const res = await sheetInterface.sheetEdit(params)
  166. if(res.Ret !==200) return
  167. if(res.Data.Status==1){
  168. this.$message.warning(res.Data.Msg)
  169. setTimeout(()=>{
  170. this.backHandle()
  171. },1000)
  172. return
  173. }
  174. this.updateTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
  175. // console.log("自动保存");
  176. },1500),
  177. /* 保存表格 */
  178. saveSheetHandle: _.debounce(async function() {
  179. const { name,classify} = this.sheetForm;
  180. luckysheet.exitEditMode()
  181. //结构类型乱飘 强制定义下
  182. let data = {...luckysheet.getAllSheets()[0],status:Number(luckysheet.getAllSheets()[0].status)}
  183. if(!name || !classify) return this.$message.warning(name ? '请选择表格分类' : '请输入表格名称')
  184. if(!data.celldata.length) return this.$message.warning('请输入表格内容');
  185. this.loading = this.$loading({
  186. target:'.addSheet-wrap',
  187. lock: true,
  188. text: '保存中...',
  189. spinner: 'el-icon-loading',
  190. background: 'rgba(255, 255, 255, 0.6)'
  191. });
  192. let img = getSheetImage(data);
  193. const form = new FormData();
  194. form.append('Image', img);
  195. const { Data } = await sheetInterface.uploadImg(form)
  196. data.luckysheet_select_save = [];
  197. let params={
  198. ExcelName: name,
  199. ExcelClassifyId: classify,
  200. ExcelImage: Data.ResourceUrl,
  201. Content: JSON.stringify(data)
  202. }
  203. let isAdd = this.sheetId?false:true
  204. const res = this.sheetId
  205. ? await sheetInterface.sheetEdit({ ExcelInfoId: Number(this.sheetId),...params })
  206. : await sheetInterface.sheetAdd(params)
  207. this.loading.close()
  208. if(res.Ret !==200) return
  209. if(res.Data.Status==1){
  210. this.$message.warning(res.Data.Msg)
  211. setTimeout(()=>{
  212. this.backHandle()
  213. },1000)
  214. return
  215. }
  216. this.updateTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
  217. this.sheetId = this.sheetId || res.Data.ExcelInfoId;
  218. this.$message.success(res.Msg);
  219. if(isAdd){
  220. this.$router.replace({path:'/addsheet',query:{id:this.sheetId}})
  221. this.cancelAutoSave=true
  222. }
  223. // const { ExcelInfoId, UniqueCode } = res.Data;
  224. // this.$router.replace({
  225. // path: '/sheetList',
  226. // query: {
  227. // code: UniqueCode,
  228. // id: ExcelInfoId
  229. // }
  230. // })
  231. },300),
  232. markFinishStatus(){
  233. if((!this.sheetId) || (!this.isCanEdit)) return
  234. sheetInterface.markSheetEditStatus({ExcelInfoId: +this.sheetId,Status:2}).then(res=>{
  235. if(res.Ret != 200) return
  236. })
  237. }
  238. },
  239. mounted() {
  240. this.getDetail()
  241. this.getClassify();
  242. window.addEventListener('beforeunload',this.markFinishStatus)
  243. },
  244. beforeDestroy(){
  245. window.removeEventListener('beforeunload',this.markFinishStatus)
  246. }
  247. }
  248. </script>
  249. <style scoped lang="scss">
  250. *{ box-sizing: border-box; }
  251. .addSheet-wrap {
  252. min-height: calc(100vh - 120px);
  253. min-width: 1000px;
  254. .wrap-top {
  255. display: flex;
  256. justify-content: space-between;
  257. align-items: center;
  258. margin-bottom: 20px;
  259. padding: 20px;
  260. background: #fff;
  261. border: 1px solid #ececec;
  262. border-radius: 4px;
  263. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
  264. display: flex;
  265. z-index: 1;
  266. .form-ul {
  267. flex: 1;
  268. display: flex;
  269. }
  270. }
  271. .main {
  272. position: relative;
  273. min-height: 700px;
  274. }
  275. }
  276. </style>