123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- const util = require('../../utils/util')
- const form = require('../../utils/formValidation')
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- isEdit: false,
- storeList: [],
- agentList:[],
- userInfo: {},
- values: {
- storeId: '',
- storeName: '',
- schemeName: '',
- schemeBillEntryList: []
-
- },
- id:''
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- var that = this
- if (options && options.id) {
- that.setData({
- isEdit: true,
- id:options.id
- })
- that.bindScheme()
- }
- wx.setNavigationBarTitle({
- title: that.data.isEdit ? '方案编辑' : '新增方案'
- })
- this.setData({
- userInfo: app.globalData.userInfo
- })
- this.getAllAgentList();
- if(this.data.userInfo.mgvmRole==3){
- this.setData({
- ['values.agentId']: this.data.userInfo.id,
- ['values.agentName']: this.data.userInfo.username,
- })
- }
- this.bindStoreList()
- },
- bindStorePickerChange: function (e) {
- var item = this.data.storeList[e.detail.value]
- this.setData({
- ['values.storeId']: item.id,
- ['values.storeName']: item.storeName
- })
- },
- bindAgentPickerChange: function (e) {
- var item = this.data.agentList[e.detail.value]
- this.setData({
- ['values.agentId']: item.id,
- ['values.agentName']: item.username
- })
- this.bindStoreList()
- },
- formSubmit: function (e) {
- console.log(this.data.values.schemeBillEntryList)
- var that = this
- //表单规则
- let rules = [{
- name: "schemeName",
- rule: ["required"], //可使用区间,此处主要测试功能
- msg: ["请输入方案名称"]
- },{
- name: "storeName",
- rule: ["required"], //可使用区间,此处主要测试功能
- msg: ["请输入门店"]
- }];
- if(this.data.userInfo.mgvmRole!=3){
- var agentRule = {
- name: "agentName",
- rule: ["required"],
- msg: ["请输入代理商"]
- }
- rules.push(agentRule)
- }else{
- this.setData({
- ['values.agentId']: this.data.userInfo.id,
- ['values.agentName']: this.data.userInfo.username,
- })
- }
- //进行表单检查
- let formData = e.detail.value;
- let checkRes = form.validation(formData, rules);
- let schemeBillEntryList = this.data.values.schemeBillEntryList;
- var schemeProduct = [];
- var productCount = 0;
- for (let i = 0; i < schemeBillEntryList.length; i++) {
- if(schemeBillEntryList[i].productId!=null&&schemeBillEntryList[i].productId!=""){
- schemeProduct.push(schemeBillEntryList[i])
- productCount+=1;
- if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].storeDivide==null){
- util.toast('请输入代理分成')
- return;
- }
- if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].areaDivide>100){
- util.toast('代理分成必须是0-100的分成')
- return;
- }
- if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].storeDivide==null){
- util.toast('请输入门店分成')
- return;
- }
- if(schemeBillEntryList[i].storeDivide<0||schemeBillEntryList[i].storeDivide>100){
- util.toast('门店分成必须是0-100的分成')
- return;
- }
- if(schemeBillEntryList[i].storeDivide+schemeBillEntryList[i].areaDivide<100||
- schemeBillEntryList[i].storeDivide+schemeBillEntryList[i].areaDivide>100){
- util.toast('分成之和应为100%')
- return;
- }
- }
- }
- return
- if(productCount==0){
- util.toast('请选择至少一个商品!')
- return;
- }
- if (!checkRes) {
- if(!that.data.isEdit){
- this.setData({
- ["values.schemeBillEntryList"]: 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.schemeBillEntryList
- var index = dataset.index;
- var itemProduct = dataset.item
- value = parseFloat(value)
- if(!value){
- value = 0.00;
- }
- console.log(name)
- if(name=="storeDivide"){
- itemProduct.storeDivide = value
- }else if(name=="areaDivide"){
- itemProduct.areaDivide = value
- }
- list.splice(index,1,itemProduct)
- that.setData({
- ["values.schemeBillEntryList"]:list
- })
- console.log(this.data.values.schemeBillEntryList)
-
- },
- bindStoreList() {
- var that = this
- util.request(util.api.storeList, {agentId:that.data.values.agentId}, "GET", false, true, app.globalData.token).then((res) => {
- if (res.code == 200) {
- that.setData({
- storeList: res.result
- })
- }
- }).catch((res) => {})
- },
- //代理商
- getAllAgentList() {
- var that = this
- util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => {
- if (res.code == 200) {
- that.setData({
- agentList: 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) => {
- 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) => {})
- },
- //添加格子
- addGrid(){
- var schemeBillEntryList =this.data.values.schemeBillEntryList;
- var product = {productName:'请选择商品',gridNum:schemeBillEntryList.length+1};
- schemeBillEntryList.push(product)
- this.setData({
- ["values.schemeBillEntryList"]: schemeBillEntryList
- })
- }
-
- })
|