123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 跟进记录
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use think\Hook;
  9. use think\Request;
  10. use think\Db;
  11. class Record extends ApiCommon
  12. {
  13. /**
  14. * 用于判断权限
  15. * @permission 无限制
  16. * @allow 登录用户可访问
  17. * @other 其他根据系统设置
  18. **/
  19. public function _initialize()
  20. {
  21. $action = [
  22. 'permission'=>[''],
  23. 'allow'=>['index','save','update','delete']
  24. ];
  25. Hook::listen('check_auth',$action);
  26. $request = Request::instance();
  27. $a = strtolower($request->action());
  28. if (!in_array($a, $action['permission'])) {
  29. parent::_initialize();
  30. }
  31. }
  32. /**
  33. * 跟进记录列表
  34. * @return
  35. */
  36. public function index()
  37. {
  38. $param = $this->param;
  39. $by = $param['by'] ? : '';
  40. unset($param['by']);
  41. $recordModel = model('Record');
  42. $data = $recordModel->getDataList($param, $by);
  43. if (!$data) {
  44. return resultArray(['error' => $recordModel->getError()]);
  45. }
  46. return resultArray(['data' => $data]);
  47. }
  48. /**
  49. * 跟进记录创建
  50. * @param
  51. * @return
  52. */
  53. public function save()
  54. {
  55. $recordModel = model('Record');
  56. $param = $this->param;
  57. $userInfo = $this->userInfo;
  58. $param['create_user_id'] = $userInfo['id'];
  59. $resData = $recordModel->createData($param);
  60. if (!$resData) {
  61. return resultArray(['error' => $recordModel->getError()]);
  62. }
  63. //同时创建日程
  64. if ($param['is_event']) {
  65. $eventModel = new \app\oa\model\Event();
  66. $data['title'] = trim($param['content']);
  67. $data['content'] = trim($param['content']);
  68. $data['start_time'] = $param['next_time'] ? : time();
  69. $data['end_time'] = $param['next_time']+86399;
  70. $data['create_user_id'] = $userInfo['id'];
  71. if ($param['types'] == 'crm_customer') $data['customer_ids'] = $param['types_id'];
  72. $data['business_ids'] = $param['business_ids'];
  73. $data['contacts_ids'] = $param['contacts_ids'];
  74. $resEvent = $eventModel->createData($data);
  75. }
  76. return resultArray(['data' => '添加成功']);
  77. }
  78. /**
  79. * 跟进记录编辑
  80. * @param
  81. * @return
  82. */
  83. public function update()
  84. {
  85. $recordModel = model('Record');
  86. $param = $this->param;
  87. $data = $recordModel->updateDataById($param, $param['id']);
  88. if (!$data) {
  89. return resultArray(['error' => $recordModel->getError()]);
  90. }
  91. return resultArray(['data' => '编辑成功']);
  92. }
  93. /**
  94. * 跟进记录删除
  95. * @param
  96. * @return
  97. */
  98. public function delete()
  99. {
  100. $recordModel = model('Record');
  101. $param = $this->param;
  102. $userInfo = $this->userInfo;
  103. //权限判断
  104. $dataInfo = $recordModel->getDataById($param['id']);
  105. if (!$dataInfo) {
  106. return resultArray(['error' => '数据不存在或已删除']);
  107. }
  108. //自己(24小时)或者管理员
  109. $adminTypes = adminGroupTypes($userInfo['id']);
  110. if(!in_array(1,$adminTypes)){
  111. if((time()-$dataInfo['create_time']) > 86400){
  112. return resultArray(['error' => '超过24小时,不能删除']);
  113. }
  114. if ($dataInfo['create_user_id'] !== $userInfo['id']){
  115. return resultArray(['error' => '无权操作']);
  116. }
  117. }
  118. $resData = $recordModel->delDataById($param['id']);
  119. if (!$resData) {
  120. return resultArray(['error' => $recordModel->getError()]);
  121. }
  122. return resultArray(['data' => '删除成功']);
  123. }
  124. }