first commit
This commit is contained in:
150
modules/Wechat/app/Controllers/Api/Index.php
Normal file
150
modules/Wechat/app/Controllers/Api/Index.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Controllers\Api;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use Modules\Wechat\Services\OauthService;
|
||||
use Modules\Wechat\Services\WechatService;
|
||||
|
||||
class Index extends BaseController {
|
||||
|
||||
/**
|
||||
* 获取微信用户授权跳转
|
||||
*/
|
||||
public function oauth(Request $request, OauthService $service){
|
||||
if($request->filled('code')){
|
||||
if($request->filled('url')){
|
||||
return redirect($request->input('url') . '?code=' . $request->input('code', ''));
|
||||
}
|
||||
}else{
|
||||
$res = $service->oauth($request);
|
||||
return redirect($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 微信登录
|
||||
*
|
||||
* @param Request $request
|
||||
* @param OauthService $service
|
||||
* @return void
|
||||
*/
|
||||
public function login(Request $request, OauthService $service){
|
||||
$type = $request->input('type', 'wechat');
|
||||
if($request->filled('code')){
|
||||
$info = [];
|
||||
switch ($type) {
|
||||
case 'wechat':
|
||||
try {
|
||||
$info = $service->wechatLogin($request->input('code', ''));
|
||||
} catch (\Throwable $th) {
|
||||
$this->data['message'] = $th->getMessage();
|
||||
$this->data['code'] = 0;
|
||||
return $this->data;
|
||||
}
|
||||
break;
|
||||
case 'miniapp':
|
||||
try {
|
||||
$info = $service->miniappLogin($request);
|
||||
} catch (\Throwable $th) {
|
||||
$this->data['message'] = $th->getMessage();
|
||||
$this->data['code'] = 0;
|
||||
return $this->data;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->data['message'] = "非法操作!";
|
||||
$this->data['code'] = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if(isset($info['member_id']) && $info['member_id']){
|
||||
$token = auth('api')->tokenById($info['member_id']);
|
||||
if($token){
|
||||
$this->data['data'] = [
|
||||
'access_token' => $token,
|
||||
'token_type' => 'bearer',
|
||||
'expires_in' => auth('api')->factory()->getTTL() * 60
|
||||
];
|
||||
}elseif(isset($info['openid']) && $info['openid']){
|
||||
$this->data['data'] = $info;
|
||||
$this->data['message'] = "初次登录未绑定用户,请先绑定用户,或注册新用户绑定!";
|
||||
$this->data['code'] = 100;
|
||||
}else{
|
||||
$this->data['message'] = "登录失败!";
|
||||
$this->data['code'] = 0;
|
||||
}
|
||||
}else{
|
||||
if(isset($info['openid']) && $info['openid']){
|
||||
$this->data['data'] = $info;
|
||||
$this->data['message'] = "初次登录未绑定用户,请先绑定用户,或注册新用户绑定!";
|
||||
$this->data['code'] = 100;
|
||||
}else{
|
||||
$this->data['message'] = "登录失败!";
|
||||
$this->data['code'] = 0;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$this->data['message'] = "非法操作!";
|
||||
$this->data['code'] = 0;
|
||||
}
|
||||
return response()->json($this->data);
|
||||
}
|
||||
/**
|
||||
* @title 微信公众号验证
|
||||
*
|
||||
* @param OauthService $service
|
||||
* @return void
|
||||
*/
|
||||
public function serve(OauthService $service){
|
||||
return $service->WechatServe();
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取微信jssdk配置
|
||||
*
|
||||
* @param WechatService $service
|
||||
* @return void
|
||||
*/
|
||||
public function jssdk(OauthService $service){
|
||||
try {
|
||||
$this->data['data'] = $service->getJsSdk($this->request);
|
||||
} catch (\think\Exception $e) {
|
||||
$this->data['message'] = $e->getMessage();
|
||||
$this->data['code'] = 0;
|
||||
}
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function invitecode(Request $request, WechatService $service){
|
||||
try {
|
||||
$this->data['data'] = $service->getInviteCode($request);
|
||||
} catch (\think\Exception $e) {
|
||||
$this->data['message'] = $e->getMessage();
|
||||
$this->data['code'] = 0;
|
||||
}
|
||||
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function qrcode(Request $request, WechatService $service){
|
||||
try {
|
||||
$request->mergeIfMissing([
|
||||
'doctor_id' => auth('doctor')->id(),
|
||||
]);
|
||||
$this->data['data'] = $service->getSingleQrcode($request);
|
||||
} catch (\think\Exception $e) {
|
||||
$this->data['message'] = $e->getMessage();
|
||||
$this->data['code'] = 0;
|
||||
}
|
||||
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
33
modules/Wechat/app/Listeners/LoginBind.php
Normal file
33
modules/Wechat/app/Listeners/LoginBind.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Listeners;
|
||||
|
||||
use Modules\Member\Events\LoginEvent;
|
||||
use Modules\Wechat\Models\MemberSocial;
|
||||
|
||||
class LoginBind {
|
||||
|
||||
/**
|
||||
* @title 会员登录后更新用户信息
|
||||
*
|
||||
* @param LoginEvent $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(LoginEvent $event) {
|
||||
$member = $event->member;
|
||||
$openid = $event->openid;
|
||||
$type = $event->type;
|
||||
|
||||
$social = MemberSocial::where('openid', $openid)->where('type', $type)->first();
|
||||
if ($social && $social->member_id == 0) {
|
||||
$social->member_id = $member->uid;
|
||||
$social->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
33
modules/Wechat/app/Listeners/RegisterBind.php
Normal file
33
modules/Wechat/app/Listeners/RegisterBind.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Listeners;
|
||||
|
||||
use Modules\Member\Events\Registered;
|
||||
use Modules\Wechat\Models\MemberSocial;
|
||||
|
||||
class RegisterBind {
|
||||
|
||||
/**
|
||||
* @title 会员登录后更新用户信息
|
||||
*
|
||||
* @param LoginEvent $event
|
||||
* @return void
|
||||
*/
|
||||
public function handle(Registered $event) {
|
||||
$member = $event->member;
|
||||
$openid = $event->openid;
|
||||
$type = $event->type;
|
||||
|
||||
$social = MemberSocial::where('openid', $openid)->where('type', $type)->first();
|
||||
if ($social && $social->member_id == 0) {
|
||||
$social->member_id = $member->uid;
|
||||
$social->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
18
modules/Wechat/app/Models/MemberSocial.php
Normal file
18
modules/Wechat/app/Models/MemberSocial.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class MemberSocial extends BaseModel {
|
||||
|
||||
protected $table = 'member_social';
|
||||
protected $fillable = ['nickname', 'member_id', 'type', 'gender', 'openid', 'avatar', 'county', 'province', 'city', 'language', 'unionid'];
|
||||
// protected $hidden = ['deleted_at'];
|
||||
}
|
||||
0
modules/Wechat/app/Providers/.gitkeep
Normal file
0
modules/Wechat/app/Providers/.gitkeep
Normal file
45
modules/Wechat/app/Providers/EventServiceProvider.php
Normal file
45
modules/Wechat/app/Providers/EventServiceProvider.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Providers;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
class EventServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* The event handler mappings for the application.
|
||||
*
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
protected $listen = [
|
||||
'Modules\Member\Events\LoginEvent' => [
|
||||
'Modules\Wechat\Listeners\LoginBind',
|
||||
],
|
||||
'Modules\Member\Events\Registered' => [
|
||||
'Modules\Wechat\Listeners\RegisterBind',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* Indicates if events should be discovered.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected static $shouldDiscoverEvents = true;
|
||||
|
||||
/**
|
||||
* Configure the proper event listeners for email verification.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function configureEmailVerification(): void
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
67
modules/Wechat/app/Providers/RouteServiceProvider.php
Normal file
67
modules/Wechat/app/Providers/RouteServiceProvider.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
||||
class RouteServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Called before routes are registered.
|
||||
*
|
||||
* Register any model bindings or pattern based filters.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the routes for the application.
|
||||
*/
|
||||
public function map(): void
|
||||
{
|
||||
$this->mapApiRoutes();
|
||||
|
||||
$this->mapWebRoutes();
|
||||
|
||||
$this->mapAdminRoutes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "web" routes for the application.
|
||||
*
|
||||
* These routes all receive session state, CSRF protection, etc.
|
||||
*/
|
||||
protected function mapWebRoutes(): void
|
||||
{
|
||||
Route::middleware('web')->group(module_path('Wechat', '/routes/web.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapApiRoutes(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('api')->name('api.')->group(module_path('Wechat', '/routes/api.php'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the "api" routes for the application.
|
||||
*
|
||||
* These routes are typically stateless.
|
||||
*/
|
||||
protected function mapAdminRoutes(): void
|
||||
{
|
||||
Route::middleware('api')->prefix('admin')->name('admin.')->group(module_path('Wechat', '/routes/admin.php'));
|
||||
}
|
||||
}
|
||||
120
modules/Wechat/app/Providers/WechatServiceProvider.php
Normal file
120
modules/Wechat/app/Providers/WechatServiceProvider.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Wechat\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class WechatServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Wechat';
|
||||
|
||||
protected string $moduleNameLower = 'wechat';
|
||||
|
||||
/**
|
||||
* Boot the application events.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerCommands();
|
||||
$this->registerCommandSchedules();
|
||||
$this->registerTranslations();
|
||||
$this->registerConfig();
|
||||
$this->registerViews();
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(EventServiceProvider::class);
|
||||
$this->app->register(RouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register commands in the format of Command::class
|
||||
*/
|
||||
protected function registerCommands(): void
|
||||
{
|
||||
// $this->commands([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register command Schedules.
|
||||
*/
|
||||
protected function registerCommandSchedules(): void
|
||||
{
|
||||
// $this->app->booted(function () {
|
||||
// $schedule = $this->app->make(Schedule::class);
|
||||
// $schedule->command('inspire')->hourly();
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Register translations.
|
||||
*/
|
||||
public function registerTranslations(): void
|
||||
{
|
||||
$langPath = resource_path('lang/modules/'.$this->moduleNameLower);
|
||||
|
||||
if (is_dir($langPath)) {
|
||||
$this->loadTranslationsFrom($langPath, $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
} else {
|
||||
$this->loadTranslationsFrom(module_path($this->moduleName, 'lang'), $this->moduleNameLower);
|
||||
$this->loadJsonTranslationsFrom(module_path($this->moduleName, 'lang'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register config.
|
||||
*/
|
||||
protected function registerConfig(): void
|
||||
{
|
||||
$this->publishes([module_path($this->moduleName, 'config/config.php') => config_path($this->moduleNameLower.'.php')], 'config');
|
||||
$this->mergeConfigFrom(module_path($this->moduleName, 'config/config.php'), $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register views.
|
||||
*/
|
||||
public function registerViews(): void
|
||||
{
|
||||
$viewPath = resource_path('views/modules/'.$this->moduleNameLower);
|
||||
$sourcePath = module_path($this->moduleName, 'resources/views');
|
||||
|
||||
$this->publishes([$sourcePath => $viewPath], ['views', $this->moduleNameLower.'-module-views']);
|
||||
|
||||
$this->loadViewsFrom(array_merge($this->getPublishableViewPaths(), [$sourcePath]), $this->moduleNameLower);
|
||||
|
||||
$componentNamespace = str_replace('/', '\\', config('modules.namespace').'\\'.$this->moduleName.'\\'.ltrim(config('modules.paths.generator.component-class.path'), config('modules.paths.app_folder', '')));
|
||||
Blade::componentNamespace($componentNamespace, $this->moduleNameLower);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public function provides(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
private function getPublishableViewPaths(): array
|
||||
{
|
||||
$paths = [];
|
||||
foreach (config('view.paths') as $path) {
|
||||
if (is_dir($path.'/modules/'.$this->moduleNameLower)) {
|
||||
$paths[] = $path.'/modules/'.$this->moduleNameLower;
|
||||
}
|
||||
}
|
||||
|
||||
return $paths;
|
||||
}
|
||||
}
|
||||
38
modules/Wechat/app/Services/MessageService.php
Normal file
38
modules/Wechat/app/Services/MessageService.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use EasyWeChat\OfficialAccount\Application;
|
||||
|
||||
class MessageService {
|
||||
/**
|
||||
* 发送模板消息
|
||||
* @param $openid
|
||||
* @param $template_id
|
||||
* @param $url
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function sendMessage($openid, $template_id, $url, $data){
|
||||
$config = Config::get('wechat.wx');
|
||||
$app = new Application($config);
|
||||
$client = $app->getClient();
|
||||
|
||||
$result = $client->postJson('/cgi-bin/message/template/send', [
|
||||
'touser' => $openid,
|
||||
'template_id' => $template_id,
|
||||
'page' => $url,
|
||||
'data' => $data,
|
||||
'miniprogram_state' => 'formal',
|
||||
'lang' => 'zh_CN',
|
||||
]);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
179
modules/Wechat/app/Services/OauthService.php
Normal file
179
modules/Wechat/app/Services/OauthService.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use EasyWeChat\OfficialAccount\Application;
|
||||
use EasyWeChat\MiniApp\Application as MiniApp;
|
||||
use Modules\Wechat\Models\MemberSocial;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class OauthService {
|
||||
|
||||
/**
|
||||
* @title 微信授权登录
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function oauth($request){
|
||||
$config = Config::get('wechat.wx');
|
||||
$url = $request->fullUrl();
|
||||
$app = new Application($config);
|
||||
try {
|
||||
//获取openid
|
||||
$oauth = $app->getOAuth();
|
||||
$redirect = $oauth->scopes(['snsapi_userinfo'])->redirect($url);
|
||||
return $redirect;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage(), 100);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 微信用户登录
|
||||
*
|
||||
* @param [type] $code
|
||||
* @return void
|
||||
*/
|
||||
public function wechatLogin($code){
|
||||
$config = Config::get('wechat.wx');
|
||||
|
||||
$app = new Application($config);
|
||||
try {
|
||||
//获取openid
|
||||
$oauth = $app->getOAuth();
|
||||
$user = $oauth->userFromCode($code);
|
||||
$userinfo = $user->toArray();
|
||||
|
||||
$social = MemberSocial::where('openid', '=', $userinfo['id'])->where('type', '=', 'wechat')->first();
|
||||
if(!$social){
|
||||
$data = [
|
||||
'type' => 'wechat',
|
||||
'member_id' => 0,
|
||||
'openid' => isset($userinfo['id']) ? $userinfo['id'] : '',
|
||||
'nickname' => isset($userinfo['nickname']) ? $userinfo['nickname'] : '',
|
||||
'avatar' => isset($userinfo['avatar']) ? $userinfo['avatar'] : '',
|
||||
'gender' => isset($userinfo['gender']) ? $userinfo['gender'] : '',
|
||||
];
|
||||
$social = MemberSocial::create($data);
|
||||
}
|
||||
return $social;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage(), 100);
|
||||
}
|
||||
}
|
||||
|
||||
public function miniappLogin($request){
|
||||
$config = Config::get('wechat.miniapp');
|
||||
|
||||
$app = new MiniApp($config);
|
||||
try {
|
||||
//获取openid
|
||||
$utils = $app->getUtils();
|
||||
$session = $utils->codeToSession($request->input('code'));
|
||||
|
||||
$social = MemberSocial::where('openid', '=', $session['openid'])->where('type', '=', 'miniapp')->first();
|
||||
if(!$social){
|
||||
if($request->filled('iv') && $request->filled('encryptedData')){
|
||||
$userinfo = $utils->decryptSession($session['session_key'], $request->input('iv'), $request->input('encryptedData'));
|
||||
}else{
|
||||
$userinfo = ['nickName' => '微信用户' . Str::substr($session['openid'], -7), 'avatarUrl' => '', 'gender' => 1];
|
||||
}
|
||||
$data = [
|
||||
'type' => 'miniapp',
|
||||
'member_id' => 0,
|
||||
'openid' => isset($session['openid']) ? $session['openid'] : '',
|
||||
'unionid' => isset($session['unionid']) ? $session['unionid'] : '',
|
||||
'nickname' => $userinfo['nickName'] ? $userinfo['nickName'] : '',
|
||||
'avatar' => isset($userinfo['avatarUrl']) ? $userinfo['avatarUrl'] : '',
|
||||
'gender' => isset($userinfo['gender']) ? $userinfo['gender'] : '',
|
||||
];
|
||||
$social = MemberSocial::create($data);
|
||||
}
|
||||
return $social;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage(), 100);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取微信JS-SDK配置
|
||||
*
|
||||
* @param [type] $request
|
||||
* @return void
|
||||
*/
|
||||
public function getJsSdk($request){
|
||||
$config = Config::get('wechat.wx');
|
||||
$url = $request->input('url', '');
|
||||
$url = $url ? urldecode($url) : $request->url(true);
|
||||
$app = new Application($config);
|
||||
try {
|
||||
$utils = $app->getUtils();
|
||||
$config = $utils->buildJsSdkConfig(
|
||||
url: $url,
|
||||
jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'scanQRCode', 'closeWindow', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'openAddress'],
|
||||
openTagList: [],
|
||||
debug: false,
|
||||
);
|
||||
return $config;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception($e->getMessage(), 100);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 微信公众号服务
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function WechatServe(){
|
||||
$config = Config::get('wechat.wx');
|
||||
$app = new Application($config);
|
||||
$server = $app->getServer();
|
||||
|
||||
|
||||
$server->with(function($message, \Closure $next){
|
||||
if ($message->MsgType === 'text') {
|
||||
return [
|
||||
'MsgType' => 'text',
|
||||
'Content' => '暂未开通自动回复功能!'
|
||||
];
|
||||
}
|
||||
return $next($message);
|
||||
});
|
||||
$server->addEventListener('subscribe', function() {
|
||||
return '欢迎!!';
|
||||
});
|
||||
$server->addEventListener('unsubscribe', function() {
|
||||
return '再见~';
|
||||
});
|
||||
return $server->serve();
|
||||
}
|
||||
|
||||
/**
|
||||
* @title 获取微信用户信息
|
||||
*
|
||||
* @param [type] $openid
|
||||
* @return void
|
||||
*/
|
||||
public function getWechatInfo($openid){
|
||||
$config = Config::get('wechat.wx');
|
||||
$app = new Application($config);
|
||||
$api = $app->getClient();
|
||||
|
||||
$userinfo = $api->post('cgi-bin/user/info', [
|
||||
'json' => [
|
||||
'openid' => $openid,
|
||||
'lang' => 'zh_CN',
|
||||
]
|
||||
]);
|
||||
return $userinfo;
|
||||
}
|
||||
}
|
||||
74
modules/Wechat/app/Services/WechatService.php
Normal file
74
modules/Wechat/app/Services/WechatService.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace Modules\Wechat\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use EasyWeChat\MiniApp\Application;
|
||||
|
||||
class WechatService {
|
||||
public function getSingleQrcode($request){
|
||||
$config = config('wechat.miniapp');
|
||||
|
||||
$doctor_id = $request->input('doctor_id');
|
||||
$refresh = $request->input('refresh', 0);
|
||||
$pic_name = md5('doctor_' . $doctor_id);
|
||||
$path = "qrcode/{$pic_name}.png";
|
||||
if (Storage::disk('public')->exists($path) && !$refresh) {
|
||||
return Storage::disk('public')->url($path);
|
||||
}
|
||||
|
||||
$app = new Application($config);
|
||||
$client = $app->getClient();
|
||||
|
||||
$response = $client->postJson('/wxa/getwxacodeunlimit', [
|
||||
'scene' => $doctor_id,
|
||||
'page' => 'pages/health/patient/form',
|
||||
'width' => 430,
|
||||
'check_path' => false,
|
||||
// 'env_version' => $config['env_version'],
|
||||
]);
|
||||
|
||||
if ($response->isFailed()) {
|
||||
throw new \Exception($response->getContent(), $response->getStatusCode());
|
||||
}else{
|
||||
Storage::disk('public')->put($path, $response->toStream());
|
||||
return Storage::disk('public')->url($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function getInviteCode($request){
|
||||
$config = Config::get('wechat.miniapp');
|
||||
|
||||
$app = new Application($config);
|
||||
$refresh = $request->input('refresh', 0);
|
||||
$pic_name = md5('invite_code_' . $request->input('uid', ''));
|
||||
$path = "qrcode/{$pic_name}.png";
|
||||
if (Storage::disk('public')->exists($path) && !$refresh) {
|
||||
return Storage::disk('public')->url($path);
|
||||
}
|
||||
|
||||
$client = $app->getClient();
|
||||
|
||||
$response = $client->postJson('/wxa/getwxacodeunlimit', [
|
||||
'scene' => 'invite_uid=' . $request->input('uid', ''),
|
||||
'page' => 'pages/ucenter/login/index',
|
||||
'width' => 430,
|
||||
'check_path' => false,
|
||||
// 'env_version' => $config['env_version'],
|
||||
]);
|
||||
|
||||
if ($response->isFailed()) {
|
||||
throw new \Exception($response->getContent(), $response->getStatusCode());
|
||||
}else{
|
||||
Storage::disk('public')->put($path, $response->toStream());
|
||||
return Storage::disk('public')->url($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user