作者 王智

'2'

... ... @@ -3,13 +3,12 @@
namespace app\api\controller;
use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use Qiniu\Auth;
use think\Config;
use think\Hook;
use EasyWeChat\Factory;
use think\Db;
/**
... ... @@ -63,70 +62,122 @@ class Common extends Api
}
/**
* 上传文件
* @ApiMethod (POST)
* @param File $file 文件流
* 上传文件-七牛
*
* @ApiTitle (上传文件-七牛)
* @ApiSummary (测试描述信息)
* @ApiMethod (POST)
* @ApiParams (name="file", type="file", required=true, description="用户名")
*/
public function upload()
public function uploadQiniu()
{
Config::set('default_return_type', 'json');
//必须设定cdnurl为空,否则cdnurl函数计算错误
Config::set('upload.cdnurl', '');
$chunkid = $this->request->post("chunkid");
if ($chunkid) {
if (!Config::get('upload.chunking')) {
$this->error(__('Chunk file disabled'));
}
$action = $this->request->post("action");
$chunkindex = $this->request->post("chunkindex/d");
$chunkcount = $this->request->post("chunkcount/d");
$filename = $this->request->post("filename");
$method = $this->request->method(true);
if ($action == 'merge') {
$attachment = null;
//合并分片文件
try {
$upload = new Upload();
$attachment = $upload->merge($chunkid, $chunkcount, $filename);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
} elseif ($method == 'clean') {
//删除冗余的分片文件
try {
$upload = new Upload();
$upload->clean($chunkid);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success();
} else {
//上传分片文件
//默认普通上传文件
$file = $this->request->file('file');
try {
$upload = new Upload($file);
$upload->chunk($chunkid, $chunkindex, $chunkcount);
} catch (UploadException $e) {
$this->error($e->getMessage());
}
$this->success();
$config = get_addon_config('qiniu');
$file = $this->request->file('file');
if (!$file || !$file->isValid()) {
$this->error("请上传有效的文件");
}
$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)$config['maxsize'] * pow(1024000, isset($typeDict[$type]) ? $typeDict[$type] : 0);
$suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
$suffix = $suffix ? $suffix : 'file';
$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']);
//检查文件大小
if (!$file->checkSize($size)) {
$this->error("起过最大可上传文件限制");
}
//验证文件后缀
if ($config['mimetype'] !== '*' &&
(
!in_array($suffix, $mimetypeArr)
|| (stripos($typeArr[0] . '/', $config['mimetype']) !== false && (!in_array($fileInfo['type'], $mimetypeArr) && !in_array($typeArr[0] . '/*', $mimetypeArr)))
)
) {
$this->error(__('上传格式限制'));
}
$savekey = '/' . $object;
$uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
$fileName = substr($savekey, strripos($savekey, '/') + 1);
//先上传到本地
$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', 'pdf'])) {
$imgInfo = getimagesize($splInfo->getPathname());
$imagewidth = isset($imgInfo[0]) ? $imgInfo[0] : $imagewidth;
$imageheight = isset($imgInfo[1]) ? $imgInfo[1] : $imageheight;
}
} else {
$attachment = null;
//默认普通上传文件
$file = $this->request->file('file');
$params = array(
'admin_id' => session('admin.id'),
'user_id' => $this->auth->id,
'filesize' => $fileInfo['size'],
'imagewidth' => $imagewidth,
'imageheight' => $imageheight,
'imagetype' => $suffix,
'imageframes' => 0,
'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, '/'),
);
$auth = new Auth($config['accessKey'], $config['secretKey']);
$token = $auth->uploadToken($config['bucket'], null, $config['expire'], $policy);
$multipart = [
['name' => 'token', 'contents' => $token],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName,
]
];
try {
$upload = new Upload($file);
$attachment = $upload->upload();
} catch (UploadException $e) {
$this->error($e->getMessage());
$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("上传失败");
}
$this->success(__('Uploaded successful'), ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
}
$url = '/' . $object;
//上传成功后将存储变更为qiniu
$attachment->storage = 'qiniu';
$attachment->save();
$this->success("上传成功", $url);
} else {
$this->error('上传失败');
}
return;
}
... ... @@ -306,7 +357,159 @@ class Common extends Api
* @ApiMethod (POST)
* @ApiRoute (/api/Common/AgreementOperation)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="battery_id", type="string", required=true, description="电池类型ID")
* @ApiParams (name="type", type="string", required=true, description="操作:1=同意,2=拒绝,3=修改租金,4=终止协议,5=删除协议")
* @ApiParams (name="id", type="string", required=true, description="协议ID")
* @ApiParams (name="money", type="int", required=true, description="修改后的金额")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
)
*/
public function AgreementOperation()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$IsSeller = Db::name('agreement')
->alias('a')
->join('seller s', 's.id=a.seller_id')
->where('s.user_id', $UserId)
->where('a.id', $params['id'])
->find();
if (empty($IsSeller)) {
$this->error('参数错误', 0);
}
//同意签订协议
if ($params['type'] == 1) {
$res = Db::name('agreement')->where('id', $params['id'])->update(['status' => 1, 'updatetime' => time(), 'EXP_time' => time() + 86400 * 30]);
}
//拒绝签订协议
if ($params['type'] == 2) {
$res = Db::name('agreement')->where('id', $params['id'])->update(['status' => 4, 'updatetime' => time()]);
//拒绝签订协议 给予用户退款
$OrderSn = Db::name('agreement')->where('id', $params['id'])->value('OrderSn');
$PayOrderInfo = Db::name('pay_order')->where('OrderSn', $OrderSn)->where('type', 1)->find();
$TuiMoney = $PayOrderInfo['UpMoney'] + $PayOrderInfo['Money'];
//配置
$config = [
'app_id' => 'wx6a9080f20326f817',
'payment' => [
'merchant_id' => '1603658973',
'key' => '8695A8185xyzKcdEVfreewayShenzhen',
'cert_path' => '/home/wwwroot/fast/book/addons/epay/certs/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => '/home/wwwroot/fast/book/addons/epay/certs/apiclient_key.pem', // XXX: 绝对路径!!!!
],
];
$app = Factory::officialAccount($config);
try {
$result = $app->refund->byTransactionId($PayOrderInfo['WeChatOrder'], $PayOrderInfo['PayOrder'], $PayOrderInfo['money'] * 100, $TuiMoney); // 总金额 100, 退款 80,操作员:商户号
//更改订单状态为已退款
Db::name('pay_order')->where('OrderSn', $OrderSn)->where('PayOrder', $PayOrderInfo['PayOrder'])->update(['type' => 0]);
} catch (Exception $e) {
$e->getMessage();
}
if (!$result) {
$this->error('退款失败', 0);
die;
}
}
//修改租金
if ($params['type'] == 3) {
$res = Db::name('agreement')->where('id', $params['id'])->update(['MonthMoney' => $params['money'], 'updatetime' => time()]);
}
//终止协议
if ($params['type'] == 4) {
$res = Db::name('agreement')->where('id', $params['id'])->update(['status' => 2, 'updatetime' => time()]);
}
//删除协议
if ($params['type'] == 5) {
$res = Db::name('agreement')->where('id', $params['id'])->delete();
}
$this->res($res);
}
/**
* 公共接口
* @ApiTitle (押金退还操作)
* @ApiSummary (押金退还操作)
* @ApiMethod (POST)
* @ApiRoute (/api/Common/TuikuanOperation)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type", type="string", required=true, description="操作:1=通过,2=拒绝,3=删除记录")
* @ApiParams (name="id", type="string", required=true, description="协议ID")
* @ApiParams (name="bili", type="string", required=true, description="退款百分比/ 1为百分之100/0.1为百分之10")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
)
*/
public function TuikuanOperation()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$IsSeller = Db::name('agreement')
->alias('a')
->join('seller s', 's.id=a.seller_id')
->where('s.user_id', $UserId)
->where('a.id', $params['id'])
->value('seller_id');
if (empty($IsSeller)) {
$this->error('参数错误', 0);
}
$OrderInfo = Db::name('agreement')->where('id', $params['id'])->find();
if ($params['type'] == 3) {
$res = Db::name('tuikuan')->where('OrderSn', $OrderInfo['OrderSn'])->where('seller_id', $IsSeller)->delete();
} else {
$IsUpdateAgreement = Db::name('agreement')->where('id', $params['id'])->update(['refind_status' => $params['type']]);
if (!$IsUpdateAgreement) {
$this->error('协议状态更改失败', 0);
}
$res = Db::name('tuikuan')->where('OrderSn', $OrderInfo['OrderSn'])->where('seller_id', $IsSeller)->update(['status' => $params['status']]);
if ($params['type'] == 1) {
//拒绝签订协议 给予用户退款
$OrderSn = Db::name('agreement')->where('id', $params['id'])->value('OrderSn');
$PayOrderInfo = Db::name('pay_order')->where('OrderSn', $OrderSn)->where('type', 1)->find();
$TuiMoney = $PayOrderInfo['UpMoney'] * $params['bili'] + $PayOrderInfo['Money'];
//配置
$config = [
'app_id' => 'wx6a9080f20326f817',
'payment' => [
'merchant_id' => '1603658973',
'key' => '8695A8185xyzKcdEVfreewayShenzhen',
'cert_path' => ' / home / wwwroot / fast / book / addons / epay / certs / apiclient_cert . pem', // XXX: 绝对路径!!!!
'key_path' => ' / home / wwwroot / fast / book / addons / epay / certs / apiclient_key . pem', // XXX: 绝对路径!!!!
],
];
$app = Factory::officialAccount($config);
try {
$result = $app->refund->byTransactionId($PayOrderInfo['WeChatOrder'], $PayOrderInfo['PayOrder'], $PayOrderInfo['money'] * 100, $TuiMoney); // 总金额 100, 退款 80,操作员:商户号
//更改订单状态为已退款
Db::name('pay_order')->where('OrderSn', $OrderSn)->where('PayOrder', $PayOrderInfo['PayOrder'])->update(['type' => 0]);
} catch (Exception $e) {
$e->getMessage();
}
if (!$result) {
$this->error('退款失败', 0);
die;
}
}
}
$this->res($res);
}
/**
* 公共接口
* @ApiTitle (删除订单)
* @ApiSummary (删除订单)
* @ApiMethod (POST)
* @ApiRoute (/api/Common/DeleteOrder)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="OrderSn", type="string", required=true, description="订单号")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
... ... @@ -314,4 +517,80 @@ class Common extends Api
'msg':'返回成功',
)
*/
public function DeleteOrder()
{
$UserId = $this->IsToken($this->request->header());
$OrderSn = input('OrderSn');
$Arr = Db::name('order')
->alias('o')
->join('stor s', 's.id=a.stor_id')
->where('s.user_id', $UserId)
->where('o.OrderSn', $OrderSn)
->find();
if (empty($Arr)) {
$this->error('参数错误', 0);
die;
}
$res = Db::name('order')->where('OrderSn', $OrderSn)->delete();
$this->res($res);
}
/**
* 公共接口
* @ApiTitle (账户押金操作)
* @ApiSummary (账户押金操作)
* @ApiMethod (POST)
* @ApiRoute (/api/Common/UpMoneyOperation)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="OrderSn", type="string", required=true, description="订单号")
* @ApiParams (name="type", type="int", required=true, description="操作:1=取消签约,2=删除,3=退还押金")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
)
*/
public function UpMoneyOperation()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$OrderInfo = Db::name('agreement')->where('OrderSn', $params['OrderSn'])->where('user_id', $UserId)->find();
if (empty($OrderInfo)) {
$this->error('参数错误', 0);
}
if ($params['type'] == 1) {
if ($OrderInfo['status'] != 0) {
$this->error('不能取消签约', 0);
}
$res = Db::name('agreement')->where('id', $OrderInfo['id'])->where('user_id', $UserId)->update(['status' => 4]);
$this->Tuikuan($params['OrderSn']);
}
if ($params['type'] == 2) {
if ($OrderInfo['status'] == 2 || $OrderInfo['status'] == 4) {
$res = Db::name('agreement')->where('id', $OrderInfo['id'])->where('user_id', $UserId)->delete();
} else {
$this->error('不能删除订单', 0);
die;
}
}
if ($params['type'] == 3) {
$res = Db::name('agreement')->where('id', $OrderInfo['id'])->where('user_id', $UserId)->update(['status' => 2, 'refind_status' => 0]);
$data = [
'user_id' => $UserId,
'OrderSn' => $params['OrderSn'],
'status' => 0,
'seller_id' => $OrderInfo['seller_id'],
'createtime' => time(),
'updatetime' => time()
];
$IsSave = Db::name('tuikuan')->insert($data);
if (!$IsSave) {
$this->error('添加退款记录失败', 0);
die;
}
}
$this->res($res);
}
}
... ...
... ... @@ -33,6 +33,12 @@ class Index extends Api
* @ApiParams (name="CompanyPeopleNum", type="string", required=true, description="公司员工数")
* @ApiParams (name="image", type="string", required=true, description="营业执照或负责人身份证照片")
* @ApiParams (name="Status", type="string", required=true, description="审核状态:0=待审核,1=审核通过,2=审核未通过")
* @ApiParams (name="CompanyName", type="string", required=true, description="公司或商号名称")
* @ApiParams (name="fa_address", type="string", required=true, description="法定地址")
* @ApiParams (name="yingye_address", type="string", required=true, description="营业地址")
* @ApiParams (name="mianji", type="string", required=true, description="营业场地面积")
* @ApiParams (name="tel", type="string", required=true, description="通信地址")
* @ApiParams (name="web", type="string", required=true, description="万维地址")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
... ... @@ -394,7 +400,7 @@ class Index extends Api
$params = $this->request->param();
if ($params['type'] == 0 || $params['type'] == 1 || $params['type'] == 3) {
$map['a.status'] = ['eq', $params['type']];
}else{
} else {
$map['a.status'] = ['IN', '2,4'];
}
$Arr = Db::name('agreement')
... ... @@ -437,4 +443,297 @@ class Index extends Api
}
$this->success('成功', $data);
}
/**
* 首页接口
* @ApiTitle (押金退还列表)
* @ApiSummary (押金退还列表)
* @ApiMethod (POST)
* @ApiRoute (/api/Index/UpMoneyList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type", type="string", required=true, description="状态:0=待审核,1=已通过,2=已拒绝")
* @ApiParams (name="pages", type="string", required=true, description="pages")
* @ApiParams (name="rows", type="string", required=true, description="rows")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function UpMoneyList()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$Arr = Db::name('tuikuan')
->alias('a')
->join('seller s', 's.id=a.seller_id')
->join('user u', 'u.id=a.user_id')
->where('s.user_id', $UserId)
->where('a.status', $params['type'])
->page($params['pages'], $params['rows'])
->order('id desc')
->field('a.id,a.OrderSn,u.avatar,u.nickname,a.updatetime,a.status');
$Count = Db::name('agreement')
->alias('a')
->join('seller s', 's.id=a.seller_id')
->join('user u', 'u.id=a.user_id')
->where('s.user_id', $UserId)
->where('a.status', $params['type'])
->order('id desc')
->field('a.id,a.OrderSn,u.avatar,u.nickname,a.updatetime,a.status');
if (empty($Arr)) {
$data = [
'Count' => 0,
'List' => []
];
} else {
foreach ($Arr as $k => $v) {
$List[$k]['id'] = $v['id'];
$List[$k]['OrderSn'] = $v['OrderSn'];
$List[$k]['avatar'] = $v['avatar'];
$List[$k]['nickname'] = $v['nickname'];
$List[$k]['updatetime'] = date('Y-m-d H:i:s', $v['updatetime']);
$List[$k]['status'] = $v['status'];
}
$data = [
'Count' => count($Count),
'List' => $List
];
}
$this->success('成功', $data);
}
/**
* 首页接口
* @ApiTitle (首页地图门店)
* @ApiSummary (首页地图门店)
* @ApiMethod (POST)
* @ApiRoute (/api/Index/MapStor)
* @ApiParams (name="battery_code", type="string", required=true, description="电池编码")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function MapStor()
{
$Arr = Db::name('stor')->select();
$Code = input('battery_code');
$map['BatteryCode'] = ['IN', $Code];
//用户电池分类ID
$Id = Db::name('battery_code')->where($map)->value('id');
if (empty($Id)) {
$this->error('系统没有找到该电池分类', 0);
die;
}
if (empty($Arr)) {
$List = [];
} else {
$i = 0;
foreach ($Arr as $k => $v) {
$List[$k]['id'] = $v['id'];
$List[$k]['name'] = $v['name'];
$List[$k]['lng'] = $v['lng'];
$List[$k]['lat'] = $v['lat'];
$JsonArr = json_decode($v['Battery'], true);
foreach ($JsonArr as $k1 => $v1) {
if ($v1['type_id'] == $Id) {
$List[$k]['num'] = $v1['num'];
$i++;
}
}
if ($i == 0) {
$List[$k]['num'] = 0;
}
}
}
$this->success('成功', $List);
}
/**
* 首页接口
* @ApiTitle (去换电)
* @ApiSummary (去换电)
* @ApiMethod (POST)
* @ApiRoute (/api/Index/GoGoing)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="string", required=true, description="门店id")
* @ApiParams (name="battery_code", type="string", required=true, description="电池编号")
* @ApiParams (name="manufactor", type="string", required=true, description="厂家")
* @ApiParams (name="Voltage", type="string", required=true, description="电压")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function GoGoing()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$map['BatteryCode'] = ['IN', $params['battery_code']];
//用户电池分类ID
$Id = Db::name('battery_code')->where($map)->value('id');
if (empty($Id)) {
$this->error('系统没有找到该电池分类', 0);
die;
}
$IsAgreement = Db::name('stor')
->alias('r')
->join('seller s', 'r.user_id=s.user_id')
->join('agreement a', 'a.seller_id=s.id')
->where('a.user_id', $UserId)
->find();
if (empty($IsAgreement)) {
$this->error('请先签订协议', '', '99998');
}
$OrderSn = $this->order_sn();
$data = [
'OrderSn' => $OrderSn,
'stor_id' => $params['id'],
'user_id' => $UserId,
'battery_id' => $Id,
'battery_code' => $params['battery_code'],
'manufactor' => $params['manufactor'],
'Voltage' => $params['Voltage'],
'status' => 0,
'EXP_time' => time() + 60 * 30,
'createtime' => time(),
'updatetime' => time()
];
$res = Db::name('order')->insert($data);
$this->res($res);
}
/**
* 首页接口
* @ApiTitle (我的订单)
* @ApiSummary (我的订单)
* @ApiMethod (POST)
* @ApiRoute (/api/Index/MyOrder)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="pages", type="string", required=true, description="pages")
* @ApiParams (name="rows", type="string", required=true, description="rows")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function MyOrder()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$Arr = Db::name('order')
->alias('o')
->join('stor s', 's.id=a.stor_id')
->where('s.user_id', $UserId)
->page($params['pages'], $params['rows'])
->join('user u', 'u.id=o.user_id')
->join('battery_code b', 'b.id=o.battery_id')
->order('id desc')
->field('o.id,o.OrderSn,o.battery_code,u.nickname,u.avatar,o.status,o.createtime,o.ok_time,b.title,o.manufactor,o.Voltage,o.yes_time');
$Count = Db::name('order')
->alias('o')
->join('stor s', 's.id=a.stor_id')
->where('s.user_id', $UserId)
->join('user u', 'u.id=o.user_id')
->order('id desc')
->field('o.id,o.OrderSn,o.battery_code,u.nickname,u.avatar,o.status,o.createtime,o.ok_time,b.title,o.manufactor,o.Voltage,o.yes_time');
if (empty($Arr)) {
$data = [
'Count' => 0,
'List' => []
];
} else {
foreach ($Arr as $k => $v) {
$List[$k]['id'] = $v['id'];
$List[$k]['OrderSn'] = $v['OrderSn'];
$List[$k]['battery_code'] = $v['battery_code'];
$List[$k]['nickname'] = $v['nickname'];
$List[$k]['avatar'] = $v['avatar'];
$List[$k]['status'] = $v['status'];
$List[$k]['createtime'] = $v['createtime'];
$List[$k]['ok_time'] = $v['ok_time'];
$List[$k]['title'] = $v['title'];
$List[$k]['manufactor'] = $v['manufactor'];
$List[$k]['Voltage'] = $v['Voltage'];
$List[$k]['yes_time'] = $v['yes_time'];
}
$data = [
'Count' => count($Count),
'List' => $List
];
}
$this->success('成功', $data);
}
/**
* 首页接口
* @ApiTitle (账户押金)
* @ApiSummary (账户押金)
* @ApiMethod (POST)
* @ApiRoute (/api/Index/MyUpMoney)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="pages", type="string", required=true, description="pages")
* @ApiParams (name="rows", type="string", required=true, description="rows")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function MyUpMoney()
{
$UserId = $this->IsToken($this->request->header());
$params = $this->request->param();
$Arr = Db::name('agreement')
->alias('a')
->where('a.user_id', $UserId)
->page($params['pages'], $params['rows'])
->order('id desc')
->field('a.id,a.OrderSn,a.UpMoney,a.Money,a.status,a.refind_status,a.CompanyName');
$Count = Db::name('agreement')
->alias('a')
->where('a.user_id', $UserId)
->order('id desc')
->field('a.id,a.OrderSn,a.UpMoney,a.Money,a.status,a.refind_status,a.CompanyName');
if (empty($Arr)) {
$data = [
'Count' => 0,
'List' => []
];
} else {
foreach ($Arr as $k => $v) {
$List[$k]['id'] = $v['id'];
$List[$k]['OrderSn'] = $v['OrderSn'];
$List[$k]['UpMoney'] = $v['UpMoney'];
$List[$k]['Money'] = $v['Money'];
$List[$k]['status'] = $v['status'];
$List[$k]['refind_status'] = $v['refind_status'];
$List[$k]['CompanyName'] = $v['CompanyName'];
}
$data = [
'Count' => count($Count),
'List' => $List
];
}
$this->success('成功', $data);
}
}
... ...
... ... @@ -124,4 +124,113 @@ class Pay extends Api
}
echo $pay->success('支付成功');
}
/**
* 支付接口
* @ApiTitle (充值租金)
* @ApiSummary (充值租金)
* @ApiMethod (POST)
* @ApiRoute (/api/Pay/AddMoney)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="OrderSn", type="integer", required=true, description="订单号")
* @ApiParams (name="Money", type="integer", required=true, description="充值金额")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
'data':{
})
*/
public function AddMoney()
{
$UserId = $this->IsToken($this->request->header());
$OrderSn = input('OrderSn');
$Money = input('Money');
$UserInfo = Db::name('user')->where('id', $UserId)->find();
$PayOrder = $this->PayOrder();
$domain = $this->request->domain();
//生成交易流水
$data = [
'PayOrder' => $PayOrder,
'OrderSn' => $OrderSn,
'type' => 1,
'status' => 0,
'user_id' => $UserId,
'money' => $Money,
'createtime' => time(),
'updatetime' => time()
];
$InsertPayOrder = Db::name('pay_order')->insert($data);
if (!$InsertPayOrder) {
$this->error('支付订单生成失败', 0);
}
$data = [
'amount' => $Money,
'orderid' => $PayOrder,
'type' => "wechat",
'title' => "充值租金",
'notifyurl' => $domain . "/api/pay/AddMoneyNotify/paytype/wechat",
'returnurl' => $domain . "/api/pay/baidu",
'method' => "miniapp",
'openid' => $UserInfo['openid']
];
$return = \addons\epay\library\Service::submitOrder($data);
echo $return;
}
/**
* 订单接口
* @ApiTitle (充值租金回调)
* @ApiSummary (充值租金回调)
* @ApiMethod (POST)
* @ApiRoute (/api/Pay/AddMoneyNotify)
* @ApiParams (name="paytype", type="string", required=true, description="")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
})
*/
public function AddMoneyNotify()
{
$paytype = $this->request->param('paytype');
$pay = \addons\epay\library\Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
die;
}
$data = $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
//$data['out_trade_no'] 支付单号
$order_sn = $data['out_trade_no'];
$order = Db::name('pay_order')->where('PayOrder', $order_sn)->find();
if ($order['status'] == 2) {
return $pay->success('支付成功');
die;
}
//在此编写订单逻辑
//修改支付订单状态
$UpdateStatus = Db::name('pay_order')->where('PayOrder', $data['out_trade_no'])->update(
[
'status' => 1,
'updatetime' => time(),
'WeChatOrder' => $data['transaction_id']
]
);
$OrderInfo = Db::name('agreement')->where('OrderSn', $order['OrderSn'])->find();
$Money = $OrderInfo['Money'] + $order['money'];
Db::name('agreement')->where('OrderSn', $order['OrderSn'])->update(['Money' => $Money]);
if (!$UpdateStatus) {
$this->error('修改支付订单状态失败', 0);
die;
}
} catch (Exception $e) {
}
echo $pay->success('支付成功');
}
}
\ No newline at end of file
... ...
... ... @@ -84,6 +84,8 @@ class User extends Api
$rult = Db::name("user")->where(['openid' => $openid])->find();
$return = [
'token' => $rult['token'],
'avatar' => $param['avatar'],
'nickname' => $param['nickname']
];
$this->success('成功', $return);
}
... ...
... ... @@ -14,7 +14,7 @@ use think\Response;
use think\Route;
use think\Validate;
use think\Db;
use EasyWeChat\Factory;
/**
* API控制器基类
*/
... ... @@ -388,4 +388,32 @@ class Api
$PayOrder = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
return $PayOrder;
}
public function Tuikuan($OrderSn){
$PayOrderInfo = Db::name('pay_order')->where('OrderSn', $OrderSn)->where('type', 1)->find();
$TuiMoney = $PayOrderInfo['UpMoney'] + $PayOrderInfo['Money'];
//配置
$config = [
'app_id' => 'wx6a9080f20326f817',
'payment' => [
'merchant_id' => '1603658973',
'key' => '8695A8185xyzKcdEVfreewayShenzhen',
'cert_path' => '/home/wwwroot/fast/book/addons/epay/certs/apiclient_cert.pem', // XXX: 绝对路径!!!!
'key_path' => '/home/wwwroot/fast/book/addons/epay/certs/apiclient_key.pem', // XXX: 绝对路径!!!!
],
];
$app = Factory::officialAccount($config);
try {
$result = $app->refund->byTransactionId($PayOrderInfo['WeChatOrder'], $PayOrderInfo['PayOrder'], $PayOrderInfo['money'] * 100, $TuiMoney); // 总金额 100, 退款 80,操作员:商户号
//更改订单状态为已退款
Db::name('pay_order')->where('OrderSn', $OrderSn)->where('PayOrder', $PayOrderInfo['PayOrder'])->update(['type' => 0]);
} catch (Exception $e) {
$e->getMessage();
}
if (!$result) {
$this->error('退款失败', 0);
die;
}
}
}
... ...
此 diff 太大无法显示。