Contacts.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Contacts as ContactsModel;
  4. use addons\qingdongams\model\ContactsFile;
  5. use addons\qingdongams\model\ContactsOther;
  6. use addons\qingdongams\model\Form;
  7. use addons\qingdongams\model\FormField;
  8. use addons\qingdongams\model\Mobilecode;
  9. use addons\qingdongams\model\Person;
  10. use addons\qingdongams\model\OperationLog;
  11. use addons\qingdongams\model\Staff;
  12. use think\Db;
  13. use think\Exception;
  14. /**
  15. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  16. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  17. * @desc 售后微信:qingdong_crm
  18. */
  19. /**
  20. * 联系人接口
  21. */
  22. class Contacts extends StaffApi {
  23. protected $noNeedLogin = [];
  24. protected $noNeedRight = [];
  25. //创建联系人
  26. public function addContacts() {
  27. $params = $this->request->post();
  28. if (empty($params['contacts'])) {
  29. $this->error('联系人信息不能为空');
  30. }
  31. // 表单验证
  32. if (($result = $this->qingdongamsValidate($params['contacts'],get_class(), 'create')) !== true) {
  33. $this->error($result);
  34. }
  35. $mobile =$params['contacts']['mobile']??'';
  36. $email =$params['contacts']['email']??'';
  37. if($mobile){
  38. if (ContactsModel::where([
  39. 'customer_id'=>$params['contacts']['customer_id'],
  40. 'mobile' => $mobile,
  41. ])->find()) {
  42. $this->error('联系人手机号已存在');
  43. }
  44. }
  45. $result = FormField::checkFields(FormField::CONTACTS_TYPE,$params['contacts']);
  46. if ($result !== true) {
  47. $this->error($result);
  48. }
  49. Db::startTrans();
  50. try {
  51. if(empty($mobile) && empty($email)){
  52. $this->error('手机号码和邮箱至少填写一项!');
  53. }
  54. $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
  55. $rule2 = '^\d+$^';
  56. if (preg_match($rule2, $mobile) ==false) {
  57. $this->error('手机号格式错误');
  58. }
  59. ContactsModel::createContacts($params['contacts']);
  60. $personModel = new Person();
  61. if($personId = $personModel->where(['account'=>$mobile])->value('id')){
  62. $personModel->where('id',$personId)->update(['customer_id'=>$params['contacts']['customer_id']]);
  63. }
  64. Db::commit();
  65. } catch (Exception $e) {
  66. Db::rollback();
  67. $this->error($e->getMessage());
  68. }
  69. if ($result) {
  70. $this->success('创建联系人成功');
  71. }
  72. }
  73. //编辑联系人
  74. public function editContacts() {
  75. $id = input('id');
  76. $params = $this->request->post();
  77. $row = ContactsModel::where(['id' => $id])->find();
  78. if (empty($row)) {
  79. $this->error('客户信息不存在');
  80. }
  81. // 表单验证
  82. if (($result = $this->qingdongamsValidate($params,get_class(), 'edit')) !== true) {
  83. $this->error($result);
  84. }
  85. if (ContactsModel::where([
  86. 'mobile' => $params['mobile'],
  87. 'customer_id' => $row['customer_id'],
  88. 'id' => ['neq', $params['id']]
  89. ])->find()) {
  90. $this->error('联系人手机号已存在');
  91. }
  92. $result = FormField::checkFields(FormField::CONTACTS_TYPE,$params,$id);
  93. if ($result !== true) {
  94. $this->error($result);
  95. }
  96. Db::startTrans();
  97. try {
  98. ContactsModel::updateContacts($params);
  99. Db::commit();
  100. } catch (Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. $this->success('编辑联系人成功');
  105. }
  106. //姓名是否重复
  107. public function isNameDuplicate(){
  108. $name=input('name');
  109. $contacts= ContactsModel::where(['name'=>$name])->with('customer')->find();
  110. if(!empty($contacts)){
  111. $this->error('联系人名称已存在于客户【'.$contacts['customer']['name'].'】名下');
  112. }
  113. $this->success('不重复');
  114. }
  115. //删除联系人
  116. public function delContacts() {
  117. $id = input('id');
  118. $row = ContactsModel::where(['id' => $id, 'owner_staff_id' => $this->auth->id])->find();
  119. if (empty($row)) {
  120. $this->error('您不是客户归属人,无法删除当前联系人');
  121. }
  122. $model = new ContactsModel();
  123. if ($model->destroy(['id' => $id])) {
  124. $this->success('删除成功');
  125. }
  126. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $row['customer_id'], '删除联系人:' . $row['name']);
  127. $this->error('删除失败');
  128. }
  129. //获取联系人列表
  130. public function getList() {
  131. $limit = input("limit/d", 10);
  132. $customer_id = input('customer_id');
  133. $name = input('name');
  134. $params = $this->request->post();
  135. $where= FormField::updateWhereField(FormField::CONTACTS_TYPE,$params);
  136. if ($customer_id) {
  137. $where['customer_id'] = $customer_id;
  138. }
  139. if ($name) {
  140. $where['name|mobile|telephone'] = ['like', "%{$name}%"];
  141. }
  142. if (isset($params['createtime']) && $params['createtime']) {//创建时间
  143. $createtime = $params['createtime'];
  144. $times = setTimes($createtime, 'time');
  145. $where['createtime'] = ['between', $times];
  146. }
  147. //0全部 1 我负责 2 下属负责的
  148. if (isset($params['staff_id']) && $params['staff_id']) {//下级员工筛选
  149. $where['owner_staff_id'] = $params['staff_id'];
  150. } else {
  151. $type = $params['type'] ?? 0;
  152. if ($type == 1) {//我负责的
  153. $where['owner_staff_id'] = $this->auth->id;
  154. } elseif ($type == 2) {//下属负责的
  155. $where['owner_staff_id'] = ['in', Staff::getLowerStaffId()];
  156. }else{
  157. $where['owner_staff_id'] = ['in', Staff::getMyStaffIds()];
  158. }
  159. }
  160. $records = ContactsModel::where($where)->with(['customer'])->order('id desc')->paginate($limit);
  161. $this->success('请求成功', $records);
  162. }
  163. //获取select联系人列表
  164. public function getSelectList() {
  165. $customer_id = input('customer_id');
  166. $name = input('name');
  167. $where = [];
  168. if ($customer_id) {
  169. $where['customer_id'] = $customer_id;
  170. }
  171. if ($name) {
  172. $where['name'] = ['like',"%{$name}%"];
  173. }
  174. $list = ContactsModel::where($where)->field('id,name,mobile,mobilecode,region')->select();
  175. $list = collection($list)->toArray();
  176. foreach ($list as $k => $v) {
  177. $v['new_mobile'] = $v['mobile'];
  178. $list[$k] = $v;
  179. }
  180. $this->success('请求成功', $list);
  181. }
  182. //获取联系人详情
  183. public function getDetail() {
  184. $id = input('id');
  185. $ContactsModel=new ContactsModel();
  186. $contract = $ContactsModel->get(['id' => $id],[
  187. 'customer',
  188. 'ownerStaff'
  189. ]);
  190. if (empty($contract)) {
  191. $this->error('信息不存在');
  192. }
  193. $mobile= $contract->getData('mobile');
  194. $contract = $contract->toArray();
  195. $contract['mobile']=$mobile;
  196. $contract['mobilecode_detail']= Mobilecode::where(['number'=>$contract['mobilecode']])->find();
  197. $contract=ContactsOther::getOther($contract);
  198. $this->success('请求成功', $contract);
  199. }
  200. //获取附件列表
  201. public function getFilesList() {
  202. $id = input('contacts_id');
  203. $files = ContactsFile::where(['contacts_id' => $id])->field('file_id')->with(['file'])->select();
  204. $this->success('请求成功', $files);
  205. }
  206. //获取国外电话区号
  207. public function getMobileCode() {
  208. $name = input('name');
  209. $where =[];
  210. if(isset($name) && !empty($name)){
  211. $where['name|number'] = ['like','%'.$name.'%'];
  212. }
  213. $list = Mobilecode::where($where)->orderRaw('CONVERT(name using gbk) asc')->select();
  214. $this->success('',$list);
  215. }
  216. // 获取邮箱列表
  217. public function getEmails() {
  218. $name = input('name','');
  219. $where['email'] = [['not null',''],['neq',''],'and'];
  220. if(isset($name) && $name){
  221. $where['name'] = ['like','%'.$name.'%'];
  222. }
  223. $list = \addons\qingdongams\model\Contacts::where($where)->field('id,name,email')->select();
  224. $this->success('',$list);
  225. }
  226. }