文件
mashangyoujiu/Application/Retail/Controller/DiyController.class.php

641 行
23 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
namespace Retail\Controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header('Access-Control-Allow-Headers:x-requested-with,content-type');
use Common\Model\AdvanceGoodsModel;
use Common\Model\CardBagModel;
use Common\Model\ImageGalleryModel;
use Common\Model\RetailGiftCardCoverModel;
use Common\Model\RetailGiftCardModel;
use Common\Model\RetailGoodsClassModel;
use Common\Model\RetailGoodsModel;
use Common\Model\RetailGroupViewModel;
use Common\Model\RetailHostModel;
use Common\Model\RetailSpikeViewModel;
use Common\Model\RetailStoreXcxConfigModel;
use Common\Model\StoreConfigModel;
use Common\Model\SubCardModel;
use Think\Controller;
use Common\Model\MerchantConfigModel;//商户基本配置表
use Common\Validate\CustomValidate;
use Common\Model\RetailMgoodsModel;//商户商品表
//[1.8.86]
/**
* 商城DIY设置
* Class DiyController
* @package Retail\Controller
*/
//class DiyController extends BaseController
class DiyController extends Controller
{
private $store_id = 1;
private $merchant_id = 1;
private $thisTime;
public function _initialize()
{
// parent::_initialize();
if (I('post.merchant_id')) {
$this->merchant_id = I('post.merchant_id');
}
if (I('post.store_id')) {
$this->store_id = I('post.store_id');
}
$this->thisTime = time();
}
public function index()
{
$this->display();
}
/**
* 获取商品分类
*/
public function getGoodsClassList()
{
/* 商品分类 */
$GoodsClassModel = new RetailGoodsClassModel();
$pid = I('post.pid') ? I('post.pid') : 0;
$map = array(
'status' => 0,
'merchantid' => $this->merchant_id,
);
if (!empty($pid)) {
$map['pid'] = $pid;
} else {
$map['type'] = 1;
}
$classList = $GoodsClassModel->getSelect($map, 'id,class_name,pid', 'id asc');
$res = array(
'status' => 1,
'msg' => '请求成功',
'data' => $classList,
);
$this->ajaxReturn($res);
}
/**
* 获取商品列表
*/
public function getGoodsList()
{
$keyword = I('post.keyword');
$page = I('post.page') ? I('post.page') : 1;
$limit = I('post.limit') ? I('post.limit') : 10;
$class_id1 = I('post.class_id1');
$class_id2 = I('post.class_id2');
$class_id3 = I('post.class_id3');
$ids = I('post.ids');
$map = array(
'store_id' => $this->store_id,
'status' => 0,
'online_state' => 1,
'online_price' => array('gt', 0),
);
if ($keyword) {
$map['goods_name|goods_code|goods_number'] = array('like', '%' . $keyword . '%');
}
if($ids){
$map['id'] = array('not in', $ids);//商品ids
}
$goods_class_id = getGoodsClass($class_id1, $class_id2, $class_id3);
if (!empty($goods_class_id)) {
$map['goods_class_id'] = $goods_class_id;
}
$fields = array(
'id',
'goods_name',
'goods_code',
'goods_number',
'goods_img_id',
'online_price',
'add_time',
);
$order = 'id desc';
$GoodsModel = new RetailGoodsModel();
$goods = $GoodsModel->getSelect($map, $fields, $order, "{$page},{$limit}");//获取商品列表
$count = $GoodsModel->getCount($map);//获取商品数量
$ImageGalleryModel = new ImageGalleryModel();
foreach ($goods as &$value) {
$value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
$value['goods_code'] = $value['goods_code'] ? $value['goods_code'] : '---';
$value['goods_number'] = $value['goods_number'] ? $value['goods_number'] : '---';
$value['goods_img'] = $ImageGalleryModel->getImagePath($value['goods_img_id']);
$value['goods_img'] = $value['goods_img'] ? $value['goods_img'] : '';
unset($value['goods_img_id']);
}
$res['status'] = 1;
$res['msg'] = empty($goods) ? '暂无数据' : '获取成功';
$res['count'] = $count;
$res['data'] = $goods;
$this->ajaxReturn($res);
}
/**
* 获取次卡或卡包列表
*/
public function getSubCardList()
{
$page = I('post.page') ? I('post.page') : 1;
$limit = I('post.limit') ? I('post.limit') : 10;
$keyword = I('post.keyword');
$type = I('post.type') ? I('post.type') : 1;//1次卡 2卡包
// 实例化模型
if ($type == 1) {
$CardModel = new SubCardModel();
} else {
$CardModel = new CardBagModel();
}
// 类型搜索
$map = array(
'type' => array('in', '4,5'),//门店购买
'merchantid' => $this->merchant_id,
'card_type' => $type,
'storeids' => array('like', array("{$this->store_id}", "{$this->store_id},%", "%,{$this->store_id},%", "%,{$this->store_id}", "0"), "OR"),
);
// 名称搜索
if (trim($keyword)) {
$map['name'] = array('like', '%' . trim($keyword) . '%');
}
$map['_string'] = "end_time > {$this->thisTime} AND surplus > 0 AND status = 0";
// 统计总数
$count = $CardModel->total($map);
$subCardList = $CardModel->toSelect($map, $page, $limit, array('id', 'name', 'end_time', 'type', 'gallery_id'));
$ImageGalleryModel = new ImageGalleryModel();
$default_img = C('HTT_UL') . '/Public/paper/images/sub_card.png';
//次卡类型 1.自主发放 2.充值赠送 3.消费赠送 4.门店购买 5.门店购买指定商品
$typeArr = array(
'1' => '自主发放',
'2' => '充值赠送',
'3' => '消费赠送',
'4' => '门店购买',
'5' => '购买指定商品',
);
foreach ($subCardList as &$val) {
$val['end_time'] = date('Y-m-d H:i:s', $val['end_time']);
if ($val['gallery_id']) {
$val['card_img'] = $ImageGalleryModel->getFieldPath(array('id' => $val['gallery_id']));
}
$val['card_img'] = $val['card_img'] ? $val['card_img'] : $default_img;
unset($val['gallery_id']);
$val['type'] = $typeArr[$val['type']];
}
$res['status'] = 1;
$res['msg'] = empty($subCardList) ? '暂无数据' : '获取成功';
$res['data'] = $subCardList;
$res['count'] = (int)$count;
$this->ajaxReturn($res);
}
/**
* 获取秒杀列表
*/
public function getSeckillList()
{
$page = I('post.page') ? I('post.page') : 1;
$limit = I('post.limit') ? I('post.limit') : 10;
$keyword = I('post.keyword');
$RetailSpikeViewModel = new RetailSpikeViewModel();
$map = array(
'end_time' => array('gt', $this->thisTime),
'store_id' => $this->store_id,
'stock' => array('gt', 0),
'status' => 1,
'RetailGoods.status' => 0,
'RetailGoods.online_state' => 1,
);
if (!empty($keyword)) {
$map['RetailSpike.active_title|goods_name'] = array('like', "%{$keyword}%");
}
$fields = array(
'id',
'name',
'goods_name',
'start_time',
'end_time',
'stock',
'goods_img_id'
);
$count = $RetailSpikeViewModel->getCount($map);
$list = $RetailSpikeViewModel->getSelect($map, $fields, $page, $limit);
$ImageGalleryModel = new ImageGalleryModel();
foreach ($list as &$value) {
$value['start_time'] = date('Y-m-d H:i:s', $value['start_time']);
$value['end_time'] = date('Y-m-d H:i:s', $value['end_time']);
$value['goods_img'] = $ImageGalleryModel->getImagePath($value['goods_img_id']);
$value['goods_img'] = $value['goods_img'] ? $value['goods_img'] : '';
unset($value['goods_img_id']);
}
$res['status'] = 1;
$res['msg'] = empty($list) ? '暂无数据' : '获取成功';
$res['data'] = $list;
$res['count'] = intval($count);
$this->ajaxReturn($res);
}
/**
* 获取拼团列表
*/
public function getGroupList()
{
$page = I('post.page') ? I('post.page') : 1;
$limit = I('post.limit') ? I('post.limit') : 10;
$keyword = I('post.keyword');
$RetailSpikeViewModel = new RetailGroupViewModel();
$map = array(
'end_time' => array('gt', $this->thisTime),
'store_id' => $this->store_id,
'act_num' => array('gt', 0),
'act_type' => 1,
'RetailGoods.status' => 0,
'RetailGoods.online_state' => 1,
);
if (!empty($keyword)) {
$map['RetailGroup.actname|goods_name'] = array('like', "%{$keyword}%");
}
$fields = array(
'id',
'act_name',
'goods_name',
'start_time',
'end_time',
'act_num',
'goods_img_id',
);
$count = $RetailSpikeViewModel->getCount($map);
$list = $RetailSpikeViewModel->getSelect($map, $fields, $page, $limit);
$ImageGalleryModel = new ImageGalleryModel();
foreach ($list as &$value) {
$value['start_time'] = date('Y-m-d H:i:s', $value['start_time']);
$value['end_time'] = date('Y-m-d H:i:s', $value['end_time']);
$value['goods_img'] = $ImageGalleryModel->getImagePath($value['goods_img_id']);
$value['goods_img'] = $value['goods_img'] ? $value['goods_img'] : '';
unset($value['goods_img_id']);
}
$res['status'] = 1;
$res['msg'] = empty($list) ? '暂无数据' : '获取成功';
$res['data'] = $list;
$res['count'] = intval($count);
$this->ajaxReturn($res);
}
/**
* 获取礼品卡列表
*/
public function getGiftList()
{
$page = I('post.page') ? I('post.page') : 1;
$limit = I('post.limit') ? I('post.limit') : 10;
$keyword = I('post.keyword');
$RetailGiftCardModel = new RetailGiftCardModel();
$map = array(
'start_time' => array('lt', $this->thisTime),
'end_time' => array('gt', $this->thisTime),
'merchantid' => $this->merchant_id,
'status' => 1,
'state' => 1,
);
if (!empty($keyword)) {
$map['title'] = array('like', "%{$keyword}%");
}
$fields = array(
'id',
'title',
'start_time',
'end_time',
);
$count = $RetailGiftCardModel->getCount($map);
$list = $RetailGiftCardModel->getSelect($map, $fields, 'id desc', $page . ',' . $limit);
$GCCModel = new RetailGiftCardCoverModel();
$ImageGalleryModel = new ImageGalleryModel();
foreach ($list as &$value) {
$value['start_time'] = date('Y-m-d H:i:s', $value['start_time']);
$value['end_time'] = date('Y-m-d H:i:s', $value['end_time']);
$map = array(
$GCCModel::GIFT_CARD_ID => $value['id'],
$GCCModel::STATUS => 1,
$GCCModel::STATE => 1,
);
$img_id = $GCCModel->getOrderFindField($map, 'img_url_id', 'id desc');
$value['img_url'] = $ImageGalleryModel->getFieldPath(array('id' => $img_id));
}
$res['status'] = 1;
$res['msg'] = empty($list) ? '暂无数据' : '获取成功';
$res['data'] = $list;
$res['count'] = $count;
$this->ajaxReturn($res);
}
/**
* 获取促销商品列表
*/
public function getPromotionList()
{
$page = I('post.page');
$limit = I('post.limit');
$keyword = I('post.keyword');
$StoreXcxConfigModel = new RetailStoreXcxConfigModel();
$sMap = array(
'storeid' => $this->store_id,
);
$promotion_status = $StoreXcxConfigModel->getFindField($sMap, 'promotion_status');
if ($promotion_status != 1) {
$res['status'] = 0;
$res['msg'] = '商家未开启促销功能';
$this->ajaxReturn($res);
}
$HostModel = new RetailHostModel();
$GoodsModel = new RetailGoodsModel();
//促销
$map = array(
'h.storeid' => $this->store_id,
'h.type' => 2,
'h.status' => 1,
'h.state' => 1,
'h.starttime' => array('elt', $this->thisTime),
'h.endtime' => array('egt', $this->thisTime),
'g.online_state' => 1,
'g.status' => 0,
);
if (!empty($keyword)) {
$map['g.goods_name|g.goods_code|g.goods_number'] = array('like', '%' . $keyword . '%');
}
$field = array(
'h.id',
'h.act_price',
'h.goods_id',
'g.goods_img_id',
'g.goods_name',
'g.attribute_state',
's.goods_num',
);
$order = 'h.id desc,g.' . $HostModel::ID . ' desc,' . 'g.' . $GoodsModel::ID . ' desc, h.act_price asc';
$promotion = $HostModel->getHostGoods_v1($map, $field, $order, $page . ',' . $limit);
$Inventory = new \Api\Controller\InventoryController();
$ImageGalleryModel = new ImageGalleryModel();
foreach ($promotion as &$val) {
$val['goods_img'] = $ImageGalleryModel->getFieldPath(array('id' => $val['goods_img_id']));
$val['goods_img'] = $val['goods_img'] ? $val['goods_img'] : '';
$val['goods_num'] = floatval($val['goods_num']);
if ($val['attribute_state'] == 2) {
$val['goods_num'] = $Inventory->goods_num(2, $this->store_id, $val['goods_id']);
}
unset($val['goods_img_id']);
unset($val['attribute_state']);
}
$res['status'] = 1;
$res['msg'] = '获取成功';
$res['data'] = $promotion;
$this->ajaxReturn($res);
}
/**
* 保存DIY设置
*/
public function saveDIY()
{
$diy_data = I('post.diy_data');
if (empty($diy_data)) {
$res = array(
'status' => 0,
'msg' => '请传入DIY设置',
);
$this->ajaxReturn($res);
}
if(isset($diy_data{60000})){
$res['status'] = 0;
$res['msg'] = '插件使用过多';
$this->ajaxReturn($res);
}
$decode_data = json_decode($diy_data, true);
if (!empty($decode_data['page'])) {
//处理状态栏色值为rgb的情况,转为16进制
if (strpos(strtolower($decode_data['page']['topBackgroundColor']), 'rgb(') !== false) {
$decode_data['page']['topBackgroundColor'] = $this->RGBToHex($decode_data['page']['topBackgroundColor']);
}
}
$diy_data = json_encode($decode_data);
$StoreConfigModel = new StoreConfigModel();
$old_data = $StoreConfigModel->getFindField(array('store_id' => $this->store_id), 'diy_mall_data');
if ($old_data == $diy_data) {
$res['status'] = 1;
$res['msg'] = '保存成功';
} else {
$set = $StoreConfigModel->setDIY($this->store_id, $diy_data);
if ($set) {
S('diy_mall_data' . $this->store_id, null);
$res['status'] = 1;
$res['msg'] = '保存成功';
} else {
$res['status'] = 0;
$res['msg'] = '保存失败';
}
}
$this->ajaxReturn($res);
}
/**
* rgb转16进制
* @param $rgb rgb色值
* @return string
*/
protected function RGBToHex($rgb)
{
$regexp = "/^rgb\(([0-9]{0,3})\,\s*([0-9]{0,3})\,\s*([0-9]{0,3})\)/";
$re = preg_match($regexp, $rgb, $match);
$re = array_shift($match);
$hexColor = "#";
$hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
for ($i = 0; $i < 3; $i++) {
$r = null;
$c = $match[$i];
$hexAr = array();
while ($c > 16) {
$r = $c % 16;
$c = ($c / 16) >> 0;
array_push($hexAr, $hex[$r]);
}
array_push($hexAr, $hex[$c]);
$ret = array_reverse($hexAr);
$item = implode('', $ret);
$item = str_pad($item, 2, '0', STR_PAD_LEFT);
$hexColor .= $item;
}
return $hexColor;
}
/**
* 获取预售活动列表
*/
public function getAdvanceList()
{
$data = I('post.');
$page = $data['page'] ? $data['page'] : 1;
$limit = $data['limit'] ? $data['limit'] : 10;
$keyword = $data['keyword'];
$advance_model = new AdvanceGoodsModel();
$ImageGalleryModel = new ImageGalleryModel();
$where = array(
'a.advance_end_time' => array('gt', $this->thisTime),
'a.store_id' => $this->store_id,
'a.advance_state' => 1,
'g.status' => 0,
'g.online_state' => 1,
);
if (!empty($keyword)) {
$where['g.goods_name'] = array('like', "%{$keyword}%");
}
$parameter = array(
'where' => $where,
'page' => $page,
'limit' => $limit,
'field' => array(
'a.id',
'a.goods_id',
'a.attribute_state',
'a.surplus_num',
'a.advance_start_time',
'a.advance_end_time',
'g.goods_name',
'g.goods_img_id',
)
);
$info = $advance_model->getList($parameter);
if ($info['status'] == 1) {
$count = $advance_model->getListCount(array('where' => $where));
$list = $info['data']['list'];
foreach ($list as $key => &$val) {
if (C('IS_SET_REDIS')) {
$frozen_param = array(
'id' => $val['id'],
'goods_id' => $val['goods_id'],
'sku_id' => '',
'attribute_state' => $val['attribute_state'],
);
$frozen_num = $advance_model->frozen_num($frozen_param);
$val['surplus_num'] = $val['surplus_num'] - $frozen_num;
}
$val['surplus_num'] = round($val['surplus_num'], 3);
$val['goods_img'] = $ImageGalleryModel->getImagePath($val['goods_img_id']);
$val['goods_img'] = $val['goods_img'] ? $val['goods_img'] : '';
$val['advance_start_time'] = date('Y-m-d H:i:s', $val['advance_start_time']);
$val['advance_end_time'] = date('Y-m-d H:i:s', $val['advance_end_time']);
}
}
$res = array(
'status' => 1,
'msg' => $list ? "获取成功" : "暂无数据",
'data' => $list,
'count' => $count ? intval($count) : 0
);
$this->ajaxReturn($res);
}
/**
* 商户DIY页面
*/
public function mer_index()
{
$this->display('index');
}
//保存商户DIY设置
public function merchantDiySave()
{
$merchantId = $this->merchant_id;//商户id
if (!$merchantId) {
$res = callBack(statusCode()['error'], '失败');
$this->ajaxReturn($res);
}
$post = I('post.');
$param = [
'diy_data'
];
//参数验证
$plugVip = new CustomValidate();
$dataOne = $plugVip->validationParameters($param, $post);
if ($dataOne !== true) {
$this->ajaxReturn($dataOne);
}
$diy = $post['diy_data'];
if ($diy == '') {
$res = callBack(statusCode()['error'], '参数不能为空');
$this->ajaxReturn($res);
}
if (isset($diy{60000})) {
$res = callBack(statusCode()['error'], '插件使用过多');
$this->ajaxReturn($res);
}
$merchantConfigModel = new MerchantConfigModel();
$data = $merchantConfigModel->diySave($merchantId, $diy);
if ($data === true) {
$res = callBack(statusCode()['success'], '成功');
} else {
$res = callBack(statusCode()['error'], '失败');
}
$this->ajaxReturn($res);
}
//获取商户商品列表
public function mGoodsList()
{
$merchantId = $this->merchant_id;//商户id
$keyword = I('post.keyword');
$page = I('post.page') ? I('post.page') : 1;
$limit = I('post.limit') ? I('post.limit') : 10;
$ids = I('post.ids');
$map = array(
'merchantid' => $merchantId,//商户id
'status' => 0,//状态:0,正常,1,下架,2,删除。
'issellonline' => 1,//是否在商城中出售:0,不在,1,在商城出售。
'online_price' => array('gt', 0),//线上价
);
if ($keyword) {
$map['goods_name|goods_code|good_number'] = array('like', '%' . $keyword . '%');
}
if($ids){
$map['id'] = array('not in', $ids);//商品ids
}
$fields = array(
'id',
'goods_name',//零售货物名称。
'goods_code',//商品条码。
'good_number',//货号
'goods_image_id',//商品图片id
'online_price',//线上价
'addtime',//添加时间
);
$order = 'id desc';
$GoodsModel = new RetailMgoodsModel();
$goods = $GoodsModel->getSelect($map, $fields, $order, "{$page},{$limit}");//获取商品列表
$count = $GoodsModel->getCount($map);//获取商品数量
$ImageGalleryModel = new ImageGalleryModel();
foreach ($goods as &$value) {
$value['add_time'] = date('Y-m-d H:i:s', $value['addtime']);
$value['goods_code'] = $value['goods_code'] ? $value['goods_code'] : '---';
$value['goods_number'] = $value['good_number'] ? $value['good_number'] : '---';
$value['goods_img'] = $ImageGalleryModel->getImagePath($value['goods_image_id']);
$value['goods_img'] = $value['goods_img'] ? $value['goods_img'] : '';
unset($value['goods_image_id'],$value['good_number'],$value['addtime']);
}
$res['status'] = 1;
$res['msg'] = empty($goods) ? '暂无数据' : '获取成功';
$res['count'] = $count;
$res['data'] = $goods;
$this->ajaxReturn($res);
}
}