prodTemplate-add-or-update.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <el-dialog
  3. :title="!dataForm.templateId ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
  4. :close-on-click-modal="false"
  5. :visible.sync="visible">
  6. <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
  7. <el-form-item label="模板正反面" prop="templateSide">
  8. <el-select v-model="dataForm.templateSide" clearable filterable>
  9. <el-option key="Front" label="正面" value="Front"></el-option>
  10. <el-option key="Back" label="背面" value="Back"></el-option>
  11. </el-select>
  12. </el-form-item>
  13. <el-form-item label="模板用途" prop="templateUsage">
  14. <el-select v-model="usageList" clearable filterable multiple>
  15. <el-option key="smallcard" label="小卡" value="smallcard"></el-option>
  16. <el-option key="squarecard" label="方卡" value="squarecard"></el-option>
  17. <el-option key="badge" label="徽章" value="badge"></el-option>
  18. <el-option key="postcard" label="明信片" value="postcard"></el-option>
  19. <el-option key="bookmark" label="书签" value="bookmark"></el-option>
  20. <el-option key="tipscard" label="手幅" value="tipscard"></el-option>
  21. <el-option key="polaroid" label="拍立得" value="polaroid"></el-option>
  22. <el-option key="covercard" label="直拍封面" value="covercard"></el-option>
  23. <el-option key="stub" label="票根" value="stub"></el-option>
  24. </el-select>
  25. </el-form-item>
  26. <el-form-item label="模板风格" prop="templateStyle">
  27. <el-select v-model="dataForm.templateStyleId" clearable filterable>
  28. <el-option
  29. v-for="node in styleList"
  30. :key="node.styleId"
  31. :label="node.styleName"
  32. :value="node.styleId"
  33. ></el-option>
  34. </el-select>
  35. </el-form-item>
  36. <el-form-item label="模板系列" prop="templateSeries">
  37. <el-select v-model="dataForm.templateSeriesId" clearable filterable>
  38. <el-option
  39. v-for="node in seriesList"
  40. :key="node.seriesId"
  41. :label="node.seriesName"
  42. :value="node.seriesId"
  43. ></el-option>
  44. </el-select>
  45. </el-form-item>
  46. <el-form-item label="模板图片" prop="templatePic">
  47. <img-upload v-model="dataForm.templatePic" :limit="20"></img-upload>
  48. </el-form-item>
  49. <!--<el-form-item label="商家ID" prop="shopId">
  50. <el-input v-model="dataForm.shopId"></el-input>
  51. </el-form-item>-->
  52. <el-form-item label="是否禁用" prop="status">
  53. <el-select v-model="dataForm.status" clearable filterable>
  54. <el-option key="1" label="启用" :value=1 ></el-option>
  55. <el-option key="0" label="禁用" :value=0 ></el-option>
  56. </el-select>
  57. </el-form-item>
  58. <el-form-item label="排序" prop="seq">
  59. <el-input v-model="dataForm.seq" type="number"/>
  60. </el-form-item>
  61. </el-form>
  62. <span slot="footer" class="dialog-footer">
  63. <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
  64. <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
  65. </span>
  66. </el-dialog>
  67. </template>
  68. <script>
  69. import ImgUpload from '@/components/imgs-upload'
  70. // import uploadPic from './upload-pic.vue'
  71. export default {
  72. components: {
  73. ImgUpload
  74. },
  75. data () {
  76. return {
  77. visible: false,
  78. dataForm: {
  79. templateId: null,
  80. templateSide: null,
  81. templateStyleId: null,
  82. templateSeriesId: null,
  83. templateStyle: null,
  84. templateSeries: null,
  85. templatePic: null,
  86. shopId: this.$store.state.user.shopId,
  87. status: null,
  88. createTime: null,
  89. templateUsage: null,
  90. seq: 1
  91. },
  92. resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
  93. seriesList: [],
  94. styleList: [],
  95. usageList:[],
  96. dataRule: {
  97. }
  98. }
  99. },
  100. created () {
  101. this.getStyleList()
  102. this.getSeriesList()
  103. },
  104. methods: {
  105. init (templateId) {
  106. this.dataForm.templateId = templateId || 0
  107. this.visible = true
  108. this.$nextTick(() => {
  109. this.$refs['dataForm'].resetFields()
  110. if (this.dataForm.templateId) {
  111. this.$http({
  112. url: this.$http.adornUrl('/prod/prodTemplate/info/' + this.dataForm.templateId),
  113. method: 'get',
  114. params: this.$http.adornParams()
  115. }).then(({data}) => {
  116. this.dataForm = data;
  117. this.usageList = data.templateUsage.split(",");
  118. })
  119. }
  120. })
  121. },
  122. //验证图片尺寸
  123. checkImageSize(imageStr, index) {
  124. let width = 0;
  125. let height = 0;
  126. let templateUsage = this.usageList.join(",");
  127. let isBack = this.dataForm.templateSide === 'Back';
  128. return new Promise((resolve, reject) => {
  129. let image = new Image();
  130. image.onload = function() {
  131. if(templateUsage.indexOf("smallcard") !== -1 || templateUsage.indexOf("polaroid") !== -1){
  132. width = 709;
  133. height = 1087;
  134. }else if(templateUsage.indexOf("stub") !== -1){
  135. width = 780;
  136. height = 1489;
  137. }else if(templateUsage.indexOf("bookmark") !== -1){
  138. width = 638;
  139. height = 1819;
  140. }else if(templateUsage.indexOf("postcard") !== -1){
  141. width = 1252;
  142. height = 1819;
  143. }
  144. if(isBack){
  145. let imageFormat = imageStr.split('.').pop();
  146. if(imageFormat != 'jpg'){
  147. reject(index);
  148. }
  149. }else if(!isBack && templateUsage.indexOf("polaroid") !== -1){
  150. let imageFormat = imageStr.split('.').pop();
  151. if(imageFormat != 'png'){
  152. reject(index);
  153. }
  154. }
  155. if ((image.width === width && image.height === height)) {
  156. resolve();
  157. } else {
  158. reject(index);
  159. }
  160. };
  161. image.src = this.resourcesUrl + imageStr;
  162. });
  163. },
  164. // 表单提交
  165. async dataFormSubmit () {
  166. try{
  167. let imgUrlArr = this.dataForm.templatePic.split(",");
  168. await Promise.all(imgUrlArr.map(this.checkImageSize));
  169. this.handleStyleName(this.dataForm.templateStyleId);
  170. this.handleSeriesName(this.dataForm.templateSeriesId);
  171. this.dataForm.templateUsage = this.usageList.join(",");
  172. this.$http({
  173. url: this.$http.adornUrl('/prod/prodTemplate'),
  174. method: this.dataForm.templateId ? 'put' : 'post',
  175. data: this.$http.adornData(this.dataForm)
  176. }).then(({data}) => {
  177. this.$message({
  178. message: this.$i18n.t('publics.operation'),
  179. type: 'success',
  180. duration: 200,
  181. onClose: () => {
  182. this.visible = false;
  183. this.$emit('refreshDataList')
  184. }
  185. })
  186. })
  187. }catch (error) {
  188. this.$message.error(`第 ${error + 1} 张图片不符合尺寸要求。`)
  189. }
  190. },
  191. getStyleList () {
  192. this.$http({
  193. url: this.$http.adornUrl('/prod/prodTemplateStyle/page'),
  194. method: 'get',
  195. params: this.$http.adornParams(
  196. Object.assign({
  197. size: 100,
  198. shopId: this.$store.state.user.shopId
  199. }
  200. )
  201. )
  202. }).then(({data}) => {
  203. this.styleList = data.records
  204. })
  205. },
  206. getSeriesList () {
  207. this.$http({
  208. url: this.$http.adornUrl('/prod/prodTemplateSeries/page'),
  209. method: 'get',
  210. params: this.$http.adornParams(
  211. Object.assign({
  212. size: 100,
  213. shopId: this.$store.state.user.shopId
  214. }
  215. )
  216. )
  217. }).then(({data}) => {
  218. this.seriesList = data.records
  219. })
  220. },
  221. handleStyleName (styleId) {
  222. this.styleList.forEach(style =>{
  223. if (style.styleId == styleId) {
  224. this.dataForm.templateStyle = style.styleName
  225. return;
  226. }
  227. })
  228. },
  229. handleSeriesName (seriesId) {
  230. this.seriesList.forEach(series =>{
  231. if (series.seriesId == seriesId) {
  232. this.dataForm.templateSeries = series.seriesName
  233. }
  234. })
  235. },
  236. }
  237. }
  238. </script>