初始化项目
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\api\service\user;
|
||||
|
||||
use app\common\service\BaseService;
|
||||
use app\api\model\Setting as SettingModel;
|
||||
use app\api\model\UploadFile as UploadFileModel;
|
||||
use app\common\enum\file\FileType as FileTypeEnum;
|
||||
use app\common\library\{Download, storage\Driver as Storage};
|
||||
use cores\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 服务类: 用户头像
|
||||
* Class Avatar
|
||||
* @package app\api\service\user
|
||||
*/
|
||||
class Avatar extends BaseService
|
||||
{
|
||||
// 存储配置
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* Avatar constructor.
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
// 存储配置
|
||||
$this->config = SettingModel::getItem('storage');
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载第三方头像并返回文件记录ID
|
||||
* @param string $avatarUrl
|
||||
* @return int
|
||||
* @throws BaseException
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function party(string $avatarUrl): int
|
||||
{
|
||||
// 下载网络图片
|
||||
$filePath = $this->download($avatarUrl);
|
||||
// 上传到本地
|
||||
$fileInfo = $this->upload($filePath);
|
||||
// 新增文件记录
|
||||
return $this->record($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 图片上传接口
|
||||
* @param string $filePath
|
||||
* @return false|mixed
|
||||
* @throws \think\Exception
|
||||
*/
|
||||
public function upload(string $filePath = '')
|
||||
{
|
||||
// 实例化存储驱动
|
||||
$storage = new Storage($this->config);
|
||||
// 设置上传文件的信息
|
||||
$storage->setUploadFileByReal($filePath)
|
||||
->setRootDirName((string)$this->storeId)
|
||||
->setValidationScene('image')
|
||||
->upload();
|
||||
// 执行文件上传
|
||||
if (!$storage->upload()) {
|
||||
throwError('图片上传失败:' . $storage->getError());
|
||||
}
|
||||
// 文件信息
|
||||
return $storage->getSaveFileInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加文件库记录
|
||||
* @param array $fileInfo
|
||||
* @return int
|
||||
*/
|
||||
private function record(array $fileInfo): int
|
||||
{
|
||||
$model = new UploadFileModel;
|
||||
$model->add($fileInfo, FileTypeEnum::IMAGE, 0);
|
||||
return (int)$model['file_id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载网络图片
|
||||
* @param string $avatarUrl
|
||||
* @return string
|
||||
* @throws BaseException
|
||||
*/
|
||||
private function download(string $avatarUrl): string
|
||||
{
|
||||
$Download = new Download;
|
||||
return $Download->saveTempImage($this->storeId, $avatarUrl, 'avatar');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\api\service\user;
|
||||
|
||||
use app\common\library\helper;
|
||||
use app\common\service\BaseService;
|
||||
use app\api\service\User as UserService;
|
||||
use app\api\model\user\Grade as UserGradeModel;
|
||||
use cores\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 服务类: 会员等级
|
||||
* Class Grade
|
||||
* @package app\api\service\user
|
||||
*/
|
||||
class Grade extends BaseService
|
||||
{
|
||||
/**
|
||||
* 获取当前登录用户的会员等级信息
|
||||
* @param bool $isForce 是否强制验证登录, 如果未登录将抛错
|
||||
* @throws BaseException
|
||||
*/
|
||||
public static function getCurrentGradeInfo(bool $isForce = false)
|
||||
{
|
||||
// 当前登录的用户信息
|
||||
$userInfo = UserService::getCurrentLoginUser($isForce);
|
||||
if (empty($userInfo) || empty($userInfo['grade_id'])) {
|
||||
return false;
|
||||
}
|
||||
// 获取会员等级信息
|
||||
$gradeInfo = UserGradeModel::detail($userInfo['grade_id']);
|
||||
if (empty($gradeInfo) || $gradeInfo['is_delete'] || empty($gradeInfo['status'])) {
|
||||
return false;
|
||||
}
|
||||
return $gradeInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取和计算折扣后的价格
|
||||
* @param $originalPrice
|
||||
* @param $discountRatio
|
||||
* @return string
|
||||
*/
|
||||
public static function getDiscountPrice($originalPrice, $discountRatio): string
|
||||
{
|
||||
// 使用高精度方法计算等级折扣; 因bcmach不支持四舍五入, 所以精确计算到3位数, 使用round四舍五入
|
||||
$discountPrice = helper::bcmul($originalPrice, $discountRatio / 10, 3);
|
||||
$discountPrice = round((float)$discountPrice, 2);
|
||||
return helper::number2($discountPrice, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 萤火科技 <admin@yiovo.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types=1);
|
||||
|
||||
namespace app\api\service\user;
|
||||
|
||||
use app\api\model\UserOauth as UserOauthModel;
|
||||
use app\api\model\wxapp\Setting as WxappSettingModel;
|
||||
use app\common\service\BaseService;
|
||||
use app\common\library\wechat\WxUser;
|
||||
use app\common\library\wechat\ErrorCode;
|
||||
use app\common\library\wechat\WXBizDataCrypt;
|
||||
use cores\exception\BaseException;
|
||||
|
||||
/**
|
||||
* 服务类: 第三方用户服务类
|
||||
* Class Avatar
|
||||
* @package app\api\service\user
|
||||
*/
|
||||
class Oauth extends BaseService
|
||||
{
|
||||
/**
|
||||
* 微信小程序通过code获取session (openid session_key unionid)
|
||||
* @param string $code
|
||||
* @return array|false
|
||||
* @throws BaseException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public static function wxCode2Session(string $code)
|
||||
{
|
||||
// 获取当前小程序信息
|
||||
$wxConfig = static::getMpWxConfig();
|
||||
// 微信登录 (获取session_key)
|
||||
$WxUser = new WxUser($wxConfig['app_id'], $wxConfig['app_secret']);
|
||||
$result = $WxUser->jscode2session($code);
|
||||
!$result && throwError($WxUser->getError());
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密微信的加密数据encryptedData
|
||||
* @param string $sessionKey
|
||||
* @param string $encryptedData
|
||||
* @param string $iv
|
||||
* @return mixed
|
||||
* @throws BaseException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @throws \cores\exception\BaseException
|
||||
*/
|
||||
public static function wxDecryptData(string $sessionKey, string $encryptedData, string $iv)
|
||||
{
|
||||
// 获取当前小程序信息
|
||||
$wxConfig = static::getMpWxConfig();
|
||||
// 微信数据解密
|
||||
$WXBizDataCrypt = new WXBizDataCrypt($wxConfig['app_id'], $sessionKey);
|
||||
$content = null;
|
||||
$code = $WXBizDataCrypt->decryptData($encryptedData, $iv, $content);
|
||||
if ($code !== ErrorCode::$OK) {
|
||||
throwError('微信数据 encryptedData 解密失败');
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信小程序配置项
|
||||
* @return array
|
||||
* @throws \cores\exception\BaseException
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
private static function getMpWxConfig(): array
|
||||
{
|
||||
$wxConfig = WxappSettingModel::getWxappConfig();
|
||||
if (empty($wxConfig['app_id']) || empty($wxConfig['app_secret'])) {
|
||||
throwError('请到后台小程序设置填写AppID和AppSecret参数');
|
||||
}
|
||||
return $wxConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据openid获取用户ID
|
||||
* @param string $oauthId 第三方用户唯一标识 (openid)
|
||||
* @param string $oauthType 第三方登陆类型
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getUserIdByOauthId(string $oauthId, string $oauthType)
|
||||
{
|
||||
return UserOauthModel::getUserIdByOauthId($oauthId, $oauthType);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user