add.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <div class="app-content">
  3. <!-- 商品基本信息 -->
  4. <form @submit="formSubmit">
  5. <div class="module-com input-line-wrap">
  6. <tui-list-cell class="line-cell" :hover="false">
  7. <div class="tui-title required">名称</div>
  8. <input
  9. v-model="form.goodsName"
  10. placeholder-class="phcolor"
  11. class="tui-input"
  12. name="goodsName"
  13. placeholder="请输入"
  14. maxlength="50"
  15. type="text"
  16. />
  17. </tui-list-cell>
  18. <tui-list-cell class="line-cell" :hover="false">
  19. <div class="tui-title">已售</div>
  20. <input
  21. v-model="form.sold"
  22. placeholder-class="phcolor"
  23. class="tui-input"
  24. name="sold"
  25. placeholder="请输入已售数量"
  26. maxlength="50"
  27. type="number"
  28. />
  29. </tui-list-cell>
  30. <tui-list-cell class="line-cell" :hover="false">
  31. <div class="tui-title">库存</div>
  32. <input
  33. v-model="form.stock"
  34. placeholder-class="phcolor"
  35. class="tui-input"
  36. name="stock"
  37. placeholder="请输入库存,留空表示库存充足"
  38. maxlength="50"
  39. type="number"
  40. />
  41. </tui-list-cell>
  42. <tui-list-cell class="line-cell category-wrap" :hover="false">
  43. <div class="tui-title">类型</div>
  44. <div class="tui-input">
  45. <block v-for="(item, index) in categoryData" :key="index">
  46. <button
  47. class="admin-button-com"
  48. :class="[(form.categoryIdList.includes(item.id + '')||form.categoryIdList.includes(item.id*1)) ? '' : 'default']"
  49. @click="selCategoryFn(item)"
  50. >{{ item.categoryName || '暂无' }}</button>
  51. </block>
  52. </div>
  53. </tui-list-cell>
  54. <tui-list-cell class="line-cell" :hover="false">
  55. <div class="tui-title required">价格</div>
  56. <input
  57. v-model="form.price"
  58. placeholder-class="phcolor"
  59. class="tui-input"
  60. name="price"
  61. placeholder="请输入"
  62. maxlength="50"
  63. type="number"
  64. />
  65. <div class="tui-prompt">元</div>
  66. </tui-list-cell>
  67. <!-- <tui-list-cell class="line-cell" :arrow="true">
  68. <div class="tui-title">分类</div>
  69. <div v-if="form.receiveFullAddress" class="tui-input">{{ form.receiveFullAddress }}</div>
  70. <div v-else class="tui-placeholder">请选择</div>
  71. </tui-list-cell>-->
  72. <tui-list-cell class="line-cell" :hover="false">
  73. <div class="tui-title">节日涨价</div>
  74. <label for="autoPrice" class="tui-input" @click="autoRiseChange(form.autoRise)">
  75. <checkbox
  76. id="autoPrice"
  77. class="ljd-checkbox"
  78. :checked="form.autoRise == 1"
  79. style="transform: scale(0.7,0.7);"
  80. ></checkbox>
  81. <span>需要</span>
  82. </label>
  83. </tui-list-cell>
  84. <tui-list-cell class="line-cell" :hover="false">
  85. <div class="tui-title">配送方式</div>
  86. <label for="sendType" class="tui-input" @click="needSendChange(form.needSend)">
  87. <checkbox
  88. id="sendType"
  89. class="ljd-checkbox"
  90. :checked="form.needSend == 1"
  91. @change="giftChange"
  92. style="transform: scale(0.7,0.7);"
  93. ></checkbox>
  94. <span>到店自取</span>
  95. </label>
  96. </tui-list-cell>
  97. <tui-list-cell class="line-cell" :hover="false">
  98. <div class="tui-title">运费</div>
  99. <radio-group class="tui-input flex-center-between" @change="freightChange">
  100. <label class="list" for="customLink">
  101. <radio
  102. id="customLink"
  103. value="0"
  104. :checked="form.freightType == 0"
  105. style="transform: scale(0.8,0.8);"
  106. />
  107. <span class="checkbox-text">按距离收费</span>
  108. </label>
  109. <label class="list" for="goodsLink">
  110. <radio
  111. id="goodsLink"
  112. value="1"
  113. :checked="form.freightType == 1"
  114. style="transform: scale(0.8,0.8);"
  115. />
  116. <span class="checkbox-text">免运费</span>
  117. </label>
  118. </radio-group>
  119. </tui-list-cell>
  120. </div>
  121. <!-- 商品图片 -->
  122. <div class="module-com input-line-wrap goods-wrap">
  123. <div class="module-tit">
  124. <span>商品图片</span>
  125. <span class="app-color-3">(最多上传4张)</span>
  126. </div>
  127. <div class="module-det">
  128. <app-uploader ref="appUploader" :imgList.sync="form.shopImg" :isMain="true" :num="4" />
  129. </div>
  130. </div>
  131. <!-- 商品简介 -->
  132. <div class="module-com input-line-wrap summary-wrap">
  133. <div class="module-tit">商品简介</div>
  134. <div class="module-det" style="padding-bottom: 10px">
  135. <textarea
  136. v-model="form.briefContent"
  137. class="goods-summary"
  138. placeholder-class="phcolor"
  139. placeholder="请输入商品简介"
  140. />
  141. </div>
  142. </div>
  143. <!-- button -->
  144. <div class="app-footer">
  145. <button class="admin-button-com middle default" formType="submit" @click="status = 0">加入仓库</button>
  146. <button class="admin-button-com middle blue" formType="submit" @click="status = 1">上架</button>
  147. </div>
  148. </form>
  149. </div>
  150. </template>
  151. <script>
  152. import { mapGetters } from "vuex";
  153. import TuiListCell from "@/components/plugin/list-cell";
  154. import AppUploader from "@/components/app-uploader";
  155. const form = require("@/utils/formValidation.js");
  156. import { getCategory } from "@/utils/config";
  157. import { getDetailB, addB, updateB } from "@/api/goods";
  158. export default {
  159. name: "goods-add",
  160. components: {
  161. TuiListCell,
  162. AppUploader
  163. },
  164. data() {
  165. return {
  166. status: 1,
  167. form: {
  168. goodsName: "",
  169. stock: "",
  170. price: "",
  171. categoryIdList: [],
  172. autoRise: "1",
  173. needSend: "0",
  174. freightType: "0",
  175. shopImg: [],
  176. briefContent: ""
  177. }
  178. };
  179. },
  180. computed: {
  181. ...mapGetters({ categoryData: "getCategory" })
  182. },
  183. onLoad() {
  184. // this.init()
  185. },
  186. watch:{
  187. categoryData(val){
  188. },
  189. 'form.categoryIdList'(val){
  190. }
  191. },
  192. methods: {
  193. init() {
  194. getCategory()
  195. .then(() => {
  196. if (this.option.id) {
  197. uni.setNavigationBarTitle({
  198. title: '修改商品'
  199. });
  200. this._getDet();
  201. }
  202. });
  203. },
  204. _getDet() {
  205. return getDetailB({ id: this.option.id })
  206. .then(res => {
  207. if (this.$util.isEmpty(res.data)) {
  208. return;
  209. }
  210. Object.keys(this.form).forEach((i, index) => {
  211. this.form[i] = res.data[i];
  212. });
  213. if (this.form.stock == -1) {
  214. this.$set(this.form, "stock", "");
  215. }
  216. this.$set(this.form, "sold", res.data.sold);
  217. this.$set(this.form, "shopImg", res.data.imgList)
  218. });
  219. },
  220. // 确认
  221. confirmFn() {
  222. if (this.form.categoryIdList == 0) {
  223. this.$msg("请选择分类");
  224. return false;
  225. }
  226. if (this.form.shopImg.length == 0) {
  227. this.$msg("请上传图片");
  228. return false;
  229. }
  230. let host = this.option.id ? updateB : addB;
  231. let form = JSON.parse(JSON.stringify(this.form));
  232. form.shopImg = this.$util.imgSubstr(form.shopImg);
  233. if (form.stock == "") {
  234. form.stock = -1;
  235. }
  236. if(this.option.id){
  237. form.id = this.option.id
  238. }
  239. host({
  240. status: this.status,
  241. ...form
  242. }).then(res => {
  243. uni.navigateBack({});
  244. this.$msg("操作成功!");
  245. });
  246. },
  247. selCategoryFn(item) {
  248. let index = this.form.categoryIdList.findIndex(e => e == item.id);
  249. if (index == -1) {
  250. this.form.categoryIdList.push(item.id);
  251. } else {
  252. this.form.categoryIdList.splice(index, 1);
  253. }
  254. },
  255. autoRiseChange(e) {
  256. this.form.autoRise = e == "0" ? "1" : "0";
  257. },
  258. needSendChange(e) {
  259. this.form.needSend = e == "0" ? "1" : "0";
  260. },
  261. freightChange(e) {
  262. this.form.freightType = e.detail.value;
  263. },
  264. // 表单验证
  265. formSubmit(e) {
  266. // 表单规则
  267. let rules = [
  268. {
  269. name: "goodsName",
  270. rule: ["required"],
  271. msg: ["请输入商品名"]
  272. },
  273. {
  274. name: "price",
  275. rule: ["required"],
  276. msg: ["请输入价格"]
  277. }
  278. ];
  279. // 进行表单检查
  280. let formData = e.detail.value;
  281. let checkRes = form.validation(formData, rules);
  282. // 验证通过!
  283. if (!checkRes) {
  284. setTimeout(() => {
  285. this.confirmFn();
  286. });
  287. } else {
  288. this.$msg(checkRes);
  289. }
  290. }
  291. }
  292. };
  293. </script>
  294. <style lang="scss" scoped>
  295. .app-content {
  296. min-height: calc(100vh - 100px);
  297. padding-bottom: 100px;
  298. }
  299. // 公共模块
  300. .module-com {
  301. margin-bottom: 20px;
  302. background-color: #fff;
  303. color: $fontColor2;
  304. .module-tit {
  305. padding: 20px 30px;
  306. font-size: 28px;
  307. }
  308. .module-det {
  309. padding: 0 30px;
  310. }
  311. }
  312. .category-wrap {
  313. align-items: flex-start;
  314. padding-bottom: 0 !important;
  315. .tui-input {
  316. @include disFlex(center, flex-start);
  317. flex-wrap: wrap;
  318. .admin-button-com {
  319. // width: 104px;
  320. margin-right: 20px;
  321. margin-bottom: 20px;
  322. position: relative;
  323. z-index: 9999;
  324. // &:nth-child(4n + 1) {
  325. // margin-left: 0;
  326. // }
  327. }
  328. }
  329. }
  330. .summary-wrap {
  331. .goods-summary {
  332. height: 400px;
  333. // font-size: 24px;
  334. }
  335. }
  336. // button
  337. .app-footer {
  338. justify-content: space-evenly;
  339. z-index: 9999;
  340. .admin-button-com {
  341. width: 46%;
  342. }
  343. }
  344. </style>