goodForm.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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" step="1" 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()" 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" 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" 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="商品说明" 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. <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 && !this.sortText) {
  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. this.$refs.uTips.show({
  149. title: '获取商品详情失败',
  150. type: 'warning',
  151. })
  152. })
  153. }
  154. },
  155. methods: {
  156. // 校验当前模式
  157. ifEdit() {
  158. return this.type == 'add' || this.type == 'edit' ? false : true
  159. },
  160. // 文件上传成功回调
  161. uploadSuccess(res, index, lists, name) {
  162. this.fileList.push(res.data.url)
  163. this.$refs.uTips.show({
  164. title: '文件上传成功',
  165. type: 'success',
  166. })
  167. return true
  168. },
  169. // 文件上传失败回调
  170. uploadError(res, index, lists, name) {
  171. this.$refs.uTips.show({
  172. title: '文件上传失败',
  173. type: 'warning',
  174. })
  175. },
  176. // 移除文件回调
  177. uploadRemove(index, lists, name) {
  178. this.fileList.splice(index, 1)
  179. },
  180. // 设置分类
  181. setSort(data) {
  182. this.goodInfo.categoryId = data[1].value
  183. this.sortText = data[0].label + '-' + data[1].label
  184. },
  185. // 设置时间
  186. setDate(data) {
  187. this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
  188. ':' + data.second
  189. },
  190. // 检查必填项
  191. getPermit() {
  192. if (this.type == 'detail') {
  193. return false
  194. } else {
  195. if (!this.fileList.length) {
  196. return true
  197. }
  198. if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
  199. return true
  200. }
  201. if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
  202. .auctionEndTime)) {
  203. return true
  204. } else if (this.formType != 2 && !this.goodInfo.bizPrice) {
  205. return true
  206. }
  207. return false
  208. }
  209. },
  210. // 提交
  211. submitData() {
  212. if (this.formType == 2) {
  213. this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
  214. }
  215. if (this.type == 'detail') {
  216. this.type = 'edit'
  217. } else if (this.type == 'edit') {
  218. NET.request(API.editGood, {
  219. ...this.goodInfo,
  220. id: this.goodId,
  221. imgPath: this.fileList,
  222. }, 'POST').then(res => {
  223. this.$refs.uTips.show({
  224. title: '编辑成功',
  225. type: 'success',
  226. })
  227. setTimeout(() => {
  228. uni.navigateBack()
  229. }, 1000)
  230. }).catch(res => {
  231. this.$refs.uTips.show({
  232. title: '编辑失败',
  233. type: 'warning',
  234. })
  235. })
  236. } else {
  237. NET.request(API.addGood, {
  238. ...this.goodInfo,
  239. imgPath: this.fileList,
  240. productType: this.formType
  241. }, 'POST').then(res => {
  242. this.$refs.uTips.show({
  243. title: '新增成功',
  244. type: 'success',
  245. })
  246. setTimeout(() => {
  247. uni.navigateBack()
  248. }, 1000)
  249. }).catch(res => {
  250. this.$refs.uTips.show({
  251. title: '新增失败',
  252. type: 'warning',
  253. })
  254. })
  255. }
  256. },
  257. // 按键被点击(点击退格键不会触发此事件)
  258. valChange(val) {
  259. // 将每次按键的值拼接到value变量中,注意+=写法
  260. this.goodInfo.originalPrice += val;
  261. console.log(this.goodInfo.originalPrice);
  262. },
  263. // 退格键被点击
  264. backspace() {
  265. // 删除value的最后一个字符
  266. if(this.goodInfo.originalPrice.length) {
  267. this.goodInfo.originalPrice = this.goodInfo.originalPrice.substr(0, this.goodInfo.originalPrice.length - 1);
  268. }
  269. console.log(this.goodInfo.originalPrice);
  270. }
  271. },
  272. }
  273. </script>
  274. <style lang="less" scoped>
  275. page {
  276. width: 100%;
  277. height: 100%;
  278. }
  279. .container {
  280. width: 100%;
  281. height: 100%;
  282. float: left;
  283. overflow-y: auto;
  284. .form-info {
  285. width: 100%;
  286. float: left;
  287. /deep/.u-label-text {
  288. color: #333333;
  289. }
  290. /deep/.u-cell_title {
  291. color: #333333;
  292. }
  293. .good-img {
  294. float: left;
  295. margin: 5px;
  296. }
  297. }
  298. .form-handle {
  299. width: calc(100% - 30px);
  300. float: left;
  301. height: 40px;
  302. margin: 30px 15px 20px 15px;
  303. .handle-custom {
  304. background-color: #51A539;
  305. }
  306. }
  307. }
  308. </style>