input->get('bid') ? $this->input->get('bid') : $this->input->post('bid'); $this->bid = intval($bid); if ($this->bid == 133) { $this->config_qyapi = ['app' => "diff_{$this->bid}"]; $this->config_qyapi_agent = ['app' => "diff_{$this->bid}_1000002"]; } else { $this->show_json(400, '参数错误~'); } $this->load->library('wx_qyapi', $this->config_qyapi); $this->secret = "different_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 = ''; } } /** * Notes:客户画像 * https://liche-dev.xiaoyu.com/h5/different * https://www.liche.cn/h5/different * Created on: 2022/3/25 11:19 * Created by: dengbw */ public function index() { $this->load->library('wx_qyapi_agent', $this->config_qyapi_agent); $params = $this->input->get(); $url = http_host_com('home') . "/h5/different?bid={$params['bid']}"; $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 , 'bid' => $this->bid]; $this->load->view('/h5/different/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'); $c_id = $buddy_type = 0; $tagList = $res_td = $sources = $userInfo = []; $tag_data = $tags_name = ''; $userid = $this->userid; if (!$userid) { $this->show_json(400, '加载企业成员失败请重试~'); } if (!$params['external_userid']) { $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; } } $tags_ary = $follow_user['tags'];//客户画像 $remark_mobiles = $follow_user['remark_mobiles'];//用户手机 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_id' => $tag_data ]; 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']; $buddy_type = $re_cus['buddy_type']; } } if (!$c_id) { $this->load->model('app/app_different_qy_model', 'mdWechatqy'); $this->set_wechatqy(['userid' => $userid, 'external_userid' => $params['external_userid']], $qy_user); $re_qy = $this->mdWechatqy->get(['brand_id' => $this->bid, 'userid' => $userid, 'external_userid' => $params['external_userid']]); $re_qy && $buddy_type = $re_qy['buddy_type']; } $userInfo['c_id'] = $c_id; //标签 $re = $this->wx_qyapi->get_external_contact(['url' => 'get_corp_tag_list']); if ($re['errcode'] == 0 && $re['tag_group']) { foreach ($re['tag_group'] as $key => $val) { $list = []; foreach ($val['tag'] as $key2 => $val2) { //检查是否选中标签 $setValue = ['id' => $val2['id'], 'name' => $val2['name'], 'order2' => $val2['order']]; $setValue['checked'] = $tag_data && in_array($val2['id'], $tag_data) ? true : false; $list[] = $setValue; } if ($list) { $tagKey = array_column($list, 'order2'); array_multisort($tagKey, SORT_DESC, $list); } $tagList[] = ['id' => $val['group_id'], 'name' => $val['group_name'], 'order' => $val['order'], 'list' => $list]; } $groupKey = array_column($tagList, 'order'); array_multisort($groupKey, SORT_DESC, $tagList); } $data['tagList'] = $tagList; $data['sources'] = $sources; $data['userInfo'] = $userInfo; $data['buddy_type'] = $buddy_type; $data['buddyTypeAry'] = $this->mdCustomers->buddyTypeAry(); $this->show_json(200, '获取用户信息成功', $data); } /** * Notes:保存用户信息 * Created on: 2022/4/27 0:55 * Created by: dengbw */ public function post_user() { $params = $this->input->post(); $userInfo = $params['userInfo']; $mobile = $userInfo['mobile']; $biz_id = intval($userInfo['biz_id']); $this->load->model('receiver/receiver_customers_model', 'mdCustomers'); $this->load->library('receiver/customers_entity'); $this->load->model('app/app_different_qy_model', 'mdWechatqy'); $uid = $userInfo['uid']; $uname = $userInfo['uname']; $c_id = $userInfo['c_id']; $result = $re_cus = ''; $tag_group = [];//客房画像 if ($params['taglList']) { foreach ($params['taglList'] as $key => $val) { $tag = []; foreach ($val['list'] as $key2 => $val2) { if ($val2['checked'] == 'true') { $tag[$val2['id']] = $val2['name']; } } if ($tag) { $tag_group[$val['id']] = ['name' => $val['name'], 'tag' => $tag]; } } } if ($c_id) {//已存在客户,更新资料 $up_data = ['buddy_type' => $params['buddy_type']]; $re_cus2 = $this->mdCustomers->get(['id' => $c_id, 'status<>' => -1]); if (!$re_cus2) { $this->show_json(200, '客户不存在'); } $jsondata = $re_cus2['jsondata'] ? json_decode($re_cus2['jsondata'], true) : []; $jsondata['tag_group'] = $tag_group; $up_data['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE); $re_cus2['wxqy'] != 1 && $up_data['wxqy'] = 1; if (!$re_cus2['unionid'] && $userInfo['unionid']) { $up_data['unionid'] = $userInfo['unionid']; } $result = $this->mdCustomers->update($up_data, ['id' => $c_id]); $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) {//已存客户绑定操作,不带入现有标签中 $up_data = ['wxqy' => 1]; $jsondata = $re_cus['jsondata'] ? json_decode($re_cus['jsondata'], true) : []; $jsondata['tag_group'] = $tag_group; $up_data['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE); $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'); $add_data = [ 'name' => $userInfo['name'], 'mobile' => $mobile, 'biz_id' => $biz_id, 'city_id' => $city_id, 'county_id' => $county_id, 'unionid' => $userInfo['unionid'], 'wxqy' => 1, 'buddy_type' => $params['buddy_type'], 'cf_title' => '自有资源', 'cf_clues' => '异业店', 'admin_id' => $uid, 'status' => 0,//未见客户 'p_time' => $date,//分配时间 'jsondata' => json_encode(['tag_group' => $tag_group], JSON_UNESCAPED_UNICODE), 'c_time' => time() ]; $c_id = $this->mdCustomers->add($add_data); if ($c_id) {//加日志 $result = true; $this->load->model('app/app_different_qy_log_model', 'mdWechatqyLog'); $res_log = $this->mdWechatqyLog->select(['brand_id' => $this->bid, 'external_userid' => $userInfo['external_userid'] , 'userid' => $userInfo['userid']], 'c_time asc', 0, 0, 'remark,c_time'); if ($res_log) {//企微跟进记录 foreach ($res_log as $key => $val) { $this->customers_entity->add_log($c_id, $uid, $uname, $val['remark'], 8, 'wxapp', '', $val['c_time']); } } $this->customers_entity->add_log($c_id, $uid, $uname, '创建客户档案', 3); } } } } //更新企微用户好友类型 $up_qy = $this->mdWechatqy->update(['buddy_type' => $params['buddy_type']], ['external_userid' => $userInfo['external_userid'] , 'userid' => $userInfo['userid']]); $up_qy && $result = true; //企微操作 $remark_mobiles = $userInfo['mobile'] ? [$userInfo['mobile']] : ''; //企微备注操作 $this->wx_qyapi->get_external_contact(['url' => 'remark', 'userid' => $userInfo['userid'] , 'external_userid' => $userInfo['external_userid'], 'remark_mobiles' => $remark_mobiles]); //标签操作 $tags_id = $userInfo['tags_id']; $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_id || !in_array($val2['id'], $tags_id)) {//未加标签,新增 $add_tag[] = $val2['id']; } } else { if ($tags_id && in_array($val2['id'], $tags_id)) {//删除标签 $remove_tag[] = $val2['id']; } } } } } if ($add_tag || $remove_tag) { $result = true; $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]); } if ($re_cus && $re_cus['admin_id'] != $uid) { $this->show_json(200, '已有同事在跟进该客户'); } if ($result) { $this->show_json(200, '保存成功'); } else { $this->show_json(400, '保存失败'); } } /** * Notes:新增/更新企微用户 * Created on: 2022/5/31 9:54 * Created by: dengbw * @param array $param * @param array $user */ private function set_wechatqy($param = [], $user = []) { if (!$param['userid'] || !$param['external_userid'] || !$user) { return; } $jsondata['external_contact'] = $user['external_contact'] ? $user['external_contact'] : ''; $jsondata['follow_info'] = $user['follow_info'] ? $user['follow_info'] : ''; $remark_mobiles = $user['follow_info']['remark_mobiles']; $mobile = ''; if ($remark_mobiles && $remark_mobiles != '[]' && is_array($remark_mobiles)) { $mobile = $remark_mobiles[0];//取第一个手机号 } $addData = array( 'brand_id' => $this->bid, 'external_userid' => $user['external_contact']['external_userid'], 'userid' => $user['follow_info']['userid'] ? $user['follow_info']['userid'] : '', 'name' => $user['external_contact']['name'], 'avatar' => $user['external_contact']['avatar'], 'unionid' => $user['external_contact']['unionid'] ? $user['external_contact']['unionid'] : '', 'remark' => $user['follow_info']['remark'] ? $user['follow_info']['remark'] : '', 'mobile' => $mobile, 'c_time' => $user['follow_info']['createtime'] ? $user['follow_info']['createtime'] : time(), 'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE), 'status' => 1, ); $re_qy = $this->mdWechatqy->get(['brand_id' => $this->bid, 'userid' => $param['userid'], 'external_userid' => $param['external_userid']]); if (!$re_qy) { $this->mdWechatqy->add($addData); } else { $this->mdWechatqy->update($addData, ['id' => $re_qy['id']]); } } /** * Notes:授权获取企业员工UserId * Created on: 2022/3/29 17:03 * Created by: dengbw * @param string $url * @param array $params * @return mixed */ 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); $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']]; $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'])]; } $ukey = liche_authcode(json_encode($session, JSON_UNESCAPED_UNICODE), 'ENCODE', $this->secret); set_cookie("ukey", $ukey, 86400 * 1); $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_privateinfo&state=STATE&agentid={$config['agentid']}#wechat_redirect"; redirect($auth_url); } } private function show_json($code, $msg, $info = []) { $data['code'] = $code; $data['msg'] = $msg; $data['data'] = $info; die(json_encode($data, JSON_UNESCAPED_UNICODE)); } }