goodForm.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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>
  9. <u-image width="98px" height="98px" border-radius="5px" v-for="(item, index) in fileList" :src="item" class="good-img"
  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 @getNum="getOriginalPrice" :isDisabled="ifEdit()" minNum="0.01" :isInt="false"></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 @getNum="getBizPrice" :isDisabled="ifEdit()" minNum="0.01" :isInt="false"></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 @getNum="getStartPrice" :isDisabled="ifEdit()" minNum="0.01" :isInt="false"></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 @getNum="getAddPrice" :isDisabled="ifEdit()" minNum="0.01" :isInt="false"></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 @getNum="getStockNum" :isDisabled="ifEdit()" minNum="1"></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. }
  112. },
  113. onLoad(options) {
  114. this.uploadUrl = API.uploadFile
  115. this.goodId = options.goodId
  116. this.type = options.type
  117. this.formType = options.formType
  118. this.startYear = new Date().getFullYear()
  119. NET.request(API.getSortList, {}, 'POST').then(res => {
  120. this.sortList = res.data.map(item => {
  121. return {
  122. value: item.cateCode,
  123. label: item.cateValue,
  124. children: item.oneCategory ? item.oneCategory.map(site => {
  125. return {
  126. value: site.cateCode,
  127. label: site.cateValue,
  128. }
  129. }) : []
  130. }
  131. })
  132. if (this.parentId && this.goodInfo.categoryId && !this.sortText) {
  133. let sort = this.sortList.find(site => site.value == this.parentId)
  134. this.sortText = sort.label + '-' + sort.children.find(site => site.value == this.goodInfo.categoryId).label
  135. }
  136. }).catch(res => {
  137. this.$refs.uTips.show({
  138. title: '获取商品分类失败',
  139. type: 'warning',
  140. })
  141. })
  142. if (options.type == 'detail') {
  143. NET.request(API.getGoodDetail, {
  144. id: options.goodId
  145. }, 'POST').then(res => {
  146. this.goodInfo = {
  147. productName: res.data.productName,
  148. originalPrice: parseFloat(res.data.originalPrice),
  149. bizPrice: parseFloat(res.data.bizPrice),
  150. unit: res.data.unit,
  151. stock: res.data.stock,
  152. productDescribe: res.data.productDescribe,
  153. categoryId: res.data.categoryId,
  154. auctionStartPrice: parseFloat(res.data.auctionStartPrice),
  155. auctionMinAddPrice: parseFloat(res.data.auctionMinAddPrice),
  156. auctionEndTime: res.data.auctionEndTime,
  157. }
  158. this.parentId = res.data.parentId
  159. this.fileList = res.data.imgPath
  160. this.defaultList = res.data.imgPath.map(site => {
  161. return {
  162. url: site
  163. }
  164. })
  165. if (this.sortList.length) {
  166. let sort = this.sortList.find(site => site.value == res.data.parentId)
  167. this.sortText = sort.label + '-' + sort.children.find(site => site.value == res.data.categoryId).label
  168. }
  169. }).catch(res => {
  170. console.log('454543', res)
  171. this.$refs.uTips.show({
  172. title: '获取商品详情失败',
  173. type: 'warning',
  174. })
  175. })
  176. }
  177. },
  178. methods: {
  179. // 获取子组件原价
  180. getOriginalPrice(num) {
  181. this.goodInfo.originalPrice = num
  182. },
  183. // 获取售价或采摘价
  184. getBizPrice(num) {
  185. this.goodInfo.bizPrice = num
  186. },
  187. // 起拍价
  188. getStartPrice(num) {
  189. this.goodInfo.auctionStartPrice = num
  190. },
  191. // 获取拍卖加价额
  192. getAddPrice(num) {
  193. this.goodInfo.auctionMinAddPrice = num
  194. },
  195. // 获取子组件的库存数量
  196. getStockNum(num) {
  197. this.goodInfo.stock = num
  198. },
  199. // 校验当前模式
  200. ifEdit() {
  201. return this.type == 'add' || this.type == 'edit' ? false : true
  202. },
  203. // 文件上传成功回调
  204. uploadSuccess(res, index, lists, name) {
  205. this.fileList.push(res.data.url)
  206. this.$refs.uTips.show({
  207. title: '文件上传成功',
  208. type: 'success',
  209. })
  210. return true
  211. },
  212. // 文件上传失败回调
  213. uploadError(res, index, lists, name) {
  214. this.$refs.uTips.show({
  215. title: '文件上传失败',
  216. type: 'warning',
  217. })
  218. },
  219. // 移除文件回调
  220. uploadRemove(index, lists, name) {
  221. this.fileList.splice(index, 1)
  222. },
  223. // 设置分类
  224. setSort(data) {
  225. this.goodInfo.categoryId = data[1].value
  226. this.sortText = data[0].label + '-' + data[1].label
  227. },
  228. // 设置时间
  229. setDate(data) {
  230. console.log('时间data', data)
  231. this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
  232. ':' + data.second
  233. },
  234. // 检查必填项
  235. getPermit() {
  236. if (this.type == 'detail') {
  237. return false
  238. } else {
  239. if (!this.fileList.length) {
  240. return true
  241. }
  242. if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
  243. return true
  244. }
  245. if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
  246. .auctionEndTime)) {
  247. return true
  248. } else if (this.formType != 2 && !this.goodInfo.bizPrice) {
  249. return true
  250. }
  251. return false
  252. }
  253. },
  254. // 提交
  255. submitData() {
  256. if (this.formType == 2) {
  257. this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
  258. }
  259. if (this.type == 'detail') {
  260. this.type = 'edit'
  261. } else if (this.type == 'edit') {
  262. NET.request(API.editGood, {
  263. ...this.goodInfo,
  264. id: this.goodId,
  265. imgPath: this.fileList,
  266. }, 'POST').then(res => {
  267. this.$refs.uTips.show({
  268. title: '编辑成功',
  269. type: 'success',
  270. })
  271. setTimeout(() => {
  272. uni.navigateBack()
  273. }, 1000)
  274. }).catch(res => {
  275. this.$refs.uTips.show({
  276. title: '编辑失败',
  277. type: 'warning',
  278. })
  279. })
  280. } else {
  281. NET.request(API.addGood, {
  282. ...this.goodInfo,
  283. imgPath: this.fileList,
  284. productType: this.formType
  285. }, 'POST').then(res => {
  286. this.$refs.uTips.show({
  287. title: '新增成功',
  288. type: 'success',
  289. })
  290. setTimeout(() => {
  291. uni.navigateBack()
  292. }, 1000)
  293. }).catch(res => {
  294. this.$refs.uTips.show({
  295. title: '新增失败',
  296. type: 'warning',
  297. })
  298. })
  299. }
  300. },
  301. // 按键被点击(点击退格键不会触发此事件)
  302. valChange(val) {
  303. // 将每次按键的值拼接到value变量中,注意+=写法
  304. this.goodInfo.originalPrice += val;
  305. console.log(this.goodInfo.originalPrice);
  306. },
  307. // 退格键被点击
  308. backspace() {
  309. // 删除value的最后一个字符
  310. if (this.goodInfo.originalPrice.length) {
  311. this.goodInfo.originalPrice = this.goodInfo.originalPrice.substr(0, this.goodInfo.originalPrice.length - 1);
  312. }
  313. console.log(this.goodInfo.originalPrice);
  314. }
  315. },
  316. }
  317. </script>
  318. <style>
  319. page {
  320. width: 100%;
  321. height: 100%;
  322. }
  323. </style>
  324. <style lang="less" scoped>
  325. .container {
  326. width: 100%;
  327. float: left;
  328. padding: 0 15px 60px 15px;
  329. .handle-fix-box {
  330. width: 100%;
  331. box-sizing: border-box;
  332. padding: 10px 15px;
  333. position: fixed;
  334. left: 0;
  335. bottom: 0;
  336. height: 60px;
  337. background-color: #FFFFFF;
  338. z-index: 999;
  339. }
  340. }
  341. </style>