Activity.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. if (!checkPerByAction('crm', 'activity', 'delete')) {
  124. return resultArray(['error' => '你没有删除跟进记录的权限!']);
  125. }
  126. if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
  127. if (!$activityLogic->delete($this->param['activity_id'])) return resultArray(['error' => '操作失败!']);
  128. return resultArray(['data' => '操作成功!']);
  129. }
  130. /**
  131. * 获取常用语
  132. *
  133. * @param ActivityLogic $activityLogic
  134. * @return \think\response\Json
  135. */
  136. public function getPhrase(ActivityLogic $activityLogic)
  137. {
  138. $data = $activityLogic->getPhrase();
  139. return resultArray(['data' => $data]);
  140. }
  141. /**
  142. * 设置常用语
  143. *
  144. * @param ActivityLogic $activityLogic
  145. * @return \think\response\Json
  146. * @throws \think\Exception
  147. * @throws \think\exception\PDOException
  148. */
  149. public function setPhrase(ActivityLogic $activityLogic)
  150. {
  151. if (empty($this->param['phrase'])) return resultArray(['error' => '缺少常用语数据!']);
  152. if (!is_array($this->param['phrase'])) return resultArray(['error' => '参数格式错误!']);
  153. if (!$activityLogic->setPhrase($this->param['phrase'])) return resultArray(['error' => '操作失败!']);
  154. return resultArray(['data' => '操作成功!']);
  155. }
  156. /**
  157. * 跟进记录权限
  158. *
  159. * @return \think\response\Json
  160. */
  161. public function getRecordAuth()
  162. {
  163. $data = [
  164. 'index' => checkPerByAction('crm', 'activity', 'index'),
  165. 'read' => checkPerByAction('crm', 'activity', 'read'),
  166. 'save' => checkPerByAction('crm', 'activity', 'save'),
  167. 'update' => checkPerByAction('crm', 'activity', 'update'),
  168. 'delete' => checkPerByAction('crm', 'activity', 'delete'),
  169. ];
  170. return resultArray(['data' => $data]);
  171. }
  172. /**
  173. * 导入模板下载
  174. * @author alvin guogaobo
  175. * @version 1.0 版本号
  176. * @since 2021/4/10 0010 16:01
  177. */
  178. public function excelDownload($save_path = ''){
  179. $param = $this->param;
  180. $excelModel = new \app\admin\model\Excel();
  181. $field_list=$this->importData($param);
  182. $types='crm_activity';
  183. $excelModel->importDown($field_list,$types,$save_path);
  184. }
  185. /**
  186. * 导入导出模板标题
  187. * @param $param
  188. *
  189. * @author alvin guogaobo
  190. * @version 1.0 版本号
  191. * @since 2021/4/13 0013 11:15
  192. */
  193. public function importData($param){
  194. switch ($param['label']){
  195. case 1 :
  196. $field = [
  197. '2' => [
  198. 'name' => '所属线索',
  199. 'field' => 'activity_type_id',
  200. 'types' => 'log',
  201. 'form_type' => 'datetime',
  202. 'is_null' => 1,
  203. ]
  204. ];
  205. break;
  206. case 3:
  207. $field = [
  208. '2' => [
  209. 'name' => '所属联系人',
  210. 'field' => 'activity_type_id',
  211. 'types' => 'log',
  212. 'form_type' => 'datetime',
  213. 'is_null' => 1,
  214. ],
  215. ];
  216. break;
  217. case 5:
  218. $field = [
  219. '2' => [
  220. 'name' => '所属商机',
  221. 'field' => 'activity_type_id',
  222. 'types' => 'log',
  223. 'form_type' => 'text',
  224. 'is_null' => 1,
  225. ]
  226. ];
  227. break;
  228. case 6:
  229. $field = [
  230. '2' => [
  231. 'name' => '所属合同',
  232. 'field' => 'activity_type_id',
  233. 'types' => 'log',
  234. 'form_type' => 'text',
  235. 'is_null' => 1,
  236. ],
  237. ];
  238. break;
  239. case 2:
  240. $field_list = [
  241. '0' => [
  242. 'name' => '跟进内容',
  243. 'field' => 'content',
  244. 'types' => 'log',
  245. 'form_type' => 'text',
  246. 'is_null' => 1,
  247. ],
  248. '1' => [
  249. 'name' => '创建人',
  250. 'field' => 'create_user_id',
  251. 'types' => 'log',
  252. 'form_type' => 'user',
  253. 'is_null' => 1,
  254. ],
  255. '2' => [
  256. 'name' => '所属客户',
  257. 'field' => 'activity_type_id',
  258. 'types' => 'log',
  259. 'form_type' => 'text',
  260. 'is_null' => 1,
  261. ],
  262. '3' => [
  263. 'name' => '跟进时间-例:2020-2-1',
  264. 'field' => 'next_time',
  265. 'types' => 'log',
  266. 'form_type' => 'datetime',
  267. ],
  268. '4' => [
  269. 'name' => '跟进方式',
  270. 'field' => 'category',
  271. 'types' => 'log',
  272. 'form_type' => 'text',
  273. ],
  274. '5' => [
  275. 'name' => '相关联系人',
  276. 'field' => 'contacts_ids',
  277. 'types' => 'log',
  278. 'form_type' => 'text',
  279. ],
  280. '6' => [
  281. 'name' => '相关商机',
  282. 'field' => 'business_ids',
  283. 'types' => 'log',
  284. 'form_type' => 'text',
  285. ]
  286. ];
  287. break;
  288. }
  289. $fields = [
  290. '0' => [
  291. 'name' => '跟进内容',
  292. 'field' => 'content',
  293. 'types' => 'log',
  294. 'form_type' => 'text',
  295. 'is_null' => 1,
  296. ],
  297. '1' => [
  298. 'name' => '创建人',
  299. 'field' => 'create_user_id',
  300. 'types' => 'log',
  301. 'form_type' => 'user',
  302. 'is_null' => 1,
  303. ],
  304. '2' => [
  305. 'name' => '所属111',
  306. 'field' => 'activity_type_id',
  307. 'types' => 'log',
  308. 'form_type' => 'text',
  309. 'is_null' => 1,
  310. ],
  311. '3' => [
  312. 'name' => '跟进时间-例:2020-2-1',
  313. 'field' => 'next_time',
  314. 'types' => 'log',
  315. 'form_type' => 'datetime',
  316. ],
  317. '4' => [
  318. 'name' => '跟进方式',
  319. 'field' => 'category',
  320. 'types' => 'log',
  321. 'form_type' => 'text',
  322. ],
  323. ];
  324. // 导入的字段列表
  325. if(!empty($param['down'])){
  326. $field_list = [
  327. '0' => ['name' => '所属客户', 'field' => 'activity_type_name'],
  328. '1' => ['name' => '跟进内容', 'field' => 'content'],
  329. '2' => ['name' => '创建人', 'field' => 'create_user_name'],
  330. '3' => ['name' => '跟进时间', 'field' => 'create_time'],
  331. '4' => ['name' => '跟进方式','field' => 'category'],
  332. '5' => ['name' => '下次联系时间', 'field' => 'next_time'],
  333. '6' => ['name' => '相关联系人', 'field' => 'contacts_ids'],
  334. '7' => ['name' => '相关商机', 'field' => 'business_ids'],
  335. ];
  336. }else{
  337. if(empty($field_list)){
  338. $field_list=array_merge($fields,$field);
  339. }
  340. }
  341. return $field_list;
  342. }
  343. /**
  344. * 导入数据
  345. *
  346. * @author alvin guogaobo
  347. * @version 1.0 版本号
  348. * @since 2021/4/10 0010 16:27
  349. */
  350. public function excelImport(){
  351. $param = $this->param;
  352. $field_list=$this->importData($param['label']);
  353. $excelModel = new \app\admin\model\Excel();
  354. $file = request()->file('file');
  355. switch ($param['label']){
  356. case 1 :
  357. $param['types']='crm_leads';
  358. $param['activity_type']=1;
  359. break;
  360. case 3:
  361. $param['types']='crm_contacts';
  362. $param['activity_type']=3;
  363. break;
  364. case 5:
  365. $param['types']='crm_business';
  366. $param['activity_type']=5;
  367. break;
  368. case 6:
  369. $param['types']='crm_contract';
  370. $param['activity_type']=6;
  371. break;
  372. case 2:
  373. $param['types']='crm_customer';
  374. $param['activity_type']=2;
  375. break;
  376. }
  377. $res = $excelModel->ActivityImport($file,$field_list, $param,$this);
  378. if (!$res) {
  379. return resultArray(['error' => $excelModel->getError()]);
  380. }
  381. return resultArray(['data' => $excelModel->getError()]);
  382. }
  383. /**
  384. * 导出跟进记录
  385. * action 列表分辨是否导出
  386. * label 导出类型 合同 客户 联系人
  387. *
  388. * @author alvin guogaobo
  389. * @version 1.0 版本号
  390. * @since 2021/4/13 0013 11:32
  391. */
  392. public function excelExport(){
  393. $activityLogic=new ActivityLogic();
  394. $indexLogic=new \app\crm\logic\IndexLogic();
  395. $param = $this->param;
  396. $param['action']='crm_activity';
  397. $list=$indexLogic->activityList($param);
  398. // $param['down']=1;
  399. $field_list=$this->importData($param);
  400. $data=$activityLogic->excelExport($field_list,$list);
  401. return $data;
  402. }
  403. }