Contacts.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace addons\qingdong\controller;
  3. use addons\qingdong\model\Contacts as ContactsModel;
  4. use addons\qingdong\model\ContactsFile;
  5. use addons\qingdong\model\ContactsOther;
  6. use addons\qingdong\model\FormField;
  7. use addons\qingdong\model\Record;
  8. use addons\qingdong\model\Event as EventModel;
  9. use addons\qingdong\model\OperationLog;
  10. use addons\qingdong\model\Staff;
  11. use think\Db;
  12. use think\Exception;
  13. /**
  14. * 联系人接口
  15. */
  16. class Contacts extends StaffApi {
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. //创建联系人
  20. public function addContacts() {
  21. $params = $this->request->post();
  22. if (empty($params['contacts'])) {
  23. $this->error('联系人信息不能为空');
  24. }
  25. // 表单验证
  26. if (($result = $this->qingdongValidate($params['contacts'], get_class(), 'create')) !== true) {
  27. $this->error($result);
  28. }
  29. $result = FormField::checkFields(FormField::CONTACTS_TYPE,$params['contacts']);
  30. if ($result !== true) {
  31. $this->error($result);
  32. }
  33. if (ContactsModel::where([
  34. 'mobile' => $params['contacts']['mobile'],
  35. 'customer_id' => $params['contacts']['customer_id']
  36. ])->find()) {
  37. $this->error('联系人手机号已存在');
  38. }
  39. Db::startTrans();
  40. try {
  41. $mobile =$params['contacts']['mobile'];
  42. $email =$params['contacts']['email']??'';
  43. if(empty($mobile) && empty($email)){
  44. $this->error('手机号码和邮箱至少填写一项!');
  45. }
  46. $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
  47. $rule2 = '^[0-9]+$^';
  48. if (preg_match($rule, $mobile) == false && preg_match($rule2, $mobile) ==false) {
  49. $this->error('手机号格式错误,国外手机号请填写区号,例如 86XXXX');
  50. }
  51. $contactsId = ContactsModel::createContacts($params['contacts']);
  52. if (isset($params['is_event']) && $params['is_event'] == 1) {//跟进任务
  53. $event = $params['event'];
  54. $event['type'] = 2;//任务
  55. $event['relation_type'] = EventModel::CONTACTS_TYPE;//联系人
  56. $event['relation_id'] = $contactsId;
  57. $event['end_time'] = $event['start_time'];
  58. $event['level'] = 1;
  59. $result = EventModel::createEvent($event);
  60. }
  61. Db::commit();
  62. } catch (Exception $e) {
  63. Db::rollback();
  64. $this->error($e->getMessage());
  65. }
  66. if ($result) {
  67. $this->success('创建联系人成功');
  68. }
  69. }
  70. //编辑联系人
  71. public function editContacts() {
  72. $id = input('id');
  73. $params = $this->request->post();
  74. $row = ContactsModel::where(['id' => $id])->find();
  75. if (empty($row)) {
  76. $this->error('客户信息不存在');
  77. }
  78. // 表单验证
  79. if (($result = $this->qingdongValidate($params, get_class(), 'edit')) !== true) {
  80. $this->error($result);
  81. }
  82. $result = FormField::checkFields(FormField::CONTACTS_TYPE,$params,$id);
  83. if ($result !== true) {
  84. $this->error($result);
  85. }
  86. if (ContactsModel::where([
  87. 'mobile' => $params['mobile'],
  88. 'customer_id' => $row['customer_id'],
  89. 'id' => ['neq', $params['id']]
  90. ])->find()) {
  91. $this->error('联系人手机号已存在');
  92. }
  93. Db::startTrans();
  94. try {
  95. $result = ContactsModel::updateContacts($params);
  96. Db::commit();
  97. } catch (Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. $this->success('编辑联系人成功');
  102. }
  103. //删除联系人
  104. public function delContacts() {
  105. $id = input('id');
  106. $row = ContactsModel::where(['id' => $id, 'owner_staff_id' => $this->auth->id])->find();
  107. if (empty($row)) {
  108. $this->error('您不是客户归属人,无法删除当前联系人');
  109. }
  110. $model = new ContactsModel();
  111. if ($model->destroy(['id' => $id])) {
  112. $this->success('删除成功');
  113. }
  114. Record::quickCreateRecord(Record::CUSTOMER_TYPE, $row['customer_id'],
  115. '删除联系人:' . $row['name']);
  116. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $row['customer_id'], '删除联系人:' . $row['name']);
  117. $this->error('删除失败');
  118. }
  119. //获取联系人列表
  120. public function getList() {
  121. $limit = input("limit/d", 10);
  122. $customer_id = input('customer_id');
  123. $name = input('name');
  124. $params = input();
  125. $where = FormField::updateWhereField(FormField::CONTACTS_TYPE, $params);
  126. if ($customer_id) {
  127. $where['customer_id'] = $customer_id;
  128. }else{
  129. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  130. }
  131. if ($name) {
  132. $where['name'] = ['like', "%{$name}%"];
  133. }
  134. $records = ContactsModel::where($where)->with(['customer'])->order('id desc')->paginate($limit);
  135. $this->success('请求成功', $records);
  136. }
  137. //获取select联系人列表
  138. public function getSelectList() {
  139. $customer_id = input('customer_id');
  140. $name = input('name','');
  141. $where = [];
  142. if ($customer_id) {
  143. $where['customer_id'] = $customer_id;
  144. }
  145. if ($name) {
  146. $where['name'] = ['like',"%$name%"];
  147. }
  148. $list = ContactsModel::where($where)->with(['customer'])->field('id,customer_id,name,mobile')->select();
  149. $list = collection($list)->toArray();
  150. foreach ($list as $k => $v) {
  151. $v['new_mobile'] = $v['mobile'];
  152. $list[$k] = $v;
  153. }
  154. $this->success('请求成功', $list);
  155. }
  156. //获取联系人详情
  157. public function getDetail() {
  158. $id = input('id');
  159. $contract = ContactsModel::where(['id' => $id])->with([
  160. 'customer',
  161. 'ownerStaff'
  162. ])->find();
  163. if (empty($contract)) {
  164. $this->error('信息不存在');
  165. }
  166. $contract = $contract->toArray();
  167. $contract=ContactsOther::getOther($contract);
  168. $this->success('请求成功', $contract);
  169. }
  170. //获取附件列表
  171. public function getFilesList() {
  172. $id = input('contacts_id');
  173. $files = ContactsFile::where(['contacts_id' => $id])->field('file_id')->with(['file'])->select();
  174. $this->success('请求成功', $files);
  175. }
  176. }