addGoods.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <view>
  3. <!-- <NavBar titleText="添加商品" textSize="34rpx" iconColor="#333333" textColor="#333333" isScrolling showBack></NavBar> -->
  4. <view class="p-20">
  5. <view class="w-full bg--w111-fff rd-16rpx pt-32 pr-30 pb-32 pl-30">
  6. <view class="fs-30 fw-500 lh-42rpx">商品信息</view>
  7. <view class="mt-30 flex-between-center">
  8. <text class="fs-30 lh-42rpx">商品名称</text>
  9. <text class="text-24 text--w111-666">{{setFormData.store_name.length}}/40</text>
  10. </view>
  11. <view class="w-full bg--w111-f5f5f5 rd-12rpx p-20 mt-12">
  12. <textarea v-model="setFormData.store_name"
  13. :maxlength="40"
  14. placeholder="请填写商品名称" placeholder-class="text--w111-ccc"
  15. class="fs-30" auto-height />
  16. </view>
  17. <view class="mt-40">
  18. <text class="fs-30 lh-42rpx">商品图片</text>
  19. </view>
  20. <view class="fs-22 text--w111-999 mt-12">建议:图片尺寸为750*750px,最多上传9张</view>
  21. <view class="grid-column-4 grid-gap-8rpx mt-20">
  22. <view class="relative h-156" v-for="(item,index) in setFormData.slider_image" :key="index">
  23. <image :src="item" mode="aspectFill" class="w-full h-156 rd-12rpx"></image>
  24. <view class="abs-rt w-32 h-32 of0b21 flex-center fs-24" @click="DelPic(index)">
  25. <text class="iconfont icon-iconfontguanbi text--w111-fff fs-20"></text>
  26. </view>
  27. </view>
  28. <view class="h-156 flex-col flex-center upload bg--w111-f5f5f5 text--w111-999 rd-12rpx"
  29. @click="uploadPicture(9)" v-if="setFormData.slider_image.length < 9">
  30. <text class="iconfont icon-paizhao fs-40"></text>
  31. <text class="fs-24 lh-34rpx pt-8">上传图片</text>
  32. </view>
  33. </view>
  34. </view>
  35. <view class="w-full bg--w111-fff rd-16rpx mt-22 px-30">
  36. <view class="h-106 flex-between-center bb-e" @click="selectCate">
  37. <text class="fs-30 lh-42rpx">商品分类</text>
  38. <view class="flex-y-center">
  39. <text class="fs-30 text--w111-333 pr-12" v-if="setFormData.cate_id.length">已选择</text>
  40. <text class="fs-30 text--w111-999 pr-12" v-else>请选择分类</text>
  41. <text class="iconfont icon-ic_rightarrow fs-36 text--w111-999"></text>
  42. </view>
  43. </view>
  44. <view class="h-106 flex-between-center bb-e">
  45. <text class="fs-30 lh-42rpx">单位</text>
  46. <view class="flex-1 flex justify-end text-right">
  47. <input type="text" maxlength="1" v-model="setFormData.unit_name" placeholder="请填写商品单位" placeholder-class="text--w111-999 fs-30" class="fs-32 fs-30" />
  48. </view>
  49. </view>
  50. </view>
  51. <view class="w-full bg--w111-fff rd-16rpx mt-22 pt-32 pr-30 pl-30">
  52. <view class="fs-30 fw-500 lh-42rpx">规格设置</view>
  53. <view class="h-106 flex-between-center bb-e">
  54. <text class="fs-30 lh-42rpx">售价</text>
  55. <view class="flex-1 flex justify-end text-right">
  56. <input type="digit" v-model="setFormData.attr.price" placeholder="请输入售价" placeholder-class=" text--w111-999" class="fs-32" />
  57. </view>
  58. </view>
  59. <view class="h-106 flex-between-center bb-e">
  60. <text class="fs-30 lh-42rpx">成本价</text>
  61. <view class="flex-1 flex justify-end text-right">
  62. <input type="digit" v-model="setFormData.attr.cost" placeholder="请输入成本价" placeholder-class=" text--w111-999" class="fs-32" />
  63. </view>
  64. </view>
  65. <view class="h-106 flex-between-center bb-e">
  66. <text class="fs-30 lh-42rpx">划线价</text>
  67. <view class="flex-1 flex justify-end text-right">
  68. <input type="digit" v-model="setFormData.attr.ot_price" placeholder="请输入划线价" placeholder-class=" text--w111-999" class="fs-32" />
  69. </view>
  70. </view>
  71. <view class="h-106 flex-between-center bb-e">
  72. <text class="fs-30 lh-42rpx">库存</text>
  73. <view class="flex-1 flex justify-end text-right">
  74. <input type="number" v-model="setFormData.attr.stock" placeholder="请输入库存" placeholder-class=" text--w111-999" class="fs-32" />
  75. </view>
  76. </view>
  77. <view class="h-106 flex-between-center bb-e" v-show="isMore">
  78. <text class="fs-30 lh-42rpx">商品编码</text>
  79. <view class="flex-1 flex justify-end text-right">
  80. <input type="number" v-model="setFormData.attr.bar_code" placeholder="请输入规格编码" placeholder-class=" text--w111-999" class="fs-32" />
  81. </view>
  82. </view>
  83. <view class="h-106 flex-between-center bb-e" v-show="isMore">
  84. <text class="fs-30 lh-42rpx">条形码</text>
  85. <view class="flex-1 flex justify-end text-right">
  86. <input type="number" v-model="setFormData.attr.bar_code_number" placeholder="请输入条形码" placeholder-class=" text--w111-999" class="fs-32" />
  87. </view>
  88. </view>
  89. <view class="h-106 flex-between-center bb-e" v-show="isMore">
  90. <text class="fs-30 lh-42rpx">重量</text>
  91. <view class="flex-1 flex justify-end text-right">
  92. <input type="number" v-model="setFormData.attr.weight" placeholder="请输入重量" placeholder-class=" text--w111-999" class="fs-32" />
  93. </view>
  94. </view>
  95. <view class="h-106 flex-between-center bb-e" v-show="isMore">
  96. <text class="fs-30 lh-42rpx">体积</text>
  97. <view class="flex-1 flex justify-end text-right">
  98. <input type="number" v-model="setFormData.attr.volume" placeholder="请输入体积" placeholder-class=" text--w111-999" class="fs-32" />
  99. </view>
  100. </view>
  101. <view class="h-106 flex-center text--w111-666" @tap="toggleMore">
  102. <text class="fs-26">{{isMore ? '收起' : '展开'}}</text>
  103. <text class="iconfont fs-26" :class="isMore ? 'icon-xiangshang2' : 'icon-xiala3'"></text>
  104. </view>
  105. </view>
  106. <view class="w-full bg--w111-fff rd-16rpx mt-22 pt-32 pr-30 pl-30 pb-32">
  107. <view class="fs-30 lh-42rpx">商品详情</view>
  108. <view class="fs-22 text--w111-999 mt-12">建议:图片尺寸为750*750px,最多上传10张</view>
  109. <view class="grid-column-4 grid-gap-8rpx mt-20">
  110. <view class="relative h-156" v-for="(item,index) in contentPicture" :key="index">
  111. <image :src="item" mode="aspectFill" class="w-full h-156 rd-12rpx"></image>
  112. <view class="abs-rt w-32 h-32 of0b21 flex-center fs-24" @click="DelPic(index,'content')">
  113. <text class="iconfont icon-iconfontguanbi text--w111-fff fs-20"></text>
  114. </view>
  115. </view>
  116. <view class="h-156 flex-col flex-center upload bg--w111-f5f5f5 text--w111-999 rd-12rpx"
  117. @click="uploadContentPicture(10)" v-if="contentPicture.length < 10">
  118. <text class="iconfont icon-paizhao fs-40"></text>
  119. <text class="fs-24 lh-34rpx pt-8">上传图片</text>
  120. </view>
  121. </view>
  122. </view>
  123. <view class="w-full bg--w111-fff rd-16rpx mt-22 pt-32 pr-30 pl-30">
  124. <view class="fs-30 lh-42rpx fw-500">其他设置</view>
  125. <view class="h-106 flex-between-center">
  126. <view class="fs-30 lh-42rpx">配送方式</view>
  127. <view class="flex-y-center">
  128. <checkbox-group class="flex-y-center" @change="deliveryWayChange">
  129. <label class="ml-48" v-for="(val, i) in deliveryFreeList" :key="val.value">
  130. <view>
  131. <checkbox :value="val.value" :checked="setFormData.logistics.includes(val.value)" style="transform:scale(0.8)" />
  132. <text class="relative top-2">{{ val.name }}</text>
  133. </view>
  134. </label>
  135. </checkbox-group>
  136. </view>
  137. </view>
  138. <view class="h-106 flex-between-center" v-show="setFormData.logistics.includes('1')">
  139. <view class="fs-30 lh-42rpx">运费设置</view>
  140. <radio-group class="flex-y-center" @change="feightChange">
  141. <label class="flex-y-center fs-30">
  142. <view>
  143. <radio value="2" :checked="setFormData.freight == 2" />
  144. </view>
  145. <view>固定邮费</view>
  146. </label>
  147. <label class="flex-y-center fs-30 ml-48">
  148. <view>
  149. <radio value="3" :checked="setFormData.freight == 3" />
  150. </view>
  151. <view>运费模板</view>
  152. </label>
  153. </radio-group>
  154. </view>
  155. <view v-show="setFormData.logistics.includes('1')">
  156. <view class="h-106 flex-between-center" v-if="setFormData.freight == 2">
  157. <text class="fs-30 lh-42rpx">固定邮费</text>
  158. <view class="flex-1 flex justify-end text-right">
  159. <input type="digit" v-model="setFormData.postage" placeholder="请输入金额" placeholder-class=" text--w111-999" class="fs-32" />
  160. </view>
  161. </view>
  162. <view class="h-106 flex-between-center"
  163. v-if="setFormData.freight == 3">
  164. <text class="fs-30 lh-42rpx">运费模板</text>
  165. <view class="flex-y-center">
  166. <picker @change="bindPickerChange" :value="tempIndex" :range="templateList" range-key="name">
  167. <view class="fs-30">{{templateList[tempIndex].name || '请选择'}}
  168. <text class="iconfont icon-ic_rightarrow"></text>
  169. </view>
  170. </picker>
  171. </view>
  172. </view>
  173. </view>
  174. </view>
  175. <view class="pb-safe">
  176. <view class="h-200"></view>
  177. </view>
  178. <view class="fixed-lb w-full pb-safe bg--w111-fff z-10">
  179. <view class="footer-box flex-center">
  180. <view class="w-690 h-88 flex-center bg-mer text--w111-fff fs-28 rd-44rpx" @tap="confirmSave">提交</view>
  181. </view>
  182. </view>
  183. </view>
  184. <classify
  185. ref="classify"
  186. :type="1"
  187. :visible="visibleClass"
  188. @closeDrawer="()=>{visibleClass = false}"
  189. @successChange="successChange"
  190. ></classify>
  191. <canvas canvas-id="canvas" v-if="canvasStatus"
  192. :style="{width: canvasWidth + 'px', height: canvasHeight + 'px',position: 'absolute',left:'-100000px',top:'-100000px'}"></canvas>
  193. </view>
  194. </template>
  195. <script>
  196. import NavBar from "@/components/NavBar.vue";
  197. import classify from "./components/classify/index.vue";
  198. import { getTemplateOption, productCreate } from "@/api/admin.js"
  199. export default {
  200. name: "addGoods",
  201. components: { NavBar, classify },
  202. data(){
  203. return {
  204. canvasWidth: "",
  205. canvasHeight: "",
  206. canvasStatus: false,
  207. setFormData: {
  208. image: '', //主图
  209. attr: {
  210. price: "",
  211. cost: "",
  212. ot_price: "",
  213. stock: "",
  214. bar_code: "",
  215. bar_code_number: "",
  216. weight: "",
  217. volume: "",
  218. is_default_select: 0,
  219. is_show : 1,
  220. pic: ""
  221. },
  222. slider_image: [],
  223. store_name: '',
  224. cate_id: '',
  225. unit_name: '',
  226. spec_type: 0,
  227. logistics: ['1','2'],
  228. freight: 2,
  229. temp_id: 0,
  230. content: "",
  231. is_show: 0,
  232. postage: 0
  233. },
  234. deliveryFreeList: [
  235. {value: '1',name: '快递'},
  236. {value: '2',name: '到店'},
  237. ],
  238. contentPicture: [],
  239. isMore: false,
  240. visibleClass: false,
  241. templateList: [],
  242. tempIndex: 0
  243. }
  244. },
  245. onLoad(){
  246. this.getTemlate();
  247. },
  248. methods: {
  249. uploadPicture(){
  250. let that = this;
  251. this.canvasStatus = true
  252. that.$util.uploadImageChange({ count: 9, url: 'upload/image' }, function(res) {
  253. that.setFormData.slider_image.push(res.data.url);
  254. if(that.setFormData.slider_image.length >= 9) that.setFormData.slider_image.length = 9;
  255. that.setFormData.image = that.setFormData.slider_image[0];
  256. that.setFormData.attr.pic = that.setFormData.slider_image[0];
  257. }, (res) => {
  258. this.canvasStatus = false
  259. }, (res) => {
  260. this.canvasWidth = res.w
  261. this.canvasHeight = res.h
  262. });
  263. },
  264. uploadContentPicture(){
  265. let that = this;
  266. this.canvasStatus = true
  267. that.$util.uploadImageChange({ count: 9, url: 'upload/image' }, function(res) {
  268. that.contentPicture.push(res.data.url);
  269. }, (res) => {
  270. this.canvasStatus = false
  271. }, (res) => {
  272. this.canvasWidth = res.w
  273. this.canvasHeight = res.h
  274. });
  275. },
  276. /**
  277. * 将图片链接数组转换为富文本 HTML
  278. * @param {string[]} urls 图片链接数组
  279. * @param {object} opts 可选项
  280. * @param {string} opts.wrapTag 包裹标签,默认 'p'
  281. * @param {string} opts.className img 的 class,默认 ''
  282. * @param {string} opts.style 内联样式,例如 'max-width:100%;height:auto;'
  283. * @param {string} opts.alt 默认的 alt 文本
  284. * @returns {string} 富文本 HTML 字符串
  285. */
  286. buildEditorImageHtml(
  287. urls = [],
  288. { wrapTag = 'p', className = '', style = 'max-width:100%;height:auto;', alt = '图片' } = {}
  289. ) {
  290. const clean = (v) => String(v).replace(/[`'"]/g, '').trim(); // 去除反引号/引号/空格
  291. const isUrl = (u) => /^https?:\/\/.+/i.test(u); // 简单校验 http/https
  292. return urls
  293. .filter(Boolean)
  294. .map(clean)
  295. .filter(isUrl)
  296. .map((url) => {
  297. const cls = className ? ` class="${className}"` : '';
  298. const sty = style ? ` style="${style}"` : '';
  299. return `<${wrapTag}><img src="${url}" alt="${alt}"${cls}${sty} /></${wrapTag}>`;
  300. })
  301. .join('');
  302. },
  303. DelPic(index, type) {
  304. if(type){
  305. this.contentPicture.splice(index,1);
  306. }else{
  307. this.setFormData.slider_image.splice(index, 1);
  308. }
  309. },
  310. selectCate(){
  311. this.$refs.classify.category('', this.setFormData.cate_id.toString());
  312. setTimeout(()=> {
  313. this.visibleClass = true;
  314. }, 200);
  315. },
  316. successChange(val) {
  317. this.setFormData.cate_id = val;
  318. this.visibleClass = false;
  319. },
  320. toggleMore(){
  321. this.isMore = !this.isMore;
  322. },
  323. // 送货方式选择
  324. deliveryWayChange(obj) {
  325. this.setFormData.logistics = obj.detail.value;
  326. },
  327. feightChange(val){
  328. this.setFormData.freight = val.detail.value;
  329. },
  330. getTemlate(){
  331. getTemplateOption().then(res=>{
  332. this.templateList = res.data;
  333. })
  334. },
  335. bindPickerChange(e){
  336. this.tempIndex = e.detail.value;
  337. this.setFormData.temp_id = this.templateList[this.tempIndex].id;
  338. },
  339. confirmSave(){
  340. if(!this.setFormData.store_name) return this.$util.Tips({title: '请输入商品名称'});
  341. if(!this.setFormData.image) return this.$util.Tips({title: '请上传商品图片'});
  342. if(!this.setFormData.cate_id) return this.$util.Tips({title: '请选择商品分类'});
  343. if(!this.setFormData.unit_name) return this.$util.Tips({title: '请填写商品单位'});
  344. if(!this.setFormData.attr.price) return this.$util.Tips({title: '请填写商品售价'});
  345. if(!this.setFormData.attr.cost) return this.$util.Tips({title: '请填写商品成本价'});
  346. if(!this.setFormData.attr.ot_price) return this.$util.Tips({title: '请填写商品划线价'});
  347. if(!this.setFormData.attr.stock) return this.$util.Tips({title: '请填写商品库存'});
  348. if(!this.setFormData.logistics.length) return this.$util.Tips({title: '请选择配送方式'});
  349. if(this.setFormData.freight == 3 && this.setFormData.temp_id == 0) return this.$util.Tips({title: '请选择运费模版'});
  350. const html = this.buildEditorImageHtml(this.contentPicture);
  351. this.$set(this.setFormData,'content',html);
  352. productCreate(this.setFormData).then(res=>{
  353. uni.showToast({
  354. title: "提交成功",
  355. icon: 'none'
  356. })
  357. uni.redirectTo({
  358. url:'/pages/admin/goods/index'
  359. })
  360. }).catch(err=>{
  361. uni.showToast({
  362. title: err,
  363. icon: 'none'
  364. })
  365. })
  366. }
  367. }
  368. }
  369. </script>
  370. <style lang="scss">
  371. ::v-deep uni-radio .uni-radio-input.uni-radio-input-checked,
  372. ::v-deep radio .wx-radio-input.wx-radio-input-checked {
  373. border: 1px solid $primary-admin!important;
  374. background-color: $primary-admin!important;
  375. }
  376. ::v-deep checkbox .uni-checkbox-input.uni-checkbox-input-checked,
  377. ::v-deep checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  378. border: 1px solid $primary-admin!important;
  379. background-color: $primary-admin!important;
  380. color: #fff!important;
  381. }
  382. ::v-deep uni-checkbox .uni-checkbox-input, wx-checkbox .wx-checkbox-input{
  383. border-radius: unset !important;
  384. }
  385. .grid-gap-8rpx {
  386. grid-gap: 8rpx;
  387. gap: 8rpx;
  388. }
  389. .of0b21{
  390. background-color: rgba(0,0,0,0.5);
  391. border-radius: 0 12rpx 0 12rpx;
  392. }
  393. .bb-e{
  394. border-bottom: 1rpx solid #eee;
  395. }
  396. .icon-a-ic_CompleteSelect{
  397. color: $primary-admin;
  398. }
  399. .bg-mer{
  400. background-color: $primary-admin;
  401. }
  402. .h-156{
  403. height: 156rpx;
  404. }
  405. .pt-8{
  406. padding-top: 8rpx;
  407. }
  408. .px-30{
  409. padding: 0 30rpx;
  410. }
  411. .footer-box {
  412. height: 126rpx;
  413. .w-690 {
  414. width: 690rpx;
  415. }
  416. }
  417. .ml-48{
  418. margin-left: 48rpx;
  419. }
  420. .top-2{
  421. top: 2rpx;
  422. }
  423. </style>