Files
liche/home/controllers/h5/Persona.php
T
2022-03-29 09:24:11 +08:00

387 lines
18 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Notes:客户画像
* Created on: 2022/3/28 11:19
* Created by: dengbw
*/
class Persona extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
/**
* Notes:客户画像
* https://liche-dev.xiaoyu.com/h5/lichene
* https://www.liche.cn/h5/lichene
* Created on: 2022/3/25 11:19
* Created by: dengbw
*/
public function index()
{
$this->load->library('wx_qyapi', ['app' => 'lichene']);
$sign_package = $this->wx_qyapi->getSignPackage();
$this->load->library('wx_qyapi_agent');
$sign_package2 = $this->wx_qyapi_agent->getSignPackage();
$sign_package2['agentid'] = '1000005';
//echo json_encode($sign_package, JSON_UNESCAPED_UNICODE);
//echo '<br><br>';
//echo json_encode($sign_package2, JSON_UNESCAPED_UNICODE);
$data = ['_title' => '客户画像', 'sign_package' => $sign_package, 'sign_package2' => $sign_package2];
$this->load->view('/h5/lichene/index', $data);
}
/**
* Notes:获取用户信息
* Created on: 2022/3/27 0:55
* Created by: dengbw
*/
public function get_user()
{
$params = $this->input->get();
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$this->load->model('receiver/receiver_customer_tag_model', 'mdCustomerTag');
$this->load->model('receiver/receiver_customer_tagdata_model', 'mdCustomerTagdata');
$c_id = $of_id = $of2_id = 0;
$of_id_index = -1;
$tagList = $res_td = $sources = $userInfo = [];
$tag_data = $tags_name = $description = '';
if (!$params['external_userid']) {
$this->show_json(400, '加载失败请重试~');
}
if ($params['external_userid']) {
$this->load->library('wx_qyapi', ['app' => 'lichene']);
//外部联系人
$re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $params['external_userid']));
if (!$re) {
$this->show_json(400, '加载失败请重试!');
}
$tags_ary = $re['follow_user'][0]['tags'];//客户画像
if ($tags_ary && $tags_ary != '[]' && is_array($tags_ary)) {
foreach ($tags_ary as $key => $val) {
if ($val['type'] == 1) {//企业标签
$tag_data[] = $val['tag_id'];
$tags_name[] = $val['tag_name'];
}
}
}
$userid = $re['follow_user'][0]['userid'] ? $re['follow_user'][0]['userid'] : '';
$avatar = $mobile = $name = $unionid = '';
$gender = $biz_id = 0;
$remark_mobiles = $re['follow_user'][0]['remark_mobiles'];//用户手机
$description = $re['follow_user'][0]['description'];//描述
if ($remark_mobiles && $remark_mobiles != '[]' && is_array($remark_mobiles)) {
$mobile = $remark_mobiles[0];//取第一个手机号
}
$external_contact = $re['external_contact'] ? $re['external_contact'] : '';//用户信息
if ($external_contact) {
$external_contact['avatar'] && $avatar = $external_contact['avatar'];
$external_contact['name'] && $name = $external_contact['name'];
$gender = intval($external_contact['gender']);
$external_contact['unionid'] && $unionid = $external_contact['unionid'];
}
$userInfo = [
'avatar' => $avatar, 'mobile' => $mobile, 'name' => $name, 'gender' => $gender, 'source' => '@微信'
, 'unionid' => $unionid, 'external_userid' => $params['external_userid'], 'userid' => $userid, 'tags_name' => $tags_name
];
//企业成员
$re_wx = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => $userid));
if ($re_wx['errcode'] == 0 && $re_wx['mobile']) {
$admin_id = 0;
$admin_name = '';
$this->load->model('app/licheb/App_licheb_users_model', 'mdUsers');
$re_u = $this->mdUsers->get(array('mobile' => $re_wx['mobile']));
if ($re_u) {
$biz_id = intval($re_u['biz_id']);
$admin_id = $re_u['id'];
$admin_name = $re_u['uname'];
}
$userInfo['biz_id'] = $biz_id;
$userInfo['admin_id'] = $admin_id;
$userInfo['admin_name'] = $admin_name;
}
if ($biz_id && $mobile) {//查找已存在客户
$re_cus = $this->mdCustomers->get(['biz_id' => $biz_id, 'mobile' => $mobile, 'status<>' => -1]);
if ($re_cus) {
$c_id = $re_cus['id'];
$of_id = $re_cus['of_id'];
$of2_id = $re_cus['of2_id'];
$res_td = $this->mdCustomerTagdata->select(['c_id' => $re_cus['id']], 'id desc', 0, 0, 't_id');//查找用户选择
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
}
}
}
$userInfo['c_id'] = $c_id;
//标签
$res = $this->mdCustomerTag->select(['status' => 1, 'pid' => 0], 'sort desc,id desc', 0, 0, 'id,name,type');
if ($res) {
$key_tag_id = $c_id ? 'id' : 'qy_id';//比对key值
foreach ($res as $key => $val) {
$list = [];
$value = '';
$res2 = $this->mdCustomerTag->select(['status' => 1, 'pid' => $val['id']], 'sort desc,id desc', 0, 0, 'id,name,qy_id');
foreach ($res2 as $key2 => $val2) {
//检查是否选中标签
$setValue = ['id' => $val2['id'], 'name' => $val2['name'], 'qy_id' => $val2['qy_id']];
if ($val['type'] == 'checkbox') {
$setValue['checked'] = $tag_data && in_array($val2[$key_tag_id], $tag_data) ? true : false;
} else {
$checked = false;
if ($tag_data && in_array($val2[$key_tag_id], $tag_data)) {
!$value && $checked = true;
$value = $val2['qy_id'];
}
$setValue['checked'] = $checked;
}
$list[] = $setValue;
}
$tagList[] = ['id' => $val['id'], 'name' => $val['name'], 'type' => $val['type'], 'list' => $list];
}
}
//线索来源
$status = 0;
$offline_sources = $this->mdCustomers->offlineSources();
if (strlen($status) && $status == 0) {//未见客户
unset($offline_sources[1]);
}
$ii = 0;
$description_arr = $description ? explode('-', $description) : [];
foreach ($offline_sources as $key => $val) {
$list = [];
if (!$c_id) {
if ($description_arr[0] == $val['name']) {
$of_id = $key;
$of_id_index = $ii;
}
} else {
$of_id == $key && $of_id_index = $ii;
}
foreach ($val['list'] as $key2 => $val2) {
if (!$c_id) {
$description_arr[1] == $val2 && $of2_id = $key2;
}
$list[] = ['id' => $key2, 'name' => $val2];
}
$sources[] = ['id' => $key, 'name' => $val['name'], 'list' => $list];
$ii++;
}
$data['tagList'] = $tagList;
$data['sources'] = $sources;
$data['userInfo'] = $userInfo;
$data['of_id'] = $of_id;
$data['of2_id'] = $of2_id;
$data['of_id_index'] = $of_id_index;
$this->show_json(200, '获取用户信息成功', $data);
}
/**
* Notes:到店
* Created on: 2022/3/27 0:57
* Created by: dengbw
*/
public function post_to_biz()
{
$params = $this->input->post();
$userInfo = $params['userInfo'];
$mobile = $userInfo['mobile'];
$biz_id = intval($userInfo['biz_id']);
if (!$mobile) {
$this->show_json(400, '请输入手机号!');
}
if (!mobile_valid($mobile)) {
$this->show_json(400, '手机号格式不正确!');
}
if (!$biz_id) {
$this->show_json(400, '未绑定狸车宝门店!');
}
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$this->load->model("biz/biz_model", 'mdBiz');
$re_biz = $this->mdBiz->get(['id' => $biz_id, 'status' => 1]);
if (!$re_biz) {
$this->show_json(400, '门店不存在!');
}
$re_cus = $this->mdCustomers->get(['biz_id' => $biz_id, 'mobile' => $mobile, 'status<>' => -1]);
$date = date('Y-m-d H:i:s');
if (!$re_cus) {//新增客户
$add_data = [
'name' => $userInfo['name'],
'mobile' => $mobile,
'biz_id' => $biz_id,
'city_id' => $re_biz['city_id'],
'county_id' => $re_biz['county_id'],
'unionid' => $userInfo['unionid'],
'of_id' => $params['of_id'],
'of2_id' => $params['of2_id'],
'cf_title' => '自有资源',
'admin_id' => $userInfo['admin_id'],
'status' => 1,//到店
'a_num' => 1,//到店次数
'dt_time' => $date,//到店时间
'cont_time' => $date,//最后联系时间
'p_time' => $date,//分配时间
'c_time' => time()
];
$c_id = $this->mdCustomers->add($add_data);
$log = '创建客户';
} else {
$c_id = $re_cus['id'];
!$re_cus['unionid'] && $up_data['unionid'] = $userInfo['unionid'];
$re_cus['status'] == 0 && $up_data['status'] = 1;//改为到店
$up_data['a_num = a_num+1'] = null;//加到店次数
if ($re_cus['dt_time'] == '0000-00-00 00:00:00') {//首次到店时间
$up_data['dt_time'] = $date;
}
$up_data['cont_time'] = $date;
$this->mdCustomers->update($up_data, ['id' => $c_id]);
$log = '到店+1';
}
$this->load->library('receiver/customers_entity');
$this->customers_entity->add_log($c_id, $userInfo['admin_id'], $userInfo['admin_name'], $log);
//客户标签
if ($params['taglList']) {
$this->load->model('receiver/receiver_customer_tagdata_model', 'mdCustomerTagdata');
$add_tag = [];
//查找已加入标签
$res_td = $this->mdCustomerTagdata->select(['c_id' => $c_id], 'id desc', 0, 0, 't_id');
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($params['taglList'] as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val2['checked'] == 'true') {
if (!$tag_data || !in_array($val2['id'], $tag_data)) {//未加标签,新增
$add_tag[] = ['c_id' => $c_id, 't_id' => $val2['id'], 'c_time' => time()];
}
} else {
if ($tag_data && in_array($val2['id'], $tag_data)) {//删除标签
$this->mdCustomerTagdata->delete(['c_id' => $c_id, 't_id' => $val2['id']]);
}
}
}
}
if ($add_tag && count($add_tag)) {
$this->mdCustomerTagdata->add_batch($add_tag);
}
}
if (!$re_cus) {//新增客户 更新企微信息
$this->load->library('wx_qyapi', ['app' => 'lichene']);
$remark_mobiles = $userInfo['mobile'] ? [$userInfo['mobile']] : '';
$description = $params['of_title'] ? $params['of_title'] : '';
if ($description && $params['of2_title']) {
$description .= '-' . $params['of2_title'];
}
//企微备注操作
$this->wx_qyapi->get_external_contact(['url' => 'remark', 'userid' => $userInfo['userid']
, 'external_userid' => $userInfo['external_userid'], 'remark_mobiles' => $remark_mobiles, 'description' => $description]);
$tags_name = $userInfo['tags_name'];
$add_tag = $remove_tag = '';
if ($params['taglList']) {
foreach ($params['taglList'] as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val2['checked'] == 'true') {
if (!$tags_name || !in_array($val2['name'], $tags_name)) {//未加标签,新增
$add_tag[] = $val2['qy_id'];
}
} else {
if ($tags_name && in_array($val2['name'], $tags_name)) {//删除标签
$remove_tag[] = $val2['qy_id'];
}
}
}
}
}
//企微标签操作
$this->wx_qyapi->get_external_contact(['url' => 'mark_tag', 'userid' => $userInfo['userid'],
'external_userid' => $userInfo['external_userid'], 'add_tag' => $add_tag, 'remove_tag' => $remove_tag]);
}
$this->show_json(200, '操作成功');
}
/**
* Notes:保存用户信息
* Created on: 2022/3/27 0:55
* Created by: dengbw
*/
public function post_user()
{
$params = $this->input->post();
$userInfo = $params['userInfo'];
$this->load->library('wx_qyapi', ['app' => 'lichene']);
$remark_mobiles = $userInfo['mobile'] ? [$userInfo['mobile']] : '';
$description = $params['of_title'] ? $params['of_title'] : '';
if ($description && $params['of2_title']) {
$description .= '-' . $params['of2_title'];
}
//企微备注操作
$re_remark = $this->wx_qyapi->get_external_contact(['url' => 'remark', 'userid' => $userInfo['userid']
, 'external_userid' => $userInfo['external_userid'], 'remark_mobiles' => $remark_mobiles, 'description' => $description]);
//标签操作
$tags_name = $userInfo['tags_name'];
$add_tag = $remove_tag = '';
if ($params['taglList']) {
foreach ($params['taglList'] as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val2['checked'] == 'true') {
if (!$tags_name || !in_array($val2['name'], $tags_name)) {//未加标签,新增
$add_tag[] = $val2['qy_id'];
}
} else {
if ($tags_name && in_array($val2['name'], $tags_name)) {//删除标签
$remove_tag[] = $val2['qy_id'];
}
}
}
}
}
//企微标签操作
$re_tag = $this->wx_qyapi->get_external_contact(['url' => 'mark_tag', 'userid' => $userInfo['userid'],
'external_userid' => $userInfo['external_userid'], 'add_tag' => $add_tag, 'remove_tag' => $remove_tag]);
$c_id = $userInfo['c_id'];
if ($c_id) {//已存在客户,更新资料
$this->load->library('receiver/customers_entity');
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
$this->load->model('receiver/receiver_customer_tagdata_model', 'mdCustomerTagdata');
$up_data = ['of_id' => $params['of_id'], 'of2_id' => $params['of2_id']];
$this->mdCustomers->update($up_data, ['id' => $c_id]);
$this->customers_entity->add_log($c_id, $userInfo['admin_id'], $userInfo['admin_name'], '修改用户基本信息');
if ($params['taglList']) {
$add_tag = [];
//查找已加入标签
$res_td = $this->mdCustomerTagdata->select(['c_id' => $c_id], 'id desc', 0, 0, 't_id');
$tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : '';
foreach ($params['taglList'] as $key => $val) {
foreach ($val['list'] as $key2 => $val2) {
if ($val2['checked'] == 'true') {
if (!$tag_data || !in_array($val2['id'], $tag_data)) {//未加标签,新增
$add_tag[] = ['c_id' => $c_id, 't_id' => $val2['id'], 'c_time' => time()];
}
} else {
if ($tag_data && in_array($val2['id'], $tag_data)) {//删除标签
$this->mdCustomerTagdata->delete(['c_id' => $c_id, 't_id' => $val2['id']]);
}
}
}
}
if ($add_tag && count($add_tag)) {
$this->mdCustomerTagdata->add_batch($add_tag);
}
}
}
if ($re_remark || $re_tag['errcode'] == 0) {
$this->show_json(200, '操作成功');
} else {
$this->show_json(400, '操作失败');
}
}
private function show_json($code, $msg, $info = [])
{
$data['code'] = $code;
$data['msg'] = $msg;
$data['data'] = $info;
die(json_encode($data, JSON_UNESCAPED_UNICODE));
}
}