123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use EasyWeChat\Factory;
- /**
- * 企业微信微信接口
- *
- * @author 刘相欣
- *
- * */
- class OpenWork extends Api{
-
- /**
- * 标签提取 /api/openwork/sys_event
- *
- * @param string $postText 话题
- *
- * */
- function extractTopics($postText) {
- // 正则表达式匹配话题标签,例如 #example #tag
- preg_match_all('/#([^\s#{}]+)/', $postText, $matches);
- return $matches;
- }
- /**
- * 系统事件回调URL /api/openwork/sys_event
- *
- * @param string $code 授权码
- *
- * */
- public function sys_event(){
- // 获取配置
- $app = Factory::openWork(config('wechat.openwork'));
- // 接收消息通知
- $app->server->push(function($message) {
- // 成功
- json_send(['code'=>'success','msg'=>'sys_event','data'=>$message]);
- return 'success';
- });
- $response = $app->server->serve();
- $response->send();
- }
- /**
- * 数据回调URL /api/openwork/call_data
- *
- * @param string $code 授权码
- *
- * */
- public function call_data(){
- // 获取配置
- $app = Factory::openWork(config('wechat.openwork'));
- // 接收消息通知
- $app->server->push(function($message) {
- // 成功
- return 'success';
- });
- $response = $app->server->serve();
- $response->send();
- }
-
- /**
- * 指令回调URL /api/openwork/call_command
- *
- * @param string $code 授权码
- *
- * */
- public function call_command(){
- // 获取配置
- $app = Factory::openWork(config('wechat.openwork'));
- // 接收消息通知
- $app->server->push(function($message) use($app) {
- try {
- // 指令回调才有此字段
- if (isset($message['InfoType'])) {
- // 第三方应用凭证更新
- if( $message['InfoType'] == 'suite_ticket' ) return 'success';
- // 授权成功通知
- if( $message['InfoType'] == 'create_auth' ) {
- // 获取企业永久授权码
- $message['PermanentByCode'] = $app->corp->getPermanentByCode($message['AuthCode']); //传入临时授权码
- // 此处务必返回的是,字符串success
- return 'success';
- }
- // 变更授权通知
- if( $message['InfoType'] == 'cancel_auth' ) return json_send(['code'=>'success','msg'=>'变更授权通知','data'=>$message]);
- // 通讯录事件通知 (部门新增,变更,删除)
- if( $message['InfoType'] == 'change_contact' ) return json_send(['code'=>'success','msg'=>'通讯录事件通知','data'=>$message]);
- // 企业客户事件 添加,编辑,删除,接替失败
- if( $message['InfoType'] == 'change_external_contact') return json_send(['code'=>'success','msg'=>'企业客户事件','data'=>$message]);
- // 客户群事件 创建,变更,解散(群名变更,群成员增加或移除,群公告变更)
- if( $message['InfoType'] == 'change_external_chat') return json_send(['code'=>'success','msg'=>'客户群事件','data'=>$message]);
- // 标签事件,创建,变更,删除,重排
- if( $message['InfoType'] == 'change_external_tag') return json_send(['code'=>'success','msg'=>'标签事件','data'=>$message]);
- }
- } catch (\Throwable $th) {
- // 成功
- return json_send(['code'=>'error','msg'=>'notify','data'=>['message'=>$message,'msg'=>$th->getMessage()]]);
- }
- });
- $response = $app->server->serve();
- $response->send();
- }
- public function get_user(){
- // 获取配置
- $app = Factory::openWork(config('wechat.openwork'));
- $authCorpId = 'wwcdbc686326b51a89';
- $permanentCode = 'TDcttQX6Xe1UaJW9nd5tVNJy5RlCoqZNbgc6i0vGE9w';
- //
- $work = $app->work($authCorpId,$permanentCode);
-
- $reuslt = $work->user->getDetailedDepartmentUsers(1,true);
- dd($reuslt);
- }
- // 从服务商网站发起 企业授权应用
- /**
- * /api/openwork/install
- */
- public function install(){
- // 授权码
- $authCode = request('auth_code','');
- // 获取配置
- $app = Factory::openWork(config('wechat.openwork'));
- if( !$authCode ) {
- // 获取预授权码
- $result = $app->corp->getPreAuthCode();
- // 预授权码
- $preAuthCode = $result['pre_auth_code'];
- // 设置授权配置
- $app->corp->setSession($preAuthCode,['auth_type'=>1]);
- // 构造第三方oauth2链接
- $redirectUri = url('api/openwork/install');
- // 引导用户进入授权页
- $url = $app->corp->getPreAuthorizationUrl($preAuthCode,$redirectUri);
- return redirect($url);
- }else{
- // 获取永久授权码
- $permanentCode = $app->corp->getPermanentByCode($authCode); //传入临时授权码
- dd($permanentCode);
- // 获取授权信息
- $authInfo = $app->corp->getAuthorization($permanentCode[''],$permanentCode['']);
- dd($authInfo);
- }
- }
- /**
- * /api/openwork/oauth_url
- */
- public function oauth_url(){
- // 编码
- $code = request('code','');
- $authCorpId = 'wwcdbc686326b51a89';
- $permanentCode = 'TDcttQX6Xe1UaJW9nd5tVNJy5RlCoqZNbgc6i0vGE9w';
- // 获取配置
- $app = Factory::openWork(config('wechat.openwork'));
- if( !$code ) {
- // 构造第三方oauth2链接
- $redirectUri = url('api/openwork/oauth_url');
- $scope = 'snsapi_privateinfo';
- // 路径
- $url = $app->corp->getOAuthRedirectUrl($redirectUri,$scope);
- return redirect($url);
- }else{
- // 第三方根据code获取企业成员信息
- $result = $app->corp->getUserByCode($code);
- // 第三方使用user_ticket获取成员详情
- $result = $app->corp->getUserByTicket($result['user_ticket']);
- dd($result);
- }
-
-
- dd($reuslt);
- }
- // /api/openwork/uni_login
- public function uni_login(){
- // 获取配置
- $mini = Factory::miniProgram(config('wechat.mini'));
- $code = request('code','');
- $result = $mini->auth->session($code);
- // 获取结果
- $unionid = $result['unionid'];
- $openid = $result['openid'];
- //
- $app = Factory::openWork(config('wechat.openwork'));
- $authCorpId = 'wwcdbc686326b51a89';
- $result = $app->corp->unionidToExternalUserid($unionid,$openid,$authCorpId);
- $extUserId = $result['external_userid_info'][0]['external_userid'];
- $corpId = $result['external_userid_info'][0]['corpid'];
- dd($result,$extUserId,$corpId);
- }
- }
|