goodForm.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <template>
  2. <view class="container">
  3. <u-form :model="goodInfo" ref="goodInfo" label-width="240">
  4. <u-form-item label="商品名称" prop="productName" required>
  5. <u-input v-model="goodInfo.productName" type="text" placeholder="请输入商品名称" :disabled="ifEdit()"/>
  6. </u-form-item>
  7. <u-form-item label="请上传商品图片(上限5张)" required label-position="top">
  8. <view class="good-img">
  9. <u-image class="good-img-item" width="98px" height="98px" border-radius="5px" v-for="(item, index) in fileList" :src="item"
  10. v-if="ifEdit()"></u-image>
  11. <u-upload :action="uploadUrl" :file-list="defaultList" :form-data="uploadData" @on-success="uploadSuccess"
  12. @on-error="uploadError" @on-remove="uploadRemove" max-count="5" :disabled="ifEdit()"></u-upload>
  13. </view>
  14. </u-form-item>
  15. <u-form-item label="原价" prop="originalPrice" required>
  16. <!-- <u-number-box :positive-integer="false" :min="0.01" step="1" v-model="goodInfo.originalPrice" bg-color="#51A539"
  17. color="#ffffff" :disabled="ifEdit()" digit></u-number-box> -->
  18. <CnumberBox :number="goodInfo.originalPrice" :isDisabled="ifEdit()" :isInt="false" :minNum="minNum" @numPlus="plusPrice($event, 1)" @numReduce="reducePrice($event, 1)" @onKeyInput="onKeyInput($event, 1)"></CnumberBox>
  19. </u-form-item>
  20. <u-form-item :label="formType == 1 ? '售价' : '采摘价格'" prop="bizPrice" required v-if="formType != 2">
  21. <!-- <u-number-box :positive-integer="false" :min="0.01" v-model="goodInfo.bizPrice" bg-color="#51A539" color="#ffffff"
  22. :disabled="ifEdit()" digit></u-number-box> -->
  23. <CnumberBox :number="goodInfo.bizPrice" :isDisabled="ifEdit()" :isInt="false" :minNum="minNum" @numPlus="plusPrice($event, 2)" @numReduce="reducePrice($event, 2)" @onKeyInput="onKeyInput($event, 2)"></CnumberBox>
  24. </u-form-item>
  25. <u-form-item label="起拍价" prop="auctionStartPrice" required v-if="formType == 2">
  26. <!-- <u-number-box :positive-integer="false" :min="0.01" v-model="goodInfo.auctionStartPrice" bg-color="#51A539" color="#ffffff"
  27. :disabled="ifEdit()" digit></u-number-box> -->
  28. <CnumberBox :isDisabled="ifEdit()" :isInt="false" :number="goodInfo.auctionStartPrice" :minNum="minNum" @numPlus="plusPrice($event, 4)" @numReduce="reducePrice($event, 4)" @onKeyInput="onKeyInput($event, 4)"></CnumberBox>
  29. </u-form-item>
  30. <u-form-item label="每次最低加价金额" prop="auctionMinAddPrice" required v-if="formType == 2">
  31. <!-- <u-number-box :positive-integer="false" :min="0.01" v-model="goodInfo.auctionMinAddPrice" bg-color="#51A539" color="#ffffff"
  32. :disabled="ifEdit()" digit></u-number-box> -->
  33. <CnumberBox :isDisabled="ifEdit()" :isInt="false" :number="goodInfo.auctionMinAddPrice" :minNum="minNum" @numPlus="plusPrice($event, 5)" @numReduce="reducePrice($event, 5)" @onKeyInput="onKeyInput($event, 5)"></CnumberBox>
  34. </u-form-item>
  35. <u-form-item label="计量单位" prop="unit" required>
  36. <u-input v-model="goodInfo.unit" type="text" placeholder="请输入计量单位" :disabled="ifEdit()"/>
  37. </u-form-item>
  38. <u-form-item label="库存" prop="stock" required>
  39. <!-- <u-number-box :min="1" v-model="goodInfo.stock" bg-color="#51A539" color="#ffffff" :disabled="ifEdit()"></u-number-box> -->
  40. <CnumberBox :isDisabled="ifEdit()" :number="goodInfo.stock" @numPlus="plusPrice($event, 3)" @numReduce="reducePrice($event, 3)" @onKeyInput="onKeyInput($event, 3)"></CnumberBox>
  41. </u-form-item>
  42. <u-form-item label="商品说明" prop="productDescribe" required>
  43. <u-input v-model="goodInfo.productDescribe" type="text" placeholder="请输入商品说明" :disabled="ifEdit()"/>
  44. </u-form-item>
  45. <u-form-item label="商品分类" right-icon="arrow-right" required @click.native="selectShow = !ifEdit()">
  46. <text>{{sortText}}</text>
  47. </u-form-item>
  48. <u-form-item label="拍卖截止时间" right-icon="arrow-right" required @click.native="dateShow = !ifEdit()" v-if="formType == 2">
  49. <text>{{goodInfo.auctionEndTime}}</text>
  50. </u-form-item>
  51. </u-form>
  52. <view class="handle-fix-box">
  53. <u-button type="success" shape="circle" :ripple="true" :custom-style="customStyle" @click="submitData" :disabled="getPermit()">{{type == 'add' ? '提交' : (type == 'detail' ? '编辑' : '保存')}}</u-button>
  54. </view>
  55. <u-picker mode="time" v-model="dateShow" :start-year="startYear" :params="params" @confirm="setDate"></u-picker>
  56. <u-select v-model="selectShow" mode="mutil-column-auto" :list="sortList" @confirm="setSort"></u-select>
  57. <u-top-tips ref="uTips"></u-top-tips>
  58. <u-keyboard mode="number" v-model="keyboardShow" @change="valChange" @backspace="backspace"></u-keyboard>
  59. </view>
  60. </template>
  61. <script>
  62. const NET = require('@/utils/request')
  63. const API = require('@/config/api')
  64. import CnumberBox from '@/components/CnumberBox.vue'
  65. export default {
  66. components: {
  67. CnumberBox
  68. },
  69. data() {
  70. return {
  71. goodId: '',
  72. type: '',
  73. formType: '1',
  74. goodInfo: {
  75. productName: '',
  76. originalPrice: 1,
  77. bizPrice: 1,
  78. unit: '',
  79. stock: 1,
  80. productDescribe: '',
  81. categoryId: '',
  82. auctionStartPrice: 1,
  83. auctionMinAddPrice: 1,
  84. auctionEndTime: '',
  85. },
  86. selectShow: false,
  87. parentId: '',
  88. sortList: [],
  89. sortText: '',
  90. dateShow: false,
  91. params: {
  92. year: true,
  93. month: true,
  94. day: true,
  95. hour: true,
  96. minute: true,
  97. second: true
  98. },
  99. startYear: '',
  100. uploadData: {
  101. folderId: 0,
  102. },
  103. uploadUrl: '',
  104. fileList: [],
  105. defaultList: [],
  106. keyboardShow: false,
  107. customStyle: { // 底部悬浮按钮样式
  108. height: '40px',
  109. backgroundColor: '#56a83a'
  110. },
  111. minNum: 0.01, // 输入框最小值
  112. }
  113. },
  114. onLoad(options) {
  115. this.uploadUrl = API.uploadFile
  116. this.goodId = options.goodId
  117. this.type = options.type
  118. this.formType = options.formType
  119. this.startYear = new Date().getFullYear()
  120. NET.request(API.getSortList, {}, 'POST').then(res => {
  121. this.sortList = res.data.map(item => {
  122. return {
  123. value: item.cateCode,
  124. label: item.cateValue,
  125. children: item.oneCategory ? item.oneCategory.map(site => {
  126. return {
  127. value: site.cateCode,
  128. label: site.cateValue,
  129. }
  130. }) : []
  131. }
  132. })
  133. if (this.parentId && this.goodInfo.categoryId && !this.sortText) {
  134. let sort = this.sortList.find(site => site.value == this.parentId)
  135. this.sortText = sort.label + '-' + sort.children.find(site => site.value == this.goodInfo.categoryId).label
  136. }
  137. }).catch(res => {
  138. this.$refs.uTips.show({
  139. title: '获取商品分类失败',
  140. type: 'warning',
  141. })
  142. })
  143. if (options.type == 'detail') {
  144. NET.request(API.getGoodDetail, {
  145. id: options.goodId
  146. }, 'POST').then(res => {
  147. this.goodInfo = {
  148. productName: res.data.productName,
  149. originalPrice: parseFloat(res.data.originalPrice),
  150. bizPrice: parseFloat(res.data.bizPrice),
  151. unit: res.data.unit,
  152. stock: res.data.stock,
  153. productDescribe: res.data.productDescribe,
  154. categoryId: res.data.categoryId,
  155. auctionStartPrice: parseFloat(res.data.auctionStartPrice),
  156. auctionMinAddPrice: parseFloat(res.data.auctionMinAddPrice),
  157. auctionEndTime: res.data.auctionEndTime,
  158. }
  159. this.parentId = res.data.parentId
  160. this.fileList = res.data.imgPath
  161. this.defaultList = res.data.imgPath.map(site => {
  162. return {
  163. url: site
  164. }
  165. })
  166. if (this.sortList.length) {
  167. let sort = this.sortList.find(site => site.value == res.data.parentId)
  168. this.sortText = sort.label + '-' + sort.children.find(site => site.value == res.data.categoryId).label
  169. }
  170. }).catch(res => {
  171. console.log('454543', res)
  172. this.$refs.uTips.show({
  173. title: '获取商品详情失败',
  174. type: 'warning',
  175. })
  176. })
  177. }
  178. },
  179. methods: {
  180. // 加事件
  181. plusPrice(num,numType) {
  182. if (this.ifEdit()) {
  183. return
  184. }
  185. let result = Number(num) + 1;
  186. switch(numType) {
  187. case 1:
  188. this.goodInfo.originalPrice = result
  189. break;
  190. case 2:
  191. this.goodInfo.bizPrice = result
  192. break;
  193. case 3:
  194. this.goodInfo.stock = result
  195. break;
  196. case 4:
  197. this.goodInfo.auctionStartPrice = result
  198. break;
  199. case 5:
  200. this.goodInfo.auctionMinAddPrice = result
  201. break;
  202. }
  203. },
  204. // 减事件
  205. reducePrice(num,numType) {
  206. if (this.ifEdit()) {
  207. return
  208. }
  209. if (numType == 3) {
  210. this.minNum = 1
  211. } else {
  212. this.minNum = 0.01
  213. }
  214. if ((Number(num) - 1) >= Number(this.minNum)) {
  215. let result = Number(num) - 1;
  216. switch(numType) {
  217. case 1:
  218. this.goodInfo.originalPrice = result
  219. break;
  220. case 2:
  221. this.goodInfo.bizPrice = result
  222. break;
  223. case 3:
  224. this.goodInfo.stock = result
  225. break;
  226. case 4:
  227. this.goodInfo.auctionStartPrice = result
  228. break;
  229. case 5:
  230. this.goodInfo.auctionMinAddPrice = result
  231. break;
  232. }
  233. }
  234. },
  235. // 输入框改变事件
  236. onKeyInput(value,numType) {
  237. let lastNum = value.charAt(value.length-1)
  238. let reg=new RegExp("^[0-9]*$")
  239. var result
  240. if(!reg.test(lastNum)) {
  241. result = value.slice(0,-1)
  242. } else {
  243. result = value
  244. }
  245. switch(numType) {
  246. case 1:
  247. this.goodInfo.originalPrice = result
  248. break;
  249. case 2:
  250. this.goodInfo.bizPrice = result
  251. break;
  252. case 3:
  253. this.goodInfo.stock = result
  254. break;
  255. case 4:
  256. this.goodInfo.auctionStartPrice = result
  257. break;
  258. case 5:
  259. this.goodInfo.auctionMinAddPrice = result
  260. break;
  261. }
  262. },
  263. // 校验当前模式
  264. ifEdit() {
  265. return this.type == 'add' || this.type == 'edit' ? false : true
  266. },
  267. // 文件上传成功回调
  268. uploadSuccess(res, index, lists, name) {
  269. this.fileList.push(res.data.url)
  270. this.$refs.uTips.show({
  271. title: '文件上传成功',
  272. type: 'success',
  273. })
  274. return true
  275. },
  276. // 文件上传失败回调
  277. uploadError(res, index, lists, name) {
  278. this.$refs.uTips.show({
  279. title: '文件上传失败',
  280. type: 'warning',
  281. })
  282. },
  283. // 移除文件回调
  284. uploadRemove(index, lists, name) {
  285. this.fileList.splice(index, 1)
  286. },
  287. // 设置分类
  288. setSort(data) {
  289. this.goodInfo.categoryId = data[1].value
  290. this.sortText = data[0].label + '-' + data[1].label
  291. },
  292. // 设置时间
  293. setDate(data) {
  294. console.log('时间data', data)
  295. this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
  296. ':' + data.second
  297. },
  298. // 检查必填项
  299. getPermit() {
  300. if (this.type == 'detail') {
  301. return false
  302. } else {
  303. if (!this.fileList.length) {
  304. return true
  305. }
  306. if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
  307. return true
  308. }
  309. if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
  310. .auctionEndTime)) {
  311. return true
  312. } else if (this.formType != 2 && !this.goodInfo.bizPrice) {
  313. return true
  314. }
  315. return false
  316. }
  317. },
  318. // 提交
  319. submitData() {
  320. if (this.formType == 2) {
  321. this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
  322. }
  323. if (this.type == 'detail') {
  324. this.type = 'edit'
  325. } else if (this.type == 'edit') {
  326. NET.request(API.editGood, {
  327. ...this.goodInfo,
  328. id: this.goodId,
  329. imgPath: this.fileList,
  330. }, 'POST').then(res => {
  331. this.$refs.uTips.show({
  332. title: '编辑成功',
  333. type: 'success',
  334. })
  335. setTimeout(() => {
  336. uni.navigateBack()
  337. }, 1000)
  338. }).catch(res => {
  339. this.$refs.uTips.show({
  340. title: '编辑失败',
  341. type: 'warning',
  342. })
  343. })
  344. } else {
  345. NET.request(API.addGood, {
  346. ...this.goodInfo,
  347. imgPath: this.fileList,
  348. productType: this.formType
  349. }, 'POST').then(res => {
  350. this.$refs.uTips.show({
  351. title: '新增成功',
  352. type: 'success',
  353. })
  354. setTimeout(() => {
  355. uni.navigateBack()
  356. }, 1000)
  357. }).catch(res => {
  358. this.$refs.uTips.show({
  359. title: '新增失败',
  360. type: 'warning',
  361. })
  362. })
  363. }
  364. },
  365. // 按键被点击(点击退格键不会触发此事件)
  366. valChange(val) {
  367. // 将每次按键的值拼接到value变量中,注意+=写法
  368. this.goodInfo.originalPrice += val;
  369. console.log(this.goodInfo.originalPrice);
  370. },
  371. // 退格键被点击
  372. backspace() {
  373. // 删除value的最后一个字符
  374. if (this.goodInfo.originalPrice.length) {
  375. this.goodInfo.originalPrice = this.goodInfo.originalPrice.substr(0, this.goodInfo.originalPrice.length - 1);
  376. }
  377. console.log(this.goodInfo.originalPrice);
  378. }
  379. },
  380. }
  381. </script>
  382. <style>
  383. page {
  384. width: 100%;
  385. height: 100%;
  386. }
  387. </style>
  388. <style lang="less" scoped>
  389. .container {
  390. width: 100%;
  391. float: left;
  392. padding: 0 15px 60px 15px;
  393. .handle-fix-box {
  394. width: 100%;
  395. box-sizing: border-box;
  396. padding: 10px 15px;
  397. position: fixed;
  398. left: 0;
  399. bottom: 0;
  400. height: 60px;
  401. background-color: #FFFFFF;
  402. z-index: 999;
  403. }
  404. .good-img {
  405. display: flex;
  406. flex-wrap: wrap;
  407. .good-img-item {
  408. margin-right: 5px;
  409. margin-bottom: 5px;
  410. }
  411. }
  412. }
  413. </style>