Activity.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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']
  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. }