Visit.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\crm\controller;
  3. use app\admin\controller\ApiCommon;
  4. use app\crm\model\NumberSequence;
  5. use app\crm\traits\AutoNumberTrait;
  6. use app\crm\logic\VisitLogic;
  7. use think\Hook;
  8. use think\Request;
  9. use app\admin\model\User;
  10. use think\Db;
  11. class Visit extends ApiCommon
  12. {
  13. use AutoNumberTrait;
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission' => [''],
  24. 'allow' => ['count']
  25. ];
  26. Hook::listen('check_auth', $action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. }
  33. public function visitUser(){
  34. $userInfo = $this->userInfo;
  35. $userModel=new User();
  36. $userInfo= $userModel->getUserById($userInfo['id']);
  37. return resultArray(['data' => $userInfo]);
  38. }
  39. /**
  40. * 回访列表
  41. */
  42. public function index()
  43. {
  44. $Visit = new VisitLogic;
  45. $param = $this->param;
  46. $userInfo = $this->userInfo;
  47. $param['user_id'] = $param['user_id']?:$userInfo['id'];
  48. $data = $Visit->getDataList($param);
  49. return resultArray(['data' => $data]);
  50. }
  51. /**
  52. * 创建回访单
  53. */
  54. public function save()
  55. {
  56. $Visit = new VisitLogic;
  57. $param = $this->param;
  58. $userInfo = $this->userInfo;
  59. # 设置回复编号
  60. $numberInfo = [];
  61. if (empty($param['number'])) {
  62. $numberInfo = $this->getAutoNumbers(3);
  63. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写回访编号!']);
  64. $param['number'] = $numberInfo['number'];
  65. }
  66. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  67. $param['create_user_id'] = $userInfo['id'];
  68. $param['update_time'] = time();
  69. $res = $Visit->createData($param);
  70. if ($res) {
  71. # 更新crm_number_sequence表中的last_date、create_time字段
  72. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  73. return resultArray(['data' => '添加成功']);
  74. } else {
  75. return resultArray(['error' => $Visit->getError()]);
  76. }
  77. }
  78. /**
  79. * 回访单详情
  80. */
  81. public function read()
  82. {
  83. $visit = new VisitLogic;
  84. $userModel = new \app\admin\model\User();
  85. $param = $this->param;
  86. $userInfo = $this->userInfo;
  87. $data = $visit->getDataById($param['id']);
  88. //判断权限
  89. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', 'read');
  90. //读权限
  91. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  92. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  93. if (!in_array($data['owner_user_id'], $auth_user_ids) && !$rwPre && !$roPre) {
  94. $authData['dataAuth'] = 0;
  95. return resultArray(['data' => $authData]);
  96. }
  97. if (!$data) {
  98. return resultArray(['error' => $visit->getError()]);
  99. }
  100. return resultArray(['data' => $data]);
  101. }
  102. /**
  103. * 编辑回访单
  104. */
  105. public function update()
  106. {
  107. $Visit = new VisitLogic;
  108. $userModel = new \app\admin\model\User();
  109. $param = $this->param;
  110. $userInfo = $this->userInfo;
  111. $param['user_id'] = $userInfo['id'];
  112. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  113. # 设置回访编号
  114. $numberInfo = [];
  115. if (empty($param['number'])) {
  116. $numberInfo = $this->getAutoNumbers(3);
  117. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写回访编号!']);
  118. $param['number'] = $numberInfo['number'];
  119. }
  120. //判断权限
  121. $data = $Visit->getDataById($param['id']);
  122. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', 'update');
  123. if ($Visit->updateDataById($param, $param['id'])) {
  124. # 更新crm_number_sequence表中的last_date、create_time字段
  125. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  126. return resultArray(['data' => '编辑成功']);
  127. } else {
  128. return resultArray(['error' => $Visit->getError()]);
  129. }
  130. }
  131. /**
  132. * 删除回访单
  133. */
  134. public function delete()
  135. {
  136. $Visit = new VisitLogic;
  137. $param = $this->param;
  138. $userInfo = $this->userInfo;
  139. if (!is_array($param['id'])) {
  140. $visit_id[] = $param['id'];
  141. } else {
  142. $visit_id = $param['id'];
  143. }
  144. $data = $Visit->del($visit_id);
  145. if ($data) {
  146. return resultArray(['error' => $data]);
  147. } else {
  148. return resultArray(['data' => '删除成功']);
  149. }
  150. }
  151. /**
  152. * 系统信息
  153. *
  154. */
  155. public function system(VisitLogic $visitLogic)
  156. {
  157. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  158. $data = $visitLogic->getSystemInfo($this->param['id']);
  159. return resultArray(['data' => $data]);
  160. }
  161. /**
  162. * table标签栏数量
  163. */
  164. public function count()
  165. {
  166. if (empty($this->param['visit_id'])) return resultArray(['error' => '参数错误!']);
  167. # 附件
  168. $fileCount = Db::name('crm_visit_file')->alias('visit')->join('__ADMIN_FILE__ file', 'file.file_id = visit.file_id', 'LEFT')->where('visit_id', $this->param['visit_id'])->count();
  169. return resultArray(['data' => ['fielCount' => $fileCount]]);
  170. }
  171. }