<?php
|
|
namespace app\shop\controller\plus\business;
|
use app\shop\controller\Controller;
|
use app\shop\model\plus\business\Business as BusinessModel;
|
use app\common\service\business\Poster;
|
use app\shop\model\plus\business\Template as TemplateModel;
|
use app\common\model\settings\Region as RegionModel;
|
|
/**
|
* 名片管理
|
*/
|
class Business extends Controller
|
{
|
public function index(){
|
$list=(new BusinessModel)->getList(request()->request());
|
|
// 获取所有地区
|
$regionData = RegionModel::getCacheTree();
|
$data = [
|
'list' => $list,
|
'regionData' => $regionData
|
];
|
return $this->renderSuccess('', $data);
|
}
|
|
/**
|
* 添加名片
|
*/
|
public function add(){
|
$param = request()->param();
|
$param = $param['business'];
|
|
// 处理数组字段
|
$this->processArrayFields($param);
|
|
// 设置默认值
|
$param['app_id'] = (new BusinessModel())::$app_id;
|
|
// 如果该用户还没有默认名片,则设置为默认
|
if (!empty($param['user_id']) && !(new BusinessModel())->where(['user_id' => $param['user_id'], 'is_default' => 1])->find()) {
|
$param['is_default'] = 1;
|
}
|
$param['position']=$param['unit'];
|
// 如果没有指定模板,则使用默认模板
|
if (empty($param['template_id'])) {
|
$defaultTemplate = (new TemplateModel())->where(['is_default' => 1])->find();
|
$param['template_id'] = $defaultTemplate ? $defaultTemplate['template_id'] : 0;
|
}
|
|
$model = new BusinessModel();
|
if ($model->save($param)) {
|
return $this->renderSuccess('添加成功');
|
}
|
return $this->renderError('添加失败');
|
}
|
|
/**
|
* 编辑名片
|
*/
|
public function edit(){
|
$param = request()->param();
|
$param = $param['business'];
|
|
// 处理数组字段
|
$this->processArrayFields($param);
|
|
// 查找要编辑的名片
|
$model = (new BusinessModel())->get($param['business_card_id']);
|
if (!$model) {
|
return $this->renderError('名片不存在');
|
}
|
$param['position']=$param['unit'];
|
// 删除旧的海报图片
|
$Qrcode = new Poster($model->toArray());
|
$image = $Qrcode->getPosterPath('business');
|
if (file_exists($image)) {
|
unlink($image);
|
}
|
$image = $Qrcode->getPosterPath('desensitization');
|
if (file_exists($image)) {
|
unlink($image);
|
}
|
|
// 更新数据
|
if ($model->save($param)) {
|
return $this->renderSuccess('编辑成功');
|
}
|
return $this->renderError('编辑失败');
|
}
|
|
/**
|
* 处理数组字段
|
*/
|
private function processArrayFields(&$param) {
|
// 处理单位字段
|
if (isset($param['unit'])) {
|
if (is_array($param['unit'])) {
|
$param['unit'] = json_encode($param['unit'], JSON_UNESCAPED_UNICODE);
|
} else if (is_string($param['unit']) && !empty($param['unit'])) {
|
// 如果是字符串,按逗号分割成数组
|
$units = explode(',', $param['unit']);
|
$units = array_map('trim', $units);
|
$param['unit'] = json_encode($units, JSON_UNESCAPED_UNICODE);
|
} else {
|
$param['unit'] = json_encode([], JSON_UNESCAPED_UNICODE);
|
}
|
}
|
|
// 处理职务字段
|
if (isset($param['duties'])) {
|
if (is_array($param['duties'])) {
|
$param['duties'] = json_encode($param['duties'], JSON_UNESCAPED_UNICODE);
|
} else if (is_string($param['duties']) && !empty($param['duties'])) {
|
// 如果是字符串,按逗号分割成数组
|
$duties = explode(',', $param['duties']);
|
$duties = array_map('trim', $duties);
|
$param['duties'] = json_encode($duties, JSON_UNESCAPED_UNICODE);
|
} else {
|
$param['duties'] = json_encode([], JSON_UNESCAPED_UNICODE);
|
}
|
}
|
|
// 处理地址字段
|
if (isset($param['address'])) {
|
if (is_array($param['address'])) {
|
$param['address'] = json_encode($param['address'], JSON_UNESCAPED_UNICODE);
|
} else if (is_string($param['address']) && !empty($param['address'])) {
|
// 如果是字符串,按逗号分割成数组
|
$addresses = explode(',', $param['address']);
|
$addresses = array_map('trim', $addresses);
|
$param['address'] = json_encode($addresses, JSON_UNESCAPED_UNICODE);
|
} else {
|
$param['address'] = json_encode([], JSON_UNESCAPED_UNICODE);
|
}
|
}
|
|
// 处理职位字段
|
if (isset($param['position'])) {
|
if (is_array($param['position'])) {
|
$param['position'] = json_encode($param['position'], JSON_UNESCAPED_UNICODE);
|
} else if (is_string($param['position']) && !empty($param['position'])) {
|
// 如果是字符串,按逗号分割成数组
|
$positions = explode(',', $param['position']);
|
$positions = array_map('trim', $positions);
|
$param['position'] = json_encode($positions, JSON_UNESCAPED_UNICODE);
|
} else {
|
// 默认使用职务信息作为职位信息
|
$param['position'] = $param['duties'] ?? json_encode([], JSON_UNESCAPED_UNICODE);
|
}
|
}
|
}
|
|
public function delete(){
|
$param=request()->param();
|
if((new BusinessModel())->where('business_card_id',$param['business_card_id'])->delete()){
|
return $this->renderSuccess('删除成功');
|
}
|
return $this->renderError('删除失败');
|
}
|
|
/**
|
* 获取名片详情
|
*/
|
public function detail($business_card_id) {
|
$data = BusinessModel::detail($business_card_id);
|
if ($data) {
|
return $this->renderSuccess('', $data);
|
}
|
return $this->renderError('名片不存在');
|
}
|
|
/**
|
* 获取模板列表
|
*/
|
public function getTemplateList() {
|
$list = (new TemplateModel())->getList();
|
return $this->renderSuccess('', compact('list'));
|
}
|
}
|