goodForm.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="container">
  3. <u-cell-group class="form-info" :border="false">
  4. <u-field label="商品名称" placeholder="请输入商品名称" :disabled="ifEdit()" label-width="180" v-model="goodInfo.productName"></u-field>
  5. <u-cell-item :title="'请上传商品图片(' + fileList.length + '/5)'" :arrow="false">
  6. <view slot="label">
  7. <u-image width="98px" height="98px" border-radius="5px" v-for="(item, index) in fileList" :src="item" class="good-img"
  8. v-if="ifEdit()"></u-image>
  9. <u-upload :action="uploadUrl" :file-list="defaultList" :form-data="uploadData" @on-success="uploadSuccess"
  10. @on-error="uploadError" @on-remove="uploadRemove" max-count="5" :disabled="ifEdit()"></u-upload>
  11. </view>
  12. </u-cell-item>
  13. <u-cell-item title="原价" :arrow="false">
  14. <u-number-box :positive-integer="false" :min="0" v-model="goodInfo.originalPrice" bg-color="#51A539" color="#ffffff"
  15. :disabled="ifEdit()" digit></u-number-box>
  16. </u-cell-item>
  17. <u-cell-item :title="formType == 1 ? '售价' : '采摘价格'" :arrow="false" v-if="formType != 2">
  18. <u-number-box :positive-integer="false" :min="0" v-model="goodInfo.bizPrice" bg-color="#51A539" color="#ffffff"
  19. :disabled="ifEdit()"></u-number-box>
  20. </u-cell-item>
  21. <u-cell-item title="起拍价" :arrow="false" v-if="formType == 2">
  22. <u-number-box :positive-integer="false" :min="0" v-model="goodInfo.auctionStartPrice" bg-color="#51A539" color="#ffffff"
  23. :disabled="ifEdit()"></u-number-box>
  24. </u-cell-item>
  25. <u-cell-item title="每次最低加价金额" :arrow="false" v-if="formType == 2">
  26. <u-number-box :positive-integer="false" :min="0" v-model="goodInfo.auctionMinAddPrice" bg-color="#51A539" color="#ffffff"
  27. :disabled="ifEdit()"></u-number-box>
  28. </u-cell-item>
  29. <u-field label="计量单位" placeholder="请输入计量单位" label-width="180" v-model="goodInfo.unit" :disabled="ifEdit()"></u-field>
  30. <u-cell-item title="库存" :arrow="false">
  31. <u-number-box :min="1" v-model="goodInfo.stock" bg-color="#51A539" color="#ffffff" :disabled="ifEdit()"></u-number-box>
  32. </u-cell-item>
  33. <u-field label="商品说明" type="textarea" placeholder="请输入商品说明" label-width="180" v-model="goodInfo.productDescribe"
  34. :disabled="ifEdit()"></u-field>
  35. <u-cell-item title="商品分类" :value="sortText" @click="selectShow = !ifEdit()"></u-cell-item>
  36. <u-cell-item title="拍卖截止时间" :value="goodInfo.auctionEndTime" @click="dateShow = !ifEdit()" v-if="formType == 2"></u-cell-item>
  37. </u-cell-group>
  38. <view class="form-handle">
  39. <u-button type="success" shape="circle" :ripple="true" @click="submitData" :disabled="getPermit()" class="handle-custom">{{type == 'add' ? '提交' : (type == 'detail' ? '编辑' : '保存')}}</u-button>
  40. </view>
  41. <u-picker mode="time" v-model="dateShow" :start-year="startYear" :params="params" @confirm="setDate"></u-picker>
  42. <u-select v-model="selectShow" mode="mutil-column-auto" :list="sortList" @confirm="setSort"></u-select>
  43. <u-top-tips ref="uTips"></u-top-tips>
  44. </view>
  45. </template>
  46. <script>
  47. const NET = require('@/utils/request')
  48. const API = require('@/config/api')
  49. export default {
  50. data() {
  51. return {
  52. goodId: '',
  53. type: '',
  54. formType: '1',
  55. goodInfo: {
  56. productName: '',
  57. originalPrice: 1,
  58. bizPrice: 1,
  59. unit: '',
  60. stock: 1,
  61. productDescribe: '',
  62. categoryId: '',
  63. auctionStartPrice: 1,
  64. auctionMinAddPrice: 1,
  65. auctionEndTime: '',
  66. },
  67. selectShow: false,
  68. parentId: '',
  69. sortList: [],
  70. sortText: '',
  71. dateShow: false,
  72. params: {
  73. year: true,
  74. month: true,
  75. day: true,
  76. hour: true,
  77. minute: true,
  78. second: true
  79. },
  80. startYear: '',
  81. uploadData: {
  82. folderId: 0,
  83. },
  84. uploadUrl: '',
  85. fileList: [],
  86. defaultList: [],
  87. }
  88. },
  89. onLoad(options) {
  90. this.uploadUrl = API.uploadFile
  91. this.goodId = options.goodId
  92. this.type = options.type
  93. this.formType = options.formType
  94. this.startYear = new Date().getFullYear()
  95. NET.request(API.getSortList, {}, 'POST').then(res => {
  96. this.sortList = res.data.map(item => {
  97. return {
  98. value: item.cateCode,
  99. label: item.cateValue,
  100. children: item.oneCategory ? item.oneCategory.map(site => {
  101. return {
  102. value: site.cateCode,
  103. label: site.cateValue,
  104. }
  105. }) : []
  106. }
  107. })
  108. if (this.parentId && this.goodInfo.categoryId && !this.sortText) {
  109. let sort = this.sortList.find(site => site.value == this.parentId)
  110. this.sortText = sort.label + '-' + sort.children.find(site => site.value == this.goodInfo.categoryId).label
  111. }
  112. }).catch(res => {
  113. this.$refs.uTips.show({
  114. title: '获取商品分类失败',
  115. type: 'warning',
  116. })
  117. })
  118. if (options.type == 'detail') {
  119. NET.request(API.getGoodDetail, {
  120. id: options.goodId
  121. }, 'POST').then(res => {
  122. this.goodInfo = {
  123. productName: res.data.productName,
  124. originalPrice: parseFloat(res.data.originalPrice),
  125. bizPrice: parseFloat(res.data.bizPrice),
  126. unit: res.data.unit,
  127. stock: res.data.stock,
  128. productDescribe: res.data.productDescribe,
  129. categoryId: res.data.categoryId,
  130. auctionStartPrice: parseFloat(res.data.auctionStartPrice),
  131. auctionMinAddPrice: parseFloat(res.data.auctionMinAddPrice),
  132. auctionEndTime: res.data.auctionEndTime,
  133. }
  134. this.parentId = res.data.parentId
  135. this.fileList = res.data.imgPath
  136. this.defaultList = res.data.imgPath.map(site => {
  137. return {
  138. url: site
  139. }
  140. })
  141. if (this.sortList.length && !this.sortText) {
  142. let sort = this.sortList.find(site => site.value == res.data.parentId)
  143. this.sortText = sort.label + '-' + sort.children.find(site => site.value == res.data.categoryId).label
  144. }
  145. }).catch(res => {
  146. this.$refs.uTips.show({
  147. title: '获取商品详情失败',
  148. type: 'warning',
  149. })
  150. })
  151. }
  152. },
  153. methods: {
  154. // 校验当前模式
  155. ifEdit() {
  156. return this.type == 'add' || this.type == 'edit' ? false : true
  157. },
  158. // 文件上传成功回调
  159. uploadSuccess(res, index, lists, name) {
  160. this.fileList.push(res.data.url)
  161. this.$refs.uTips.show({
  162. title: '文件上传成功',
  163. type: 'success',
  164. })
  165. return true
  166. },
  167. // 文件上传失败回调
  168. uploadError(res, index, lists, name) {
  169. this.$refs.uTips.show({
  170. title: '文件上传失败',
  171. type: 'warning',
  172. })
  173. },
  174. // 移除文件回调
  175. uploadRemove(index, lists, name) {
  176. this.fileList.splice(index, 1)
  177. },
  178. // 设置分类
  179. setSort(data) {
  180. this.goodInfo.categoryId = data[1].value
  181. this.sortText = data[0].label + '-' + data[1].label
  182. },
  183. // 设置时间
  184. setDate(data) {
  185. this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
  186. ':' + data.second
  187. },
  188. // 检查必填项
  189. getPermit() {
  190. if (this.type == 'detail') {
  191. return false
  192. } else {
  193. if (!this.fileList.length) {
  194. return true
  195. }
  196. if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
  197. return true
  198. }
  199. if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
  200. .auctionEndTime)) {
  201. return true
  202. } else if (this.formType != 2 && !this.goodInfo.bizPrice) {
  203. return true
  204. }
  205. return false
  206. }
  207. },
  208. // 提交
  209. submitData() {
  210. if (this.formType == 2) {
  211. this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
  212. }
  213. if (this.type == 'detail') {
  214. this.type = 'edit'
  215. } else if (this.type == 'edit') {
  216. NET.request(API.editGood, {
  217. ...this.goodInfo,
  218. id: this.goodId,
  219. imgPath: this.fileList,
  220. }, 'POST').then(res => {
  221. this.$refs.uTips.show({
  222. title: '编辑成功',
  223. type: 'success',
  224. })
  225. setTimeout(() => {
  226. uni.navigateBack()
  227. }, 1000)
  228. }).catch(res => {
  229. this.$refs.uTips.show({
  230. title: '编辑失败',
  231. type: 'warning',
  232. })
  233. })
  234. } else {
  235. NET.request(API.addGood, {
  236. ...this.goodInfo,
  237. imgPath: this.fileList,
  238. productType: this.formType
  239. }, 'POST').then(res => {
  240. this.$refs.uTips.show({
  241. title: '新增成功',
  242. type: 'success',
  243. })
  244. setTimeout(() => {
  245. uni.navigateBack()
  246. }, 1000)
  247. }).catch(res => {
  248. this.$refs.uTips.show({
  249. title: '新增失败',
  250. type: 'warning',
  251. })
  252. })
  253. }
  254. },
  255. },
  256. }
  257. </script>
  258. <style lang="less" scoped>
  259. page {
  260. width: 100%;
  261. height: 100%;
  262. }
  263. .container {
  264. width: 100%;
  265. height: 100%;
  266. float: left;
  267. overflow-y: auto;
  268. .form-info {
  269. width: 100%;
  270. float: left;
  271. /deep/.u-label-text {
  272. color: #333333;
  273. }
  274. /deep/.u-cell_title {
  275. color: #333333;
  276. }
  277. .good-img {
  278. float: left;
  279. margin: 5px;
  280. }
  281. }
  282. .form-handle {
  283. width: calc(100% - 30px);
  284. float: left;
  285. height: 40px;
  286. margin: 30px 15px 20px 15px;
  287. .handle-custom {
  288. background-color: #51A539;
  289. }
  290. }
  291. }
  292. </style>