You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1668 lines
58 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\mall\controller\admin;
use ali\pay\AliPay;
use app\base\model\freight\Freight;
use app\base\model\platform\PlatformKdniaoCode;
use app\fire\model\MallSearchRecord;
use app\integral\model\IntegralSearchHot;
use app\mall\logic\Order;
use app\mall\model\MallAttach;
use app\mall\model\MallCategory;
use app\mall\model\MallCollect;
use app\mall\model\MallConfig;
use app\mall\model\MallConfigAdvertise;
use app\mall\model\MallConfigCarousel;
use app\mall\model\MallEvaluate;
use app\mall\model\MallLabel;
use app\mall\model\MallOrder;
use app\mall\model\MallOrderProduct;
use app\mall\model\MallOrderRefund;
use app\mall\model\MallPool;
use app\mall\model\MallProduct;
use app\mall\model\MallProductAttach;
use app\mall\model\MallProductCategory;
use app\mall\model\MallProductDistribution;
use app\mall\model\MallProductLabel;
use app\mall\model\MallProductSku;
use app\mall\model\MallProductSpec;
use app\mall\model\MallProductSpecItem;
use app\mall\model\MallRefundCause;
use app\mall\model\MallSearchHot;
use excel\Excel;
use Godruoyi\Snowflake\Snowflake;
use think\facade\Db;
use think\facade\View;
class Mall extends Base
{
/**
* 商城设置
* @return array|mixed|string
*/
public function config()
{
$param = input();
$mall_config_model = new MallConfig();
$mall_config_advertise_model = new MallConfigAdvertise();
$mall_config_carousel_model = new MallConfigCarousel();
if (request()->isPost()) {
switch ($param['r']) {
case 'base':
//基础设置
$param['uid'] = UID;
$res = $mall_config_model->dataUpdate($param);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
break;
case 'carousel':
//轮播图
$carousel = [];
foreach ($param['carousel_image'] as $k_1 => $value) {
$carousel[] = [
'uid' => UID,
'image' => $value,
'link_url' => $param['carousel_link_url'][$k_1],
'id' => $param['carousel_id'][$k_1],
];
}
$res = $mall_config_carousel_model->dataUpdateByAll($carousel);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
break;
case 'advert':
//广告位招租
$advert = [];
foreach ($param['advert_image'] as $k_2 => $value) {
$advert[] = [
'uid' => UID,
'image' => $value,
'link_url' => $param['advert_link_url'][$k_2],
'id' => $param['advert_id'][$k_2],
'type' => 1
];
}
//促销
$promotion = [];
foreach ($param['promotion_image'] as $k_3 => $value) {
$promotion[] = [
'uid' => UID,
'image' => $value,
'link_url' => $param['promotion_link_url'][$k_3],
'id' => $param['promotion_id'][$k_3],
'type' => 2
];
}
$res1 = $mall_config_advertise_model->dataUpdateByAll($advert);
if ($res1 === false) {
return sendErrorMessage();
}
$res2 = $mall_config_advertise_model->dataUpdateByAll($promotion);
if ($res2 === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
break;
default:
}
} else {
switch ($param['r']) {
case 'base':
$data = $mall_config_model->getOneData(['uid' => UID]);
View::assign('data', $data);
break;
case 'carousel':
$carousel = $mall_config_carousel_model->getAllData(['uid' => UID], 'id,image,link_url', 'id asc')->toArray();
$count = 5 - count($carousel);
if ($count > 0) {
for ($i = 0; $i <= $count; $i++) {
$temp = array(
'id' => "",
'image' => "",
'link_url' => ""
);
array_push($carousel, $temp);
}
}
View::assign('carousel', $carousel);
break;
case 'advert':
//首页广告图 位置1图一张
$advert = $mall_config_advertise_model->getAllData(['uid' => UID, 'type' => 1], 'id,image,link_url', 'id asc')->toArray();
$advert_count = 1 - count($advert);
if ($advert_count > 0) {
for ($i = 0; $i <= $advert_count; $i++) {
$temp = array(
'id' => "",
'image' => "",
'link_url' => ""
);
array_push($advert, $temp);
}
}
View::assign('advert', $advert);
//首页广告图 位置2图三种
$promotion = $mall_config_advertise_model->getAllData(['uid' => UID, 'type' => 2], 'id,image,link_url', 'id asc')->toArray();
$count = 3 - count($promotion);
if ($count > 0) {
for ($i = 0; $i <= $count; $i++) {
$temp = array(
'id' => "",
'image' => "",
'link_url' => ""
);
array_push($promotion, $temp);
}
}
View::assign('promotion', $promotion);
break;
default:
}
View::assign('r', $param['r']);
return View::fetch();
}
}
//产品分类 2017-10-15
public function category()
{
$search = input('get.');
autoSearch(['keyword'], $search);
View::assign('search', $search);
$where [] = ['uid', '=', UID];
if ($search['keyword']) {
$where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
}
$mall_category_model = new MallCategory();
// $dataList = $mall_category_model->getInfinite($where, 'pid,sort desc', 2);
$dataList = $mall_category_model->getListAll($where);
View::assign('dataList', $dataList);
$attach = [
'total_html' => [['一级分类总数', count($dataList)]]
];
View::assign('attach', $attach);
return View::fetch();
}
//商品分类添加 2017-10-15
public function categoryAdd()
{
$mall_category_model = new MallCategory();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
if ($data['pid'] == $data['id']) {
return sendErrorMessage(1, '您不能选择自己为下级');
}
$res = $mall_category_model->dataUpdate($data);
if (!$res) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_category_model->getOneData(['id' => $id]);
$where = [
'uid' => UID,
'pid' => 0
];
$categoryList = $mall_category_model->getAllData($where, '', 'id asc');
View::assign('data', $data);
View::assign('categoryList', $categoryList);
return View::fetch('categoryadd');
}
}
/**
* 商品分类修改
* @return array|mixed|string
*/
public function categoryUpdate()
{
return $this->categoryadd();
}
/*
* 添加二级分类
*/
public function categoryaddbytwo()
{
$mall_category_model = new MallCategory();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$res = $mall_category_model->dataUpdate($data);
if (!$res) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$pid = input('param.pid', 0);
$data = $mall_category_model->getOneData(['id' => $pid]);
View::assign('data', $data);
return View::fetch('categoryaddbytwo');
}
}
/**
* 标识/服务保障
* @return array|mixed|string
*/
public function attach()
{
$search = input('get.');
autoSearch(['time', 'keyword', 'type'], $search);
View::assign('search', $search);
$where [] = ['uid', '=', UID];
if ($search['keyword']) {
$where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
}
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
$page = array_key_exists("page", $search) ? (string)$search['page'] : "1";
$mall_attach_model = new MallAttach();
$dataList = $mall_attach_model
->where($where)->order('sort desc,id desc')
->paginate(['list_rows' => 20, 'page' => $page, 'query' => $search], false);
View::assign('dataList', $dataList);
$attach = [
'total_html' => [['服务保障总数', $dataList->total()]]
];
View::assign('attach', $attach);
return View::fetch();
}
/**
* 标识/服务保障添加
* @return array|mixed|string
*/
public function attachadd()
{
$mall_attach_model = new MallAttach();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$res = $mall_attach_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_attach_model->getOneData(['id' => $id]);
View::assign('data', $data);
return View::fetch('attachadd');
}
}
/**
* 标识/服务保障修改
* @return bool
*/
public function attachupdate()
{
return $this->attachadd();
}
/**
* 热门标签列表
* @return array|mixed|string
*/
public function search()
{
$mall_search_hot_model = new MallSearchHot();
$where = ['uid' => UID];
$dataList = $mall_search_hot_model->getAllData($where, '', 'sort desc,id desc');
View::assign('dataList', $dataList);
return View::fetch("search");
}
/**
* 热门搜索添加
* @return array|mixed|string
*/
public function searchAdd()
{
$mall_search_hot_model = new MallSearchHot();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$res = $mall_search_hot_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_search_hot_model->getOneData(['id' => $id]);
View::assign('data', $data);
return View::fetch('searchadd');
}
}
public function searchupdate()
{
return $this->searchAdd();
}
/**
* 热门标签列表
* @return array|mixed|string
*/
public function record()
{
$search = input('get.');
autoSearch(['time', 'type'], $search);
View::assign('search', $search);
$record_model = new MallSearchRecord();
$where = [['uid', '=', UID]];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if (isset($search['type']) && $search['type'] !== '') {
$where [] = ['type', '=', $search['type']];
}
session('where',$where);
$dataList = $record_model->with(['userInfo'])
->where($where)->order('id desc')->paginate([
'list_rows' => 50,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)
->each(function ($item, $key) {
return $item;
});
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 导出订单
*/
public function recordexport()
{
$where = session('where');
$record_model = new MallSearchRecord();
$dataList = $record_model->with(['userInfo'])
->where($where)->order('id desc')->select();
$data[] = [];
$type = [1=>'专家',2=>'消防知识',3=>'消防材料'];
foreach ($dataList as $key => $value) {
$data[$key] = [
$value['content'], //关键词
$value['create_time'], //搜索时间
$type[$value['type']], //搜索类型
$value['nick_name'], //搜索人
];
};
$fileName = "搜索记录表";
$headArr = ["关键词", "搜索时间", '搜索类型', "搜索人"];
$msg = '';
$excel_class = new Excel();
$res = $excel_class->excelExport($fileName, $headArr, $data, $msg);
return $res;
}
//产品列表
public function product()
{
$search = input('get.');
autoSearch(['time', 'keyword', 'is_publish', 'category_id', 'is_recommend'], $search);
View::assign('search', $search);
$mall_product_model = new MallProduct();
$where = [['uid', '=', UID], ['type', '=', 1]];
$hasWhere = [];
if ($search['keyword']) {
$where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
}
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if (isset($search['is_publish']) && $search['is_publish'] !== '') {
$where [] = ['is_publish', '=', $search['is_publish']];
}
if (isset($search['category_id']) && $search['category_id'] != '') {
$hasWhere[] = ['category_id', '=', $search['category_id']];
}
if (isset($search['is_recommend']) && $search['is_recommend'] !== '') {
$where [] = ['is_recommend', '=', $search['is_recommend']];
}
$where = $mall_product_model->whereChange($where);
$dataList = $mall_product_model::hasWhere('mallProductCategory', $hasWhere)
->where($where)->order('id desc')->paginate([
'list_rows' => 20,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)
->each(function ($item, $key) {
$category_model = new MallProductCategory();
$item['category_id'] = $category_model->where(['product_id' => $item['id']])->column('category_id');
//计算收藏量
$interactive_model = new MallCollect();
$item['collect_number'] = $interactive_model->getNumber(['product_id' => $item['id']]);
//计算总库存数
if ($item['is_spec_open'] == 1) {
$sku_model = new MallProductSku();
$item['stock'] = $sku_model->where(['product_id' => $item['id']])->sum('stock');
}
return $item;
});
View::assign('dataList', $dataList);
$attach = [
'total_html' => [['商品总数', $dataList->total()]]
];
View::assign('attach', $attach);
//获取二级分类信息
$mall_category_model = new MallCategory();
$where = [['uid', '=', UID], ['pid', '>=', 0]];
$category_list = $mall_category_model->getInfiniteNameData($where, 'sort', 2);
View::assign('category_list', $category_list);
return View::fetch();
}
//添加产品 2017-10-15
public function productadd()
{
$mall_label_model = new MallLabel();
$mall_attach_model = new MallAttach();
$mall_product_model = new MallProduct();
$mall_category_model = new MallCategory();
$mall_product_label_model = new MallProductLabel();
$mall_product_attach_model = new MallProductAttach();
$mall_product_category_model = new MallProductCategory();
$mall_product_distribution_model = new MallProductDistribution();
if (request()->isPost()) {
$data = input('post.');
if (!empty($data['is_change'])) {
return sendErrorMessage(1, '规格数据有变动,请重新点击[刷新规格项目表] 按钮!');
}
if ($data['video_url'] && !$data['video_cover_img']) {
return sendErrorMessage(1, '请上传视频封面图');
}
$data['uid'] = UID;
$data['is_spec_open'] = array_key_exists('is_spec_open', $data) ? $data['is_spec_open'] : 0;
if ($data['is_spec_open'] == 1 && !array_key_exists('option_ids', $data)) {
return sendErrorMessage(1, '请完善规格数据');
}
//获取商品最高价与最低价
$price_min = $data['price'];
$price_max = $data['price'];
if ($data['is_spec_open'] == 1) {
if (!array_key_exists('spec_id', $data)) {
return sendErrorMessage(1, '请添加规格');
}
$price = [];
foreach ($data['option_ids'] as $key => $value) {
$price[] = $data['option_price_' . $value];
}
$price_min = min($price);
$price_max = max($price);
}
$update = [
'id' => $data['id'],
'uid' => UID,
'name' => $data['name'],
'price' => $data['price'],
'price_original' => $data['price_original'],
'price_min' => $price_min,
'price_max' => $price_max,
'attach_id' => array_key_exists('attach_id', $data) ? implode(',', $data['attach_id']) : null,
'cover_img' => $data['cover_img'],
'img_path' => $data['img_path'],
'video_url' => $data['video_url'],
'video_cover_img' => $data['video_cover_img'],
'description' => array_key_exists('description', $data) ? $data['description'] : '',
'is_recommend' => array_key_exists('is_recommend', $data) ? $data['is_recommend'] : 0,
'publish_time' => array_key_exists('is_publish', $data) ? time() : 0,
'is_publish' => array_key_exists('is_publish', $data) ? $data['is_publish'] : 0,
'recommend_time' => array_key_exists('is_recommend', $data) ? time() : 0,
'sales_origial_number' => $data['sales_origial_number'],
'is_spec_open' => $data['is_spec_open'],
'stock' => $data['stock'],
'freight_type' => $data['freight_type'],
'freight_id' => $data['freight_type'] == 2 ? $data['freight_id'] : 0,
'freight_money' => $data['freight_type'] == 1 ? $data['freight_money'] : 0,
'weight' => $data['weight']
];
if (!empty($update['video_url']) && empty($update['video_cover_img'])) {
return sendErrorMessage(4001, '请上传视频封面图');
}
Db::startTrans();
$product_id = $mall_product_model->dataUpdate($update);
if ($product_id === false) {
Db::rollback();
return sendErrorMessage();
}
//商品标签操作
$res = $this->productLabel($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品分类操作
$res = $this->productCategory($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品规格操作
$res = $this->productSpec($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品服务保障操作
$res = $this->productAttach($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
//商品服务保障操作
$res = $this->productDistribution($product_id, $data);
if ($res == false) {
Db::rollback();
return sendErrorMessage();
}
Db::commit();
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_product_model->getOneData(['id' => $id]);
$where = [['uid', '=', UID], ['pid', '>=', 0]];
$categoryList = $mall_category_model->getInfiniteNameData($where, 'sort', 2);
//查询商品关联分类
$category_id = $mall_product_category_model->where('product_id', $id)->column('category_id');
if (empty($category_id)) {
$category_id = [];
}
foreach ($categoryList as $key => &$value) {
if (in_array($value['id'], $category_id)) {
$value['is_select'] = 1;
} else {
$value['is_select'] = 0;
}
}
View::assign('categoryList', $categoryList);
//查询服务保障
$attach_id = $mall_product_attach_model->where('product_id', $id)->column('attach_id');
$where_attach = ['uid' => UID];
$attach = $mall_attach_model->getAllData($where_attach);
foreach ($attach as $key => &$value) {
if (in_array($value['id'], $attach_id)) {
$value['is_select'] = 1;
} else {
$value['is_select'] = 0;
}
}
View::assign('attach', $attach);
//查询标签
$labelList = $mall_label_model->getAllData(['uid' => UID]);
$label_id = $mall_product_label_model->where('product_id', $id)->column('label_id');
if (empty($label_id)) {
$label_id = [];
}
foreach ($labelList as $key => &$value) {
if (in_array($value['id'], $label_id)) {
$value['is_select'] = 1;
} else {
$value['is_select'] = 0;
}
}
View::assign('labelList', $labelList);
if (!empty($data)) {
$mall_product_spec_model = new MallProductSpec();
$mall_product_spec_item_model = new MallProductSpecItem();
//查询属性及属性项
$spec = $mall_product_spec_model->getAllData(['uid' => UID, 'product_id' => $id], '', 'id');
foreach ($spec as $key => &$value) {
$value['item'] = $mall_product_spec_item_model->getAllData(['uid' => UID, 'spec_id' => $value['spec_id'], 'product_id' => $id], '', 'id asc')->toArray();
}
//查询SKU
$mall_product_sku_model = new MallProductSku();
$sku = $mall_product_sku_model->getSkuListByProduct(['product_id' => $id], 'id asc');
$product_distribution_data = $mall_product_distribution_model->getOneData(['product_id' => $data['id']]);
if ($product_distribution_data) {
$data['first_percent'] = $product_distribution_data['first_percent'];
$data['first_pay'] = $product_distribution_data['first_pay'];
$data['second_percent'] = $product_distribution_data['second_percent'];
$data['second_pay'] = $product_distribution_data['second_pay'];
$data['third_percent'] = $product_distribution_data['third_percent'];
$data['third_pay'] = $product_distribution_data['third_pay'];
} else {
$data['first_percent'] = '';
$data['first_pay'] = '';
$data['second_percent'] = '';
$data['second_pay'] = '';
$data['third_percent'] = '';
$data['third_pay'] = '';
}
} else {
$spec = [];
$sku = [];
}
// dump($sku);
// exit();
//获取运费模板
$freight_model = new Freight();
$freight = $freight_model->getAllData(['uid' => UID], '*', 'is_default desc');
View::assign('freight', $freight);
View::assign('spec', $spec);
// dump(json_encode($sku));exit();
View::assign('sku', json_encode($sku));
View::assign('data', $data);
return View::fetch('productadd');
}
}
/**
* 产品发布
*/
public function productPublish()
{
$data = input('post.');
$product_model = new MallProduct();
$update_data = ['is_publish' => $data['value'], 'publish_time' => time()];
if ($data['value'] == 0) {
$update_data['publish_time'] = 0;
}
$res = $product_model->where(['id' => $data['id']])->update($update_data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
}
/**
* 订单列表
* @return string
*/
public function order()
{
$search = input('get.');
$r = input('param.r');
$mall_order_model = new MallOrder();
$mall_order_product_model = new MallOrderProduct();
autoSearch(['time', 'keyword', 'status', 'pay_type', 'product_name', 'remind', 'linkman', 'mobile_phone'], $search);
$where = [['uid', '=', UID], ['type', '=', 1]];
$hasWhere = [];
if ($r > 0) {//订单状态
$where [] = ['status', '=', $r];
}
if ($search['keyword']) {//关键字
$where[] = ['order_number', 'like', '%' . $search['keyword'] . '%'];
}
if ($search['time']) {//订单时间
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if ($search['product_name'] !== null && $search['product_name'] !== '') {//商品名称
$hasWhere[] = ['product_name', 'like', '%' . $search['product_name'] . '%'];
}
if ($search['pay_type'] !== null && $search['pay_type'] !== '') {//支付方式
$where [] = ['pay_type', '=', $search['pay_type']];
}
if (!empty($search['remind'])) {
if ($search['remind'] == 1) {
if (!isset($where['status']) || $where['status'] == 2) {
$where [] = ['remind_time', 'not null', ''];
$where [] = ['status', '=', 2];
}
} else {
if (!isset($where['status']) || $where['status'] == 2) {
$where [] = ['remind_time', 'null', ''];
$where [] = ['status', '=', 2];
}
}
}
if ($search['linkman'] !== null && $search['linkman'] !== '') {//联系人
$where[] = ['linkman', 'like', '%' . $search['linkman'] . '%'];
}
if ($search['mobile_phone'] !== null && $search['mobile_phone'] !== '') {//联系电话
$where[] = ['mobile_phone', 'like', '%' . $search['mobile_phone'] . '%'];
}
$where = $mall_order_model->whereChange($where);
session('where', $where);
session('hasWhere', $hasWhere);
$dataList = $mall_order_model::hasWhere('mallOrderProduct', $hasWhere)
->where($where)->order('id desc')->paginate([
'list_rows' => 20,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false)
->each(function ($item, $key) use ($mall_order_product_model) {
$item['product_list'] = $mall_order_product_model->getAllData(['order_id' => $item['id'], 'uid' => UID], 'product_id,product_name,is_product_spec_open,product_sku_name,product_price,number,total_price', 'create_time desc')->toArray();
return $item;
});
$order_total_money = $mall_order_model::hasWhere('mallOrderProduct', $hasWhere)->where($where)->sum('MallOrder.total_price');
$attach = [
'total_html' => [
['订单总数', $dataList->total()],
['订单金额', formatNumber($order_total_money)]
],
];
View::assign('r', $r);
View::assign('search', $search);
View::assign('dataList', $dataList);
View::assign('attach', $attach);
return View::fetch();
}
/**
* 订单详情信息
* @return string
*/
public function orderdetail()
{
$express_model = new PlatformKdniaoCode();
$mall_order_model = new MallOrder();
$id = input('param.id');
$data = $mall_order_model->with('userInfo')->where(['id' => $id])->find();
if ($id) {
$order_id = $data['id'];
$mall_order_product_model = new MallOrderProduct();
$order_product = $mall_order_product_model->getAllData([['order_id', '=', $order_id]]);
$data['total_number'] = 0;
foreach ($order_product as $key => $value) {
$value['sku_name'] = $this->skuToName($value['product_sku'], $value['product_id']);
$data['total_number'] += $value['number'] - $value['refund_total_number'] - $value['refund_cur_number'];
$order_product[$key] = $value;
}
$data['order_product'] = $order_product;
}
$expressList = $express_model->getAllData(['is_show' => 1]);
View::assign('data', $data);
View::assign('expressList', $expressList);
return View::fetch();
}
/**
* 订单发货
*/
public function orderSend()
{
$mall_order_model = new MallOrder();
$express_model = new PlatformKdniaoCode();
if (request()->isPost()) {
$data = input('post.');
$order = $mall_order_model->getOneData(['id' => $data['id']]);
if (!in_array($order['status'], [2, 3])) {
return sendErrorMessage(1, '订单状态已发生变化,请重试');
}
if ($order['status'] == 2) {
if (!isMobilephone($data['mobile_phone'])) {
return sendErrorMessage(1, '手机号格式不正确');
}
}
$express = $express_model->getOneData(['id' => $data['express_id']]);
$data['express_code'] = $express['express_code'];
$data['express_name'] = $express['express_company'];
$data['express_time'] = time();
$res = $mall_order_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id');
$data = $mall_order_model->getOneData(['id' => $id]);
$expressList = $express_model->getAllData(['is_show' => 1]);
View::assign('data', $data);
View::assign('expressList', $expressList);
return View::fetch('ordersend');
}
}
/*
* 退款订单列表
*/
public function refund()
{
$search = input('get.');
autoSearch(['time', 'keyword', 'status', 'refund_type', 'product_order_number'], $search);
$mall_refund_model = new MallOrderRefund();
$where = [['uid', '=', UID],
// ['type' ,'=', 1]
];
$hasWhere = [];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if ($search['keyword'] !== null && $search['keyword'] !== '') {
$where[] = ['order_number', 'like', '%' . $search['keyword'] . '%'];
}
if ($search['refund_type'] !== null && $search['refund_type'] !== '') {
$where[] = ['refund_type', '=', $search['refund_type']];
}
if ($search['status'] !== null && $search['status'] !== '') {
$where[] = ['status', '=', $search['status']];
}
$where = $mall_refund_model->whereChange($where);
// dump($where);exit();
session('where', $where);
$dataList = $mall_refund_model
::hasWhere('mallOrder', $hasWhere)
->with(['userInfo', 'mallOrder', 'mallProduct'])
->where($where)->order('id desc')->paginate([
'list_rows' => 20,
'page' => array_key_exists("page", $search) ? (string)$search['page'] : "1",
'query' => $search
], false);
$refund_money = $mall_refund_model::hasWhere('mallOrder', $hasWhere)->where($where)->sum('refund_money');
$attach = [
'total_html' => [['订单总数', $dataList->total()], ['退款金额', formatNumber($refund_money)]],
];
View::assign('attach', $attach);
View::assign('search', $search);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 退款订单审核通过(同步做退款操作)
* @return array|string
* @throws \think\Exception
*/
public function refundagree()
{
$id = input('param.id', 0);
if (empty($id)) {
return sendErrorMessage(1, '参数错误,审核失败!');
}
$order_logic = new \app\mall\logic\Order();
Db::startTrans();
$res = $order_logic->orderRefundSuccess($id);
if ($res['code'] != 0) {
Db::rollback();
return json($res);
}
Db::commit();
return sendSuccessMessage('', '审核退款成功');
}
/**
* 审核订单审核不通过
* @date 2021-10-27
*/
public function refundRefuse()
{
$id = input('param.id', 0);
$refuse_text = input('param.refuse_text', '');
if (empty($id)) {
return sendErrorMessage(1, '参数错误,审核状态变更失败!');
}
$order_logic = new \app\mall\logic\Order();
Db::startTrans();
$res = $order_logic->orderRefundFail($id, $refuse_text);
if ($res['code'] != 0) {
Db::rollback();
return json($res);
}
Db::commit();
return sendSuccessMessage('', '审核拒绝成功');
}
// /*
// * 退款库存恢复
// */
// public function stockchange($id,$refund_number)
// {
// //获取订单信息
// $mall_order_product_model = new MallOrderProduct();
// $data = $mall_order_product_model->field('id,product_id,is_product_spec_open,product_sku,number')->where(['id' => $id])->find();
// $mall_product_model = new MallProduct();
// $mall_product_sku_model = new MallProductSku();
// if (!empty($data)) {
// if ($data['is_product_spec_open'] == 1) {
// $mall_product_model->where(['id' => $data['product_id']])->inc('stock', $refund_number)->update();
// } else {
// $mall_product_sku_model->where(['product_id' => $data['product_id'], 'sku' => $data['product_sku']])->inc('stock', $refund_number)->update();
// }
// }
// }
/**
* 导出订单
*/
public function orderexport()
{
$where = session('where');
$hasWhere = session('hasWhere');
$mall_order_model = new MallOrder();
$dataList = $mall_order_model::hasWhere('mallOrderProduct', $hasWhere)
->with(['userInfo'])
->where($where)->order('id desc')->select();
$data[] = [];
$mall_order_product_model = new MallOrderProduct();
$pay_way_name = [0 => '0元单', 1 => '微信支付', 2 => '余额支付', 3 => '支付宝支付', 4 => '付呗微信', 5 => '付呗支付宝'];
foreach ($dataList as $key => $value) {
$value['status_'] = ORDER_STATUS[$value['status']]['name'];
$value['pay_type_name'] = $pay_way_name[$value['pay_type']];
$product_list = $mall_order_product_model->getAllData(['order_id' => $value['id'], 'uid' => UID], 'product_name,number', 'create_time desc');
$temp_list = [];
if (!empty($product_list)) {
$temp_list = [];
foreach ($product_list as $val) {
$str = $val['product_name'] . "(×" . $val['number'] . ")";
$temp_list[] = $str;
}
$value['product_list'] = implode('  ', $temp_list);
} else {
$value['product_list'] = "";
}
$data[$key] = [
$value['nick_name'], //昵称
' ' . $value['order_number'], //订单编号
$value['product_list'], //购买商品
$value['total_price'], //价钱
$value['pay_money'], //实际付款
$value['coupon_money'], //优惠券抵扣金额
$value['pay_type_name'], //付款方式
$value['linkman'], //联系人
$value['mobile_phone'], //联系电话
$value['province'] . $value['city'] . $value['area'] . $value['address'], //地址
$value['express_name'], //快递名称
' ' . $value['express_number'], //快递单号
$value['create_time'], //时间
$value['status_'], //状态
];
};
$fileName = "商城订单表";
$headArr = ["昵称", "订单编号", '购买商品信息', "价钱", "实际付款", "优惠券抵扣金额", "付款方式", "联系人", "联系电话", "地址", "快递名称", "快递单号", "时间", "状态"];
$msg = '';
$excel_class = new Excel();
$res = $excel_class->excelExport($fileName, $headArr, $data, $msg);
return $res;
}
/**
* sku转换成对应名称
* @param $sku
* @param $product_id
* @return string
*/
public function skuToName($sku, $product_id)
{
$mall_product_spec_item_model = new MallProductSpecItem();
$sku_name = '';
if (!empty($sku)) {
$where_spec = ['spec_item_id' => explode('_', $sku), 'product_id' => $product_id];
$spec = $mall_product_spec_item_model->where($where_spec)->order('spec_item_id')->column('spec_item_value');
$sku_name = implode(' ', $spec);
}
return $sku_name;
}
/**
* 商品编辑
* @return mixed
*/
public function productupdate()
{
return $this->productadd();
}
public function productLabel($product_id, $data = [])
{
$product_label_model = new MallProductLabel();
$product_label_data = [];
if (!isset($data['label_id'])) {
$data['label_id'] = [];
}
foreach ($data['label_id'] as $key => $value) {
$product_label_data[] = [
'product_id' => $product_id,
'label_id' => $value,
'create_time' => time()
];
}
$res = $product_label_model->where(['product_id' => $product_id])->delete();
if ($res === false) {
return false;
}
$res = $product_label_model->insertAll($product_label_data);
if ($res === false) {
return false;
}
return true;
}
public function productAttach($product_id, $data = [])
{
$product_attach_model = new MallProductAttach();
$product_attach_data = [];
$res = $product_attach_model->where(['product_id' => $product_id])->delete();
if (array_key_exists('attach_id', $data)) {
foreach ($data['attach_id'] as $key => $value) {
$product_attach_data[] = [
'product_id' => $product_id,
'attach_id' => $value,
'create_time' => time()
];
}
if ($res === false) {
return false;
}
$res = $product_attach_model->insertAll($product_attach_data);
if ($res === false) {
return false;
}
}
return true;
}
public function productDistribution($product_id, $data = [])
{
$mall_product_distribution_model = new MallProductDistribution();
$product_distribution_data = [];
$res = $mall_product_distribution_model->where(['product_id' => $product_id])->delete();
$product_distribution_data = [
'uid' => UID,
'product_id' => $product_id,
'first_percent' => $data['first_percent'],
'first_pay' => $data['first_pay'],
'second_percent' => $data['second_percent'],
'second_pay' => $data['second_pay'],
'third_percent' => $data['third_percent'],
'third_pay' => $data['third_pay'],
];
$res = $mall_product_distribution_model->dataUpdate($product_distribution_data);
if (!$res) {
return false;
}
return true;
}
/**
* 商品-分类 操作
* @param $product_id
* @param array $data
* @return array|string
* @throws \think\Exception
*/
public function productCategory($product_id, $data = [])
{
$mall_product_category_model = new MallProductCategory();
$res = $mall_product_category_model->where(['product_id' => $product_id])->delete();
if ($res === false) {
return sendErrorMessage();
}
$insert_array = [];
if (array_key_exists('category_id', $data)) {
foreach ($data['category_id'] as $key => $value) {
$category_id = explode('_', $value);
if ($category_id[0] == 0) {
$insert_array[] = [
'product_id' => $product_id,
'category_id' => $category_id[1],
'type' => 1
];
} else {
$insert_array[] = [
'product_id' => $product_id,
'category_id' => $category_id[0],
'type' => 1
];
$insert_array[] = [
'product_id' => $product_id,
'category_id' => $category_id[1],
'type' => 2
];
}
}
$insert_array = array_values(array_unique($insert_array, SORT_REGULAR));
$res = $mall_product_category_model->insertAll($insert_array);
if ($res === false) {
return false;
}
}
return true;
}
/**
* 商品规格操作
* @param $product_id
* @param array $data
* @return array|string
* @throws \think\Exception
*/
public function productSpec($product_id, $data = [])
{
$snow_flake = new Snowflake();
$mall_product_model = new MallProduct();
$mall_product_sku_model = new MallProductSku();
$mall_product_spec_model = new MallProductSpec();
$mall_product_spec_item_model = new MallProductSpecItem();
//清空数据
$where = ['uid' => UID, 'product_id' => $product_id];
$res = $mall_product_spec_model->destroy($where);
if ($res === false) {
return sendErrorMessage();
}
$res = $mall_product_spec_item_model->destroy($where);
if ($res === false) {
return sendErrorMessage();
}
$res = $mall_product_sku_model->destroy($where);
if ($res === false) {
return sendErrorMessage();
}
if ($res === false) {
return sendErrorMessage();
}
if ($data['is_spec_open'] == 1) { //商品规格开启
//保存规格名
$spec_data = [];
$spec_item_data = [];
if (!array_key_exists('spec_id', $data)) {
return sendErrorMessage(1, '请添加规格');
}
foreach ($data['spec_id'] as $key => $value) {
$spec_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $product_id,
'spec_id' => $value,
'spec_title' => $data['spec_title'][$value],
'create_time' => time(),
];
if (!array_key_exists('spec_item_name_' . $value, $data)) {
return sendErrorMessage(1, '请添加' . $data['spec_title'][$value] . '的规格项');
}
foreach ($data['spec_item_name_' . $value] as $k => $v) {
$spec_item_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $product_id,
'spec_id' => $value,
'spec_item_id' => $data['spec_item_id_' . $value][$k],
'spec_item_value' => $data['spec_item_name_' . $value][$k],
'create_time' => time(),
];
}
}
$res = $mall_product_spec_model->insertAll($spec_data);
if ($res === false) {
return sendErrorMessage();
}
//保存规格值
$res = $mall_product_spec_item_model->insertAll($spec_item_data);
if ($res === false) {
return sendErrorMessage();
}
//保存SKU
$sku_data = [];
foreach ($data['option_ids'] as $key => $value) {
$sku_data[] = [
'id' => $snow_flake->id(),
'uid' => UID,
'product_id' => $product_id,
'sku' => $value,
'price' => $data['option_price_' . $value],
'stock' => $data['option_stock_' . $value],
'cover_img' => $data['option_cover_img_' . $value],
'create_time' => time()
];
}
$res = $mall_product_sku_model->insertAll($sku_data);
if ($res === false) {
return sendErrorMessage();
}
$all_stock = array_sum(array_column($sku_data, 'stock'));
$res = $mall_product_model->update(['stock' => $all_stock], ['id' => $product_id]);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
return sendSuccessMessage();
}
}
/**
* @return string
* 退款原因设置
*/
public function refundcause()
{
$search = input('get.');
autoSearch(['keyword'], $search);
$mall_attach_model = new MallRefundCause();
$where = [['uid', '=', UID]];
if ($search['keyword']) {
$where[] = ['content', 'like', '%' . $search['keyword'] . '%'];
}
$page = array_key_exists("page", $search) ? (string)$search['page'] : "1";
$dataList = $mall_attach_model
->where($where)->order('id asc')
->paginate(['list_rows' => 50, 'page' => $page], false)->each(function ($item, $key) {
return $item;
});
$attach = [
'total_html' => [['退款原因总数', $dataList->total()]]
];
View::assign('search', $search);
View::assign('attach', $attach);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* @return string
* 退款原因添加
*/
public function refundcauseadd()
{
$refund_model = new MallRefundCause();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$res = $refund_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $refund_model->getOneData(['id' => $id]);
View::assign('data', $data);
return View::fetch('refundcauseadd');
}
}
/**
* 退款原因修改
* @return string
*/
public function refundcauseupdate()
{
return $this->refundcauseadd();
}
/**
* 订单评论
* @return string
*/
public function review()
{
$search = input('get.');
$mall_review_model = new MallEvaluate();
autoSearch(['time', 'keyword', 'nick_name', 'is_replay', 'is_add', 'star_rank'], $search);
$where = [
['uid', '=', UID],
['type', '=', 1]
];
$hasWhere = [];
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
if ($search['keyword'] !== null && $search['keyword'] !== '') {
$where[] = ['content', 'like', '%' . $search['keyword'] . '%'];
}
if ($search['nick_name'] !== null && $search['nick_name'] !== '') {
$hasWhere[] = ['nick_name', 'like', '%' . $search['nick_name'] . '%'];
}
if ($search['is_replay'] !== null && $search['is_replay'] !== '') {
$where[] = ['is_replay', '=', $search['is_replay']];
}
if ($search['is_add'] !== null && $search['is_add'] !== '') {
$where[] = ['is_add', '=', $search['is_add']];
}
if ($search['star_rank'] !== null && $search['star_rank'] !== '') {
$where[] = ['star_rank', '=', $search['star_rank']];
}
$where = $mall_review_model->whereChange($where);
$page = array_key_exists("page", $search) ? (string)$search['page'] : "1";
$dataList = $mall_review_model::hasWhere('userInfo', $hasWhere)
->with(['mallEvaluate'])->where($where)->order('id desc')
->paginate(['list_rows' => 50, 'page' => $page], false)->each(function ($item, $key) {
return $item;
});
$attach = [
'total_html' => [['评论总数', $dataList->total()]]
];
View::assign('search', $search);
View::assign('attach', $attach);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 评论详情
* @return string|\think\response\Json
*/
public function reviewDetail()
{
$mall_review_model = new MallEvaluate();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$data['reply_time'] = time();
$res = $mall_review_model->dataUpdate($data);
if (!$res) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_review_model->with('userInfo')->where(['id' => $id])->find();
$add_review = $mall_review_model->getOneData(['evaluate_id' => $id]);
if ($add_review) {
$before_day = ceil((strtotime($data['create_time']) - strtotime($add_review['create_time'])) / 86400);
if ($before_day) {
$add_review['before_day'] = '用户' . $before_day . '天后追评';
} else {
$add_review['before_day'] = '用户当天追评';
}
}
View::assign('data', $data);
View::assign('add_review', $add_review);
return View::fetch('reviewdetail');
}
}
/**
* 评论回复
* @return string|\think\response\Json
*/
public function reviewReply()
{
return $this->reviewDetail();
}
/**
* 评论删除
* @return \think\response\Json
*/
public function reviewDelete()
{
$data = input('post.');
$evaluate_model = new MallEvaluate();
$evaluate = $evaluate_model->getOneData(['id' => $data['id']]);
Db::startTrans();
$res = $evaluate_model->where('id', $data['id'])->useSoftDelete('delete_time', time())->delete();
if (!$res) {
Db::rollback();
return sendErrorMessage();
}
if ($evaluate['evaluate_id'] > 0) {
$res = $evaluate_model->where(['id' => $evaluate['evaluate_id']])->update(['is_add' => 0]);
if ($res === false) {
Db::rollback();
return sendErrorMessage();
}
}
Db::commit();
return sendSuccessMessage();
}
/**
* 凑单选项
* @return array|mixed|string
*/
public function gather()
{
$search = input('get.');
$mall_pool_model = new MallPool();
autoSearch(['time', 'keyword', 'type'], $search);
$where [] = ['uid', '=', UID];
if ($search['keyword']) {
$where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
}
if ($search['time']) {
$time = explode('-', $search['time']);
$where[] = ['create_time', 'between time', [$time[0], $time[1]]];
}
$page = array_key_exists("page", $search) ? (string)$search['page'] : "1";
$dataList = $mall_pool_model
->where($where)->order('limit asc')
->paginate(['list_rows' => 50, 'page' => $page], false)->each(function ($item, $key) {
return $item;
});
$attach = [
'total_html' => [['总数', $dataList->total()]]
];
View::assign('search', $search);
View::assign('attach', $attach);
View::assign('dataList', $dataList);
return View::fetch();
}
/**
* 凑单选项添加
* @return array|mixed|string
*/
public function gatherAdd()
{
$mall_pool_model = new MallPool();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$count = $mall_pool_model->where(['uid' => UID])->count();
if ($count == 4) {
return sendErrorMessage(1, '最多添加4个选项');
}
$res = $mall_pool_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_pool_model->getOneData(['id' => $id]);
View::assign('data', $data);
return View::fetch('gatheradd');
}
}
/**
* 凑单选项修改
* @return bool
*/
public function gatherUpdate()
{
return $this->gatheradd();
}
/**
* 标签列表
* @return string
*/
public function label()
{
$search = input('get.');
autoSearch(['keyword'], $search);
View::assign('search', $search);
$where [] = ['uid', '=', UID];
if ($search['keyword']) {
$where[] = ['name', 'like', '%' . $search['keyword'] . '%'];
}
$page = array_key_exists("page", $search) ? (string)$search['page'] : "1";
$mall_label_model = new MallLabel();
$dataList = $mall_label_model
->where($where)->order('id asc')
->paginate(['list_rows' => 50, 'page' => $page], false)->each(function ($item, $key) {
return $item;
});
View::assign('dataList', $dataList);
$attach = [
'total_html' => [['标签总数', $dataList->total()]]
];
View::assign('attach', $attach);
return View::fetch();
}
/**
* 标签添加
* @return string|\think\response\Json
*/
public function labeladd()
{
$mall_label_model = new MallLabel();
if (request()->isPost()) {
$data = input('post.');
$data['uid'] = UID;
$res = $mall_label_model->dataUpdate($data);
if ($res === false) {
return sendErrorMessage();
}
return sendSuccessMessage();
} else {
$id = input('param.id', 0);
$data = $mall_label_model->getOneData(['id' => $id]);
View::assign('data', $data);
return View::fetch('labeladd');
}
}
public function labelupdate()
{
return $this->labeladd();
}
/**
* 查看order表是否有新的订单
* @date 2022-12-28
*/
public function indierequest()
{
$order_model = new MallOrder();
$count = $order_model
->where('is_pay', 1)
->whereTime('create_time', 'between', [time() - 60, time()])
->count();
return sendSuccessMessage([
'number' => $count
]);
}
}