文件
Henglianshutong/public/assets/js/backend/shopro/user/user.js

790 行
35 KiB
JavaScript

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: () => {
const { reactive, onMounted } = Vue
const index = {
setup() {
const state = reactive({
data: [],
order: '',
sort: '',
stats: {
total_users: 0,
maker_count: 0,
venue_count: 0,
county_count: 0,
partner_count: 0,
active_count: 0,
},
filter: {
drawer: false,
data: {
user: { field: 'id', value: '' },
identity: '',
maker_level: '',
active_30d: '',
has_agent: '',
parent_user: '',
createtime: [],
logintime: [],
},
tools: {
user: {
type: 'tinputprepend',
label: '用户信息',
placeholder: '请输入查询内容',
value: {
field: 'id',
value: '',
},
options: {
data: [{
label: '用户ID',
value: 'id',
},
{
label: '用户名',
value: 'username',
},
{
label: '昵称',
value: 'nickname',
},
{
label: '手机号',
value: 'mobile',
},
{
label: '邮箱',
value: 'email',
}],
}
},
identity: {
type: 'tselect',
label: '业务身份',
value: '',
options: {
data: [
{ value: 'taster', label: '品酒师' },
{ value: 'maker', label: '创客 (V1~V6)' },
{ value: 'venue', label: '酒道馆' },
{ value: 'county', label: '区县代理' },
{ value: 'partner', label: '合伙股东' },
]
}
},
maker_level: {
type: 'tselect',
label: '创客等级',
value: '',
options: {
data: [
{ value: '1', label: '创客 V1' },
{ value: '2', label: '创客 V2' },
{ value: '3', label: '创客 V3' },
{ value: '4', label: '创客 V4' },
{ value: '5', label: '创客 V5' },
{ value: '6', label: '创客 V6' },
]
}
},
active_30d: {
type: 'tselect',
label: '30日活跃',
value: '',
options: {
data: [
{ value: '1', label: '有自购(正常领奖)' },
{ value: '0', label: '已暂停(无自购)' },
]
}
},
has_agent: {
type: 'tselect',
label: '收益档案',
value: '',
options: {
data: [
{ value: '1', label: '具备分销/收益档案' },
{ value: '0', label: '普通注册会员' },
]
}
},
parent_user: {
type: 'tinput',
label: '推荐人',
placeholder: '请输入推荐人ID/手机号/昵称',
value: '',
},
createtime: {
type: 'tdatetimerange',
label: '注册时间',
value: [],
},
logintime: {
type: 'tdatetimerange',
label: '上次登录',
value: [],
},
},
condition: {},
}
})
function getData() {
let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
let search = composeFilter(tempSearch, {
username: 'like',
nickname: 'like',
mobile: 'like',
email: 'like',
identity: '=',
maker_level: '=',
active_30d: '=',
has_agent: '=',
parent_user: '=',
createtime: 'range',
logintime: 'range',
});
Fast.api.ajax({
url: 'shopro/user/user',
type: 'GET',
data: {
page: pagination.page,
list_rows: pagination.list_rows,
order: state.order,
sort: state.sort,
...search,
},
}, function (ret, res) {
state.data = res.data.data
pagination.total = res.data.total
if (res.data.stats) {
state.stats = res.data.stats;
}
return false
}, function (ret, res) { })
}
function onChangeSort({ prop, order }) {
state.order = order == 'ascending' ? 'asc' : 'desc';
state.sort = prop;
getData();
}
function onOpenFilter() {
state.filter.drawer = true
}
function onChangeFilter() {
pagination.page = 1
getData()
state.filter.drawer && (state.filter.drawer = false)
}
const pagination = reactive({
page: 1,
list_rows: 10,
total: 0,
})
function onDetail(id, row) {
const url = `shopro/user/user/detail?id=${id}`;
const name = (row && (row.nickname || row.username)) ? (row.nickname || row.username) : '';
const title = name ? `会员详情 - ${name} (#${id})` : `会员详情 #${id}`;
if (typeof Backend !== 'undefined' && Backend.api && typeof Backend.api.addtabs === 'function') {
Backend.api.addtabs(url, title);
} else if (top && top.Backend && top.Backend.api && typeof top.Backend.api.addtabs === 'function') {
top.Backend.api.addtabs(url, title);
} else if (Fast && Fast.api && typeof Fast.api.addtabs === 'function') {
Fast.api.addtabs(url, title);
} else {
window.location.href = Fast.api.fixurl(url);
}
}
function onOpenAgentDetail(id) {
const url = `shopro/commission/agent/detail/id/${id}?id=${id}`;
const title = `分销商详情 #${id}`;
if (typeof Backend !== 'undefined' && Backend.api && typeof Backend.api.addtabs === 'function') {
Backend.api.addtabs(url, title);
} else if (top && top.Backend && top.Backend.api && typeof top.Backend.api.addtabs === 'function') {
top.Backend.api.addtabs(url, title);
} else if (Fast && Fast.api && typeof Fast.api.addtabs === 'function') {
Fast.api.addtabs(url, title);
} else {
window.location.href = Fast.api.fixurl(url);
}
}
function onRecharge(type, row) {
const id = typeof row === 'object' ? row.id : row;
Fast.api.open(`shopro/user/user/recharge?type=${type}&id=${id}`, "账户充值", {
callback: function () {
getData();
}
});
}
function onDelete(id) {
Fast.api.ajax({
url: `shopro/user/user/delete/id/${id}`,
type: 'DELETE',
}, function (ret, res) {
getData()
}, function (ret, res) { })
}
onMounted(() => {
getData()
})
return {
state,
getData,
onChangeSort,
onOpenFilter,
onChangeFilter,
pagination,
onDetail,
onOpenAgentDetail,
onRecharge,
onDelete
}
}
}
createApp('index', index);
},
detail: () => {
const { reactive, onMounted } = Vue
const detail = {
setup() {
const queryUserId = (typeof Config !== 'undefined' && (Config.id || Config.user_id))
? (Config.id || Config.user_id)
: (new URLSearchParams(location.search).get('id') || (location.pathname.match(/\/id\/(\d+)/i) || [])[1] || Fast.api.query('id'));
const state = reactive({
type: new URLSearchParams(location.search).get('type'),
id: queryUserId,
detail: {}
})
function onBack() {
if (parent && parent.Layer && typeof parent.Layer.getFrameIndex === 'function') {
try {
var index = parent.Layer.getFrameIndex(window.name);
if (index) {
parent.Layer.close(index);
return;
}
} catch (e) {}
}
if (top && top.Backend && top.Backend.api && typeof top.Backend.api.closetabs === 'function') {
top.Backend.api.closetabs();
return;
}
if (typeof Backend !== 'undefined' && Backend.api && typeof Backend.api.closetabs === 'function') {
Backend.api.closetabs();
return;
}
window.history.back();
}
function getDetail() {
Fast.api.ajax({
url: `shopro/user/user/detail/id/${state.id}`,
type: 'GET',
}, function (ret, res) {
state.detail = res.data;
return false
}, function (ret, res) { })
}
const platform = {
wechat: {
openPlatform: '微信开放平台',
miniProgram: '微信小程序',
officialAccount: '微信公众平台',
},
};
function statusStyle(key) {
let flag = state.detail.verification?.[key];
return `<span style="color:${flag ? 'var(--el-color-success)' : 'var(--el-color-warning)'}">
${flag ? '已' : '未'}${key == 'username' || key == 'password' ? '设置' : '认证'}
</span>`;
}
function onSelectAvatar() {
Fast.api.open(`general/attachment/select`, "选择", {
callback: function (data) {
state.detail.avatar = data.url;
}
});
}
async function onSave() {
Fast.api.ajax({
url: `shopro/user/user/edit/id/${state.id}`,
type: 'POST',
data: state.detail,
}, function (ret, res) {
getDetail()
}, function (ret, res) { })
}
function onChangeParentUser() {
Fast.api.open(`shopro/commission/agent/select?id=${state.id}`, "更换上级分销商", {
callback() {
getDetail()
}
})
}
function onRecharge(type) {
Fast.api.open(`shopro/user/user/recharge?type=${type}&id=${state.detail.id}`, "账户充值", {
callback: function () {
getDetail();
getLog()
}
});
}
function onOpenAgentDetail(id) {
const url = `shopro/commission/agent/detail/id/${id}?id=${id}`;
const title = `分销商详情 #${id}`;
if (typeof Backend !== 'undefined' && Backend.api && typeof Backend.api.addtabs === 'function') {
Backend.api.addtabs(url, title);
} else if (top && top.Backend && top.Backend.api && typeof top.Backend.api.addtabs === 'function') {
top.Backend.api.addtabs(url, title);
} else if (Fast && Fast.api && typeof Fast.api.addtabs === 'function') {
Fast.api.addtabs(url, title);
} else {
window.location.href = Fast.api.fixurl(url);
}
}
function onOpenUserDetail(id) {
if (!id) return;
const url = `shopro/user/user/detail?id=${id}`;
const title = `会员详情 #${id}`;
if (typeof Backend !== 'undefined' && Backend.api && typeof Backend.api.addtabs === 'function') {
Backend.api.addtabs(url, title);
} else if (top && top.Backend && top.Backend.api && typeof top.Backend.api.addtabs === 'function') {
top.Backend.api.addtabs(url, title);
} else if (Fast && Fast.api && typeof Fast.api.addtabs === 'function') {
Fast.api.addtabs(url, title);
} else {
window.location.href = Fast.api.fixurl(url);
}
}
const formulaDialog = reactive({
visible: false,
row: null,
})
function onShowFormula(row) {
formulaDialog.row = row;
formulaDialog.visible = true;
}
const log = reactive({
tabActive: 'partner_reward',
showSkip: false,
data: [],
})
function getLog() {
let url = ''
let search = {}
if (log.tabActive == 'partner_reward') {
url = `shopro/user/user/rewards/id/${state.id}`
search = {
show_skip: log.showSkip ? 1 : 0
}
} else if (log.tabActive == 'voucher_plan') {
url = `shopro/user/user/voucherPlans/id/${state.id}`
} else if (log.tabActive == 'team') {
url = `shopro/user/user/team/id/${state.id}`
} else if (log.tabActive == 'money' || log.tabActive == 'score' || log.tabActive == 'commission') {
url = `shopro/user/wallet_log/${log.tabActive}/id/${state.id}`
} else if (log.tabActive == 'order') {
url = `shopro/order/order`
search = {
search: JSON.stringify({
user_id: state.id
})
}
} else if (log.tabActive == 'share') {
url = `shopro/share`
search = {
id: state.id
}
} else if (log.tabActive == 'coupon') {
url = `shopro/user/user/coupon/id/${state.id}`
}
if (!url) return;
Fast.api.ajax({
url: url,
type: 'GET',
data: {
page: pagination.page,
list_rows: pagination.list_rows,
...search,
},
}, function (ret, res) {
if (log.tabActive == 'order') {
log.data = res.data?.orders?.data || []
pagination.total = res.data?.orders?.total || 0
} else {
log.data = res.data?.data || []
pagination.total = res.data?.total || 0
}
return false
}, function (ret, res) { })
}
const pagination = reactive({
page: 1,
list_rows: 10,
total: 0,
})
function onChangeTab() {
log.data = []
pagination.page = 1
pagination.total = 0
getLog()
}
onMounted(() => {
getDetail()
getLog()
})
return {
state,
getDetail,
onBack,
platform,
statusStyle,
onSelectAvatar,
onSave,
onChangeParentUser,
onRecharge,
onOpenAgentDetail,
onOpenUserDetail,
formulaDialog,
onShowFormula,
log,
getLog,
pagination,
onChangeTab,
}
}
}
createApp('detail', detail);
},
recharge: () => {
const { reactive, getCurrentInstance, onMounted, computed } = Vue
const recharge = {
setup() {
const { proxy } = getCurrentInstance();
const initialType = new URLSearchParams(location.search).get('type') || 'money';
const validTypes = ['money', 'score', 'madow', 'voucher'];
const defaultType = validTypes.includes(initialType) ? initialType : 'money';
const state = reactive({
type: defaultType,
id: new URLSearchParams(location.search).get('id'),
user: null,
loading: false,
submitting: false
})
const form = reactive({
model: {
id: state.id,
type: state.type,
amount: '',
memo: '',
},
rules: {
type: [{ required: true, message: '请选择资产类型', trigger: 'change' }],
amount: [
{ required: true, message: '请输入变更数值', trigger: 'blur' },
{
validator: (rule, value, callback) => {
if (value === '' || isNaN(value) || parseFloat(value) === 0) {
callback(new Error('请输入非零有效数值'));
} else {
callback();
}
},
trigger: 'blur'
}
],
},
})
// 获取会员详情与当前四大资产快照
function getUserInfo() {
if (!state.id) return;
state.loading = true;
Fast.api.ajax({
url: `shopro/user/user/detail?id=${state.id}`,
loading: false
}, function (ret, res) {
state.loading = false;
state.user = res.data || null;
}, function (ret, res) {
state.loading = false;
});
}
// 切换资产类型
function onTypeChange(val) {
state.type = val;
form.model.type = val;
}
// 资产单位与名称
const assetLabel = computed(() => {
const map = {
money: '金额',
madow: 'dmepa',
score: '积分',
voucher: '兑换券'
};
return map[form.model.type] || '数量';
});
const assetUnit = computed(() => {
const map = {
money: '元',
madow: '颗',
score: '积分',
voucher: '元'
};
return map[form.model.type] || '';
});
// 当前选中资产的当前持有值
const currentBalance = computed(() => {
if (!state.user) return '0';
switch (form.model.type) {
case 'money':
return state.user.money || '0.00';
case 'score':
return state.user.score || 0;
case 'madow':
return state.user.madow || '0.00000000';
case 'voucher':
return state.user.voucher_available || '0.00';
default:
return '0';
}
});
// 预计变动后数值
const previewBalance = computed(() => {
const val = parseFloat(form.model.amount);
if (isNaN(val) || val === 0) {
return null;
}
const cur = parseFloat(currentBalance.value) || 0;
const res = cur + val;
if (form.model.type === 'madow') {
return res.toFixed(8);
} else if (form.model.type === 'score') {
return Math.floor(res);
} else {
return res.toFixed(2);
}
});
// 是否超额扣除
const isOverdrawn = computed(() => {
if (previewBalance.value === null) return false;
return parseFloat(previewBalance.value) < 0;
});
function onCancel() {
Fast.api.close();
}
function onConfirm() {
proxy.$refs['formRef'].validate((valid) => {
if (valid) {
if (isOverdrawn.value) {
Fast.api.msg('扣除数额已超出当前持有量,请核对后重试');
return;
}
state.submitting = true;
Fast.api.ajax({
url: `shopro/user/user/recharge`,
type: 'POST',
data: form.model,
loading: true
}, function (ret, res) {
state.submitting = false;
Fast.api.close();
}, function (ret, res) {
state.submitting = false;
});
}
});
}
onMounted(() => {
getUserInfo();
})
return {
state,
form,
assetLabel,
assetUnit,
currentBalance,
previewBalance,
isOverdrawn,
onTypeChange,
onCancel,
onConfirm
}
}
}
createApp('recharge', recharge);
},
select: function () {
const { reactive, onMounted } = Vue
const select = {
setup() {
const state = reactive({
id: new URLSearchParams(location.search).get('id') || false,
filter: {
drawer: false,
data: {
keyword: '',
},
tools: {},
condition: {},
},
data: [],
ids: [],
isSelectAll: false,
isIndeterminate: false,
})
function getData() {
let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
let search = composeFilter(tempSearch, {
keyword: 'like',
});
Fast.api.ajax({
url: 'shopro/user/user/select',
type: 'GET',
data: {
page: pagination.page,
list_rows: pagination.list_rows,
...search,
},
}, function (ret, res) {
state.data = res.data.data
pagination.total = res.data.total
calculateSelect();
return false
}, function (ret, res) { })
}
const pagination = reactive({
page: 1,
list_rows: 10,
total: 0,
})
function onChangeFilter() {
pagination.page = 1
getData()
}
function onSelect(type, row) {
if (type) {
state.ids.push(row.id);
} else {
let findIndex = state.ids.findIndex((id) => id == row.id);
state.ids.splice(findIndex, 1);
}
calculateSelect();
};
function onSelectAll(type) {
if (type) {
state.data.forEach((item) => {
state.ids.push(item.id);
});
state.ids = Array.from(new Set(state.ids));
} else {
state.data.forEach((item) => {
if (state.ids.findIndex((id) => id == item.id) !== -1) {
state.ids.splice(
state.ids.findIndex((id) => id == item.id),
1,
);
}
});
}
calculateSelect();
}
function calculateSelect() {
state.isSelectAll = false;
state.isIndeterminate = false;
if (state.data.every((item) => state.ids.includes(item.id))) {
state.isSelectAll = true;
state.isIndeterminate = false;
} else if (state.data.some((item) => state.ids.includes(item.id))) {
state.isSelectAll = false;
state.isIndeterminate = true;
}
if (state.data.length === 0) {
state.isSelectAll = false;
state.isIndeterminate = false;
}
}
function onConfirm() {
Fast.api.ajax({
url: `shopro/coupon/send/id/${state.id}`,
type: 'POST',
data: {
user_ids: state.ids
},
}, function (ret, res) {
Fast.api.close()
}, function (ret, res) { })
}
onMounted(() => {
getData()
})
return {
state,
getData,
pagination,
onChangeFilter,
onSelect,
onSelectAll,
onConfirm
}
}
}
createApp('select', select);
},
};
return Controller;
});