goodForm.vue 8.9 KB

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