Sms.php
12.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace app\admin\controller\facrm\apps;
use app\common\controller\Backend;
use think\Db;
use think\Queue;
use think\Validate;
/**
* 短息模板
* @icon fa fa-tags
*/
class Sms extends Backend
{
protected $key = "sms_tpl_setting_lists";
protected $addon_config = array();
protected $noNeedRight = ['getfield', 'selectpage'];
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\facrm\Setting();
$this->modelList = ['customer' => __('客户'), 'contacts' => __('联系人'), 'clues' => __('线索')];
$this->view->assign('modelList', $this->modelList);
}
/**
* 查看列表
* @return string|\think\response\Json
* @throws \think\Exception
*/
public function index()
{
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
$this->request->request(['custom' => ['key' => $this->key]]);
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->where('key', $this->key)
->order($sort, $order)
->limit($offset, $limit)
->select();
$total = $this->model
->where($where)
->where('key', $this->key)
->order($sort, $order)
->count();
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$source = $this->request->param('source', 'customer');
$this->view->assign('source', $source);
return $this->view->fetch();
}
/**
* 添加
* @return mixed
*/
public function add()
{
if ($this->request->isPost()) {
$this->request->filter('xss_clean');
$params = $this->request->post("row/a");
if ($params['content']) {
$params['values']['content'] = $params['content'];
unset($params['content']);
}
if (empty($params['values'])) $this->error();
$this->request->post(['row' => array_merge($params, [
'values' => json_encode($params['values'])
])]);
}
$this->assign('template_data', json_encode($this->getfield("customer", false)));
$this->addon_config = get_addon_config('facrm');
$this->view->assign("recordTypeList", $this->addon_config['record_type']);
$this->view->assign('key', $this->key);
return parent::add();
}
/**
* 修改
* @return mixed
*/
public function edit($ids = null)
{
if ($this->request->isPost()) {
$this->request->filter('xss_clean');
$params = $this->request->post("row/a");
if ($params['content']) {
$params['values']['content'] = $params['content'];
unset($params['content']);
}
if (empty($params['values'])) $this->error();
$this->request->post(['row' => array_merge($params, [
'values' => json_encode($params['values'])
])]);
}
$this->assign('template_data', json_encode($this->getfield("customer", false)));
$this->view->assign('key', $this->key);
$this->addon_config = get_addon_config('facrm');
$this->view->assign("recordTypeList", $this->addon_config['record_type']);
return parent::edit($ids);
}
/**
* 发短息
*/
public function send()
{
$data = $this->request->param('data');
$data = json_decode($data, true);
$types_data = array();//发送对象数据,如客户,联系人
if ($data && $data['types']) {
$auth = new \addons\facrm\library\Auth();
//TODO 还可以精简,为了明了,暂时不做
switch ($data['types']) {
case "customer":
$custModel = new \app\admin\model\facrm\Customer();
$types_data = $custModel->find($data['typesid']);
if (!$auth->checkCustomerAuth($types_data, $this->auth)) {
$this->error(__('您没有权限'));
}
break;
case "business":
$custModel = new \app\admin\model\facrm\Customer();
$types_data = $custModel->find($data['customer_id']);
if (!$auth->checkCustomerAuth($types_data, $this->auth)) {
$this->error(__('您没有权限'));
}
break;
case "contacts":
$contactsModel = new \app\admin\model\facrm\customer\Contacts();
$types_data = $contactsModel->find($data['typesid']);
if (!$types_data) break;
if (!$auth->checkCustomerAuth($types_data['customer_id'], $this->auth)) {
$this->error(__('您没有权限'));
}
break;
case "clues":
$custModel = new \app\admin\model\facrm\Clues();
$types_data = $custModel->find($data['typesid']);
if (!$auth->checkCluesAuth($types_data, $this->auth)) {
$this->error(__('您没有权限'));
}
break;
default:
break;
}
}
if ($types_data){
$types_data = $types_data->toArray();
}
$sms_id = $this->request->param('sms_id', '', 'intval');
$config_row = array();//模板配置
if ($sms_id) {
$config_row = $this->model
->where('key', $this->key)->find($sms_id);
if (!$config_row) $this->error(__("短息模板不存在"));
$config_row['values'] = json_decode($config_row['values'], true);
$tpl = $config_row['values'];
$tpl['content'] = is_array($tpl['content']) ? $tpl['content'] : (array)json_decode($tpl['content'], true);
//封装msg
$temp_val = array();
foreach ($tpl['content'] as $item) {
if ($item['value']) {
$value = $item['key'] == 'diy_text' ? $item['def_val'] : '';
$value = (isset($types_data[$item['key']]) && $types_data[$item['key']] && !$value ? $types_data[$item['key']] : $item['def_val']);
$temp_val[$item['value']] = $value;
}
}
$config_row['tpl_content'] = __($tpl['tpl_content'], $temp_val);//处理之后的模板数据
}
if ($this->request->isPost()) {
$row = $this->request->post('row/a');
if ($row['mobile']) {
if (!Validate::is($row['mobile'], "/^1[3456789]{1}\d{9}$/")) {
$this->error(__('手机有误'));
}
if (!$config_row) $this->error(__('短息模板不存在'));
//短息队列
$data = array_merge($data, [
'mobile' => $row['mobile'],
'template' => $config_row['values']['tpl_id'],
]);
$data['msg'] = $temp_val;
Queue::push("addons\\facrm\\library\\queue\\SmsJob", $data);
//队列end
if (true) {
//param_data types类型如customer,contacts| typesid对应的ID
//send_data发送的内容
//types_data 当前对象的数据
$row['create_user_id'] = $this->auth->id;//用于标明是谁发送的
$row['content'] = $config_row['tpl_content'] ? $config_row['tpl_content'] : $row['content'];
$row['record_type']=isset($config_row['values']['record_type']) ? $config_row['values']['record_type'] : 0;//跟进类型
hook("facrm_send_sms_success", array('param_data' => $data, 'send_data' => $row, 'types_data' => $types_data));
$this->success();
}
} else {
$this->error(__('Invalid parameters'));
}
}
return $this->view->fetch('', ['row' => $config_row, 'types_data' => $types_data]);
}
/**
* 批量发送
* @param $data ['types'] //发送配置,types:customer,contacts
* @param $data ['typesid'] //对象集合ID,多个以逗号隔开
* @param $data ['content'] //短息预览
* @param $data ['create_user_id'] //发送人ID
* @param $data ['record_type'] //跟进类型ID
* @param $data ['sms_id'] //短息模板ID
* @return string
*/
public function sends()
{
$data = $this->request->param('data');
$data = json_decode($data, true);
if ($this->request->isPost()) {
$row = $this->request->post('row/a');
//TODO 未判断选择客户和联系人的权限 批量发送可以是运营人员组发送
//队列
$row['create_user_id'] = $this->auth->id;
$data = array_merge($data, $row);
Queue::push("addons\\facrm\\library\\queue\\SmssJob", $data);
$this->success();
}
$sms_id = $this->request->param('sms_id', '', 'intval');
$config_row = array();
if ($sms_id) {
$config_row = $this->model
->where('key', $this->key)->find($sms_id);
if (!$config_row) $this->error(__("短息模板不存在"));
$config_row['values'] = json_decode($config_row['values'], true);
switch ($data['types']) {
case "customer":
$custModel = new \app\admin\model\facrm\Customer();
$types_data = $custModel->where('id', 'in', $data['typesid'])->find();
$types_data = $types_data->toArray();
break;
case "contacts":
$contactsModel = new \app\admin\model\facrm\customer\Contacts();
$types_data = $contactsModel->where('id', 'in', $data['typesid'])->find();
if (!$types_data) break;
break;
case "clues":
$custModel = new \app\admin\model\facrm\Clues();
$types_data = $custModel->where('id', 'in', $data['typesid'])->find($data['typesid']);
$types_data = $types_data->toArray();
break;
default:
break;
}
$tpl = $config_row['values'];
$tpl['content'] = is_array($tpl['content']) ? $tpl['content'] : (array)json_decode($tpl['content'], true);
//封装msg
$temp_val = array();
foreach ($tpl['content'] as $item) {
if ($item['value']) {
$value = $item['key'] == 'diy_text' ? $item['def_val'] : '';
$value = (isset($types_data[$item['key']]) && $types_data[$item['key']] && !$value ? $types_data[$item['key']] : $item['def_val']);
$temp_val[$item['value']] = $value;
}
}
$config_row['tpl_content'] = __($tpl['tpl_content'], $temp_val);//处理之后的模板数据
}
return $this->view->fetch('', ['row' => $config_row, 'types_data' => []]);
}
/**
* 获取参数字段
* @param string $types types:customer,contacts
* @internal
*/
public function getfield($types = "customer", $return_ajax = true)
{
$fieldlist=\addons\facrm\library\Helper::getfield($types,'*',['diy_text'=>__("自定义文本")]);
if ($return_ajax) {
$this->success("", null, $fieldlist);
}
return $fieldlist;
}
/**
* 选择模板
* @Internal
* @return \think\response\Json
*/
public function selectpage()
{
$this->request->request(['custom' => ['key' => $this->key,'status'=>1]]);
return parent::selectpage(); // TODO: Change the autogenerated stub
}
}