goodForm.vue 10.0 KB

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