123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- const uploadJS = require('../../mixin/upload.js')
- const { getProductCategoryList, getProductBrandList } = require('./api/index')
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- // * 商品品牌:下拉选择
- // * 单位:下拉选择
- // * 商品货号:系统自动生成(只读)
- // * 批发价
- // * 零售价
- // * 库存
- // * 状态:现售产品有上架、下架状态
- // * 发售时间:不能低于当前创建时间
- form: {
- 'product_img_url': [], // 商品主图
- 'product_rotation_img_list': [
- {
- 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg',
- 'formkey': 'product_rotation_img_list'
- },
- {
- 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe51c4.jpg',
- 'formkey': 'product_rotation_img_list'
- },
- {
- 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffefa54.jpg',
- 'formkey': 'product_rotation_img_list'
- },
- {
- 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi6323230067e26.png',
- 'formkey': 'product_rotation_img_list'
- }
- ], // 商品轮播图
- 'product_detail_img_list': [], // 详情图
- 'product_title': '', // 商品标题
- 'product_desc': '', // 商品简介
- 'product_category_id': {
- 'category_name': '',
- 'id': ''
- }, // 分类ID
- 'product_brand_id': {
- 'brand_name': '',
- 'id': ''
- }, // 品牌ID
- 'product_spec': '', // 规格
- 'product_unit': '', // 单位
- 'product_all_price': '', // 批发价
- 'product_price': '', // 零售价
- 'product_count': '', // 库存
- 'product_sale_at': '' // 预售时间(0代表预售)
- },
- product_img_url_max: 1,
- product_rotation_img_list_max: 5,
- product_detail_img_list_max: 5,
- booCategory: false,
- categoryInDefaultIndex: 0,
- categoryList: [],
- booBrand: false,
- brandInDefaultIndex: 0,
- brandList: [],
- booUnit: false,
- unitInDefaultIndex: 0,
- unitInColumns: ['件', '个']
- },
- /**
- * 生命周期函数--监听页面加载
- */
- async onLoad(options) {
- await this.fetchProductCategoryList()
- await this.fetchProductBrandList()
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- async fetchProductCategoryList() {
- try {
- const { status, data, msg } = await getProductCategoryList()
- if (status) {
- this.setData({
- categoryList: data.map(item => {
- return {
- ...item,
- text: item.category_name
- }
- })
- })
- } else {
- wx.showToast({
- title: msg,
- icon: 'none'
- })
- }
- } catch (err) {}
- },
- async fetchProductBrandList() {
- const { status, data, msg } = await getProductBrandList()
- if (status) {
- this.setData({
- brandList: data.map(item => {
- return {
- ...item,
- text: item.brand_name
- }
- })
- })
- } else {
- wx.showToast({
- title: msg,
- icon: 'none'
- })
- }
- },
- ...uploadJS,
- uploadCallBack(res) {
- const temp = res.map(item => {
- return {
- 'url': item.url,
- 'formkey': item.formkey
- }
- })
- let tempForm = {}
- let formkey = ''
- if (temp.length > 0) {
- formkey = temp[0].formkey
- }
- switch (formkey) {
- case 'product_img_url':
- tempForm[`form.${formkey}`] = temp
- break
- case 'product_rotation_img_list':
- case 'product_detail_img_list':
- tempForm[`form.${formkey}`] = this.data.form[formkey].concat(...temp)
- break
- default:
- }
- if (Object.keys(tempForm).length > 0) {
- this.setData(tempForm)
- }
- },
- categoryShow() {
- this.setData({
- booCategory: true
- })
- },
- categoryHide() {
- if (this.data.categoryList.length > 0) {
- this.selectComponent('#picker-category').setIndexes([this.data.categoryInDefaultIndex])
- }
- this.setData({
- booCategory: false
- })
- },
- categoryConfirm(event) {
- const { value, index } = event.detail
- this.setData({
- 'form.product_category_id': value,
- categoryInDefaultIndex: index
- })
- this.categoryHide()
- },
- brandShow() {
- this.setData({
- booBrand: true
- })
- },
- brandHide() {
- if (this.data.brandList.length > 0) {
- this.selectComponent('#picker-brand').setIndexes([this.data.brandInDefaultIndex])
- }
- this.setData({
- booBrand: false
- })
- },
- brandConfirm(event) {
- const { value, index } = event.detail
- this.setData({
- 'form.product_brand_id': value,
- brandInDefaultIndex: index
- })
- this.brandHide()
- },
- unitShow() {
- this.setData({
- booUnit: true
- })
- },
- unitHide() {
- if (this.data.unitInColumns.length > 0) {
- this.selectComponent('#picker-unit').setIndexes([this.data.unitInDefaultIndex])
- }
- this.setData({
- booUnit: false
- })
- },
- unitConfirm(event) {
- const { value, index } = event.detail
- this.setData({
- 'form.product_unit': value,
- unitInDefaultIndex: index
- })
- this.unitHide()
- },
- setFormValue(event) {
- const { value } = event.detail
- const { formkey } = event.target.dataset
- let tempForm = {}
- switch (formkey) {
- case 'product_title':
- case 'product_desc':
- case 'product_spec':
- tempForm[`form.${formkey}`] = value
- break
- case 'product_all_price':
- case 'product_price':
- tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0').replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
- break
- case 'product_count':
- tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0')
- break
- default:
- }
- this.setData(tempForm)
- },
- onSubmit(e) {
- console.log(e.detail)
- }
- })
|