初始上传
This commit is contained in:
92
addon/wechat/api/controller/Auth.php
Executable file
92
addon/wechat/api/controller/Auth.php
Executable file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\api\controller;
|
||||
|
||||
use addon\wechat\model\Material as MaterialModel;
|
||||
use addon\wechat\model\Wechat as WechatModel;
|
||||
use app\Controller;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Log;
|
||||
|
||||
class Auth extends Controller
|
||||
{
|
||||
|
||||
public $wechat;
|
||||
public $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$site_id = request()->siteid();
|
||||
$this->wechat = new WechatModel($site_id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ************************************************************************微信公众号消息相关方法 开始******************************************************
|
||||
*/
|
||||
|
||||
/**
|
||||
* 关联公众号微信unserialize
|
||||
*/
|
||||
public function relateWeixin()
|
||||
{
|
||||
Log::write('微信公众号消息');
|
||||
$this->wechat->app = $this->wechat->app();
|
||||
$this->wechat->relateWeixin();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* ************************************************************************微信公众号消息相关方法 结束******************************************************
|
||||
*/
|
||||
|
||||
/**
|
||||
* 关联公众号微信unserialize
|
||||
*/
|
||||
public function wechatArticle()
|
||||
{
|
||||
$id = input('id', '');
|
||||
$index = input('i', 0);
|
||||
$material_model = new MaterialModel();
|
||||
$info = $material_model->getMaterialInfo([ 'id' => $id ]);
|
||||
if (!empty($info[ 'data' ][ 'value' ]) && json_decode($info[ 'data' ][ 'value' ], true)) {
|
||||
$info[ 'data' ][ 'value' ] = json_decode($info[ 'data' ][ 'value' ], true);
|
||||
}
|
||||
$this->assign('info', $info[ 'data' ]);
|
||||
$this->assign('index', $index);
|
||||
$replace = [
|
||||
'WECHAT_CSS' => __ROOT__ . '/addon/wechat/admin/view/public/css',
|
||||
'WECHAT_JS' => __ROOT__ . '/addon/wechat/admin/view/public/js',
|
||||
'WECHAT_IMG' => __ROOT__ . '/addon/wechat/admin/view/public/img',
|
||||
];
|
||||
return $this->fetch('wechat/article', [], $replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取授权信息
|
||||
*/
|
||||
public function getAuthInfo()
|
||||
{
|
||||
$cache_key = input("cache_key");
|
||||
$site_id = request()->siteid();
|
||||
$wechat_model = new WechatModel($site_id);
|
||||
$res = $wechat_model->authCodeToOpenid(input());
|
||||
if ($res["code"] >= 0) {
|
||||
Cache::set($cache_key, $res["data"]);
|
||||
$this->assign('result', '授权成功!');
|
||||
return $this->fetch('auth/result');
|
||||
} else {
|
||||
$this->assign('result', $res['message']);
|
||||
return $this->fetch('auth/result');
|
||||
}
|
||||
}
|
||||
}
|
||||
186
addon/wechat/api/controller/Wechat.php
Executable file
186
addon/wechat/api/controller/Wechat.php
Executable file
@@ -0,0 +1,186 @@
|
||||
<?php
|
||||
/**
|
||||
* Niushop商城系统 - 团队十年电商经验汇集巨献!
|
||||
* =========================================================
|
||||
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
|
||||
* ----------------------------------------------
|
||||
* 官方网址: https://www.niushop.com
|
||||
* =========================================================
|
||||
*/
|
||||
|
||||
namespace addon\wechat\api\controller;
|
||||
|
||||
use addon\wechat\model\Config as ConfigModel;
|
||||
use addon\wechat\model\Fans;
|
||||
use addon\wechat\model\Wechat as WechatModel;
|
||||
use app\api\controller\BaseApi;
|
||||
use app\model\member\Member as MemberModel;
|
||||
use think\facade\Cache;
|
||||
|
||||
class Wechat extends BaseApi
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取openid
|
||||
*/
|
||||
public function authCodeToOpenid()
|
||||
{
|
||||
$weapp_model = new WechatModel($this->site_id);
|
||||
$res = $weapp_model->getAuthByCode($this->params);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取网页授权code
|
||||
*/
|
||||
public function authcode()
|
||||
{
|
||||
$redirect_url = $this->params[ 'redirect_url' ] ?? '';
|
||||
$scopes = $this->params[ 'scopes' ] ?? 'snsapi_base';
|
||||
$weapp_model = new WechatModel($this->site_id);
|
||||
$res = $weapp_model->getAuthCode($redirect_url, $scopes);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取jssdk配置
|
||||
*/
|
||||
public function jssdkConfig()
|
||||
{
|
||||
$url = $this->params[ 'url' ] ?? '';
|
||||
$weapp_model = new WechatModel($this->site_id);
|
||||
$res = $weapp_model->getJssdkConfig($url);
|
||||
return $this->response($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分享设置
|
||||
*/
|
||||
public function share()
|
||||
{
|
||||
$this->checkToken();
|
||||
|
||||
//页面路径
|
||||
$url = $this->params[ 'url' ] ?? '';
|
||||
$url = htmlspecialchars_decode($url);
|
||||
|
||||
//sdk配置
|
||||
$weapp_model = new WechatModel($this->site_id);
|
||||
$jssdk_config = $weapp_model->getJssdkConfig($url);
|
||||
if ($jssdk_config[ 'code' ] < 0) return $this->response($jssdk_config);
|
||||
|
||||
//分享配置
|
||||
$share_config = [];
|
||||
$share_data = event('WchatShareData', [
|
||||
'url' => $url,
|
||||
'site_id' => $this->site_id,
|
||||
'member_id' => $this->member_id,
|
||||
], true);
|
||||
if (!empty($share_data)) {
|
||||
$share_config[ 'permission' ] = $share_data[ 'permission' ];
|
||||
$share_config[ 'data' ] = $share_data[ 'data' ];
|
||||
} else {
|
||||
$share_config[ 'permission' ] = [
|
||||
'hideOptionMenu' => true,
|
||||
'hideMenuItems' => [],
|
||||
];
|
||||
$share_config[ 'data' ] = null;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'jssdk_config' => $jssdk_config[ 'data' ],
|
||||
'share_config' => $share_config,
|
||||
];
|
||||
|
||||
return $this->response($this->success($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定店铺openid
|
||||
* @return false|string
|
||||
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
*/
|
||||
public function shopBindOpenid()
|
||||
{
|
||||
$key = $this->params[ "key" ];
|
||||
$weapp_model = new WechatModel($this->site_id);
|
||||
$res = $weapp_model->authCodeToOpenid($this->params);
|
||||
if ($res[ "code" ] >= 0) {
|
||||
Cache::set($key, $res[ "data" ][ "openid" ]);
|
||||
return $this->response($res);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录二维码
|
||||
* @return false|string
|
||||
*/
|
||||
public function loginCode()
|
||||
{
|
||||
$key = str_replace('==', '', base64_encode(uniqid('')));
|
||||
$expire_time = 600;
|
||||
|
||||
$wechat_model = new WechatModel($this->site_id);
|
||||
$res = $wechat_model->getTempQrcode('key_' . $key, $expire_time);
|
||||
if ($res[ 'code' ] != 0) return $this->response($res);
|
||||
|
||||
Cache::set('wechat_' . $key, [ 'expire_time' => ( time() + $expire_time ) ], 3600);
|
||||
$data = $this->success([
|
||||
'key' => $key,
|
||||
'expire_time' => $expire_time,
|
||||
'qrcode' => $res[ 'data' ]
|
||||
]);
|
||||
return $this->response($data);
|
||||
}
|
||||
|
||||
/*
|
||||
* 验证公众号是否配置
|
||||
*/
|
||||
public function verificationWx()
|
||||
{
|
||||
$config_model = new ConfigModel();
|
||||
$config_info = $config_model->getWechatConfig($this->site_id);
|
||||
if (!empty($config_info[ 'data' ])) {
|
||||
$config = $config_info[ 'data' ][ "value" ];
|
||||
if ($config[ 'appid' ] || $config[ 'appsecret' ]) {
|
||||
return $this->response($this->success());
|
||||
}
|
||||
}
|
||||
return $this->response($this->error());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公众号关注二维码
|
||||
* @return false|string
|
||||
*/
|
||||
public function followQrcode()
|
||||
{
|
||||
if (!in_array($this->params[ 'app_type' ], [ 'weapp', 'wechat' ])) return $this->response($this->error('', '该接口仅支持微信浏览器中调用'));
|
||||
|
||||
$token = $this->checkToken();
|
||||
if ($token[ 'code' ] < 0) return $this->response($token);
|
||||
|
||||
$member_model = new MemberModel();
|
||||
$member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $this->member_id, [ 'site_id', '=', $this->site_id ] ] ], 'member_id,wx_openid')[ 'data' ];
|
||||
if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
|
||||
|
||||
$wechat_fans = ( new Fans() )->getFansInfo([ [ 'site_id', '=', $this->site_id ], [ 'openid', '=', $member_info[ 'wx_openid' ] ], [ 'is_subscribe', '=', 1 ] ], 'fans_id')[ 'data' ];
|
||||
if (!empty($wechat_fans)) return $this->response($this->success());
|
||||
|
||||
if (!empty($wechat_fans[ 'fans_id' ])) {
|
||||
$wechat_model = new WechatModel();
|
||||
$res = $wechat_model->getFollowQrcode($wechat_fans[ 'fans_id' ]);
|
||||
if ($res[ 'code' ] == 0) return $this->response($res);
|
||||
}
|
||||
}
|
||||
|
||||
// $fans_info = ( new MemberFans() )->getFansInfo([ [ 'fans_id', '=', $this->fans_id ] ], 'openid')[ 'data' ];
|
||||
// $wechat_fans = ( new Fans() )->getFansInfo([ [ 'site_id', '=', $this->site_id ], [ 'is_subscribe', '=', 1 ], [ 'openid', '=', $fans_info[ 'openid' ] ] ], 'fans_id');
|
||||
// if (!empty($wechat_fans[ 'data' ])) return $this->response($this->success());
|
||||
|
||||
$config_model = new ConfigModel();
|
||||
$config_result = $config_model->getWechatConfig($this->site_id)[ 'data' ][ 'value' ];
|
||||
|
||||
return $this->response($this->success([ 'qrcode' => $config_result[ 'qrcode' ] ]));
|
||||
}
|
||||
}
|
||||
20
addon/wechat/api/view/auth/result.html
Executable file
20
addon/wechat/api/view/auth/result.html
Executable file
@@ -0,0 +1,20 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="renderer" content="webkit">
|
||||
<meta name="referrer" content="never">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover"/>
|
||||
<meta http-equiv="X-UA-COMPATIBLE" content="IE=edge,chrome=1">
|
||||
<title>微信授权结果</title>
|
||||
<meta name="keywords" content="">
|
||||
<meta name="description" content="">
|
||||
</head>
|
||||
<body>
|
||||
<div class='preview-box'>
|
||||
<p style="margin-top: 20vh;text-align: center;color: orangered;font-size: 20px;">{$result}</p>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
|
||||
</script>
|
||||
</html>
|
||||
52
addon/wechat/api/view/wechat/article.html
Executable file
52
addon/wechat/api/view/wechat/article.html
Executable file
@@ -0,0 +1,52 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="renderer" content="webkit">
|
||||
<meta name="referrer" content="never">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0,viewport-fit=cover"/>
|
||||
<meta http-equiv="X-UA-COMPATIBLE" content="IE=edge,chrome=1">
|
||||
<title>{$info.value[$index]['title']}</title>
|
||||
<meta name="keywords" content="">
|
||||
<meta name="description" content="">
|
||||
<link rel="stylesheet" type="text/css" href="__STATIC__/ext/layui/css/layui.css">
|
||||
<link rel="stylesheet" href="SITEHOME_CSS/home.css">
|
||||
<link rel="stylesheet" href="WECHAT_CSS/wx_preview_graphic_message.css">
|
||||
<script src="__STATIC__/js/jquery-2.2.js"></script>
|
||||
<style>
|
||||
#content p{
|
||||
width: 100%;
|
||||
word-wrap:break-word;
|
||||
word-break:normal;
|
||||
}
|
||||
#content p>img{
|
||||
max-width: 100%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class='preview-box'>
|
||||
<div class='graphic-message'>
|
||||
<h1 class='title'>{$info.value[$index]['title']}</h1>
|
||||
{if condition="$info.update_time eq 0"}
|
||||
<span class='time'>{$info['create_time']|time_to_date='Y-m-d'}</span>
|
||||
{else/}
|
||||
<span class='time'>{$info['update_time']|time_to_date='Y-m-d'}</span>
|
||||
{/if}
|
||||
<span class='author'>{$info.value[$index]['autor']}</span>
|
||||
<content>
|
||||
{if condition="$info.value[$index]['show_cover_pic'] == 1"}
|
||||
<div class="rich-media-thumb" id="media" style="width: 100%">
|
||||
<img style="max-width: 100%" onerror="this.parentNode.removeChild(this)" src="{$info.value[$index]['cover']['path']}">
|
||||
</div>
|
||||
{/if}
|
||||
<div id="content">
|
||||
{:html_entity_decode($info.value[$index]['content'])}
|
||||
</div>
|
||||
</content>
|
||||
{if condition="!empty($info.value[$index]['url'])"}
|
||||
<a class='original-text' href="{$info.value[$index]['url']}">阅读原文</a>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user