123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- // pages/goods/goods-list.js
- import * as API_Goods from '../../api/goods.js'
- import * as API_Passport from '../../api/passport.js'
- import {
- $wuxToptips,
- $wuxToast,
- $wuxLoading,
- $wuxDialog
- } from '../../wux/index.js'
- import {unpGoods} from "../../api/goods.js";
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- goods: [],
- pageParam: {
- page_size: 10,
- page_no: 1
- },
- finished: false,
- stockEditShow: false,
- editingGoodsId: null,
- stock: null
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- //this.getGoodsList(this.data.pageParam)
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function() {
- this.getTabBar().init();
- this.setData({
- ['pageParam.page_no']: 1,
- finished: false,
- goods: []
- })
- this.getGoodsList()
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function() {
- wx.showNavigationBarLoading()
- this.setData({
- ['pageParam.page_no']: 1,
- finished: false,
- goods:[]
- })
- this.getGoodsList().then(res=>{
- wx.hideNavigationBarLoading()
- wx.stopPullDownRefresh()
- })
- },
- onCollapseChange(event) {
- this.setData({
- activeNames: event.detail
- });
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function() {
- if (!this.data.finished) {
- this.setData({
- ['pageParam.page_no']: this.data.pageParam.page_no + 1
- })
- this.getGoodsList()
- }
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function() {
- },
- /**触发搜索 */
- onSearch(e) {
- const kw = e.detail
- this.setData({
- ['pageParam.keyword']: kw,
- ['pageParam.page_no']: 1,
- goods: []
- })
- this.getGoodsList()
- },
- /**取消搜索 */
- onCancel(e) {
- console.log('cancel')
- this.setData({
- ['pageParam.page_no']: 1,
- goods: [],
- finished: false
- })
- delete this.data.pageParam.keyword
- this.getGoodsList()
- },
- /** 编辑产品 */
- editGoods(e) {
- this.setData({
- ['pageParam.page_no']: 1,
- finished: false,
- goods: []
- })
- const {
- id
- } = e.currentTarget.dataset
- wx.navigateTo({
- url: '/pages/goods/edit?id=' + id,
- })
- },
- publishGoods(e) {
- wx.navigateTo({
- url: '/pages/goods/edit',
- })
- },
- getGoodsList() {
- return new Promise((resolve,reject)=>{
- const that = this
- API_Goods.getGoodsList(this.data.pageParam).then(res => {
- const {
- data
- } = res
- let goods = []
- if (this.data.goods.length === 0) {
- goods = [...data]
- } else {
- goods = that.data.goods
- goods.push(...data)
- }
- if (data.length < 10) {
- that.setData({
- finished: true
- })
- }
- that.setData({
- goods: goods
- })
- resolve(res)
- }).catch(err=>{
- reject(err)
- })
- })
- },
- /** 下架产品 */
- offProduct(e) {
- const that = this
- const goods_id = e.currentTarget.dataset.id
- $wuxDialog().confirm({
- resetOnClose: true,
- closable: true,
- title: '操作提示',
- content: '你确定要下架商品?',
- onConfirm(e) {
- API_Goods.underGoods(goods_id, {}).then(res => {
- if (res === '') {
- that.data.goods.forEach((item, index) => {
- if (item.goods_id === parseInt(goods_id)) {
- that.setData({
- ['goods[' + index + '].market_enable']: 0
- })
- }
- })
- $wuxToptips().success({
- hidden: false,
- text: '产品已下架',
- duration: 2000,
- success: () => {}
- })
- } else {
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: res.message,
- duration: 2000
- })
- }
- }).catch(error => {
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: error.message,
- duration: 2000
- })
- })
- },
- onCancel(e) {
- },
- })
- },
- /** 下架产品 */
- onProduct(e) {
- const that = this
- const goods_id = e.currentTarget.dataset.id
- API_Goods.unpGoods(goods_id).then(res => {
- if (res === '') {
- that.data.goods.forEach((item, index) => {
- if (item.goods_id === parseInt(goods_id)) {
- that.setData({
- ['goods[' + index + '].market_enable']: 1
- })
- }
- })
- $wuxToptips().success({
- hidden: false,
- text: '产品已上架',
- duration: 2000,
- success: () => {}
- })
- } else {
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: res.message,
- duration: 2000
- })
- }
- }).catch(error => {
- $wuxToptips().error({
- icon: 'cancel',
- hidden: false,
- text: error.message,
- duration: 2000
- })
- })
- },
- /** 此次修改库存 只考虑默认SKU,没有考虑多种规格的情况 */
- stockPopShow(e) {
- const that = this
- const goods_id = e.currentTarget.dataset.id
- API_Goods.getGoodsStockList(goods_id).then(res => {
- that.setData({
- stockEditShow: true,
- editingGoodsId: goods_id,
- stock: res[0].quantity,
- currentSkuId: res[0].sku_id
- })
- })
- },
- stockEditClose(e) {
- this.setData({
- stockEditShow: false
- })
- },
- /** 表单输入 */
- inputChange(e) {
- let dataKey = e.currentTarget.dataset.name
- this.setData({
- [dataKey]: e.detail.value
- })
- },
- onError(e) {
- const that = this
- let dataKey = e.currentTarget.dataset.name
- let {
- label,
- rules
- } = e.currentTarget.dataset
- const {
- value
- } = e.detail
- if (!value && rules.indexOf('required') > -1) {
- label = label + '必填'
- } else {
- label = label + '格式错误'
- }
- $wuxToptips().show({
- icon: 'cancel',
- hidden: false,
- text: label,
- duration: 3000,
- success() {
- that.setData({
- [dataKey + '_focus']: true
- })
- },
- })
- },
- onBlur(e) {
- const that = this
- let dataKey = e.currentTarget.dataset.name
- const {
- rules
- } = e.currentTarget.dataset
- const {
- value
- } = e.detail
- if (rules) {
- that.setData({
- [dataKey + '_error']: false
- })
- rules.split(',').forEach(n => {
- if (n === 'required' && !value) {
- that.setData({
- [dataKey + '_error']: true
- })
- }
- if (n !== 'required' && !Reg[n].test(value)) {
- that.setData({
- [dataKey + '_error']: true
- })
- }
- })
- }
- },
- updateQuantity(e) {
- if (this.data.stock && this.data.currentSkuId) {
- const param = [{
- quantity_count: parseInt(this.data.stock),
- sku_id: this.data.currentSkuId
- }]
- let that = this
- API_Goods.reserveStockGoods(this.data.editingGoodsId, param).then(res => {
- that.data.goods.forEach((item, index) => {
- if (item.goods_id === that.data.editingGoodsId) {
- that.setData({
- ['goods[' + index + '].quantity']: that.data.stock
- })
- }
- })
- that.setData({
- stockEditShow: false
- })
- })
- }
- },
- formSubmit(e) {
- console.log(e)
- API_Passport.saveFormId({ form_id: e.detail.formId }).then(res => {
- })
- }
- })
|