1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
 
namespace app\operations\controller\plus\business;
use app\operations\controller\Controller;
use app\operations\model\plus\business\Business as BusinessModel;
use app\common\service\business\Poster;
use app\operations\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())->where('business_card_id',$param['business_card_id'])->find();
        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);
        }
        unset($param['sex']);
        // 更新数据
        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) {
            $Qrcode = new Poster($data, 'business');
            $data['mp'] = $Qrcode->getImage();
            return $this->renderSuccess('', $data);
        }
        return $this->renderError('名片不存在');
    }
    
    /**
     * 获取模板列表
     */
    public function getTemplateList() {
        $list = (new TemplateModel())->getList();
        return $this->renderSuccess('', compact('list'));
    }
}