OpenWork.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use EasyWeChat\Factory;
  4. /**
  5. * 企业微信微信接口
  6. *
  7. * @author 刘相欣
  8. *
  9. * */
  10. class OpenWork extends Api{
  11. /**
  12. * 标签提取 /api/openwork/sys_event
  13. *
  14. * @param string $postText 话题
  15. *
  16. * */
  17. function extractTopics($postText) {
  18. // 正则表达式匹配话题标签,例如 #example #tag
  19. preg_match_all('/#([^\s#{}]+)/', $postText, $matches);
  20. return $matches;
  21. }
  22. /**
  23. * 系统事件回调URL /api/openwork/sys_event
  24. *
  25. * @param string $code 授权码
  26. *
  27. * */
  28. public function sys_event(){
  29. // 获取配置
  30. $app = Factory::openWork(config('wechat.openwork'));
  31. // 接收消息通知
  32. $app->server->push(function($message) {
  33. // 成功
  34. json_send(['code'=>'success','msg'=>'sys_event','data'=>$message]);
  35. return 'success';
  36. });
  37. $response = $app->server->serve();
  38. $response->send();
  39. }
  40. /**
  41. * 数据回调URL /api/openwork/call_data
  42. *
  43. * @param string $code 授权码
  44. *
  45. * */
  46. public function call_data(){
  47. // 获取配置
  48. $app = Factory::openWork(config('wechat.openwork'));
  49. // 接收消息通知
  50. $app->server->push(function($message) {
  51. // 成功
  52. return 'success';
  53. });
  54. $response = $app->server->serve();
  55. $response->send();
  56. }
  57. /**
  58. * 指令回调URL /api/openwork/call_command
  59. *
  60. * @param string $code 授权码
  61. *
  62. * */
  63. public function call_command(){
  64. // 获取配置
  65. $app = Factory::openWork(config('wechat.openwork'));
  66. // 接收消息通知
  67. $app->server->push(function($message) use($app) {
  68. try {
  69. // 指令回调才有此字段
  70. if (isset($message['InfoType'])) {
  71. // 第三方应用凭证更新
  72. if( $message['InfoType'] == 'suite_ticket' ) return 'success';
  73. // 授权成功通知
  74. if( $message['InfoType'] == 'create_auth' ) {
  75. // 获取企业永久授权码
  76. $message['PermanentByCode'] = $app->corp->getPermanentByCode($message['AuthCode']); //传入临时授权码
  77. // 此处务必返回的是,字符串success
  78. return 'success';
  79. }
  80. // 变更授权通知
  81. if( $message['InfoType'] == 'cancel_auth' ) return json_send(['code'=>'success','msg'=>'变更授权通知','data'=>$message]);
  82. // 通讯录事件通知 (部门新增,变更,删除)
  83. if( $message['InfoType'] == 'change_contact' ) return json_send(['code'=>'success','msg'=>'通讯录事件通知','data'=>$message]);
  84. // 企业客户事件 添加,编辑,删除,接替失败
  85. if( $message['InfoType'] == 'change_external_contact') return json_send(['code'=>'success','msg'=>'企业客户事件','data'=>$message]);
  86. // 客户群事件 创建,变更,解散(群名变更,群成员增加或移除,群公告变更)
  87. if( $message['InfoType'] == 'change_external_chat') return json_send(['code'=>'success','msg'=>'客户群事件','data'=>$message]);
  88. // 标签事件,创建,变更,删除,重排
  89. if( $message['InfoType'] == 'change_external_tag') return json_send(['code'=>'success','msg'=>'标签事件','data'=>$message]);
  90. }
  91. } catch (\Throwable $th) {
  92. // 成功
  93. return json_send(['code'=>'error','msg'=>'notify','data'=>['message'=>$message,'msg'=>$th->getMessage()]]);
  94. }
  95. });
  96. $response = $app->server->serve();
  97. $response->send();
  98. }
  99. public function get_user(){
  100. // 获取配置
  101. $app = Factory::openWork(config('wechat.openwork'));
  102. $authCorpId = 'wwcdbc686326b51a89';
  103. $permanentCode = 'TDcttQX6Xe1UaJW9nd5tVNJy5RlCoqZNbgc6i0vGE9w';
  104. //
  105. $work = $app->work($authCorpId,$permanentCode);
  106. $reuslt = $work->user->getDetailedDepartmentUsers(1,true);
  107. dd($reuslt);
  108. }
  109. // 从服务商网站发起 企业授权应用
  110. /**
  111. * /api/openwork/install
  112. */
  113. public function install(){
  114. // 授权码
  115. $authCode = request('auth_code','');
  116. // 获取配置
  117. $app = Factory::openWork(config('wechat.openwork'));
  118. if( !$authCode ) {
  119. // 获取预授权码
  120. $result = $app->corp->getPreAuthCode();
  121. // 预授权码
  122. $preAuthCode = $result['pre_auth_code'];
  123. // 设置授权配置
  124. $app->corp->setSession($preAuthCode,['auth_type'=>1]);
  125. // 构造第三方oauth2链接
  126. $redirectUri = url('api/openwork/install');
  127. // 引导用户进入授权页
  128. $url = $app->corp->getPreAuthorizationUrl($preAuthCode,$redirectUri);
  129. return redirect($url);
  130. }else{
  131. // 获取永久授权码
  132. $permanentCode = $app->corp->getPermanentByCode($authCode); //传入临时授权码
  133. dd($permanentCode);
  134. // 获取授权信息
  135. $authInfo = $app->corp->getAuthorization($permanentCode[''],$permanentCode['']);
  136. dd($authInfo);
  137. }
  138. }
  139. /**
  140. * /api/openwork/oauth_url
  141. */
  142. public function oauth_url(){
  143. // 编码
  144. $code = request('code','');
  145. $authCorpId = 'wwcdbc686326b51a89';
  146. $permanentCode = 'TDcttQX6Xe1UaJW9nd5tVNJy5RlCoqZNbgc6i0vGE9w';
  147. // 获取配置
  148. $app = Factory::openWork(config('wechat.openwork'));
  149. if( !$code ) {
  150. // 构造第三方oauth2链接
  151. $redirectUri = url('api/openwork/oauth_url');
  152. $scope = 'snsapi_privateinfo';
  153. // 路径
  154. $url = $app->corp->getOAuthRedirectUrl($redirectUri,$scope);
  155. return redirect($url);
  156. }else{
  157. // 第三方根据code获取企业成员信息
  158. $result = $app->corp->getUserByCode($code);
  159. // 第三方使用user_ticket获取成员详情
  160. $result = $app->corp->getUserByTicket($result['user_ticket']);
  161. dd($result);
  162. }
  163. dd($reuslt);
  164. }
  165. // /api/openwork/uni_login
  166. public function uni_login(){
  167. // 获取配置
  168. $mini = Factory::miniProgram(config('wechat.mini'));
  169. $code = request('code','');
  170. $result = $mini->auth->session($code);
  171. // 获取结果
  172. $unionid = $result['unionid'];
  173. $openid = $result['openid'];
  174. //
  175. $app = Factory::openWork(config('wechat.openwork'));
  176. $authCorpId = 'wwcdbc686326b51a89';
  177. $result = $app->corp->unionidToExternalUserid($unionid,$openid,$authCorpId);
  178. $extUserId = $result['external_userid_info'][0]['external_userid'];
  179. $corpId = $result['external_userid_info'][0]['corpid'];
  180. dd($result,$extUserId,$corpId);
  181. }
  182. }