goodForm.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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="请上传商品图片(上限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.01" step="1" v-model="goodInfo.originalPrice" bg-color="#51A539"
  15. color="#ffffff" :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.01" v-model="goodInfo.bizPrice" bg-color="#51A539" color="#ffffff"
  19. :disabled="ifEdit()" digit></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.01" v-model="goodInfo.auctionStartPrice" bg-color="#51A539" color="#ffffff"
  23. :disabled="ifEdit()" digit></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.01" v-model="goodInfo.auctionMinAddPrice" bg-color="#51A539" color="#ffffff"
  27. :disabled="ifEdit()" digit></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="商品说明" 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. <u-keyboard mode="number" v-model="keyboardShow" @change="valChange" @backspace="backspace"></u-keyboard>
  45. </view>
  46. </template>
  47. <script>
  48. const NET = require('@/utils/request')
  49. const API = require('@/config/api')
  50. export default {
  51. data() {
  52. return {
  53. goodId: '',
  54. type: '',
  55. formType: '1',
  56. goodInfo: {
  57. productName: '',
  58. originalPrice: 1,
  59. bizPrice: 1,
  60. unit: '',
  61. stock: 1,
  62. productDescribe: '',
  63. categoryId: '',
  64. auctionStartPrice: 1,
  65. auctionMinAddPrice: 1,
  66. auctionEndTime: '',
  67. },
  68. selectShow: false,
  69. parentId: '',
  70. sortList: [],
  71. sortText: '',
  72. dateShow: false,
  73. params: {
  74. year: true,
  75. month: true,
  76. day: true,
  77. hour: true,
  78. minute: true,
  79. second: true
  80. },
  81. startYear: '',
  82. uploadData: {
  83. folderId: 0,
  84. },
  85. uploadUrl: '',
  86. fileList: [],
  87. defaultList: [],
  88. keyboardShow: false
  89. }
  90. },
  91. onLoad(options) {
  92. this.uploadUrl = API.uploadFile
  93. this.goodId = options.goodId
  94. this.type = options.type
  95. this.formType = options.formType
  96. this.startYear = new Date().getFullYear()
  97. NET.request(API.getSortList, {}, 'POST').then(res => {
  98. this.sortList = res.data.map(item => {
  99. return {
  100. value: item.cateCode,
  101. label: item.cateValue,
  102. children: item.oneCategory ? item.oneCategory.map(site => {
  103. return {
  104. value: site.cateCode,
  105. label: site.cateValue,
  106. }
  107. }) : []
  108. }
  109. })
  110. if (this.parentId && this.goodInfo.categoryId && !this.sortText) {
  111. let sort = this.sortList.find(site => site.value == this.parentId)
  112. this.sortText = sort.label + '-' + sort.children.find(site => site.value == this.goodInfo.categoryId).label
  113. }
  114. }).catch(res => {
  115. this.$refs.uTips.show({
  116. title: '获取商品分类失败',
  117. type: 'warning',
  118. })
  119. })
  120. if (options.type == 'detail') {
  121. NET.request(API.getGoodDetail, {
  122. id: options.goodId
  123. }, 'POST').then(res => {
  124. this.goodInfo = {
  125. productName: res.data.productName,
  126. originalPrice: parseFloat(res.data.originalPrice),
  127. bizPrice: parseFloat(res.data.bizPrice),
  128. unit: res.data.unit,
  129. stock: res.data.stock,
  130. productDescribe: res.data.productDescribe,
  131. categoryId: res.data.categoryId,
  132. auctionStartPrice: parseFloat(res.data.auctionStartPrice),
  133. auctionMinAddPrice: parseFloat(res.data.auctionMinAddPrice),
  134. auctionEndTime: res.data.auctionEndTime,
  135. }
  136. this.parentId = res.data.parentId
  137. this.fileList = res.data.imgPath
  138. this.defaultList = res.data.imgPath.map(site => {
  139. return {
  140. url: site
  141. }
  142. })
  143. if (this.sortList.length) {
  144. let sort = this.sortList.find(site => site.value == res.data.parentId)
  145. this.sortText = sort.label + '-' + sort.children.find(site => site.value == res.data.categoryId).label
  146. }
  147. }).catch(res => {
  148. console.log('454543', res)
  149. this.$refs.uTips.show({
  150. title: '获取商品详情失败',
  151. type: 'warning',
  152. })
  153. })
  154. }
  155. },
  156. methods: {
  157. // 校验当前模式
  158. ifEdit() {
  159. return this.type == 'add' || this.type == 'edit' ? false : true
  160. },
  161. // 文件上传成功回调
  162. uploadSuccess(res, index, lists, name) {
  163. this.fileList.push(res.data.url)
  164. this.$refs.uTips.show({
  165. title: '文件上传成功',
  166. type: 'success',
  167. })
  168. return true
  169. },
  170. // 文件上传失败回调
  171. uploadError(res, index, lists, name) {
  172. this.$refs.uTips.show({
  173. title: '文件上传失败',
  174. type: 'warning',
  175. })
  176. },
  177. // 移除文件回调
  178. uploadRemove(index, lists, name) {
  179. this.fileList.splice(index, 1)
  180. },
  181. // 设置分类
  182. setSort(data) {
  183. this.goodInfo.categoryId = data[1].value
  184. this.sortText = data[0].label + '-' + data[1].label
  185. },
  186. // 设置时间
  187. setDate(data) {
  188. this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
  189. ':' + data.second
  190. },
  191. // 检查必填项
  192. getPermit() {
  193. if (this.type == 'detail') {
  194. return false
  195. } else {
  196. if (!this.fileList.length) {
  197. return true
  198. }
  199. if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
  200. return true
  201. }
  202. if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
  203. .auctionEndTime)) {
  204. return true
  205. } else if (this.formType != 2 && !this.goodInfo.bizPrice) {
  206. return true
  207. }
  208. return false
  209. }
  210. },
  211. // 提交
  212. submitData() {
  213. if (this.formType == 2) {
  214. this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
  215. }
  216. if (this.type == 'detail') {
  217. this.type = 'edit'
  218. } else if (this.type == 'edit') {
  219. NET.request(API.editGood, {
  220. ...this.goodInfo,
  221. id: this.goodId,
  222. imgPath: this.fileList,
  223. }, 'POST').then(res => {
  224. this.$refs.uTips.show({
  225. title: '编辑成功',
  226. type: 'success',
  227. })
  228. setTimeout(() => {
  229. uni.navigateBack()
  230. }, 1000)
  231. }).catch(res => {
  232. this.$refs.uTips.show({
  233. title: '编辑失败',
  234. type: 'warning',
  235. })
  236. })
  237. } else {
  238. NET.request(API.addGood, {
  239. ...this.goodInfo,
  240. imgPath: this.fileList,
  241. productType: this.formType
  242. }, 'POST').then(res => {
  243. this.$refs.uTips.show({
  244. title: '新增成功',
  245. type: 'success',
  246. })
  247. setTimeout(() => {
  248. uni.navigateBack()
  249. }, 1000)
  250. }).catch(res => {
  251. this.$refs.uTips.show({
  252. title: '新增失败',
  253. type: 'warning',
  254. })
  255. })
  256. }
  257. },
  258. // 按键被点击(点击退格键不会触发此事件)
  259. valChange(val) {
  260. // 将每次按键的值拼接到value变量中,注意+=写法
  261. this.goodInfo.originalPrice += val;
  262. console.log(this.goodInfo.originalPrice);
  263. },
  264. // 退格键被点击
  265. backspace() {
  266. // 删除value的最后一个字符
  267. if (this.goodInfo.originalPrice.length) {
  268. this.goodInfo.originalPrice = this.goodInfo.originalPrice.substr(0, this.goodInfo.originalPrice.length - 1);
  269. }
  270. console.log(this.goodInfo.originalPrice);
  271. }
  272. },
  273. }
  274. </script>
  275. <style lang="less" scoped>
  276. page {
  277. width: 100%;
  278. height: 100%;
  279. }
  280. .container {
  281. width: 100%;
  282. height: 100%;
  283. float: left;
  284. overflow-y: auto;
  285. .form-info {
  286. width: 100%;
  287. float: left;
  288. /deep/.u-label-text {
  289. color: #333333;
  290. }
  291. /deep/.u-cell_title {
  292. color: #333333;
  293. }
  294. .good-img {
  295. float: left;
  296. margin: 5px;
  297. }
  298. }
  299. .form-handle {
  300. width: calc(100% - 30px);
  301. float: left;
  302. height: 40px;
  303. margin: 30px 15px 20px 15px;
  304. .handle-custom {
  305. background-color: #51A539;
  306. /deep/button {
  307. background-color: #56a83a;
  308. }
  309. /deep/.u-btn--success--disabled {
  310. background-color: #74bd60 !important;
  311. }
  312. }
  313. }
  314. }
  315. </style>