addSheet.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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="$t('OnlineExcelPage.please_table_name_ipt')"
  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="$t('OnlineExcelPage.select_table_category')"
  27. />
  28. </li>
  29. </ul>
  30. <div v-if="updateTime" style="color:#999999;margin-right: 30px;">{{$t('OnlineExcelPage.recent_save_time_info')}}:{{ updateTime }}</div>
  31. <div>
  32. <el-button type="primary" size="medium" @click="saveSheetHandle" v-if="hasPermission">{{$t('ETable.Btn.save_btn')}}</el-button>
  33. <el-button type="primary" size="medium" plain @click="backHandle">{{$t('ETable.Btn.back_btn')}}</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. return this.sheetButton?
  92. this.permissionBtn.isShowBtn('etaTablePermission','etaTable_excel_save')&&this.sheetButton.OpButton:
  93. this.permissionBtn.isShowBtn('etaTablePermission','etaTable_excel_save')
  94. }
  95. },
  96. methods: {
  97. backHandle() {
  98. this.$router.back()
  99. },
  100. /* 获取分类 */
  101. getClassify() {
  102. sheetInterface.excelClassifyOne().then(res => {
  103. if(res.Ret !==200) return
  104. this.classifyArr = res.Data.AllNodes || [];
  105. })
  106. },
  107. /* 获取表格详情 */
  108. async getDetail() {
  109. if(!this.sheetId){
  110. // 初始化数据
  111. this.option={data:[{}]}
  112. this.sheetInit=true
  113. return
  114. }
  115. const res = await sheetInterface.sheetDetail({
  116. ExcelInfoId: Number(this.sheetId)
  117. })
  118. if(res.Ret !== 200) return
  119. this.isCanEdit = res.Data.CanEdit
  120. if(!res.Data.CanEdit){
  121. this.$message.warning(`${res.Data.Editor}${this.$t('OnlineExcelPage.currently_editing_msg')}`)
  122. setTimeout(()=>{
  123. this.backHandle()
  124. },1000)
  125. return
  126. }
  127. const { ExcelInfoId,ExcelName,ExcelClassifyId,ExcelType,ModifyTime,Content,Source,Button} = res.Data;
  128. this.sheetButton=Button
  129. this.sheetForm = {
  130. infoId:ExcelInfoId,
  131. name: ExcelName,
  132. classify: ExcelClassifyId,
  133. sheetType: ExcelType,
  134. source:Source
  135. }
  136. this.updateTime = this.$moment(ModifyTime).format('YYYY-MM-DD HH:mm:ss')
  137. this.option={
  138. data: [{
  139. ...JSON.parse(Content),
  140. scrollTop: 0,
  141. scrollLeft: 0
  142. }]
  143. }
  144. this.$nextTick(()=>{
  145. this.sheetInit=true
  146. })
  147. },
  148. autoSaveFun:_.debounce(async function(){
  149. // cancelAutoSave -- 由于是延时自动保存 防止用户在聚焦文本框时点击保存,文本框失焦后又发起自动保存的情况
  150. if(!this.sheetId && this.sheetInit || this.cancelAutoSave) return
  151. const { name,classify,infoId } = this.sheetForm;
  152. luckysheet.exitEditMode()
  153. let data = luckysheet.getAllSheets()[0];
  154. if(!name || !classify) return this.$message.warning(name ? this.$t('OnlineExcelPage.select_table_category') : this.$t('OnlineExcelPage.please_table_name_ipt') )
  155. if(!data.celldata.length) return this.$message.warning(this.$t('OnlineExcelPage.input_content_msg') );
  156. let params={
  157. ExcelInfoId:infoId,
  158. ExcelName: name,
  159. ExcelClassifyId: classify,
  160. ExcelImage: "",
  161. Content: JSON.stringify(data)
  162. }
  163. const res = await sheetInterface.sheetEdit(params)
  164. if(res.Ret !==200) return
  165. if(res.Data.Status==1){
  166. this.$message.warning(res.Data.Msg)
  167. setTimeout(()=>{
  168. this.backHandle()
  169. },1000)
  170. return
  171. }
  172. this.updateTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
  173. // console.log("自动保存");
  174. },1500),
  175. /* 保存表格 */
  176. saveSheetHandle: _.debounce(async function() {
  177. const { name,classify} = this.sheetForm;
  178. luckysheet.exitEditMode()
  179. //结构类型乱飘 强制定义下
  180. let data = {...luckysheet.getAllSheets()[0],status:Number(luckysheet.getAllSheets()[0].status)}
  181. if(!name || !classify) return this.$message.warning(name ? this.$t('OnlineExcelPage.select_table_category') : this.$t('OnlineExcelPage.please_table_name_ipt') )
  182. if(!data.celldata.length) return this.$message.warning(this.$t('OnlineExcelPage.input_content_msg') );
  183. this.loading = this.$loading({
  184. target:'.addSheet-wrap',
  185. lock: true,
  186. text: this.$t('OnlineExcelPage.saving_loading_text') ,
  187. spinner: 'el-icon-loading',
  188. background: 'rgba(255, 255, 255, 0.6)'
  189. });
  190. let img = getSheetImage(data);
  191. const form = new FormData();
  192. form.append('Image', img);
  193. const { Data } = await sheetInterface.uploadImg(form)
  194. data.luckysheet_select_save = [];
  195. let params={
  196. ExcelName: name,
  197. ExcelClassifyId: classify,
  198. ExcelImage: Data.ResourceUrl,
  199. Content: JSON.stringify(data)
  200. }
  201. let isAdd = this.sheetId?false:true
  202. const res = this.sheetId
  203. ? await sheetInterface.sheetEdit({ ExcelInfoId: Number(this.sheetId),...params })
  204. : await sheetInterface.sheetAdd(params)
  205. this.loading.close()
  206. if(res.Ret !==200) return
  207. if(res.Data.Status==1){
  208. this.$message.warning(res.Data.Msg)
  209. setTimeout(()=>{
  210. this.backHandle()
  211. },1000)
  212. return
  213. }
  214. this.updateTime = this.$moment().format('YYYY-MM-DD HH:mm:ss')
  215. this.sheetId = this.sheetId || res.Data.ExcelInfoId;
  216. this.$message.success(res.Msg);
  217. if(isAdd){
  218. this.$router.replace({path:'/addsheet',query:{id:this.sheetId}})
  219. this.cancelAutoSave=true
  220. }
  221. },300),
  222. markFinishStatus(){
  223. if((!this.sheetId) || (!this.isCanEdit)) return
  224. sheetInterface.markSheetEditStatus({ExcelInfoId: +this.sheetId,Status:2}).then(res=>{
  225. if(res.Ret != 200) return
  226. })
  227. }
  228. },
  229. mounted() {
  230. this.getDetail()
  231. this.getClassify();
  232. window.addEventListener('beforeunload',this.markFinishStatus)
  233. },
  234. beforeDestroy(){
  235. window.removeEventListener('beforeunload',this.markFinishStatus)
  236. }
  237. }
  238. </script>
  239. <style scoped lang="scss">
  240. *{ box-sizing: border-box; }
  241. .addSheet-wrap {
  242. min-height: calc(100vh - 120px);
  243. min-width: 1000px;
  244. .wrap-top {
  245. display: flex;
  246. justify-content: space-between;
  247. align-items: center;
  248. margin-bottom: 20px;
  249. padding: 20px;
  250. background: #fff;
  251. border: 1px solid #ececec;
  252. border-radius: 4px;
  253. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
  254. display: flex;
  255. z-index: 1;
  256. .form-ul {
  257. flex: 1;
  258. display: flex;
  259. }
  260. }
  261. .main {
  262. position: relative;
  263. min-height: 700px;
  264. }
  265. }
  266. </style>