Visit.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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['create_time'] = time();
  69. $param['update_time'] = time();
  70. $res = $Visit->createData($param);
  71. if ($res) {
  72. # 更新crm_number_sequence表中的last_date、create_time字段
  73. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  74. return resultArray(['data' => '添加成功']);
  75. } else {
  76. return resultArray(['error' => $Visit->getError()]);
  77. }
  78. }
  79. /**
  80. * 回访单详情
  81. */
  82. public function read()
  83. {
  84. $visit = new VisitLogic;
  85. $userModel = new \app\admin\model\User();
  86. $param = $this->param;
  87. $userInfo = $this->userInfo;
  88. $data = $visit->getDataById($param['id'], $userInfo['id']);
  89. //判断权限
  90. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', 'read');
  91. //读权限
  92. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  93. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  94. if (!in_array($data['owner_user_id'], $auth_user_ids) && !$rwPre && !$roPre) {
  95. $authData['dataAuth'] = (int)0;
  96. return resultArray(['data' => $authData]);
  97. }
  98. if (!$data) {
  99. return resultArray(['error' => $visit->getError()]);
  100. }
  101. return resultArray(['data' => $data]);
  102. }
  103. /**
  104. * 编辑回访单
  105. */
  106. public function update()
  107. {
  108. $Visit = new VisitLogic;
  109. $userModel = new \app\admin\model\User();
  110. $param = $this->param;
  111. $userInfo = $this->userInfo;
  112. $param['user_id'] = $userInfo['id'];
  113. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  114. # 设置回访编号
  115. $numberInfo = [];
  116. if (empty($param['number'])) {
  117. $numberInfo = $this->getAutoNumbers(3);
  118. if (empty($numberInfo['number'])) return resultArray(['error' => '请填写回访编号!']);
  119. $param['number'] = $numberInfo['number'];
  120. }
  121. //判断权限
  122. $data = $Visit->getDataById($param['id']);
  123. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', 'update');
  124. $param['update_time'] = time();
  125. if ($Visit->updateDataById($param, $param['id'])) {
  126. # 更新crm_number_sequence表中的last_date、create_time字段
  127. if (!empty($numberInfo['data'])) (new NumberSequence())->batchUpdate($numberInfo['data']);
  128. return resultArray(['data' => '编辑成功']);
  129. } else {
  130. return resultArray(['error' => $Visit->getError()]);
  131. }
  132. }
  133. /**
  134. * 删除回访单
  135. */
  136. public function delete()
  137. {
  138. $Visit = new VisitLogic;
  139. $param = $this->param;
  140. $userInfo = $this->userInfo;
  141. if (!is_array($param['id'])) {
  142. $visit_id[] = $param['id'];
  143. } else {
  144. $visit_id = $param['id'];
  145. }
  146. $data = $Visit->del($visit_id);
  147. if ($data) {
  148. return resultArray(['error' => $data]);
  149. } else {
  150. return resultArray(['data' => '删除成功']);
  151. }
  152. }
  153. /**
  154. * 系统信息
  155. *
  156. */
  157. public function system(VisitLogic $visitLogic)
  158. {
  159. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  160. $data = $visitLogic->getSystemInfo($this->param['id']);
  161. return resultArray(['data' => $data]);
  162. }
  163. /**
  164. * table标签栏数量
  165. */
  166. public function count()
  167. {
  168. if (empty($this->param['visit_id'])) return resultArray(['error' => '参数错误!']);
  169. # 附件
  170. $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();
  171. return resultArray(['data' => ['fielCount' => $fileCount]]);
  172. }
  173. }