You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
13 KiB
PHP

<?php
namespace Payment\Controller;
/**
* 后台首页控制器
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class WxPaymentController extends BaseController
{
public $PayStatus=[
"-1"=>"打款失败",
"0"=>"未打款",
"1"=>"打款成功"
];
public $CompanyType = [
"1"=>"下游公司",
"2"=>"下游个人",
"3"=>"上游CP"
];
public $WXRSP=[
"return_code"=>"返回状态码",
"return_msg"=>"返回信息",
"mchid"=>"商户号",
'mch_appid'=>"商户appid",
"result_code"=>"业务结果",
"err_code"=>"错误代码",
"err_code_des"=>"错误描述",
"partner_trade_no"=>"商户订单号",
"payment_no"=>"微信付款单号",
"payment_time"=>"付款成功时间"
];
public function _initialize()
{
$this->admininfo = session('payment_user');;
// $this->DBModel = M("CompanyStatementPool","tab_");
parent::_initialize();
}
public function customerLists()
{
$params = I('get.');
$page = $params['p'] ? intval($params['p']) : 1;
$row = $params['row'] ? intval($params['row']) : 10;
$where = [
'_string'=>'1=1'
];
if(isset($_REQUEST['nickname'])){
$where['nickname'] = ["LIKE","%{$_REQUEST['nickname']}%"];
}
if(isset($_REQUEST['realname'])){
$where['realname'] = ["LIKE","%{$_REQUEST['realname']}%"];
}
if(isset($_REQUEST['type'])){
if($_REQUEST['type'] == 1){
$where['_string'] .= " AND realname = ''";
}
if($_REQUEST['type'] == 2){
$where['_string'] .= " AND realname != ''";
}
}
$data = M('mini_program_user', 'tab_')
->field("wx_json",true)
->where($where)
->page($page,$row)
->select();
foreach ($data as $key => $value) {
if(empty($value['realname'])){
$data[$key]['is_pay'] =0;
}else{
$data[$key]['is_pay'] =1;
}
$data[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
}
$count = M('mini_program_user', 'tab_')->where($where)->count();
$page = set_pagination_all($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign('data', $data);
$this->display();
}
public function updateRealname()
{
$params = I('post.');
M('mini_program_user', 'tab_')->save([
'id'=>$params['id'],
'realname'=>trim($params['realname'])
]);
$this->ajaxReturn([
'success'=>'ok'
]);
}
public function addPayList()
{
$params = I('post.');
$batch = date('Ymd').date('His').sp_random_num(3);
$item = M('mini_program_user',"tab_")->field('realname,openid')->where(['id'=>$params['id']])->find();
$item['statement_money'] = $params['statement_money'];
$this->addListItem($batch,$item);
$this->ajaxReturn(["status"=>1,"msg"=>"添加成功"]);
}
public function customerExport()
{
$where["_string"]="1=1";
if($_REQUEST['ids']){
$where = [
"id"=>["in",$_REQUEST['ids']]
];
}
$data = M('mini_program_user', 'tab_')
->field("wx_json",true)
->where($where)
->select();
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
Vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load("Public/Admin/excel/customer.xlsx");
$line = 2;
//设置模板文件
foreach ($data as $key => $value) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$line, $line-1)->setCellValue('B'.$line, $value['openid'])->setCellValue('C'.$line, $value['realname']);
$line++;
}
$fileName = "微信打款模板";
ob_end_clean();//清除缓冲区,避免乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $fileName .'".xlsx');
header("Content-Disposition:attachment;filename={$fileName}.xlsx");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}
public function lists()
{
$params = I('get.');
$page = $params['p'] ? intval($params['p']) : 1;
$row = $params['row'] ? intval($params['row']) : 10;
$map=[
"_string"=>"1=1",
];
if (isset($_REQUEST['pay_time_start']) && isset($_REQUEST['pay_time_end'])) {
$map['pay_time'] = ['between', [strtotime($_REQUEST['pay_time_start']), strtotime($_REQUEST['pay_time_end']) + 86399]];
} elseif (isset($_REQUEST['pay_time_start'])) {
$map['pay_time'] = ['EGT', strtotime($_REQUEST['pay_time_start'])];
} elseif (isset($_REQUEST['pay_time_end'])) {
$map['pay_time'] = ['ELT', strtotime($_REQUEST['pay_time_end']) + 86399];
}
if(isset($_REQUEST['realname'])){
$map['realname'] = ['LIKE',"%".$_REQUEST['realname']."%"];
}
if(isset($_REQUEST['pay_status'])){
$map['pay_status'] = $_REQUEST['pay_status'];
}
if(isset($_REQUEST['batch_num'])){
$map['batch_num'] = ["LIKE","%{$_REQUEST['batch_num']}%"];
}
$data = M("wx_payment","tab_")
->field("*")
->where($map)
->order("FIELD(pay_status,0,-1,1)");
if (isset($_REQUEST['export']) && $_REQUEST['export']==1) {
$data = $data->select();
}else{
$data = $data->page($page,$row)->select();
}
foreach($data as $k=>&$v){
$v["pay_status_str"] = $this->PayStatus[$v['pay_status']];
$v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
if(empty($v['pay_time'])){
$v['pay_time'] = "--";
}else{
$v['pay_time'] = date('Y-m-d H:i:s',$v['pay_time']);
}
}
if(isset($_REQUEST['export']) && $_REQUEST['export']==1){
data2csv($data,'微信线下打款导出',[
'realname'=>'真实姓名',
'openid'=>'openid',
'statement_money'=>'结算金额',
'batch_num'=>'批次号',
'pay_status_str'=>'打款状态',
'create_time'=>'创建时间',
'pay_time'=>'支付时间'
]);
}
//统计待打款金额
$field = "IFNULL(SUM(CASE WHEN pay_status = 1 THEN statement_money ELSE 0 END),0) as success_money,
IFNULL(SUM(CASE WHEN pay_status = 0 THEN statement_money ELSE 0 END),0) as statement_money,
IFNULL(SUM(CASE WHEN pay_status = -1 THEN statement_money ELSE 0 END),0) as error_money";
$money = M("wx_payment","tab_")->field( $field)->where($map)->find();
$count = M("wx_payment","tab_")->field("count(id) count")->where($map)->find();
$page = set_pagination_all($count['count'], $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign("data",$data);
$this->assign("money",$money);
$this->assign("PayStatus", $this->PayStatus);
$this->display();
}
public function add()
{
$batch = date('Ymd').date('His').sp_random_num(3);
$this->assign("batch",$batch);
$this->meta_title = 'EXCEL导入';
$this->display();
}
public function loopAdd()
{
$p = $_REQUEST;
$batch = $p['batch'];
$checkarr = $p['checkarr'];
//循环获取添加
if(count($checkarr) < 1){
$this->ajaxReturn(["status"=>1,"msg"=>"ok"]);
}
foreach ($checkarr as $k => &$v) {
$this->addListItem($batch,$v);
}
$this->ajaxReturn(["status"=>1,"msg"=>"ok"]);
}
private function addListItem($batch,$v)
{
$pinyin = new \Think\Pinyin();
$wxPayment = M('wx_payment',"tab_");
if(!isset($v['realname']) || !isset($v['openid']) || !isset($v['statement_money']) || $v['realname'] == '' || $v['openid'] == '' || $v['statement_money'] <= 0){
$wxPayment->where("batch_num = '{$batch}'")->delete();
$this->ajaxReturn(["status"=>0,"msg"=>"真实姓名,openid,打款金额均不允许为空"]);
}
$num = mb_strlen($v['realname'], 'UTF8');
$short = '';
for ($i = 0; $i < $num; $i++) {
$str = mb_substr($v['realname'], $i, $i + 1, 'UTF8');
$short .= $pinyin->getFirstChar($str);
}
$v['batch_num'] = $batch;
$v['statement_num'] = "WX{$short}".date('Ymd').date('His').sp_random_string(5);
$v['create_time'] = time();
$res = $wxPayment->add($v);
if($res === false){
$wxPayment->where("batch_num = '{$batch}'")->delete();
$this->ajaxReturn(["status"=>0,"msg"=>"添加失败"]);
}
}
public function showPayment()
{
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$data = M("wx_payment","tab_")
->field("*")
->where("id in ({$ids}) and pay_status < 1")
->select();
$count = M("wx_payment","tab_")->field("sum(statement_money) statement_money")->where("id in ({$ids}) and pay_status < 1")->find();
$paymentInfo = C("WX_PAYMENT") ? C("WX_PAYMENT")['mchid'] : false;
// $this->getAccountMoney();
$this->assign("data",$data);
$this->assign("mobile",$this->admininfo['mobile']);
$this->assign("count",$count);
$this->assign("paymentInfo",$paymentInfo);
$this->display();
}
public function checkVerify()
{
$mobile = $this->admininfo['mobile'];
$verify = $_REQUEST['verify'];
if (!A("Public")->checksafecode($mobile, $verify)) {
$this->ajaxReturn(["error"=>"验证码错误"]);
}
$this->ajaxReturn(["success"=>"验证码验证成功","data"=>[]]);
}
public function doPayment()
{
$id = $_REQUEST['id'];
if (empty(C("WX_PAYMENT"))) {
$this->ajaxReturn(["success"=>"配置错误,无法打款","data"=>['status'=>0]]);
}
Vendor("WeixinPayBtoC/WeixinPayBtoC");
$fund = new \WeixinPayBtoC(C("WX_PAYMENT"));
$dbres = M("wx_payment", "tab_")->where("id='{$id}' and pay_status < 1")->find();
if(!$dbres){
$this->ajaxReturn(["success"=>"打款成功无法重复打款","data"=>['status'=>0]]);
}
if ($dbres['pay_status'] != 1) {
//执行打款
$payres = $fund->wxPayBtoC($dbres['statement_money'], $dbres['openid'], $dbres['realname'], $dbres['statement_num']);
$returnCode = $payres['return_code'];
$resultCode = $payres['result_code'];
$savedata = ["id"=>$dbres['id']];
if (!empty($returnCode) && $returnCode == 'SUCCESS' && !empty($resultCode) && $resultCode == 'SUCCESS') {
$savedata["pay_status"]=1;
} else {
$savedata["pay_status"]=-1;
}
$savedata["pay_info"] = json_encode($payres, JSON_UNESCAPED_UNICODE);
$savedata["pay_time"]=time();
}
M("wx_payment", "tab_")->save($savedata);
if ($savedata['pay_status'] == 1) {
$this->ajaxReturn(["success"=>"打款成功","data"=>['status'=>1]]);
} else {
$this->ajaxReturn(["success"=>"打款失败","data"=>['status'=>0]]);
}
}
public function paymentInfo()
{
if(!isset($_REQUEST['id'])) $this->error("参数错误");
$id = $_REQUEST['id'];
$info = M("wx_payment","tab_")->field("pay_info")->where("id='{$id}'")->find()['pay_info'];
$info = json_decode($info,true);
$senddata = [];
foreach($info as $k => &$v){
if(isset($this->WXRSP[$k])){
$senddata[$this->WXRSP[$k]] = $v;
}
}
$this->assign("info",$senddata);
$this->display();
}
public function delItem()
{
if(!isset($_REQUEST['ids'])){
$this->error('参数错误');
}
$ids = $_REQUEST['ids'];
$map = [
"pay_status"=>["NEQ",1],
"id"=>["IN",$ids]
];
$dbres = M("wx_payment","tab_")->where($map)->delete();
if($dbres === false){
$this->ajaxReturn(["status"=>0,"info"=>"删除失败"]);
}
$this->ajaxReturn(["status"=>1,"info"=>"删除成功"]);
}
}