Quellcode durchsuchen

【Add】企微服务商

liuxiangxin vor 5 Monaten
Ursprung
Commit
b7e919b17c

+ 3 - 2
app/Http/Controllers/Admin/Orders.php

@@ -18,6 +18,7 @@ use PhpOffice\PhpSpreadsheet\Style\Alignment;
 use PhpOffice\PhpSpreadsheet\Style\Fill;
 use Intervention\Image\Facades\Image;
 use Intervention\Image\Gd\Font;
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
 
 /**
  * 订单管理
@@ -444,9 +445,9 @@ class Orders extends Auth{
 			// 单元格内容写入
 			$sheet->setCellValue('A'.$row, $value['order_id']);
 			$sheet->setCellValue('B'.$row, $value['custom_uid']);
-			$sheet->setCellValue('C'.$row, $value['custom_name']);
+			$sheet->setCellValueExplicit('C'.$row, $value['custom_name'],DataType::TYPE_STRING);
 			$sheet->setCellValue('D'.$row, $value['status']);
-			$sheet->setCellValue('E'.$row, $value['contact_name']);
+			$sheet->setCellValueExplicit('E'.$row, $value['contact_name'],DataType::TYPE_STRING);
 			$sheet->setCellValue('F'.$row, $value['contact_phone']);
 			$sheet->setCellValue('G'.$row, $value['contact_province']);
 			$sheet->setCellValue('H'.$row, $value['contact_city']);

+ 76 - 0
app/Http/Controllers/Api/OpenPlatform.php

@@ -0,0 +1,76 @@
+<?php namespace App\Http\Controllers\Api;
+
+use App\Http\Controllers\Api\Api;
+use EasyWeChat\Factory;
+use EasyWeChat\OpenPlatform\Server\Guard;
+
+/**
+ * 微信开放平台,第三方平台
+ * 
+ * @author 刘相欣
+ * 
+ * */
+class OpenPlatform extends Api{
+	
+	/**
+	 * 接收授权事件			api/openplatform/authorized_callback
+	 * 用于接收平台推送给第三方平台账号的消息与事件,如授权事件通知、component_verify_ticket等
+	 *
+	 */
+	public function authorized_callback(){
+
+		$openPlatform = Factory::openPlatform(config('wechat.openplatform'));
+
+		$server = $openPlatform->server;
+
+		// 处理授权成功事件
+		$server->push(function ($message) {
+			json_send(['code'=>'success','msg'=>'EVENT_AUTHORIZED','data'=>$message]);
+		}, Guard::EVENT_AUTHORIZED);
+
+		// 处理授权更新事件
+		$server->push(function ($message) {
+			json_send(['code'=>'success','msg'=>'EVENT_UPDATE_AUTHORIZED','data'=>$message]);
+		}, Guard::EVENT_UPDATE_AUTHORIZED);
+
+		// 处理授权取消事件
+		$server->push(function ($message) {
+			json_send(['code'=>'success','msg'=>'EVENT_UNAUTHORIZED','data'=>$message]);
+		}, Guard::EVENT_UNAUTHORIZED);
+
+		return $server->serve();
+	}
+
+
+	/**
+	 * 接收授权事件		api/openplatform/callback
+	 * 
+	 * 用于接收平台推送给第三方平台账号的消息与事件,如授权事件通知、component_verify_ticket等
+	 *
+	 */
+	public function callback($appid){
+		json_send(['code'=>'success','msg'=>'EVENT_APPID','data'=>$appid]);
+	}
+
+
+	/**
+	 * 使用授权码换取接口调用凭据和授权信息		api/openplatform/auth_code
+	 *
+	 */
+	public function auth_code(){
+		// 授权码
+		$authCode 			= request('auth_code');
+		// 实例
+		$openPlatform		= Factory::openPlatform(config('wechat.openplatform'));
+		// 没有授权码
+		if( !$authCode )	{
+			// 获取预授权URL
+			$url = $openPlatform->getPreAuthorizationUrl(url('api/openplatform/auth_code'));
+			// 
+			return redirect($url);
+		}
+		// 使用授权码换取接口调用凭据和授权信息
+		$result 			= $openPlatform->handleAuthorize($authCode);
+		dd($result);
+	}
+}

+ 201 - 0
app/Http/Controllers/Api/OpenWork.php

@@ -0,0 +1,201 @@
+<?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);
+	}
+
+}

