Contacts.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\admin\controller\qingdongams\weixin;
  3. use addons\qingdongams\model\DingContacts;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Exception;
  7. use addons\qingdongams\model\DingStaff;
  8. use addons\qingdongams\model\DingCustomer;
  9. use addons\qingdongams\model\Contacts as ContactsModel;
  10. use addons\qingdongams\model\Customer;
  11. /**
  12. * 企业微信联系人同步
  13. */
  14. class Contacts extends Backend
  15. {
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new DingContacts();
  20. }
  21. /**
  22. * 企业微信联系人列表
  23. */
  24. public function index()
  25. {
  26. $this->request->filter(['strip_tags']);
  27. if ($this->request->isAjax()) {
  28. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  29. $wheres['ding_contacts.type'] = 1;
  30. $list = $this->model->with(['ownerstaff','customer'])->where($where)->where($wheres)->order($sort, $order)->paginate($limit);
  31. $row = $list->items();
  32. $result = array("total" => $list->total(), "rows" => $row);
  33. return json($result);
  34. }
  35. return $this->view->fetch();
  36. }
  37. /**
  38. * 删除企业微信联系人
  39. */
  40. public function del($ids = null)
  41. {
  42. if ($this->request->isAjax()) {
  43. $map['id'] = array('in', $ids);
  44. $result = $this->model->where($map)->delete();
  45. if (!$result) {
  46. $this->error('删除失败');
  47. }
  48. $this->success('删除成功');
  49. }
  50. return $this->view->fetch();
  51. }
  52. /**
  53. * 同步到CRM
  54. */
  55. public function batch()
  56. {
  57. $info = $this->model->where(array('type'=>1,'status' => 0))->select();
  58. if (!$info) {
  59. $this->error('无数据可同步');
  60. }
  61. Db::startTrans();
  62. try {
  63. foreach ($info as $k => $v) {
  64. $customer = DingCustomer::where(array('id' => $v['customer_id']))->find();
  65. if (!$customer['customer_id']) {
  66. throw new Exception('请先同步客户');
  67. }
  68. $cusData = Customer::where(array('id' => $customer['customer_id']))->find();
  69. if (!$cusData) {
  70. throw new Exception('客户不存在,请重新同步客户');
  71. }
  72. $contactData = array(
  73. 'customer_id' => $customer['customer_id'],
  74. 'name' => $v['name'],
  75. 'post' => $v['post'],
  76. 'email' => $v['email'],
  77. 'mobile' => $v['mobile'],
  78. 'remarks' => $v['remarks'],
  79. 'create_staff_id' => DingStaff::where(array('user_id' => $v['create_staff_id']))->value('staff_id'),
  80. 'owner_staff_id' => DingStaff::where(array('user_id' => $v['owner_staff_id']))->value('staff_id'),
  81. );
  82. $contactinfo = ContactsModel::create($contactData);
  83. //更新状态
  84. $dingStatus = $this->model->where(array('id' => $v['id']))->update(array('status' => 1));
  85. if (!$contactinfo || !$dingStatus) {
  86. throw new Exception('同步失败');
  87. }
  88. }
  89. Db::commit();
  90. } catch (Exception $e) {
  91. Db::rollback();
  92. $this->error($e->getMessage());
  93. }
  94. $this->success('同步成功');
  95. }
  96. }