作者 杨育虎

ok

<?php
return array(
array(
'name' => 'wechat',
'title' => '微信',
'type' => 'array',
'content' =>
array(),
'value' => [
'appid' => '', // APP APPID
'app_id' => '', // 公众号 APPID
'app_secret' => '', // 公众号 APPSECRET
'miniapp_id' => '', // 小程序 APPID
'mch_id' => '', //支付商户ID
'key' => '',
'notify_url' => '/addons/epay/api/notifyx/type/wechat', //请勿修改此配置
'cert_client' => '/epay/certs/apiclient_cert.pem', // 可选, 退款,红包等情况时需要用到
'cert_key' => '/epay/certs/apiclient_key.pem',// 可选, 退款,红包等情况时需要用到
'log' => 1,
// 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
],
'rule' => '',
'msg' => '',
'tip' => '微信参数配置',
'ok' => '',
'extend' => '',
return array (
0 =>
array (
'name' => 'wechat',
'title' => '微信',
'type' => 'array',
'content' =>
array (
),
array(
'name' => 'alipay',
'title' => '支付宝',
'type' => 'array',
'content' =>
array(),
'value' => [
'app_id' => '',
'notify_url' => '/addons/epay/api/notifyx/type/alipay', //请勿修改此配置
'return_url' => '/addons/epay/api/returnx/type/alipay', //请勿修改此配置
'ali_public_key' => '',
'private_key' => '',
'log' => 1,
//'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
],
'rule' => 'required',
'msg' => '',
'tip' => '支付宝参数配置',
'ok' => '',
'extend' => '',
'value' =>
array (
'appid' => '',
'app_id' => '',
'app_secret' => '',
'miniapp_id' => 'wxf06ef1898e5e6d55',
'mch_id' => '1591095591',
'key' => 'iqEyMLJMfBzOyJYwEAZGrGXqOIxWp176',
'notify_url' => '/addons/epay/api/notifyx/type/wechat',
'cert_client' => '/epay/certs/apiclient_cert.pem',
'cert_key' => '/epay/certs/apiclient_key.pem',
'log' => '1',
),
array(
'name' => '__tips__',
'title' => '温馨提示',
'type' => 'array',
'content' =>
array(),
'value' => '请注意微信支付证书路径位于/addons/epay/certs目录下,请替换成你自己的证书<br>appid:APP的appid<br>app_id:公众号的appid<br>app_secret:公众号的secret<br>miniapp_id:小程序ID<br>mch_id:微信商户ID<br>key:微信商户支付的密钥',
'rule' => '',
'msg' => '',
'tip' => '微信参数配置',
'ok' => '',
'extend' => '',
)
);
\ No newline at end of file
'rule' => '',
'msg' => '',
'tip' => '微信参数配置',
'ok' => '',
'extend' => '',
),
1 =>
array (
'name' => 'alipay',
'title' => '支付宝',
'type' => 'array',
'content' =>
array (
),
'value' =>
array (
'app_id' => '',
'notify_url' => '/addons/epay/api/notifyx/type/alipay',
'return_url' => '/addons/epay/api/returnx/type/alipay',
'ali_public_key' => '',
'private_key' => '',
'log' => 1,
),
'rule' => 'required',
'msg' => '',
'tip' => '支付宝参数配置',
'ok' => '',
'extend' => '',
),
2 =>
array (
'name' => '__tips__',
'title' => '温馨提示',
'type' => 'array',
'content' =>
array (
),
'value' => '请注意微信支付证书路径位于/addons/epay/certs目录下,请替换成你自己的证书<br>appid:APP的appid<br>app_id:公众号的appid<br>app_secret:公众号的secret<br>miniapp_id:小程序ID<br>mch_id:微信商户ID<br>key:微信商户支付的密钥',
'rule' => '',
'msg' => '',
'tip' => '微信参数配置',
'ok' => '',
'extend' => '',
),
);
... ...
... ... @@ -38,8 +38,8 @@ return array (
),
'value' =>
array (
'app_id' => 'wx2c7b31bdf1d86b2f',
'app_secret' => '1ce6fcb158bfd6c76abd90a0a4498071',
'app_id' => 'wxf06ef1898e5e6d55',
'app_secret' => 'afdf55fd5d385f0f0900c6b350576310',
'scope' => 'snsapi_base',
),
'rule' => 'required',
... ...
... ... @@ -9,7 +9,7 @@ return array (
'content' =>
array (
),
'value' => 'wx2c7b31bdf1d86b2f',
'value' => 'wxf06ef1898e5e6d55',
'rule' => 'required',
'msg' => '',
'tip' => '你的微信公众号appid',
... ... @@ -24,7 +24,7 @@ return array (
'content' =>
array (
),
'value' => '1ce6fcb158bfd6c76abd90a0a4498071',
'value' => 'afdf55fd5d385f0f0900c6b350576310',
'rule' => 'required',
'msg' => '',
'tip' => '你的微信公众号appsecret',
... ...
... ... @@ -9,6 +9,7 @@ use app\api\model\Order;
use app\common\controller\Api;
use app\common\library\Auth;
use app\common\model\Area;
use app\common\model\Attachment;
use app\common\model\Version;
use EasyWeChat\Encryption\EncryptionException;
use EasyWeChat\Foundation\Application;
... ... @@ -22,7 +23,7 @@ use think\Db;
*/
class Common extends Api
{
protected $noNeedLogin = ['init', 'getSessionKey', 'login', 'codeToToken', 'upload'];
protected $noNeedLogin = ['init', 'getSessionKey', 'login', 'codeToToken', 'upload', 'payNotify'];
protected $noNeedRight = '*';
/**
... ... @@ -58,97 +59,111 @@ class Common extends Api
*/
public function upload()
{
$config = get_addon_config('qiniu');
$file = $this->request->file('file');
if (empty($file)) {
$this->error(__('No file upload or server upload limit exceeded'));
if (!$file || !$file->isValid()) {
$this->error("请上传有效的文件");
}
//判断是否已经存在附件
$sha1 = $file->hash();
$upload = Config::get('upload');
preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
$fileInfo = $file->getInfo();
$filePath = $file->getRealPath() ?: $file->getPathname();
preg_match('/(\d+)(\w+)/', $config['maxsize'], $matches);
$type = strtolower($matches[2]);
$typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
$size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
$fileInfo = $file->getInfo();
$size = (int)$config['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
$suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
$suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
$suffix = $suffix ? $suffix : 'file';
$mimetypeArr = explode(',', strtolower($upload['mimetype']));
$md5 = md5_file($filePath);
$search = ['$(year)', '$(mon)', '$(day)', '$(etag)', '$(ext)'];
$replace = [date("Y"), date("m"), date("d"), $md5, '.' . $suffix];
$object = ltrim(str_replace($search, $replace, $config['savekey']), '/');
$mimetypeArr = explode(',', strtolower($config['mimetype']));
$typeArr = explode('/', $fileInfo['type']);
//禁止上传PHP和HTML文件
if (in_array($fileInfo['type'], ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm'])) {
$this->error(__('Uploaded file format is limited'));
//检查文件大小
if (!$file->checkSize($size)) {
$this->error("起过最大可上传文件限制");
}
//验证文件后缀
if ($upload['mimetype'] !== '*' &&
if ($config['mimetype'] !== '*' &&
(
!in_array($suffix, $mimetypeArr)
|| (stripos($typeArr[0] . '/', $upload['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
|| (stripos($typeArr[0] . '/', $config['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
)
) {
$this->error(__('Uploaded file format is limited'));
$this->error(__('上传格式限制'));
}
//验证是否为图片文件
$imagewidth = $imageheight = 0;
if (in_array($fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
$imgInfo = getimagesize($fileInfo['tmp_name']);
if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
$this->error(__('Uploaded file is not a valid image'));
}
$imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
$imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
}
$replaceArr = [
'{year}' => date("Y"),
'{mon}' => date("m"),
'{day}' => date("d"),
'{hour}' => date("H"),
'{min}' => date("i"),
'{sec}' => date("s"),
'{random}' => Random::alnum(16),
'{random32}' => Random::alnum(32),
'{filename}' => $suffix ? substr($fileInfo['name'], 0, strripos($fileInfo['name'], '.')) : $fileInfo['name'],
'{suffix}' => $suffix,
'{.suffix}' => $suffix ? '.' . $suffix : '',
'{filemd5}' => md5_file($fileInfo['tmp_name']),
];
$savekey = $upload['savekey'];
$savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);
$savekey = '/' . $object;
$uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
$fileName = substr($savekey, strripos($savekey, '/') + 1);
//
$splInfo = $file->validate(['size' => $size])->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
//先上传到本地
$splInfo = $file->move(ROOT_PATH . '/public' . $uploadDir, $fileName);
if ($splInfo) {
$extparam = $this->request->post();
$filePath = $splInfo->getRealPath() ?: $splInfo->getPathname();
$sha1 = sha1_file($filePath);
$imagewidth = $imageheight = 0;
if (in_array($suffix, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'])) {
$imgInfo = getimagesize($splInfo->getPathname());
$imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
$imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
}
$params = array(
'admin_id' => 0,
'user_id' => (int)$this->auth->id,
'filesize' => $fileInfo['size'],
'imagewidth' => $imagewidth,
'admin_id' => session('admin.id'),
'user_id' => $this->auth->id,
'filesize' => $fileInfo['size'],
'imagewidth' => $imagewidth,
'imageheight' => $imageheight,
'imagetype' => $suffix,
'imagetype' => $suffix,
'imageframes' => 0,
'mimetype' => $fileInfo['type'],
'url' => $uploadDir . $splInfo->getSaveName(),
'uploadtime' => time(),
'storage' => 'local',
'sha1' => $sha1,
'mimetype' => $fileInfo['type'],
'url' => $uploadDir . $splInfo->getSaveName(),
'uploadtime' => time(),
'storage' => 'local',
'sha1' => $sha1,
'extparam' => json_encode($extparam),
);
$attachment = Attachment::create(array_filter($params), true);
$policy = array(
'saveKey' => ltrim($savekey, '/'),
);
$attachment = model("attachment");
$attachment->data(array_filter($params));
$auth = new \addons\qiniu\library\Auth($config['app_key'], $config['secret_key']);
$token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
$multipart = [
['name' => 'token', 'contents' => $token],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName,
]
];
try {
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', $config['uploadurl'], [
'multipart' => $multipart
]);
$code = $res->getStatusCode();
//成功不做任何操作
} catch (\GuzzleHttp\Exception\ClientException $e) {
$attachment->delete();
unlink($filePath);
$this->error("上传失败");
}
$url = '/' . $object;
//上传成功后将存储变更为qiniu
$attachment->storage = 'qiniu';
$attachment->save();
\think\Hook::listen("upload_after", $attachment);
$this->success(__('Upload successful'), [
'url' => $uploadDir . $splInfo->getSaveName()
]);
$this->success("上传成功", ['url' => cdnurl($url), 'save_path' => $url]);
} else {
// 上传失败获取错误信息
$this->error($file->getError());
$this->error('上传失败');
}
return;
}
... ...
<?php
namespace app\api\controller;
use addons\epay\library\Service;
use app\api\model\Order;
use app\api\model\Price;
use app\api\model\Third;
use app\api\model\VipConfig;
use app\common\controller\Api;
/**
* 答谢
*/
class Thank extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['getPriceList'];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
/**
* 支付下单
* @ApiTitle (支付下单)
* @ApiSummary (支付下单)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="money", type="integer", required=true, description="价格")
* @ApiParams (name="userb_id", type="integer", required=true, description="学长ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function pay()
{
$userId = $this->auth->id;
$money = $this->request->param('money', 0, 'int');
if(!$money || $money <= 0) {
$this->error('您的操作有误');
}
$userbId = $this->request->param('userb_id', 0, 'int');
if(!$userbId) {
$this->error('您的操作有误');
}
//open_id
$third = new Third();
$thirdInfo = $third->getOneByUserId($userId);
if(!$thirdInfo) {
$this->error('您提交的内容有误');
}
$openid = $thirdInfo['openid'];
$order = new Order();
$out_trade_no = date("YmdHis", time()) . mt_rand(100000, 999999);
$insertData = [
'createtime' => time(),
'updatetime' => time(),
'order_id' => $out_trade_no,
'money' => $money,
'type' => 1,
'status' => 1,
'other' => json_encode(['user_id' => $userbId, 'money' => $money]),
'user_id' => $userId
];
$res = $order->addOne($insertData);
if(!$res) {
$this->error('订单提交错误');
}
//订单标题
$title = 'FastAdmin测试订单';
//其他
$type = 'wechat';
$method = 'miniapp';
$amount = $money;
//回调链接
$notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
$returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
//订单号
$params = [
'amount' => $amount,
'orderid' => $out_trade_no,
'type' => $type,
'title' => $title,
'notifyurl' => $notifyurl,
'returnurl' => $returnurl,
'method' => $method,
'openid' => $openid,
'auth_code' => uniqid()
];
$this->success('申请支付', json_decode(Service::submitOrder($params), true));
}
/**
* 获取价目表
* @ApiTitle (获取价目表)
* @ApiSummary (获取价目表)
* @ApiMethod (POST)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取价目表成功",
"time": "1586398313",
"data": [
{
"id": 1,
"price": 1,
"weigh": 0,
"createtime": 0,
"updatetime": 0,
"deletetime": null,
"show_switch": 1
}
]
})
*/
public function getPriceList()
{
$price = new Price();
$priceList = $price->listAll();
$this->success('获取价目表成功', $priceList);
}
<?php
namespace app\api\controller;
use addons\epay\library\Service;
use app\api\model\Order;
use app\api\model\Price;
use app\api\model\Third;
use app\api\model\VipConfig;
use app\common\controller\Api;
/**
* 答谢
*/
class Thank extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['getPriceList', 'pay'];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
/**
* 支付下单
* @ApiTitle (支付下单)
* @ApiSummary (支付下单)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="money", type="integer", required=true, description="价格")
* @ApiParams (name="userb_id", type="integer", required=true, description="学长ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function pay()
{
$userId = $this->auth->id;
$money = $this->request->param('money', 0, 'int');
if(!$money || $money <= 0) {
$this->error('您的操作有误');
}
$userbId = $this->request->param('userb_id', 0, 'int');
if(!$userbId) {
$this->error('您的操作有误');
}
//open_id
$third = new Third();
$thirdInfo = $third->getOneByUserId($userId);
if(!$thirdInfo) {
$this->error('您提交的内容有误');
}
$openid = $thirdInfo['openid'];
$order = new Order();
$out_trade_no = date("YmdHis", time()) . mt_rand(100000, 999999);
$insertData = [
'createtime' => time(),
'updatetime' => time(),
'order_id' => $out_trade_no,
'money' => $money,
'type' => 1,
'status' => 1,
'other' => json_encode(['user_id' => $userbId, 'money' => $money]),
'user_id' => $userId
];
$res = $order->addOne($insertData);
if(!$res) {
$this->error('订单提交错误');
}
//订单标题
$title = 'FastAdmin测试订单';
//其他
$type = 'wechat';
$method = 'miniapp';
$amount = $money;
//回调链接
$notifyurl = 'http://school.t.brotop.cn/api/Common/payNotify';
$returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
//订单号
$params = [
'amount' => $amount,
'orderid' => $out_trade_no,
'type' => $type,
'title' => $title,
'notifyurl' => $notifyurl,
'returnurl' => $returnurl,
'method' => $method,
'openid' => $openid,
'auth_code' => uniqid()
];
$this->success('申请支付', json_decode(Service::submitOrder($params), true));
}
/**
* 获取价目表
* @ApiTitle (获取价目表)
* @ApiSummary (获取价目表)
* @ApiMethod (POST)
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取价目表成功",
"time": "1586398313",
"data": [
{
"id": 1,
"price": 1,
"weigh": 0,
"createtime": 0,
"updatetime": 0,
"deletetime": null,
"show_switch": 1
}
]
})
*/
public function getPriceList()
{
$price = new Price();
$priceList = $price->listAll();
$this->success('获取价目表成功', $priceList);
}
}
\ No newline at end of file
... ...
<?php
namespace app\api\controller;
use addons\epay\library\Service;
use app\api\model\Order;
use app\api\model\Third;
use app\api\model\VipConfig;
use app\common\controller\Api;
use app\api\model\Student as StudentModel;
/**
* 高中相关
*/
class Vip extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = [''];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
/**
* 获取vip卡购买的配置
* @ApiTitle (获取vip卡购买的配置)
* @ApiSummary (获取vip卡购买的配置)
* @ApiMethod (POST)
* @ApiParams (name="name", type="string", required=true, description="高中名")
* @ApiParams (name="province_id", type="integer", required=true, description="省ID")
* @ApiParams (name="city_id", type="integer", required=true, description="城市ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取vip卡购买的配置成功",
"time": "1586511854",
"data": [
{
"id": 1,
"money": 100,
"name": "省月卡",
"des_content": "e额的撒大苏打",
"show_switch": 1,
"weigh": 0,
"createtime": 0,
"updatetime": 0,
"deletetime": null,
"vip_endtime": "2020-08-02"
}
]
})
*/
public function getVipConf()
{
$userId = $this->auth->id;
$student = new StudentModel();
$studentInfo = $student->infoByUserIdCanShow($userId);
if(!$studentInfo) {
$this->error('您的操作有误');
}
$vipConfig = new VipConfig();
$vipConfigList = $vipConfig->listAll();
foreach ($vipConfigList as $k => $v) {
if($studentInfo['vip_endtime'] > time() && $v['id'] == $studentInfo['vip_level']) {
$vipConfigList[$k]['vip_endtime'] = date('Y-m-d', $studentInfo['vip_endtime']);
} else {
$vipConfigList[$k]['vip_endtime'] = '';
}
}
$this->success('获取vip卡购买的配置成功', $vipConfigList);
}
/**
* 购买vip卡
* @ApiTitle (购买vip卡)
* @ApiSummary (购买vip卡)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="vip_id", type="string", required=true, description="vip卡ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function buyVip()
{
$userId = $this->auth->id;
$vipId = $this->request->param('vip_id', 0, 'int');
if(!$vipId) {
$this->error('您的操作有误');
}
$vipConfig = new VipConfig();
$vipConfigInfo = $vipConfig->infoById($vipId);
if(!$vipConfigInfo) {
$this->error('您的操作有误');
}
//open_id
$third = new Third();
$thirdInfo = $third->getOneByUserId($userId);
if(!$thirdInfo) {
$this->error('您提交的内容有误');
}
$openid = $thirdInfo['openid'];
$order = new Order();
$out_trade_no = date("YmdHis", time()) . mt_rand(100000, 999999);
$insertData = [
'createtime' => time(),
'updatetime' => time(),
'order_id' => $out_trade_no,
'money' => $vipConfigInfo['money'],
'type' => 2,
'status' => 1,
'other' => json_encode(['user_id' => $userId, 'keep' => $vipConfigInfo['keep'], 'vip_level' => $vipConfigInfo['vip_level']]),
'user_id' => $userId
];
$res = $order->addOne($insertData);
if(!$res) {
$this->error('订单提交错误');
}
//订单标题
$title = 'FastAdmin测试订单';
//其他
$type = 'wechat';
$method = 'miniapp';
$amount = $vipConfigInfo['money'];
//回调链接
$notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
$returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
//订单号
$params = [
'amount' => $amount,
'orderid' => $out_trade_no,
'type' => $type,
'title' => $title,
'notifyurl' => $notifyurl,
'returnurl' => $returnurl,
'method' => $method,
'openid' => $openid,
'auth_code' => uniqid()
];
$this->success('申请支付', json_decode(Service::submitOrder($params), true));
}
<?php
namespace app\api\controller;
use addons\epay\library\Service;
use app\api\model\Order;
use app\api\model\Third;
use app\api\model\VipConfig;
use app\common\controller\Api;
use app\api\model\Student as StudentModel;
/**
* 高中相关
*/
class Vip extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = [''];
// 无需鉴权的接口,*表示全部
protected $noNeedRight = ['*'];
/**
* 获取vip卡购买的配置
* @ApiTitle (获取vip卡购买的配置)
* @ApiSummary (获取vip卡购买的配置)
* @ApiMethod (POST)
* @ApiParams (name="name", type="string", required=true, description="高中名")
* @ApiParams (name="province_id", type="integer", required=true, description="省ID")
* @ApiParams (name="city_id", type="integer", required=true, description="城市ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ({
"code": 1,
"msg": "获取vip卡购买的配置成功",
"time": "1586511854",
"data": [
{
"id": 1,
"money": 100,
"name": "省月卡",
"des_content": "e额的撒大苏打",
"show_switch": 1,
"weigh": 0,
"createtime": 0,
"updatetime": 0,
"deletetime": null,
"vip_endtime": "2020-08-02"
}
]
})
*/
public function getVipConf()
{
$userId = $this->auth->id;
$student = new StudentModel();
$studentInfo = $student->infoByUserIdCanShow($userId);
if(!$studentInfo) {
$this->error('您的操作有误');
}
$vipConfig = new VipConfig();
$vipConfigList = $vipConfig->listAll();
foreach ($vipConfigList as $k => $v) {
if($studentInfo['vip_endtime'] > time() && $v['id'] == $studentInfo['vip_level']) {
$vipConfigList[$k]['vip_endtime'] = date('Y-m-d', $studentInfo['vip_endtime']);
} else {
$vipConfigList[$k]['vip_endtime'] = '';
}
}
$this->success('获取vip卡购买的配置成功', $vipConfigList);
}
/**
* 购买vip卡
* @ApiTitle (购买vip卡)
* @ApiSummary (购买vip卡)
* @ApiMethod (POST)
* @ApiHeaders (name="token", type="string", required=true, description="请求的Token")
* @ApiParams (name="vip_id", type="string", required=true, description="vip卡ID")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
* @ApiReturn ()
*/
public function buyVip()
{
$userId = $this->auth->id;
$vipId = $this->request->param('vip_id', 0, 'int');
if(!$vipId) {
$this->error('您的操作有误');
}
$vipConfig = new VipConfig();
$vipConfigInfo = $vipConfig->infoById($vipId);
if(!$vipConfigInfo) {
$this->error('您的操作有误');
}
//open_id
$third = new Third();
$thirdInfo = $third->getOneByUserId($userId);
if(!$thirdInfo) {
$this->error('您提交的内容有误');
}
$openid = $thirdInfo['openid'];
$order = new Order();
$out_trade_no = date("YmdHis", time()) . mt_rand(100000, 999999);
$insertData = [
'createtime' => time(),
'updatetime' => time(),
'order_id' => $out_trade_no,
'money' => $vipConfigInfo['money'],
'type' => 2,
'status' => 1,
'other' => json_encode(['user_id' => $userId, 'keep' => 30, 'vip_level' => $vipConfigInfo['vip_level']]),
'user_id' => $userId
];
$res = $order->addOne($insertData);
if(!$res) {
$this->error('订单提交错误');
}
//订单标题
$title = 'FastAdmin测试订单';
//其他
$type = 'wechat';
$method = 'miniapp';
$amount = $vipConfigInfo['money'];
//回调链接
$notifyurl = 'http://school.t.brotop.cn/api/Common/payNotify';
$returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
//订单号
$params = [
'amount' => $amount,
'orderid' => $out_trade_no,
'type' => $type,
'title' => $title,
'notifyurl' => $notifyurl,
'returnurl' => $returnurl,
'method' => $method,
'openid' => $openid,
'auth_code' => uniqid()
];
$this->success('申请支付', json_decode(Service::submitOrder($params), true));
}
}
\ No newline at end of file
... ...
<?php
namespace app\api\model;
use think\Model;
class Student extends Model
{
protected $name = 'student';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 添加一条学生的数据
public function addOne($data)
{
return $this->insert($data);
}
// 获取高中同校的本校学长总数
public function countByUniversityIdAndSchoolId($universityId, $schoolId)
{
return $this->where(['university_id' => ['=', $universityId], 'show_switch' => ['=', 1], 'school_id' => ['=', $schoolId]])->useSoftDelete($this->deleteTime)->count();
}
// 获取高中同城/省的本校学长总数
public function countByUniversityIdAndSchoolIds($universityId, $schoolIds)
{
return $this->where(['university_id' => ['=', $universityId], 'show_switch' => ['=', 1], 'school_id' => ['in', $schoolIds]])->useSoftDelete($this->deleteTime)->count();
}
// 根据用户ID获取一个可以展示的学生的数据
public function infoByUserIdCanShow($userId)
{
return $this
->where(['user_id' => ['=', $userId], 'show_switch' => ['=', 1]])
->useSoftDelete($this->deleteTime)
->find();
}
// 获取高中同城/省的本校学长
public function listByUniversityIdAndSchoolIds($where, $page, $size)
{
return $this
->where($where)
->useSoftDelete($this->deleteTime)
->order($this->createTime, 'desc')
->page($page, $size)
->select();
}
// 修改一个学生的数据
public function updateOne($id, $data)
{
return $this->where(['user_id' => ['=', $id]])->update($data);
}
// 修改一群学生的数据
public function updateByIds($userIds, $data)
{
return $this->where(['user_id' => ['in', $userIds]])->update($data);
}
// 根据条件查询
public function listByWhere($where)
{
return $this->where($where)->select();
}
<?php
namespace app\api\model;
use think\Model;
class Student extends Model
{
protected $name = 'student';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 添加一条学生的数据
public function addOne($data)
{
return $this->insert($data);
}
// 获取高中同校的本校学长总数
public function countByUniversityIdAndSchoolId($universityId, $schoolId)
{
return $this->where(['university_id' => ['=', $universityId], 'show_switch' => ['=', 1], 'school_id' => ['=', $schoolId]])->useSoftDelete($this->deleteTime)->count();
}
// 获取高中同城/省的本校学长总数
public function countByUniversityIdAndSchoolIds($universityId, $schoolIds)
{
return $this->where(['university_id' => ['=', $universityId], 'show_switch' => ['=', 1], 'school_id' => ['in', $schoolIds]])->useSoftDelete($this->deleteTime)->count();
}
// 根据用户ID获取一个可以展示的学生的数据
public function infoByUserIdCanShow($userId)
{
return $this
->where(['user_id' => ['=', $userId], 'show_switch' => ['=', 1]])
->useSoftDelete($this->deleteTime)
->find();
}
// 获取高中同城/省的本校学长
public function listByUniversityIdAndSchoolIds($where, $page, $size)
{
return $this
->where($where)
->useSoftDelete($this->deleteTime)
->order($this->createTime, 'desc')
->page($page, $size)
->select();
}
// 修改一个学生的数据
public function updateOne($id, $data)
{
return $this->where(['user_id' => ['=', $id]])->update($data);
}
// 修改一群学生的数据
public function updateByIds($userIds, $data)
{
return $this->where(['user_id' => ['in', $userIds]])->update($data);
}
// 根据条件查询
public function listByWhere($where)
{
return $this->where($where)->select();
}
}
\ No newline at end of file
... ...
此 diff 太大无法显示。