first commit

This commit is contained in:
2026-01-18 09:52:48 +08:00
commit 836bdc9409
584 changed files with 40891 additions and 0 deletions
@@ -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;
}
}
@@ -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;
}
}
@@ -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);
}
}
}