sifangpay/app/Http/Controllers/Merchant/ApiController.php

278 lines
8.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Merchant;
use App\Models\Area;
use App\Models\DrawMoney;
use App\Models\DrawSet;
use App\Models\MerchantMoneyLog;
use App\Models\MerchantWallet;
use App\Models\Order;
use App\Models\Shop;
use App\Models\ShopServer;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ApiController extends BaseController
{
//
public function index($type, $name, Request $request)
{
switch ($type) {
case 'draw_money_fee':
return $this->drawMoneyFee($request);
break;
case 'draw_money_list':
return $this->drawMoneyTop($request);
break;
case 'money_log_list':
return $this->moneyLog($request);
break;
case 'update_order_fail_number':
return $this->updateOrderWallter($request);
break;
case 'create_key':
return $this->createKey($request);
break;
}
}
/*
* 计算提现手续费
*/
public function drawMoneyFee($request)
{
//搜索费率
$draw_money = $request->input('draw_money');
$draw_type = $request->input('draw_type');
$max_fee = DrawSet::where('draw_type', $draw_type)->whereRaw('start_money <= ? and end_money >= ?', [$draw_money, $draw_money])->first();
$data['money'] = empty($max_fee) ? 0 : $max_fee['money'];
//判断是否在这个范围如果没有判断是否大于最多的那个end_money如果大于取得最后一个
if (empty($max_fee)) {
//$data['money']=DrawSet::max('money');//最大的手续费
//如果没有设置的话则按0.3%计算
$radio = config_cache('draw_config.draw_money_ratio');
$ratio = is_null($radio) || $radio == '' ? '0.3' : $radio;
$data['money'] = $draw_money * $ratio / 100;
}
return $data;
}
public function drawMoneyTop($request)
{
$draw_money = DrawMoney::where(['merchant_id' => $this->getMerchantId()])->limit(10)->orderBy('id', 'desc')->get();
$data = [];
if (count($draw_money) > 0) {
foreach ($draw_money as $k => $v) {
$v['status_name'] = $v->status_name;
$data[] = $v;
}
}
return $this->jsonFormat($data, $request);
}
public function moneyLog($request)
{
$draw_money = MerchantMoneyLog::where(['merchant_id' => $this->getMerchantId()])->limit(10)->orderBy('id', 'desc')->get();
$data = [];
if (count($draw_money) > 0) {
foreach ($draw_money as $k => $v) {
$data[] = $v;
}
}
return $this->jsonFormat($data, $request);
}
//未支付数量
public function updateOrderWallter($request)
{
//更新没有支付订单数量
$order_no_pay = Order::where(['merchant_id' => $this->getMerchantId(), 'pay_status' => 0])->count('id');
$r = MerchantWallet::where(['merchant_id' => $this->getMerchantId()])->update([
'trans_fail_number' => $order_no_pay
]);
$data['number'] = $order_no_pay;
return $this->jsonFormat($data, $request);
}
public function jsonFormat($msg, $request)
{
if ($request->ajax()) {
return response()->json(['error' => 0, 'data' => $msg]);
}
return (['error' => 0, 'data' => $msg]);
}
public function area($request)
{
$input_type = $request->input('input_type', 'option');
$select_id = $request->input('select_id', '');
$list = Area::where('parent_id', $request->input('id', 0))->pluck('name', 'id')->toArray();
$html_str = '';
switch ($input_type) {
case 'option':
$html_str = ' <option value=""></option>';
foreach ($list as $k => $v) {
$html_str .= '<option ' . ($select_id == $k ? "selected" : "") . ' value="' . $k . '">' . $v . '</option>';
}
return ['data' => $html_str];
break;
}
}
protected function uerList($name, Request $request)
{
$json = $request->input('json');
if ($json) {
return $this->apiJson(new User(), 'user', $request);
}
$this->setViewPath('user', 'index');
return $this->display(['group_type' => $name]);
}
protected function shopList($name, Request $request)
{
$json = $request->input('json');
if ($json) {
return $this->apiJson(new Shop(), 'shop', $request);
}
$this->setViewPath('shop', 'index');
return $this->display(['group_type' => $name,]);
}
protected function shopUserAndList($name, Request $request)
{
$json = $request->input('json');
if ($json) {
return $this->apiJson(new Shop(), 'shop', $request);
}
$this->setViewPath('shop_and_user', 'index');
return $this->display(['group_type' => $name,]);
}
protected function shopServerList($name, Request $request)
{
$json = $request->input('json');
if ($json) {
$model = new ShopServer();
$model = $model->where('shop_id', $this->getShopId());
return $this->apiJson($model, 'shop_server', $request);
}
$this->setViewPath('shop_server', 'index');
return $this->display(['group_type' => $name]);
}
public function parseJson($type, $result)
{
$narr = [];
switch ($type) {
case 'user':
foreach ($result as $k => $v) {
$v['sex'] = config('adconfig.sex')[$v['sex']];
$v['thumb'] = picurl($v['thumb']);
$v['type_name'] = config('adconfig.user_group')[$v['type']];
$narr[] = $v;
}
break;
default:
foreach ($result as $k => $v) {
$v['thumb'] = picurl($v['thumb']);
$narr[] = $v;
}
break;
}
return $narr;
}
public function parseSearch($type, $request)
{
$search_arr = [];
switch ($type) {
case 'user':
$name = $request->input('name', '');
$search_arr = [
'type' => [
'type' => '=',
'value' => $request->input('group_type') == 'all' ? '' : $request->input('group_type')
],
'name' => [
'type' => 'likesql',
'value' => $name ? "nickname like '%" . $name . "%' or mobile like '%" . $name . "%' or real_name like '%" . $name . "%'" : ""
]
];
break;
break;
}
return $search_arr;
}
public function apiJson($model, $type, Request $request)
{
$offset = $request->input('page', 1);
$pagesize = $request->input('limit', 1);
$offset = ($offset - 1) * $pagesize;
$order_id = $request->input('sort', 'id');
$order_type = $request->input('order', 'desc');
$debug = $request->input('debug', 0);
$search_arr = $this->parseSearch($type, $request);
$model = $model;
$model = $this->searchKey($model, $search_arr, 2);
$total = $model->count();
$result = $model->skip($offset)->orderBy($order_id, $order_type)->orderBy('id', 'desc')->take($pagesize)->get();;
$narr = array();
$narr = $this->parseJson($type, $result);
$json = [
"status" => 1,
'code' => $total > 0 ? 0 : 1,
'msg' => $total > 0 ? '请求数据成功' : '暂无数据',
'count' => $total,
'data' => $narr
];
if ($debug) {
return $this->jsonDebug($json);
}
return response()->json($json);
}
public function createKey(Request $request)
{
if ($request->input('type_name') == 'appkey') {
$number = date('YmdHis') . str_random(6);
}
if ($request->input('type_name') == 'token') {
$number = str_random(32);
}
return response(['error' => 0, 'msg' => '生成成功', 'number' => $number]);
}
}