| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- const util = require('../../utils/util')
- const form = require('../../utils/formValidation')
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- isEdit: false,
- storeList: [],
- values: {
- storeId: '',
- storeName: '',
- schemeName: '',
- schemeProductList: []
- },
- id:''
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- var that = this
- if (options && options.id) {
- that.setData({
- isEdit: true,
- id:options.id
- })
- that.bindScheme()
- } else {
- if (!that.data.isEdit) {
-
- var schemeProductList = []
- for (var i = 1; i <= 22; i++) {
- var item = {
- schemeId: '',
- productId: '',
- productName: '请选择商品',
- productImg: '',
- productSchemePrice: 0,
- storeDivide: 0,
- gridNum:i
- }
- schemeProductList.push(item)
- }
- this.setData({
- ["values.schemeProductList"]: schemeProductList
- })
- console.log("---------------------------------------")
- console.log(this.data.values.schemeProductList)
- }
- }
- wx.setNavigationBarTitle({
- title: that.data.isEdit ? '方案编辑' : '新增方案'
- })
- this.bindStoreList()
- },
- bindStorePickerChange: function (e) {
- var item = this.data.storeList[e.detail.value]
- this.setData({
- ['values.storeId']: item.id,
- ['values.storeName']: item.storeName
- })
- },
- formSubmit: function (e) {
- var that = this
- //表单规则
- let rules = [{
- name: "schemeName",
- rule: ["required"], //可使用区间,此处主要测试功能
- msg: ["请输入方案名称"]
- }];
- //进行表单检查
- let formData = e.detail.value;
- let checkRes = form.validation(formData, rules);
- let schemeProductList = this.data.values.schemeProductList;
- var schemeProduct = [];
- var productCount = 0;
- for (let i = 0; i < schemeProductList.length; i++) {
- if(schemeProductList[i].productId!=null&&schemeProductList[i].productId!=""){
- schemeProduct.push(schemeProductList[i])
- productCount+=1;
- if(schemeProductList[i].productSchemePrice==0){
- util.toast("请输入商品价格")
- return;
- }
- if(schemeProductList[i].storeDivide==0){
- util.toast('请输入门店分成')
- return;
- }
- if(schemeProductList[i].storeDivide<1||schemeProductList[i].storeDivide>100){
- util.toast('门店分成必须是1-100的分成')
- return;
- }
- }
- }
- if(productCount==0){
- util.toast('请选择至少一个商品!')
- return;
- }
- if (!checkRes) {
- if(!that.data.isEdit){
- this.setData({
- ["values.schemeProductList"]: schemeProduct
- })
- util.request(util.api.addScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
- if (res.code == 200) {
- // wx.navigateBack()
- wx.navigateTo({
- url: '/pages/scheme/index',
- })
- }
- }).catch((res) => {})
- }else{
- util.request(util.api.editScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
- if (res.code == 200) {
- wx.showToast({
- title: '编辑成功!',
- })
- setTimeout(() => {
- wx.navigateBack()
- }, 500);
- // wx.navigateBack()
-
- }
- }).catch((res) => {})
- }
- } else {
- wx.showToast({
- title: checkRes,
- icon: "none"
- });
- }
- },
- formReset: function (e) {
- console.log("清空数据")
- },
- inputedit: function (e) {
- let _this = this;
- let dataset = e.currentTarget.dataset;
- let value = e.detail.value;
- let name = dataset.name;
- _this.data[name] = value;
- _this.setData({
- [name]: _this.data[name]
- });
- },
- inputeditRow: function (e) {
- let that = this;
- let dataset = e.currentTarget.dataset;
- let value = e.detail.value;
- let name = dataset.name;
- var list = that.data.values.schemeProductList
- var index = dataset.index;
- var itemProduct = dataset.item
- value = parseFloat(value)
- if(!value){
- value = 0.00;
- }
- switch(name){
- case "productSchemePrice":
- value = parseInt(parseFloat(value)*100);
- itemProduct.productSchemePrice = value
- break;
- case "storeDivide":
- itemProduct.storeDivide = value
- break;
- }
- list.splice(index,1,itemProduct)
- that.setData({
- ["values.schemeProductList"]:list
- })
-
- },
- bindStoreList() {
- var that = this
- util.request(util.api.storeList, {}, "GET", false, true, app.globalData.token).then((res) => {
- if (res.code == 200) {
- that.setData({
- storeList: res.result
- })
- }
- }).catch((res) => {})
- },
- toSelect(e) {
- wx.navigateTo({
- url: '/pages/scheme/select?index=' + e.currentTarget.dataset.index,
- })
- },
- bindScheme(){
- var that = this
- util.request(util.api.queryBySchemeId, {
- id: that.data.id
- }, "GET", true, true, app.globalData.token).then((res) => {
- let schemeProductLists = res.result.schemeProductList;
- console.log(schemeProductLists)
- console.log("==================================")
- var schemeProductList = []
- for (var i = 1; i <= 22; i++) {
- var item = {
- schemeId: '',
- productId: '',
- productName: '请选择商品',
- productImg: '',
- productSchemePrice: 0,
- storeDivide: 0,
- gridNum:i
- }
- for (let k = 0; k < schemeProductLists.length; k++) {
- if(schemeProductLists[k].gridNum == i){
- item = schemeProductLists[k];
- }
- }
- schemeProductList.push(item)
- }
- res.result.schemeProductList = schemeProductList;
- if (res.code == 200) {
- that.setData({
- values: res.result,
- })
- }
- }).catch((res) => {})
- },
- deleteScheme(e){
- let that = this;
- util.request(util.api.deleteSchemeById, {'id':that.data.id}, "POST", true, true, app.globalData.token).then((res) => {
- if(res.code == 200){
- wx.navigateTo({
- url: '/pages/scheme/index',
- })
- }
-
- }).catch((res) => {})
- }
-
- })
|