Activity.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * 活动控制器
  4. *
  5. * @author qifan
  6. * @date 2020-12-09
  7. */
  8. namespace app\crm\controller;
  9. use app\admin\controller\ApiCommon;
  10. use app\crm\logic\ActivityLogic;
  11. use think\Hook;
  12. use think\Request;
  13. class Activity extends ApiCommon
  14. {
  15. /**
  16. * 用于判断权限
  17. * @permission 无限制
  18. * @allow 登录用户可访问
  19. * @other 其他根据系统设置
  20. **/
  21. public function _initialize()
  22. {
  23. $action = [
  24. 'permission' => [],
  25. 'allow' => ['index', 'save', 'read', 'update', 'delete', 'getphrase', 'setphrase', 'getrecordauth', 'excelimport', 'excelexport', 'exceldownload']
  26. ];
  27. Hook::listen('check_auth', $action);
  28. $request = Request::instance();
  29. $a = strtolower($request->action());
  30. if (!in_array($a, $action['permission'])) {
  31. parent::_initialize();
  32. }
  33. }
  34. /**
  35. * 活动列表
  36. *
  37. * @param ActivityLogic $activityLogic
  38. * @return \think\response\Json
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. */
  43. public function index(ActivityLogic $activityLogic)
  44. {
  45. $param = $this->param;
  46. $param['user_id'] = $this->userInfo['id'];
  47. $data = $activityLogic->index($param);
  48. return resultArray(['data' => $data]);
  49. }
  50. /**
  51. * 创建活动【跟进记录】
  52. *
  53. * @param ActivityLogic $activityLogic
  54. * @return \think\response\Json
  55. * @throws \think\Exception
  56. * @throws \think\exception\PDOException
  57. */
  58. public function save(ActivityLogic $activityLogic)
  59. {
  60. if (!checkPerByAction('crm', 'activity', 'save')) {
  61. return resultArray(['error' => '你没有创建跟进记录的权限!']);
  62. }
  63. if (empty($this->param['activity_type'])) return resultArray(['error' => '缺少模块类型!']);
  64. if (empty($this->param['activity_type_id'])) return resultArray(['error' => '缺少活动类型ID!']);
  65. if (empty($this->param['content'])) return resultArray(['error' => '请填写跟进内容!']);
  66. if (!empty($this->param['next_time']) && strtotime($this->param['next_time']) < time()) {
  67. return resultArray(['error' => '下次联系时间不能在当前时间之前!']);
  68. }
  69. $param = $this->param;
  70. $param['user_id'] = $this->userInfo['id'];
  71. if (!$activityLogic->save($param)) return resultArray(['error' => '操作失败!']);
  72. return resultArray(['data' => '操作成功!']);
  73. }
  74. /**
  75. * 活动详情
  76. *
  77. * @param ActivityLogic $activityLogic
  78. * @return \think\response\Json
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. * @throws \think\exception\DbException
  82. */
  83. public function read(ActivityLogic $activityLogic)
  84. {
  85. if (!checkPerByAction('crm', 'activity', 'read')) {
  86. return resultArray(['error' => '你没有查看跟进记录的权限!']);
  87. }
  88. if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
  89. $data = $activityLogic->read($this->param['activity_id']);
  90. return resultArray(['data' => $data]);
  91. }
  92. /**
  93. * 编辑活动【跟进记录】
  94. *
  95. * @param ActivityLogic $activityLogic
  96. * @return \think\response\Json
  97. * @throws \think\Exception
  98. * @throws \think\exception\PDOException
  99. */
  100. public function update(ActivityLogic $activityLogic)
  101. {
  102. if (!checkPerByAction('crm', 'activity', 'update')) {
  103. return resultArray(['error' => '你没有编辑跟进记录的权限!']);
  104. }
  105. if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
  106. if (empty($this->param['activity_type'])) return resultArray(['error' => '缺少活动类型!']);
  107. if (empty($this->param['activity_type_id'])) return resultArray(['error' => '缺少活动类型ID!']);
  108. if (empty($this->param['content'])) return resultArray(['error' => '请填写跟进内容!']);
  109. $param = $this->param;
  110. $userId = $this->userInfo['id'];
  111. if (!$activityLogic->update($param)) return resultArray(['error' => '操作失败!']);
  112. $data = $activityLogic->getFollowData($param['activity_id'], $userId);
  113. return resultArray(['data' => $data]);
  114. }
  115. /**
  116. * 删除活动【跟进记录】
  117. *
  118. * @param ActivityLogic $activityLogic
  119. * @return \think\response\Json
  120. */
  121. public function delete(ActivityLogic $activityLogic)
  122. {
  123. $userInfo = $this->userInfo;
  124. if (!checkPerByAction('crm', 'activity', 'delete')) {
  125. return resultArray(['error' => '你没有删除跟进记录的权限!']);
  126. }
  127. if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
  128. if (!$activityLogic->delete($this->param['activity_id'], $userInfo['id'])) return resultArray(['error' => '操作失败!']);
  129. return resultArray(['data' => '操作成功!']);
  130. }
  131. /**
  132. * 获取常用语
  133. *
  134. * @param ActivityLogic $activityLogic
  135. * @return \think\response\Json
  136. */
  137. public function getPhrase(ActivityLogic $activityLogic)
  138. {
  139. $data = $activityLogic->getPhrase();
  140. return resultArray(['data' => $data]);
  141. }
  142. /**
  143. * 设置常用语
  144. *
  145. * @param ActivityLogic $activityLogic
  146. * @return \think\response\Json
  147. * @throws \think\Exception
  148. * @throws \think\exception\PDOException
  149. */
  150. public function setPhrase(ActivityLogic $activityLogic)
  151. {
  152. if (empty($this->param['phrase'])) return resultArray(['error' => '缺少常用语数据!']);
  153. if (!is_array($this->param['phrase'])) return resultArray(['error' => '参数格式错误!']);
  154. if (!$activityLogic->setPhrase($this->param['phrase'])) return resultArray(['error' => '操作失败!']);
  155. return resultArray(['data' => '操作成功!']);
  156. }
  157. /**
  158. * 跟进记录权限
  159. *
  160. * @return \think\response\Json
  161. */
  162. public function getRecordAuth()
  163. {
  164. $data = [
  165. 'index' => checkPerByAction('crm', 'activity', 'index'),
  166. 'read' => checkPerByAction('crm', 'activity', 'read'),
  167. 'save' => checkPerByAction('crm', 'activity', 'save'),
  168. 'update' => checkPerByAction('crm', 'activity', 'update'),
  169. 'delete' => checkPerByAction('crm', 'activity', 'delete'),
  170. ];
  171. return resultArray(['data' => $data]);
  172. }
  173. /**
  174. * 导入模板下载
  175. * @author alvin guogaobo
  176. * @version 1.0 版本号
  177. * @since 2021/4/10 0010 16:01
  178. */
  179. public function excelDownload($save_path = '')
  180. {
  181. $param = $this->param;
  182. $excelModel = new \app\admin\model\Excel();
  183. $field_list = $this->importData($param);
  184. $types = 'crm_activity';
  185. $excelModel->excelImportDownload($field_list, $types, $save_path);
  186. }
  187. /**
  188. * 导入导出模板标题
  189. * @param $param
  190. *
  191. * @author alvin guogaobo
  192. * @version 1.0 版本号
  193. * @since 2021/4/13 0013 11:15
  194. */
  195. public function importData($param)
  196. {
  197. $field_list = [];
  198. switch ($param['crmType']) {
  199. case 1 :
  200. $field = [
  201. '2' => [
  202. 'name' => '所属线索',
  203. 'field' => 'activity_type_id',
  204. 'form_type' => 'leads_id',
  205. 'is_null' => 1,
  206. ]
  207. ];
  208. $name = '所属线索';
  209. break;
  210. case 3:
  211. $field = [
  212. '2' => [
  213. 'name' => '所属联系人',
  214. 'field' => 'activity_type_id',
  215. 'form_type' => 'contacts_id',
  216. 'is_null' => 1,
  217. ],
  218. ];
  219. $name = '所属联系人';
  220. break;
  221. case 5:
  222. $field = [
  223. '2' => [
  224. 'name' => '所属商机',
  225. 'field' => 'activity_type_id',
  226. 'form_type' => 'business_id',
  227. 'is_null' => 1,
  228. ],
  229. ];
  230. $name = '所属商机';
  231. break;
  232. case 6:
  233. $field = [
  234. '2' => [
  235. 'name' => '所属合同',
  236. 'field' => 'activity_type_id',
  237. 'form_type' => 'contract_id',
  238. 'is_null' => 1,
  239. ],
  240. ];
  241. $name = '所属合同';
  242. break;
  243. case 2:
  244. $field = [
  245. '2' => [
  246. 'name' => '所属客户',
  247. 'field' => 'activity_type_id',
  248. 'form_type' => 'customer_id',
  249. 'is_null' => 1,
  250. ],
  251. ];
  252. $name = '所属客户';
  253. break;
  254. }
  255. $fields = [
  256. '0' => [
  257. 'name' => '跟进内容',
  258. 'field' => 'content',
  259. 'form_type' => 'text',
  260. 'is_null' => 1,
  261. ],
  262. '1' => [
  263. 'name' => '创建人',
  264. 'field' => 'create_user_id',
  265. 'form_type' => 'user',
  266. 'is_null' => 1,
  267. ],
  268. '2' => [
  269. 'name' => '跟进时间-例:2020-2-1',
  270. 'field' => 'next_time',
  271. 'form_type' => 'datetime',
  272. ],
  273. '3' => [
  274. 'name' => '跟进方式',
  275. 'field' => 'category',
  276. 'form_type' => 'select',
  277. 'setting' =>
  278. array
  279. (
  280. 0 => "见面拜访",
  281. 1 => "电话",
  282. 2 => "短信",
  283. 3 => "邮件",
  284. 4 => "微信"
  285. )
  286. ],
  287. ];
  288. // 导入的字段列表
  289. if ($param['is_excel']) {
  290. $field_lists = [
  291. '0' => ['name' => $name, 'field' => 'activity_type_name'],
  292. '1' => ['name' => '跟进内容', 'field' => 'content'],
  293. '2' => ['name' => '创建人', 'field' => 'create_user_name'],
  294. '3' => ['name' => '跟进时间', 'field' => 'create_time'],
  295. '4' => ['name' => '跟进方式', 'field' => 'category'],
  296. '5' => ['name' => '下次联系时间', 'field' => 'next_time']
  297. ];
  298. if ($param['crmType'] == 2) {
  299. $fields = [
  300. '6' => ['name' => '相关联系人', 'field' => 'contacts_ids'],
  301. '7' => ['name' => '相关商机', 'field' => 'business_ids'],];
  302. $field_list = array_merge($field_lists, $fields);
  303. } else {
  304. $field_list =$field_lists;
  305. }
  306. } else {
  307. $first_array = array_splice($fields, 2, 0, $field);
  308. $field_list = $fields;
  309. if ($param['crmType'] == 2) {
  310. $field = [
  311. '5' => [
  312. 'name' => '相关联系人',
  313. 'field' => 'contacts_ids',
  314. 'form_type' => 'contacts_id',
  315. ],
  316. '6' => [
  317. 'name' => '相关商机',
  318. 'field' => 'business_ids',
  319. 'form_type' => 'business_id',
  320. ]
  321. ];
  322. $field_list = array_merge($field_list, $field);
  323. }
  324. }
  325. return $field_list;
  326. }
  327. /**
  328. * 导入数据
  329. *
  330. * @author alvin guogaobo
  331. * @version 1.0 版本号
  332. * @since 2021/4/10 0010 16:27
  333. */
  334. public function excelImport()
  335. {
  336. $param = $this->param;
  337. $userInfo = $this->userInfo;
  338. $param['user_id'] = $userInfo['id'];
  339. $field_list = $this->importData($param);
  340. $excelModel = new \app\admin\model\Excel();
  341. $file = request()->file('file');
  342. switch ($param['crmType']) {
  343. case 1 :
  344. $param['types'] = 'crm_leads';
  345. $param['activity_type'] = 1;
  346. break;
  347. case 3:
  348. $param['types'] = 'crm_contacts';
  349. $param['activity_type'] = 3;
  350. break;
  351. case 5:
  352. $param['types'] = 'crm_business';
  353. $param['activity_type'] = 5;
  354. break;
  355. case 6:
  356. $param['types'] = 'crm_contract';
  357. $param['activity_type'] = 6;
  358. break;
  359. case 2:
  360. $param['types'] = 'crm_customer';
  361. $param['activity_type'] = 2;
  362. break;
  363. }
  364. $res = $excelModel->ActivityImport($file, $field_list, $param, $this);
  365. if (!$res) {
  366. return resultArray(['error' => $excelModel->getError()]);
  367. }
  368. return resultArray(['data' => $excelModel->getError()]);
  369. }
  370. /**
  371. * 导出跟进记录
  372. * action 列表分辨是否导出
  373. * label 导出类型 合同 客户 联系人
  374. *
  375. * @author alvin guogaobo
  376. * @version 1.0 版本号
  377. * @since 2021/4/13 0013 11:32
  378. */
  379. public function excelExport()
  380. {
  381. $activityLogic = new ActivityLogic();
  382. $indexLogic = new \app\crm\logic\IndexLogic();
  383. $userInfo = $this->userInfo;
  384. $param = $this->param;
  385. $param['action'] = 'crm_activity';
  386. $param['is_excel'] = 1;
  387. $param['id'] = $userInfo['id'];
  388. $list = $indexLogic->activityList($param);
  389. $field_list = $this->importData($param);
  390. $data = $activityLogic->excelExport($field_list, $list);
  391. return $data;
  392. }
  393. }