yuminge-app/yun-min-program-plugin-master/packageG/cps/component/cpsApi.js

375 lines
11 KiB
JavaScript

var app = getApp();
module.exports = Behavior({
data: {
header: {},
goodsData: [],
isLoadMore: true,
page: 1,
goodsDetail: {},
ratio_data: {},
},
methods: {
_getNetWork({
url,
data,
header,
success
}) {
let headers = {
"Content-Type": "application/json",
Accept: "application/json",
};
if (header) {
headers = Object.assign(headers, header);
}
wx.showNavigationBarLoading();
wx.request({
url: url,
data: data,
header: headers,
success: (res) => {
wx.hideNavigationBarLoading({});
success(res);
},
fail: function (res) {
wx.showToast({
title: "请求数据失败",
icon: "loading",
duration: 2000,
mask: true,
});
console.log("请求数据失败:", url, res);
},
});
},
getCPSLogin(flag) {
this.setData({
page: 1,
isLoadMore: true,
goodsData: []
});
let CPSHeader = wx.getStorageSync("CPSHeader");
if (CPSHeader) {
this.data.header = JSON.parse(CPSHeader);
if ((this.data.header.t + 60*7) > Math.round(new Date() / 1000)) {
if (flag === 'list') {
this.searchCPSGood();
} else if (flag === 'good') {
this.getGoodDetail();
} else if (flag === 'link') {
this.getCPSLink();
} else {
this.getCPSGood(0, this.data.datas.list[0].text);
}
return;
}else {
// CPSHeader缓存保留7分钟 过7分钟更换
wx.removeStorageSync("CPSHeader");
}
}
let _url = app.getNetAddresss("plugin.aggregation-cps.api.web.mergeLogin");
app._getNetWork({
url: _url,
success: (res) => {
let resData = res.data;
if (resData.result == 1) {
this.setData({
header: resData.data.header
});
wx.setStorageSync("CPSHeader", JSON.stringify(this.data.header));
if (flag === 'list') {
this.searchCPSGood();
} else if (flag === 'good') {
this.getGoodDetail();
} else if (flag === 'link') {
this.getCPSLink();
} else {
this.getCPSGood(0, this.datas.list[0].text);
}
} else {
wx.showToast({
title: resData.msg,
icon: "none",
});
}
},
fail: (error) => {
console.log(error, "错误");
},
});
},
getPDDLogin(index, title) {
// 拼多多授权
this._getNetWork({
url: "https://v3.biyingniao.com/api/v3/user/is_union_oauth",
data: {
platform: 2
},
header: this.data.header,
success: (res) => {
let resData = res.data;
if (resData.code === 0) {
if (resData.data.is_oauth) {
if (index === 'list') {
this.searchCPSGood();
} else if (index === 'good') {
this.getGoodDetail();
} else {
this.getCPSGood(index, title);
}
} else {
wx.showModal({
title: "提示",
content: "请先确认授权",
showCancel: false,
success(res) {
if (res.confirm) {
let we_app_info = resData.data.oauth_url_info.we_app_info;
wx.navigateToMiniProgram({
appId: we_app_info.app_id,
path: we_app_info.page_path,
extraData: {},
});
}
},
});
}
}else if(response.code === 30004) {
this.getCPSLogin(index, title);
} else {
wx.showToast({
title: resData.message,
icon: "none",
});
}
}
});
},
getCPSGood(index, title, flag) {
// 装修组件使用
if (!this.data.isLoadMore) {
return;
}
this.setData({
isLoadMore: false,
});
if (flag !== 'more') {
this.setData({
show_page: false,
show_text: '加载中...'
});
}
let url = "https://v3.biyingniao.com/api/v3/goods/pdd/search";
if (this.data.datas.list[index].is_minApp == 2) {
url = "https://v3.biyingniao.com/api/v3/goods/jd/search";
}
this._getNetWork({
url: url,
data: {
keyword: title,
page: this.data.page
},
header: this.data.header,
success: (res) => {
let resData = res.data;
if (resData.code === 0) {
if (this.data.page === 1) {
this.getRatio();
}
let goodsData = [];
if (flag !== 'more') {
goodsData = resData.data.data || [];
} else {
goodsData = this.data.goodsData.concat(resData.data.data || []);
}
this.setData({
isLoadMore: resData.data.has_next,
show_page: true,
goodsData: goodsData
});
} else if (resData.code === 30009 || resData.code === 10000) {
// 需要登录授权
this.setData({
isLoadMore: true,
});
this.getPDDLogin(index, title, flag);
} else if (resData.code === 30004) {
// 签名过期
this.getCPSLogin(index);
} else {
this.setData({
isLoadMore: true,
show_page: false,
show_text: '数据报错'
});
wx.showToast({
title: resData.message,
icon: "none",
});
}
}
});
},
searchCPSGood() {
// 搜索页使用
if (!this.data.isLoadMore) {
return;
}
this.setData({
isLoadMore: false,
});
let url = "https://v3.biyingniao.com/api/v3/goods/pdd/search";
if (this.data.searchActive === 'jd') {
url = "https://v3.biyingniao.com/api/v3/goods/jd/search";
}
let json = {
keyword: this.data.searchActive === 'jd' ? this.data.searchKey || '百货' : this.data.searchKey,
page: this.data.page
};
if (!this.data.searchKey && this.data.channel_type) {
json = {
channel_type: this.data.channel_type,
page: this.data.page
};
url = "https://v3.biyingniao.com/api/v3/goods/pdd/recommend";
if (this.data.searchActive === 'jd') {
url = "https://v3.biyingniao.com/api/v3/goods/jd/jf";
}
}
if (this.data.searchKey) {
this.data.channel_type_name = "搜索";
} else if (this.data.options.channel_type_name) {
this.data.channel_type_name = `${this.data.options.channel_type_name}${this.data.searchActive === 'jd' ? '-京东': '-拼多多'}` || "商品列表";
}
wx.setNavigationBarTitle({
title: this.data.channel_type_name,
});
this._getNetWork({
url: url,
data: json,
header: this.data.header,
success: (res) => {
let resData = res.data;
if (resData.code === 0) {
if (this.data.page === 1) {
this.getRatio();
}
this.setData({
isLoadMore: resData.data.has_next,
goodsData: this.data.goodsData.concat(resData.data.data || []),
page: this.data.page += 1,
});
} else if (resData.code === 30009 || resData.code === 10000) {
// 需要登录授权
this.setData({
isLoadMore: true,
});
this.getPDDLogin('list');
} else if (resData.code === 30004) {
// 签名过期
this.getCPSLogin('list');
} else {
this.setData({
isLoadMore: true,
});
wx.showToast({
title: resData.message,
icon: "none",
});
}
}
});
},
getGoodDetail() {
// 商品详情页使用
this._getNetWork({
url: "https://v3.biyingniao.com/api/v3/goods/detail",
data: {
item_id: this.data.good.item_id,
platform_id: this.data.good.platform_id,
activity_id: this.data.good.activity_id
},
header: this.data.header,
success: (res) => {
let resData = res.data;
if (resData.code === 0) {
this.setData({
goodsDetail: resData.data || {},
});
this.getRatio();
} else if (resData.code === 30009 || resData.code === 10000) {
// 需要登录授权
this.getPDDLogin('good');
} else if (resData.code === 30004) {
// 签名过期
this.getCPSLogin('good');
} else {
wx.showToast({
title: resData.message,
icon: "none",
});
}
}
});
},
getCPSLink() {
// 推广取链
this._getNetWork({
url: "https://v3.biyingniao.com/api/v3/popularize/cps",
data: {
id: this.data.id,
phone: this.data.phone,
},
header: this.data.header,
success: (res) => {
let resData = res.data;
if (resData.code === 0) {
wx.navigateToMiniProgram({
appId: resData.data.wx_app_id,
path: resData.data.wx_path,
extraData: {},
});
} else {
wx.showToast({
title: resData.message,
icon: "none",
});
}
}
});
},
getRatio() {
if (this.data.ratio_data && this.data.ratio_data.ratio) {
return;
}
// discount_price*tk_rate/100*0.78 * 0.97*ratio/100
let _url = app.getNetAddresss("plugin.aggregation-cps.api.setting.index");
app._getNetWork({
url: _url,
success: (res) => {
let resData = res.data;
if (resData.result == 1) {
this.setData({
ratio_data: resData.data,
ratioCommissionAmount: (this.data.goodsDetail.discount_price * (this.data.goodsDetail.tk_rate / 100) * 0.78 * 0.97 * (resData.data.ratio / 100)).toFixed(2),
shareCommissionAmount: Number((this.data.goodsDetail.discount_price * (this.data.goodsDetail.tk_rate / 100) * 0.78 * 0.97 * (resData.data.agent_ratio / 100)).toFixed(2))
});
} else {
wx.showToast({
title: resData.msg,
icon: "none",
});
}
},
fail: (error) => {
console.log(error, "错误");
},
});
},
},
});