product-edit.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. const Radio = createnRadio();
  2. const Select = createSelect();
  3. const Option = createOption();
  4. const UploadImage = createUploadImage();
  5. const Editor = createEditorComponent();
  6. const InputGroup = createInputGroup();
  7. const QuestionGroup = createQuestionGroup()
  8. new Vue({
  9. el: '#productEdit',
  10. mixins: [validFormMixin],
  11. components: {
  12. 'cm-radio': Radio,
  13. 'cm-select': Select,
  14. 'cm-option': Option,
  15. 'cm-upload-image': UploadImage,
  16. 'cm-editor': Editor,
  17. 'cm-input-group': InputGroup,
  18. 'cm-question-group': QuestionGroup
  19. },
  20. data: {
  21. // 商品类型 1: 产品 2: 仪器
  22. productId: 0,
  23. commodityType: 1,
  24. shopId: GLOBAL_SHOP_ID,
  25. formData: {
  26. // 产品名称
  27. name: '',
  28. // 产品别名
  29. alias: '',
  30. // 产品概述
  31. discription: '',
  32. // 产品图片
  33. image: '',
  34. // 产品参数
  35. paramList: [],
  36. // 产品优点
  37. advantage: '',
  38. // 产品缺点
  39. disadvantage: '',
  40. // 产品原理
  41. principle: '',
  42. // 品牌
  43. brand: '',
  44. // 产地
  45. producePlace: '',
  46. // 上市时间
  47. marketTime: '',
  48. // 公司/厂商
  49. company: '',
  50. // NMPA认证时间
  51. nmpaTime: '',
  52. // 产品认证
  53. authImageList: [],
  54. // 适应人群
  55. adaptiveMan: '',
  56. // 不适应人群
  57. unAdaptiveMan: '',
  58. // 术前术后
  59. aroundOperation: '',
  60. // 效果展示图片列表
  61. displayImageList: [],
  62. // 常见问题
  63. questionList: [],
  64. // 产品类别
  65. typeId: '',
  66. // 产品状态
  67. status: 1,
  68. },
  69. // 预览图片列表
  70. imageList: [],
  71. authImageList: [],
  72. displayImageList: [],
  73. // 产品分类
  74. typeList: [],
  75. // 产品参数表单数据
  76. paramsPlaceholderList: [{
  77. label: '例如:名牌',
  78. value: '请输入名牌信息'
  79. }],
  80. paramsMinLimit: 2,
  81. paramskeyMap: {
  82. key: 'name',
  83. value: 'content'
  84. },
  85. // 问题数据表单列表
  86. questionPlaceHolderList: [{
  87. label: '请输入问题',
  88. value: '请输入回答'
  89. }],
  90. questionMinLimit: 2,
  91. questionkeyMap: {
  92. key: 'question',
  93. value: 'answer'
  94. },
  95. // 表单校验规则
  96. rules: {
  97. // 产品名称
  98. name: [{
  99. type: 'string',
  100. required: true,
  101. message: '名称不能为空',
  102. trigger: ['blur','change']
  103. }],
  104. // 产品别名
  105. alias: [{
  106. type: 'string',
  107. required: true,
  108. message: '别名不能为空',
  109. trigger: ['blur','change']
  110. }],
  111. // 产品概述
  112. discription: [{
  113. type: 'string',
  114. required: true,
  115. message: '概述不能为空',
  116. trigger: ['blur','change']
  117. }],
  118. // 产品图片
  119. image: [{
  120. type: 'string',
  121. required: true,
  122. message: '图片不能为空',
  123. trigger: 'change'
  124. }],
  125. // 产品参数
  126. paramList: [{
  127. type: 'array',
  128. required: true,
  129. message: '参数信息不能为空',
  130. trigger: ['change', 'blur'],
  131. valid: ['name', 'content']
  132. }],
  133. // 产品优点
  134. advantage: [{
  135. type: 'string',
  136. required: true,
  137. message: '优点不能为空',
  138. trigger: 'change'
  139. }],
  140. // 产品缺点
  141. disadvantage: [{
  142. type: 'string',
  143. required: true,
  144. message: '缺点不能为空',
  145. trigger: 'change'
  146. }],
  147. // 产品原理
  148. principle: [{
  149. type: 'string',
  150. required: true,
  151. message: '原理不能为空',
  152. trigger: 'change'
  153. }],
  154. // 品牌
  155. brand: [{
  156. type: 'string',
  157. required: true,
  158. message: '品牌不能为空',
  159. trigger: ['change','blur']
  160. }],
  161. // 产地
  162. producePlace: [{
  163. type: 'string',
  164. required: true,
  165. message: '产地不能为空',
  166. trigger: ['change','blur']
  167. }],
  168. // 上市时间
  169. marketTime: [{
  170. type: 'string',
  171. required: true,
  172. message: '上市时间不能为空',
  173. trigger: ['change','blur']
  174. }],
  175. // 公司/厂商
  176. company: [{
  177. type: 'string',
  178. required: true,
  179. message: '公司/厂商不能为空',
  180. trigger: ['change','blur']
  181. }],
  182. // NMPA认证时间
  183. nmpaTime: [{
  184. type: 'string',
  185. required: true,
  186. message: 'NMPA认证时间不能为空',
  187. trigger: ['change','blur']
  188. }],
  189. // 产品认证
  190. authImageList: [{
  191. type: 'array',
  192. required: true,
  193. message: '产品认证图片不能为空',
  194. trigger: 'change'
  195. }],
  196. // 适应人群
  197. adaptiveMan: [{
  198. type: 'string',
  199. required: true,
  200. message: '适应人群不能为空',
  201. trigger: 'change'
  202. }],
  203. // 不适应人群
  204. unAdaptiveMan: [{
  205. type: 'string',
  206. required: true,
  207. message: '不适应人群不能为空',
  208. trigger: 'change'
  209. }],
  210. // 术前术后
  211. aroundOperation: [{
  212. type: 'string',
  213. required: true,
  214. message: '术前术后内容不能为空',
  215. trigger: 'change'
  216. }],
  217. // 效果展示
  218. displayImageList: [{
  219. type: 'array',
  220. required: true,
  221. message: '效果展示图片不能为空',
  222. trigger: 'change'
  223. }],
  224. // 常见问题
  225. questionList: [{
  226. type: 'array',
  227. required: true,
  228. message: '常见问题不能为空',
  229. trigger: ['change','blur'],
  230. valid: ['question', 'answer']
  231. }],
  232. // 产品类别
  233. typeId: [{
  234. type: 'number',
  235. required: true,
  236. message: '产品类别不能为空',
  237. trigger: 'change'
  238. }],
  239. // 产品状态
  240. status: [{
  241. type: 'number',
  242. required: true,
  243. message: '状态不能为空',
  244. trigger: 'change'
  245. }],
  246. },
  247. },
  248. created() {
  249. this.getTypeList();
  250. this.getProductFormData();
  251. this.resetQuestionListLimit();
  252. this.resetParamsListLimit();
  253. this.registerForm(this.formData, this.rules);
  254. },
  255. mounted: function () {
  256. $('.navLayout').find('.navList').removeClass("on").find('.con').hide().find('a').removeClass("on");
  257. $('.navLayout').find('.navList').eq(3).addClass("on").find('.con').show().find('a').eq(0).addClass("on");
  258. },
  259. methods: {
  260. // 获取分类列表
  261. getTypeList() {
  262. const that = this;
  263. SupplierApi.ShopBaikeProductTypeList({commodityType: this.commodityType}, function (res) {
  264. that.typeList = res.data;
  265. console.log(res);
  266. })
  267. },
  268. // 初始化表单数据
  269. initFormData(formData){
  270. console.log(formData);
  271. // 初始化this.formData
  272. for (var key in this.formData) {
  273. if (formData.hasOwnProperty(key)) {
  274. // this.$set(this.formData, key, formData[key]);
  275. this.formData[key] = formData[key];
  276. }
  277. }
  278. this.formData.marketTime = dateFormat(new Date(this.formData.marketTime), 'yyyy-MM-dd');
  279. this.formData.nmpaTime = dateFormat(new Date(this.formData.nmpaTime), 'yyyy-MM-dd');
  280. // 初始化图片列表 imageList authImageList displayImageList
  281. this.imageList = this.initPreviewImageList([formData.image], { name: '授权图片' });
  282. this.authImageList = this.initPreviewImageList(formData.authImageList, { name: '授权图片' });
  283. this.displayImageList = this.initPreviewImageList(formData.displayImageList, { name: '效果图' });
  284. },
  285. // 初始化图片
  286. initPreviewImageList(imageList, options){
  287. if(!options){
  288. options.name = '图片'
  289. }
  290. return imageList.map(function(item){
  291. return {
  292. name: options.name,
  293. url: item
  294. }
  295. });
  296. },
  297. // 获取表单数据
  298. getProductFormData() {
  299. this.productId = localStorage.getItem('productId');
  300. localStorage.removeItem('productId');
  301. if(!this.productId) return;
  302. const that = this;
  303. SupplierApi.GetShopBaikeProductForm({ productId: this.productId }, function(res){
  304. if(res.code === 0){
  305. that.initFormData(res.data);
  306. }else{
  307. CAIMEI.dialog('获取产品数据失败', false);
  308. setTimeout(function () {
  309. that.handleBack();
  310. }, 2000);
  311. }
  312. });
  313. },
  314. // 保存表单数据
  315. saveFormData() {
  316. const that = this;
  317. const params = {
  318. ...this.formData,
  319. shopId: this.shopId,
  320. commodityType: this.commodityType,
  321. paramList: JSON.stringify(this.formData.paramList),
  322. questionList: JSON.stringify(this.formData.questionList)
  323. };
  324. if(this.productId) params.productId = this.productId;
  325. SupplierApi.ShopBaikeProductSave(params, function(res){
  326. if(res.code === 0) {
  327. CAIMEI.dialog('保存成功', false);
  328. setTimeout(function () {
  329. that.handleBack();
  330. }, 2000);
  331. }else{
  332. CAIMEI.Alert(res.msg, '确认', false)
  333. }
  334. })
  335. },
  336. // 返回文章列表页面
  337. handleBack: function handleBack() {
  338. localStorage.removeItem('target-name');
  339. window.open('/supplier/encyclopedia/product-list.html', 'supplier-product-list');
  340. window.close();
  341. },
  342. // 保存事件
  343. handleSave() {
  344. console.log(this.formData);
  345. const that = this;
  346. // that.saveFormData();
  347. this.validFormData(this.formData, this.rules).then(res=>{
  348. that.saveFormData()
  349. }).catch(()=>{
  350. CAIMEI.dialog('必填项不能为空', false)
  351. })
  352. },
  353. // 上传封面图片
  354. handleUploadProductImage(files){
  355. this.uploadImage(files[0]).then(res=>{
  356. this.formData.image = res;
  357. this.imageList.push({ url: res , name: '封面'})
  358. }).catch(err=>{
  359. console.log(err)
  360. })
  361. },
  362. // 删除上传的封面图片
  363. handleRemoveProductImage(){
  364. this.formData.image = ''
  365. },
  366. // 上传授权图片
  367. handleUploadAuthImage(files){
  368. const that = this;
  369. files.forEach(function (file) {
  370. if(file.type){
  371. // resultFiles 是 input 中选中的文件列表
  372. // insertImgFn 是获取图片 url 后,插入到编辑器的方法
  373. that.uploadImage(file).then(function (res) {
  374. that.formData.authImageList.push(res);
  375. that.authImageList.push({ url: res, name: '图片' })
  376. });
  377. }
  378. });
  379. },
  380. // 删除上传的授权图片
  381. handleRemoveAuthImage(index){
  382. this.formData.authImageList.splice(index, 1);
  383. this.authImageList.splice(index, 1);
  384. console.log(this.formData.authImageList);
  385. console.log(this.authImageList);
  386. },
  387. // 上传效果展示图片
  388. handleUploadDisplayImageList(files){
  389. const that = this;
  390. files.forEach(function (file) {
  391. if(file.type){
  392. // resultFiles 是 input 中选中的文件列表
  393. // insertImgFn 是获取图片 url 后,插入到编辑器的方法
  394. that.uploadImage(file).then(function (res) {
  395. that.formData.displayImageList.push(res);
  396. that.displayImageList.push({ url: res, name: '图片' })
  397. });
  398. }
  399. });
  400. },
  401. // 删除上传的效果展示图片
  402. handleRemoveDisplayImageList(index){
  403. this.formData.displayImageList.splice(index, 1);
  404. this.displayImageList.splice(index, 1);
  405. },
  406. // 上传图片
  407. uploadImage: function uploadImage(file) {
  408. var formData = new FormData();
  409. formData.append('file', file);
  410. return new Promise(function (resolve, reject) {
  411. PublicApi.uploadimg(formData, function (res) {
  412. if (res.code === 0) {
  413. resolve(res.data);
  414. }
  415. reject();
  416. });
  417. });
  418. },
  419. // 初始化问题表单个数
  420. resetParamsListLimit() {
  421. for (let i = 0; i < this.paramsMinLimit; i++) {
  422. this.formData.paramList.push({
  423. name: '',
  424. content: ''
  425. });
  426. }
  427. },
  428. // 移除参数
  429. handleRemoveParams(params) {
  430. console.log(params);
  431. this.formData.paramList.splice(params.index, 1);
  432. },
  433. // 添加参数
  434. handleAddParams() {
  435. this.formData.paramList.push({
  436. name: '',
  437. content: ''
  438. });
  439. this.registerForm(this.formData, this.rules);
  440. },
  441. // 初始化问题表单个数
  442. resetQuestionListLimit() {
  443. for (let i = 0; i < this.questionMinLimit; i++) {
  444. this.formData.questionList.push({
  445. question: '',
  446. answer: '',
  447. });
  448. }
  449. },
  450. // 移除问题
  451. handleRemoveQuestion(question) {
  452. this.formData.paramList.splice(question.index, 1);
  453. },
  454. // 添加问题
  455. handleAddQuestion() {
  456. this.formData.questionList.push({
  457. question: '',
  458. answer: '',
  459. });
  460. this.registerForm(this.formData, this.rules);
  461. },
  462. }
  463. });