From 881848eb82a56efe2e73e8079cf2a34f847e17c7 Mon Sep 17 00:00:00 2001 From: dengbw Date: Wed, 22 Jun 2022 15:15:14 +0800 Subject: [PATCH] persona_622 --- api/controllers/wechat/Liche.php | 9 ++ api/controllers/wechat/Lichene.php | 31 +++-- home/controllers/h5/Persona.php | 212 +++++++++++++++++------------ home/views/h5/persona/index.php | 2 - 4 files changed, 149 insertions(+), 105 deletions(-) diff --git a/api/controllers/wechat/Liche.php b/api/controllers/wechat/Liche.php index af2e32e1..101548d6 100644 --- a/api/controllers/wechat/Liche.php +++ b/api/controllers/wechat/Liche.php @@ -158,8 +158,17 @@ Class Liche extends HD_Controller $param = $this->input->get(); $param['page'] = intval($param['page']); //获取配置了客户联系功能的成员列表 + //获取配置了客户联系功能的成员列表 // $re = $this->wx_qyapi->get_external_contact(array('url' => 'get_follow_user_list')); // echo json_encode($re, JSON_UNESCAPED_UNICODE); +// exit; + //读取成员 + $re = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => 'A.Bing')); + echo json_encode($re, JSON_UNESCAPED_UNICODE); + exit; + //获取客户详情 +// $re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => 'wm2x4YKwAAqdNIMj9eOt1LXroR6cn-NQ')); +// echo json_encode($re, JSON_UNESCAPED_UNICODE); // exit; //用户分页分别获取避免超时 // $userid = "DianCheDaRenXiaoLi"; diff --git a/api/controllers/wechat/Lichene.php b/api/controllers/wechat/Lichene.php index f7675189..7eb561cb 100644 --- a/api/controllers/wechat/Lichene.php +++ b/api/controllers/wechat/Lichene.php @@ -89,15 +89,15 @@ Class Lichene extends HD_Controller $this->load->model('app/licheb/App_licheb_users_model', 'mdUsers'); $this->load->library('wx_qyapi', ['app' => 'lichene']); $re_use = $this->mdUsers->get(['userid' => $userid]); - if (!$re_use) {//企业成员关连狸车宝用户 - $re_wx = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => $userid)); - if ($re_wx['errcode'] == 0 && $re_wx['mobile']) {//获取企业成员手机 - $re_use = $this->mdUsers->get(array('mobile' => $re_wx['mobile'])); - if ($re_use) {//userid与手机绑定关连 - $this->mdUsers->update(['userid' => $userid], ['id' => $re_use['id']]); - } - } - } +// if (!$re_use) {//企业成员关连狸车宝用户 //企业成员手机已取不到废弃6.20 +// $re_wx = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => $userid)); +// if ($re_wx['errcode'] == 0 && $re_wx['mobile']) {//获取企业成员手机 +// $re_use = $this->mdUsers->get(array('mobile' => $re_wx['mobile'])); +// if ($re_use) {//userid与手机绑定关连 +// $this->mdUsers->update(['userid' => $userid], ['id' => $re_use['id']]); +// } +// } +// } $re_msg['log_id'] = $this->mdWechatqyLog->add(['external_userid' => $external_userid, 'userid' => $userid, 'change_type' => $re_msg['ChangeType'], 'remark' => $change_type, 'c_time' => $re_msg['CreateTime']]); $wxqy = 0; @@ -526,20 +526,21 @@ Class Lichene extends HD_Controller { $this->load->model('app/app_lichene_qy_model', 'mdWechatqy'); $this->load->library('wx_qyapi', ['app' => 'lichene']); + echo 'access_token=' . $this->wx_qyapi->access_token() . '
'; $param = $this->input->get(); $param['page'] = intval($param['page']); //获取配置了客户联系功能的成员列表 - //$re = $this->wx_qyapi->get_external_contact(array('url' => 'get_follow_user_list')); - //echo json_encode($re, JSON_UNESCAPED_UNICODE); - //exit; - //读取成员 -// $re = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => 'DengBingWen')); +// $re = $this->wx_qyapi->get_external_contact(array('url' => 'get_follow_user_list')); // echo json_encode($re, JSON_UNESCAPED_UNICODE); // exit; + //读取成员 + $re = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => 'DengBingWen')); + echo json_encode($re, JSON_UNESCAPED_UNICODE); + exit; //获取客户详情 // $re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => 'wm2x4YKwAAqdNIMj9eOt1LXroR6cn-NQ')); // echo json_encode($re, JSON_UNESCAPED_UNICODE); -// exit; + exit; //用户分页分别获取避免超时 // $userid = "DianCheDaRenXiaoLi"; // if ($param['next_cursor'] || !$param['page']) { diff --git a/home/controllers/h5/Persona.php b/home/controllers/h5/Persona.php index 5f8b21e3..2ab1f5d4 100644 --- a/home/controllers/h5/Persona.php +++ b/home/controllers/h5/Persona.php @@ -8,6 +8,9 @@ defined('BASEPATH') OR exit('No direct script access allowed'); */ class Persona extends CI_Controller { + private $session; + private $secret; + private $userid = '';//企业成员userid private $config_qyapi; private $config_qyapi_agent; private $aid; @@ -25,6 +28,20 @@ class Persona extends CI_Controller $this->config_qyapi_agent = ['app' => 'lichene_1000005']; } $this->load->library('wx_qyapi', $this->config_qyapi); + $this->secret = "persona_h5_" . $this->config_qyapi_agent['app']; + $this->load->helper('cookie'); + $ukey = get_cookie('ukey'); + if ($ukey) { + $this->session = json_decode(liche_authcode($ukey, 'DECODE', $this->secret), true); + $this->userid = $this->session['userid']; + } + //重置cookie + $reset = $this->input->get('reset'); + if ($reset) { + set_cookie("ukey", "", 86400 * 1); + $this->session = []; + $this->userid = ''; + } } /** @@ -39,15 +56,11 @@ class Persona extends CI_Controller $this->load->library('wx_qyapi_agent', $this->config_qyapi_agent); $params = $this->input->get(); $url = http_host_com('home') . "/h5/persona?aid={$params['aid']}"; - $result = $this->set_auth($url, $params); - $user_id = 0; - if ($result['errcode'] == 0 && $result['UserId']) {//获取企业员工userid - $user_id = $result['UserId']; - } + $this->set_auth($url, $params); $sign_package = $this->wx_qyapi->getSignPackage(); $sign_package_agent = $this->wx_qyapi_agent->getSignPackage(); $data = ['_title' => '客户画像', 'sign_package' => $sign_package, 'sign_package_agent' => $sign_package_agent - , 'user_id' => $user_id, 'aid' => $this->aid]; + , 'aid' => $this->aid]; $this->load->view('/h5/persona/index', $data); } @@ -66,86 +79,77 @@ class Persona extends CI_Controller $of_id_index = -1; $tagList = $res_td = $sources = $userInfo = []; $tag_data = $tags_name = $description = ''; + $userid = $this->session['userid']; + if (!$userid) { + $this->show_json(400, '加载企业成员失败请重试~'); + } if (!$params['external_userid']) { $this->show_json(400, '加载失败请重试~'); } - if ($params['external_userid']) { - //外部联系人 - $re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $params['external_userid'])); - if ($re['errcode'] != 0) { - $this->show_json(400, '加载失败请重试!'); + //外部联系人 + $re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $params['external_userid'])); + if ($re['errcode'] != 0) { + $this->show_json(400, '加载失败请重试!'); + } + $follow_user = $qy_user = []; + $qy_user['external_contact'] = $re['external_contact']; + foreach ($re['follow_user'] as $key => $val) { + if ($val['userid'] == $userid) {//找出当前企业员工 + $follow_user = $val; + $qy_user['follow_info'] = $val; + break; } - $follow_user = $qy_user = []; - $qy_user['external_contact'] = $re['external_contact']; - foreach ($re['follow_user'] as $key => $val) { - if ($val['userid'] == $params['user_id']) {//找出当前企业员工 - $follow_user = $val; - $qy_user['follow_info'] = $val; - break; + } + $tags_ary = $follow_user['tags'];//客户画像 + $remark_mobiles = $follow_user['remark_mobiles'];//用户手机 + $description = $follow_user['description'];//描述 + 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']; } } - $tags_ary = $follow_user['tags'];//客户画像 - $userid = $follow_user['userid']; - $remark_mobiles = $follow_user['remark_mobiles'];//用户手机 - $description = $follow_user['description'];//描述 - 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']; - } - } - } - $avatar = $mobile = $name = $unionid = ''; - $gender = $biz_id = 0; - 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, 'remark_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']; - $buddy_type = $re_cus['buddy_type']; - $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')) : ''; - } - } - if (!$c_id && $params['user_id']) { - $this->load->model('app/app_lichene_qy_model', 'mdWechatqy'); - $this->set_wechatqy(['userid' => $params['user_id'], 'external_userid' => $params['external_userid']], $qy_user); - $re_qy = $this->mdWechatqy->get(['userid' => $params['user_id'], 'external_userid' => $params['external_userid']]); - $re_qy && $buddy_type = $re_qy['buddy_type']; + } + $avatar = $mobile = $name = $unionid = ''; + $gender = $biz_id = 0; + 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, 'remark_mobile' => $mobile, 'name' => $name, 'gender' => $gender, 'source' => '@微信' + , 'unionid' => $unionid, 'external_userid' => $params['external_userid'], 'userid' => $userid, 'tags_name' => $tags_name + ]; + if ($this->session['biz_id']) { + $biz_id = intval($this->session['biz_id']); + $userInfo['biz_id'] = $biz_id; + $userInfo['uid'] = $this->session['uid']; + $userInfo['uname'] = $this->session['uname']; + } + 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']; + $buddy_type = $re_cus['buddy_type']; + $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')) : ''; } } + if (!$c_id) { + $this->load->model('app/app_lichene_qy_model', 'mdWechatqy'); + $this->set_wechatqy(['userid' => $userid, 'external_userid' => $params['external_userid']], $qy_user); + $re_qy = $this->mdWechatqy->get(['userid' => $userid, 'external_userid' => $params['external_userid']]); + $re_qy && $buddy_type = $re_qy['buddy_type']; + } $userInfo['c_id'] = $c_id; //标签 $res = $this->mdCustomerTag->select(['status' => 1, 'pid' => 0], 'sort desc,id desc', 0, 0, 'id,name,type'); @@ -226,6 +230,8 @@ class Persona extends CI_Controller $this->load->model('receiver/receiver_customers_model', 'mdCustomers'); $this->load->model('receiver/receiver_customer_tagdata_model', 'mdCustomerTagdata'); $this->load->model('app/app_lichene_qy_model', 'mdWechatqy'); + $uid = $userInfo['uid']; + $uname = $userInfo['uname']; $c_id = $userInfo['c_id']; $result = $re_cus = ''; if ($c_id) {//已存在客户,更新资料 @@ -237,20 +243,20 @@ class Persona extends CI_Controller $re_cus2['wxqy'] != 1 && $up_data['wxqy'] = 1; !$re_cus2['unionid'] && $up_data['unionid'] = $userInfo['unionid']; $result = $this->mdCustomers->update($up_data, ['id' => $c_id]); - $this->customers_entity->add_log($c_id, $userInfo['admin_id'], $userInfo['admin_name'], '修改用户基本信息'); + $this->customers_entity->add_log($c_id, $uid, $uname, '修改用户基本信息'); } else { if ($mobile && $biz_id) {//有手机号查找客户是否已存在 $re_cus = $this->mdCustomers->get(['biz_id' => $biz_id, 'mobile' => $mobile, 'status<>' => -1]); if ($re_cus) {//已存客户绑定操作,不带入现有标签中 - $result = $this->mdCustomers->update(['unionid' => $userInfo['unionid'], 'wxqy' => 1], ['id' => $re_cus['id']]); + $up_data = ['wxqy' => 1]; + $userInfo['unionid'] && $up_data['unionid'] = $userInfo['unionid']; + $result = $this->mdCustomers->update($up_data, ['id' => $re_cus['id']]); } else {//创建新客户 $this->load->model("biz/biz_model", 'mdBiz'); $re_biz = $this->mdBiz->get(['id' => $biz_id, 'status' => 1]); $city_id = intval($re_biz['city_id']); $county_id = intval($re_biz['county_id']); $date = date('Y-m-d H:i:s'); - $admin_id = $userInfo['admin_id']; - $admin_name = $userInfo['admin_name']; $add_data = [ 'name' => $userInfo['name'], 'mobile' => $mobile, @@ -264,7 +270,7 @@ class Persona extends CI_Controller 'buddy_type' => $params['buddy_type'], 'cf_title' => '自有资源', 'cf_clues' => '企微私域', - 'admin_id' => $admin_id, + 'admin_id' => $uid, 'status' => 0,//未见客户 'p_time' => $date,//分配时间 'c_time' => time() @@ -277,10 +283,10 @@ class Persona extends CI_Controller , 'userid' => $userInfo['userid']], 'c_time asc', 0, 0, 'remark'); if ($res_log) {//企微跟进记录 foreach ($res_log as $key => $val) { - $this->customers_entity->add_log($c_id, $admin_id, $admin_name, $val['remark'], 8); + $this->customers_entity->add_log($c_id, $uid, $uname, $val['remark'], 8); } } - $this->customers_entity->add_log($c_id, $admin_id, $admin_name, '创建客户档案', 3); + $this->customers_entity->add_log($c_id, $uid, $uname, '创建客户档案', 3); } } } @@ -403,18 +409,48 @@ class Persona extends CI_Controller */ private function set_auth($url = '', $params = array()) { + if ($this->userid) { + return; + } $config = $this->wx_qyapi_agent->getConfig(); $code = $params['code']; if ($code) {//授权码获取微信信息 $this->load->library('mycurl'); $access_token = $this->wx_qyapi_agent->access_token(); + //获取访问用户身份 https://developer.work.weixin.qq.com/document/path/91023 $auth_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={$access_token}&code={$code}"; $res = $this->mycurl->httpGet($auth_url); - return json_decode($res, true); - } else {//静默授权获取用户openid + $res = $res ? json_decode($res, true) : []; + if ($res && $res['errcode'] == 0 && $res['user_ticket']) { + //获取访问用户敏感信息 https://developer.work.weixin.qq.com/document/path/95833 + $auth_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token={$access_token}"; + $res_detail = $this->mycurl->httpPost($auth_url, ['user_ticket' => $res['user_ticket']], 'is_json'); + $res_detail = $res_detail ? json_decode($res_detail, true) : []; + if ($res_detail && $res_detail['errcode'] == 0 && $res_detail['mobile']) { + $session = ['userid' => $res_detail['userid']]; + $expire = 86400 * 1; + $this->load->model('app/licheb/App_licheb_users_model', 'mdUsers'); + $re_user = $this->mdUsers->get(['mobile' => $res_detail['mobile']]); + if ($re_user) { + if (!$re_user['userid'] || $re_user['userid'] != $res_detail['userid']) { + $this->mdUsers->update(['userid' => $res_detail['userid']], ['id' => $re_user['id']]); + } + $session = ['userid' => $res_detail['userid'], 'uid' => $re_user['id'], "uname" => $re_user['uname'] + , "mobile" => $re_user['mobile'], "biz_id" => intval($re_user['biz_id'])]; + $expire = 86400 * 30; + } + $ukey = liche_authcode(json_encode($session, JSON_UNESCAPED_UNICODE), 'ENCODE', $this->secret); + set_cookie("ukey", $ukey, $expire); + $this->session = $session; + } + } + } else {//构造网页授权链接 https://developer.work.weixin.qq.com/document/path/91022 $this->load->helper('url'); $redirect_uri = urlencode($url); - $auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['corpid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; + //静默授权 + //$auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['corpid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; + //手动授权 + $auth_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$config['corpid']}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_privateinfo&state=STATE&agentid={$config['agentid']}#wechat_redirect"; redirect($auth_url); } } diff --git a/home/views/h5/persona/index.php b/home/views/h5/persona/index.php index 5d7ae637..330489b1 100644 --- a/home/views/h5/persona/index.php +++ b/home/views/h5/persona/index.php @@ -112,7 +112,6 @@ of2_id: 0, sources: [], aid: , - user_id: "", buddy_type:0, buddyTypeAry: [], }, @@ -145,7 +144,6 @@ data: { external_userid: external_userid, aid: that.aid, - user_id: that.user_id, }, beforeSend: function () { },