汪总电商平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
13 KiB

3 months ago
<?php
namespace app\api\controller\wanlshop;
use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use addons\wanlshop\library\EasyWeChat\Easywechat;
use addons\wanlshop\library\WanlSdk\QRcode;
use fast\Random;
use fast\Tree;
use think\Config;
use think\Db;
use think\Hook;
use think\Exception;
/**
* WanlShop公共接口
*/
class Common extends Api
{
protected $noNeedLogin = ['init','category','search','update',
'area', 'adverts','searchList','setSearch','about', 'thumbnail', 'upload', 'qrcode', 'mpQrcode'];
protected $noNeedRight = '*';
/**
* 一次性加载App
*
* @ApiSummary (WanlShop 获取首页、购物车、类目数据)
* @ApiMethod (GET)
*
*/
public function init()
{
$cacheTime = 60; // 1.1.2升级 查询缓存
// 首页
$homeList = model('app\api\model\wanlshop\Page')
->where('type','index')
->cache(true, $cacheTime)
->field('page, item')
->find();
if(!$homeList){
$this->error(__('尚未添加首页,请到后台【页面管理】添加首页'));
}
// 类目
$tree = Tree::instance();
$tree->init(model('app\api\model\wanlshop\Category')
->where(['type' => 'goods', 'isnav' => 1])
->cache(true, $cacheTime)
->field('id, pid, name, image')
->order('weigh asc')
->select());
// 搜索关键字
$searchList = model('app\api\model\wanlshop\Search')
->where(['flag' => 'index'])
->field('keywords')
->order('views desc')
->limit(10)
->select();
// 获取配置
$config = get_addon_config('wanlshop');
// 一次性获取模块
$modulesData = [
"homeModules" => $homeList,
"categoryModules" => $tree->getTreeArray(0),
"searchModules" => $searchList
];
// 追加h5地址用于分享二维码等
$config['config']['domain'] = $config['h5']['domain'].($config['h5']['router_mode'] == 'hash' ? '/#/':'/');
$config['config']['logo'] = $config['ini']['logo'];
$config['config']['guide_logo'] = $config['ini']['guide_logo'];
// 输出
$this->success('返回成功', [
"modulesData" => $modulesData,
"appStyle" => $config['style'],
"appConfig" => $config['config']
]);
}
/**
* 加载类目页
*
* @param string $id 类目ID
*/
public function category()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$category_id = implode(',', array_column(Tree::instance()->init(model('app\api\model\wanlshop\Category')->cache(true, 60)->select())->getChildren($this->request->get('id'), true), 'id'));
// 商品
$goods = model('app\api\model\wanlshop\Goods')
->where('category_id', 'in', $category_id)
->where('status', 'normal')
->orderRaw('rand()')
->paginate();
foreach ($goods as $row) {
$row->shop->visible(['state','shopname']);
$row->isLive = model('app\api\model\wanlshop\Live')->where(['shop_id' => $row['shop_id'], 'state' => 1])->field('id')->find();
}
// 拼团
$groups = model('app\api\model\wanlshop\groups\Goods')
->where('category_id', 'in', $category_id)
->where('status', 'normal')
->orderRaw('rand()')
->select();
$list = [
'goods' => $goods,
'seckill' => [],
'groups' => $groups
];
$this->success('返回成功', $list);
}
/**
* APP热更新 1.0.3升级
*
* @ApiSummary (WanlShop APP热更新)
* @ApiMethod (GET)
*
*/
public function update()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$row = model('app\api\model\wanlshop\Version')
->order('versionCode desc')
->find();
$this->success('返回成功', $row);
}
/**
* 获取后端地址 1.1.7升级
*
* @ApiSummary (WanlShop APP热更新)
* @ApiMethod (GET)
*
*/
public function area()
{
$tree = [];
$packData = [];
$cacheTime = 60; // 1.1.7升级 查询缓存
// 把数组的引用赋给新数组
foreach (collection(model('app\common\model\Area')->field('id,pid,name,code,zip,level')->cache(true, $cacheTime)->select())->toArray() as $vo) {
$packData[$vo['id']] = $vo;
}
// 获取树状结构的数组
foreach ($packData as $key => $val) {
if ($val['pid'] == 0)
$tree[] = &$packData[$key];
else
if($val['level'] === 3){
$packData[$val['pid']]['area'][] = &$packData[$key];
}else{
$packData[$val['pid']]['city'][] = &$packData[$key];
}
}
$this->success('返回成功', $tree);
}
/**
* 加载广告
*
* @ApiSummary (WanlShop 加载广告)
* @ApiMethod (GET)
*
*/
public function adverts()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$data = [
'openAdverts' => [],
'pageAdverts' => [],
'categoryAdverts' => [],
'firstAdverts' => [],
'otherAdverts' => []
];
$list = model('app\api\model\wanlshop\Advert')
->field('id,category_id,media,module,type,url')
->select();
foreach ($list as $value) {
$category_id = $value['category_id'];
unset($value['category_id']);
if($value['module'] == 'open'){
$openData[] = $value;
$data['openAdverts'] = $openData[array_rand($openData,1)];
}
if($value['module'] == 'page'){
$data['pageAdverts'][] = $value;
}
if($value['module'] == 'category'){
$data['categoryAdverts'][$category_id][] = $value;
}
if($value['module'] == 'first'){
$data['firstAdverts'][] = $value;
}
if($value['module'] == 'other'){
$data['otherAdverts'][] = $value;
}
}
// 如果syspopup存在客户端将弹窗, 用于开启后通知用户
$syspopup = '';
// 通过大版本号查询,对应数据,未来版本升级开发
$version = $this->request->request("version", '');
$this->success('返回成功', ['data' => $data, 'version' => $version, 'syspopup' => $syspopup]);
}
/**
* 热门搜索
*
* @ApiSummary (WanlShop 搜索关键词列表)
* @ApiMethod (GET)
*
*/
public function searchList()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$list = model('app\api\model\wanlshop\Search')
->field('id,keywords,flag')
->order('views desc')
->limit(20)
->select();
$this->success('返回成功', $list);
}
/**
* 提交搜索关键字给系统
*
* @ApiSummary (WanlShop 搜索关键词列表)
* @ApiMethod (GET)
*
* @param string $keywords 关键字
*/
public function setSearch()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$keywords = $this->request->request("keywords", '');
$model = model('app\api\model\wanlshop\Search');
if($model->where('keywords',$keywords)->count() > 0){
$model->where('keywords',$keywords)->setInc('views');
}else{
$model->save(['keywords'=>$keywords]);
}
$this->success('返回成功');
}
/**
* 实时搜索类目&相关类目
*
* @ApiSummary (WanlShop 搜索关键词列表)
* @ApiMethod (GET)
*
* @param string $search 搜索内容
*/
public function search()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$search = $this->request->request('search', '');
if($search){
// 查询相关类目
$categoryList = model('app\api\model\wanlshop\Category')
->where('name','like','%'.$search.'%')
->field('id,name')
->limit(20)
->select();
// 查询搜索数据
$searchList = model('app\api\model\wanlshop\Search')
->where('keywords','like','%'.$search.'%')
->field('keywords')
->limit(20)
->select();
$result = array("categoryList" => $categoryList, "searchList" => $searchList);
$this->success('返回成功', $result);
}else{
$this->success('请输入关键字');
}
}
public function qrcode()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$page = $this->request->request('page', '');
if($page){
$config = get_addon_config('wanlshop');
$page = $config['h5']['domain'].($config['h5']['router_mode'] == 'hash' ? '/#/':'/') .urldecode($page);
echo QRcode::png(urldecode($page), false, 'L', 6, 1);
exit;
}else{
$this->success('请输入页面链接');
}
}
/**
* 获取二维码
*
* @ApiSummary (WanlShop 查询二维码配置)
* @ApiMethod (POST)
* @env_version 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
*
*/
public function mpQrcode()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
$data = $this->request->request('data', '');
if($data == ''){
$this->success('请输入二维码生成数据');
}
$data = urldecode($data);
$data = json_decode($data, true);
$pages = array_key_exists('page', $data) ? $data['page'] : 'pages/index';
$scene = array_key_exists('scene', $data) ? http_build_query($data['scene']) : [];
$width = array_key_exists('width', $data) ? $data['width'] : 320;
$env = array_key_exists('env', $data) ? $data['env'] : 'release';
$path = '';
// 路径
$url = $env == 'release' ? ('/uploads/mpcode/'. date('Y'). date('m'). '/') : ('/uploads/mpcode/'. $env .'/'. date('Y'). date('m').'/'.time().'/');
$md5 = md5($pages. $scene);
// 判断是否已经生成二维码
if(file_exists($_SERVER['DOCUMENT_ROOT']. $url. $md5. '.jpg')){
$path = $url. $md5. '.jpg';
}else{
try{
$response = Easywechat::app()
->app_code
->getUnlimit($scene, [
'page' => $pages,
'width' => $width,
'env_version' => $env
]);
if(!array_key_exists('errcode', $response)){
// 保存小程序码到文件
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$filename = $response
->saveAs($_SERVER['DOCUMENT_ROOT']. $url, $md5);
}
}
} catch (\Exception $e) {
$this->error($e->getMessage());
}
if(array_key_exists('errcode', $response)){
$this->error('海报小程序码生成失败:'.$response['errmsg']);
}
// 输出
$path = $url.$filename;
}
$img = file_get_contents($_SERVER['DOCUMENT_ROOT'].$path,true);
//使用图片头输出浏览器
header("Content-Type: image/jpeg;text/html; charset=utf-8");
echo $img;
exit;
}
/**
* 关于系统
*
* @ApiSummary (WanlShop 关于系统)
* @ApiMethod (GET)
*
*/
public function about()
{
$config = get_addon_config('wanlshop');
$this->success('返回成功', [
'name' => $config['ini']['name'],
'logo' => $config['ini']['logo'],
'copyright' => $config['ini']['copyright']
]);
}
/**
* 通用无权限上传 1.1.9升级
*
* @ApiSummary (WanlShop 上传配置)
* @ApiMethod (GET)
*
*/
public function upload()
{
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success('ok', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
}
/**
* 获取上传配置 1.0.2升级
*
* @ApiSummary (WanlShop 上传配置)
* @ApiMethod (GET)
*
*/
public function uploadData()
{
$config = get_addon_config('wanlshop');
//配置信息
$upload = Config::get('upload');
//如果非服务端中转模式需要修改为中转
if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
//临时修改上传模式为服务端中转
set_addon_config($upload['storage'], ["uploadmode" => "server"], false);
$upload = \app\common\model\Config::upload();
// 上传信息配置后
Hook::listen("upload_config_init", $upload);
$upload = Config::set('upload', array_merge(Config::get('upload'), $upload));
}
// 1.0.8升级
if($upload['storage'] == 'local'){
$upload['uploadurl'] = url('/api/common/upload', '', false, true);
}
// $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
// $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
$this->success('返回成功', $upload);
}
/**
* 生成后缀图标
*/
public function thumbnail($text = '暂无缩略图', $size = '11')
{
header('Content-type: image/svg+xml');
$icon = <<<EOT
<svg style="background-color: #E1F5FF;" viewBox="0 0 200 200" version="1.1" id="wanlshop_com" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve">
<path fill="#313FA0" d="M157.5,115.9v-4.7l-28.8-28.8c-3.2-3.2-8.4-3.2-11.5,0L76.5,123h73.8C154.3,123,157.5,119.8,157.5,115.9z"/>
<path fill="#8C9EFF" d="M50.4,123H136L86.3,73.4c-3.2-3.2-8.4-3.2-11.5,0l-31.5,31.3v11.2C43.2,119.8,46.4,123,50.4,123z"/>
<path fill="#FFD600" d="M117.8,58.5c0,4.5,3.7,8.2,8.2,8.2s8.2-3.7,8.2-8.2s-3.7-8.2-8.2-8.2S117.8,54,117.8,58.5z"/>
<text fill="#8C9EFF" font-size="{$size}" x="100" y="150" text-anchor="middle">{$text}</text>
</svg>
EOT;
echo $icon;
exit;
}
}