goodForm.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. console.log('时间data', data)
  189. this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
  190. ':' + data.second
  191. },
  192. // 检查必填项
  193. getPermit() {
  194. if (this.type == 'detail') {
  195. return false
  196. } else {
  197. if (!this.fileList.length) {
  198. return true
  199. }
  200. if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
  201. return true
  202. }
  203. if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
  204. .auctionEndTime)) {
  205. return true
  206. } else if (this.formType != 2 && !this.goodInfo.bizPrice) {
  207. return true
  208. }
  209. return false
  210. }
  211. },
  212. // 提交
  213. submitData() {
  214. if (this.formType == 2) {
  215. this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
  216. }
  217. if (this.type == 'detail') {
  218. this.type = 'edit'
  219. } else if (this.type == 'edit') {
  220. NET.request(API.editGood, {
  221. ...this.goodInfo,
  222. id: this.goodId,
  223. imgPath: this.fileList,
  224. }, 'POST').then(res => {
  225. this.$refs.uTips.show({
  226. title: '编辑成功',
  227. type: 'success',
  228. })
  229. setTimeout(() => {
  230. uni.navigateBack()
  231. }, 1000)
  232. }).catch(res => {
  233. this.$refs.uTips.show({
  234. title: '编辑失败',
  235. type: 'warning',
  236. })
  237. })
  238. } else {
  239. NET.request(API.addGood, {
  240. ...this.goodInfo,
  241. imgPath: this.fileList,
  242. productType: this.formType
  243. }, 'POST').then(res => {
  244. this.$refs.uTips.show({
  245. title: '新增成功',
  246. type: 'success',
  247. })
  248. setTimeout(() => {
  249. uni.navigateBack()
  250. }, 1000)
  251. }).catch(res => {
  252. this.$refs.uTips.show({
  253. title: '新增失败',
  254. type: 'warning',
  255. })
  256. })
  257. }
  258. },
  259. // 按键被点击(点击退格键不会触发此事件)
  260. valChange(val) {
  261. // 将每次按键的值拼接到value变量中,注意+=写法
  262. this.goodInfo.originalPrice += val;
  263. console.log(this.goodInfo.originalPrice);
  264. },
  265. // 退格键被点击
  266. backspace() {
  267. // 删除value的最后一个字符
  268. if (this.goodInfo.originalPrice.length) {
  269. this.goodInfo.originalPrice = this.goodInfo.originalPrice.substr(0, this.goodInfo.originalPrice.length - 1);
  270. }
  271. console.log(this.goodInfo.originalPrice);
  272. }
  273. },
  274. }
  275. </script>
  276. <style lang="less" scoped>
  277. page {
  278. width: 100%;
  279. height: 100%;
  280. }
  281. .container {
  282. width: 100%;
  283. height: 100%;
  284. float: left;
  285. overflow-y: auto;
  286. .form-info {
  287. width: 100%;
  288. float: left;
  289. /deep/.u-label-text {
  290. color: #333333;
  291. }
  292. /deep/.u-cell_title {
  293. color: #333333;
  294. }
  295. .good-img {
  296. float: left;
  297. margin: 5px;
  298. }
  299. }
  300. .form-handle {
  301. width: calc(100% - 30px);
  302. float: left;
  303. height: 40px;
  304. margin: 30px 15px 20px 15px;
  305. .handle-custom {
  306. background-color: #51A539;
  307. /deep/button {
  308. background-color: #56a83a;
  309. }
  310. /deep/.u-btn--success--disabled {
  311. background-color: #74bd60 !important;
  312. }
  313. }
  314. }
  315. }
  316. </style>