+ 19 - 0
config/wechat.php

@@ -20,5 +20,24 @@ return [
         // server config
         'token' => env('WECHAT_WORK_TOKEN', 'Efd61RPH3GzEwfEATRwdJVKhi'),
         'aes_key' => env('WECHAT_WORK_ENCODING_AES_KEY', 'XL1brptJzQeDMwp5nvuojb8YEDx9GXy9kxaQ8JQl2Zu'),
+    ],
+    'openwork'=>[
+        'corp_id'              => 'wwcdbc686326b51a89',
+        'secret'               => '25Qobn6WkDWGGWkhnvOCrPbSG-U2cryWh0G7bz-hJjhI4MeU2D2FyrPBgADqW6p4',
+        'suite_id'             => 'ww6ab67e7c449df032',
+        'suite_secret'         => 'cvPbU2pz0C9YlVVmm_WgNTEnnxBzMK36GrpAk51kUvc',
+        'token'                => 'Efd61RPH3GzEwfEATRwdJVKhi',
+        'aes_key'              => 'XL1brptJzQeDMwp5nvuojb8YEDx9GXy9kxaQ8JQl2Zu',
+        'reg_template_id'      => '',//注册定制化模板ID
+        'redirect_uri_install' => '', //(安装应用的回调url(可选) 
+        'redirect_uri_single'  => '', //单点登录回调url (可选)
+        'redirect_uri_oauth'   => '', //网页授权第三方回调url 
+        
+    ],
+    'openplatform'=>[
+        'app_id'               => 'wx31a0009c5aa482c4',
+        'secret'               => '7af436ed36f77405bacb7602d6549895',
+        'token'                => 'Efd61RPH3GzEwfEATRwdJVKhi',
+        'aes_key'              => 'XL1brptJzQeDMwp5nvuojb8YEDx9GXy9kxaQ8JQl2Zu',
     ]
 ];

+ 1 - 0
public/39PbAfnC88.txt

@@ -0,0 +1 @@
+9f5ffe91ad2d346b57d6729f678e8627

+ 0 - 0
public/订单.xlsx


Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 18
resources/views/welcome.blade.php


+ 31 - 1
routes/api.php

@@ -177,4 +177,34 @@ Route::any('weizan_orders/import',[\App\Http\Controllers\Api\WeiZan\Orders::clas
  * 订单回执上传
  * 
  * */
-Route::any('orders_receipt/apply',[\App\Http\Controllers\Api\Orders\Receipt::class,'apply']);
+Route::any('orders_receipt/apply',[\App\Http\Controllers\Api\Orders\Receipt::class,'apply']);
+
+
+/**
+ * 企微服务商通知回调
+ * 
+ * */
+// 数据回调
+Route::any('openwork/call_data',[\App\Http\Controllers\Api\OpenWork::class,'call_data']);
+// 指令回调
+Route::any('openwork/call_command',[\App\Http\Controllers\Api\OpenWork::class,'call_command']);
+
+// 指令回调
+Route::any('openwork/get_user',[\App\Http\Controllers\Api\OpenWork::class,'get_user']);
+// 指令回调
+Route::any('openwork/oauth_url',[\App\Http\Controllers\Api\OpenWork::class,'oauth_url']);
+// 指令回调
+Route::any('openwork/uni_login',[\App\Http\Controllers\Api\OpenWork::class,'uni_login']);
+// 指令回调
+Route::any('openwork/install',[\App\Http\Controllers\Api\OpenWork::class,'install']);
+
+
+
+// 回调配置
+Route::any('openplatform/authorized_callback',[\App\Http\Controllers\Api\OpenPlatform::class,'authorized_callback']);
+// 回调配置
+Route::any('openplatform/{appid}/callback',[\App\Http\Controllers\Api\OpenPlatform::class,'callback']);
+
+
+// 使用授权码换取接口调用凭据和授权信息
+Route::any('openplatform/auth_code',[\App\Http\Controllers\Api\OpenPlatform::class,'auth_code']);

+ 5 - 0
routes/web.php

@@ -14,6 +14,11 @@ use Illuminate\Support\Facades\Route;
 |
 */
 
+/* 首页 */
+Route::any('/',function(){
+    return view('welcome');
+});
+
 /* 后台 */
 Route::middleware('admin')->prefix('admin')->group(function(){
     /* 首页 */

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.