微信支付开发-前端api实现

一、操作流程图

二、代码实现

<?php
/**
 * 数字人答题业务流
 * User: 龙哥·三年风水
 * Date: 2024/9/11
 * Time: 14:59
 */
namespace app\controller\shuziren;
use app\controller\Base;
use app\model\param\QuestionParam as PQPModel;
use app\model\answer\QuestionBank;
use app\model\answer\QuestionOpt;
use app\model\answer\StatisticsAnswer;
use app\service\WechatPayNative;
use Redis\Redis;
use app\model\answer\QuestionPrize;
use app\model\answer\QuestionOrder;
use Rabbit\order\Delayed\Pro;
class Answer extends Base
{
    // 获取答题
    public function index(){
        $questParam = PQPModel::dataFind(['id' => 1],'answer_number,answer_accuracy');
        $allList = QuestionBank::getAll(['status' => 1],'id','create_time DESC');
        if(count($allList) < $questParam['answer_number'])return error('题目数量不够');
        $answerArr = array_rand(array_column($allList,'id'),$questParam['answer_number']);
        $answerList = [];
        foreach($answerArr as $k => $v){
            $answerList[$k]['id'] = $k + 1;
            $answerList[$k]['title'] = QuestionBank::dataFind(['id' => $v],'title',false);
            $answerList[$k]['opt'] = QuestionOpt::getAll(['bank_id' => $v],'opt,content,is_correct','id ASC');
        }
        $data['list'] = $answerList;
        $data['answer_accuracy'] = $questParam['answer_accuracy'];
        return success($data);
    }

    // 记录答题
    public function statistics(){
        $param = $this->request->param();
        $validate = new \app\validate\answer\StatisticsAnswer;
        if (!$validate->check($param))return error($validate->getError());//验证参数
        $questParam = PQPModel::dataFind(['id' => 1],'answer_number,answer_interval_period');
        if(($param['correct_rate'] + $param['error_rate']) != $questParam['answer_number'])return error('非法操作');//验证参数
        $data['digitized_human_id'] = $this->token;
        $data['digitized_human_name'] = $param['digitized_human_name'];
        $data['correct_rate'] = $param['correct_rate'];
        $data['error_rate'] = $param['error_rate'];
        $data['id_card'] = $param['id_card'];
        $data['involved_date'] = date('Y-m-d',time());
        $data['status'] = 1;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $data['update_time'] = date('Y-m-d H:i:s',time());
        StatisticsAnswer::save($data,[]);
        $res = [];
        if((int)$questParam['answer_interval_period'] > 0){
            $redis = Redis::select(config('cache.stores.redis.other_db'));
            $keyLog = 'answer_'.$param['id_card'];
            $answerLog = $redis->get($keyLog);
            if(empty($answerLog)){
                $redis->setex($keyLog,(int)$questParam['answer_interval_period']*24*3600,1);
                $res['receive_prize'] = 0;
                $res['order_no'] = create_order();
            }else{
                $res['receive_prize'] = $redis->ttl($keyLog);
                $res['order_no'] = '';
            }
        }
        return success($res);
    }

    // 获取奖品
    public function prize(){
        $idCard = $this->request->param('id_card','');
        if(empty($idCard))return error('参数错误');
        $redis = Redis::select(config('cache.stores.redis.other_db'));
        $keyLog = 'answer_'.$idCard;
        $answerLog = $redis->get($keyLog);
        if(empty($answerLog))return error('非法访问');
        $data = QuestionPrize::dataFind(['id' => 1,'status' => 1],'id,prize_name,prize_price,postage_price,prize_picture',true);
        return success($data);
    }

    // 创建订单
    public function order(){
        $param = $this->request->param();
        $validate = new \app\validate\answer\QuestionOrder;
        if(!$validate->check($param))return error($validate->getError());//验证参数
        $redis = Redis::select(config('cache.stores.redis.other_db'));
        $keyLog = 'answer_'.$param['id_card'];
        $answerLog = $redis->get($keyLog);
        if(empty($answerLog))return error('非法访问');
        $prizeName = QuestionPrize::dataFind(['id' => $param['prize_id'],'status' => 1],'prize_name',false);
        if(empty($prizeName))return error('奖品走丢咯~~~~~');
        $data['digitized_human_id'] = $this->token;
        $data['order_no'] = $param['order_no'];
        $data['consignee_name'] = $param['consignee_name'];
        $data['consignee_phone'] = $param['consignee_phone'];
        $data['prize_name'] = $prizeName;
        $data['area'] = $param['area'];
        $data['details_addr'] = $param['details_addr'];
        $data['id_card'] = $param['id_card'];
        $data['receipt_status'] = 1;
        $data['express_company'] = '';
        $data['express_number'] = '';
        $data['status'] = 1;
        $data['create_time'] = date('Y-m-d H:i:s',time());
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $res = QuestionOrder::save($data,[]);
        Pro::pushSend($res,20*60);
        return success([],'操作成功');
    }

    //获取支付二维码
    public function orderPayment(){
        $orderNo = $this->request->param('order_no');
        if(empty($orderNo))return error('参数错误');
        $receiptStatus = QuestionOrder::dataFind(['order_no' => $orderNo],'receipt_status',false);
        if($receiptStatus != 1)return error('没有未支付订单');
        $postagePrice = QuestionPrize::dataFind(['id' => 1,'status' => 1],'postage_price',false);
        $res = [];
        if($postagePrice > 0){
            $postagePrice = 0.01;
            $nativeService = new WechatPayNative();
            $res = $nativeService->pay_payment($orderNo,$postagePrice);
        }
        return success($res,'操作成功');
    }

    //获取支付结果
    public function orderPaymentResult(){
        $orderNo = $this->request->param('order_no');
        if(empty($orderNo))return error('参数错误');
        $receiptStatus = QuestionOrder::dataFind(['order_no' => $orderNo],'receipt_status',false);
        return success(['receipt_status' => empty($receiptStatus) ? 0 : $receiptStatus],'操作成功');
    }

    //重新生成订单号
    public function answerOrderRepeat(){
        $orderNo = $this->request->param('order_no');
        if(empty($orderNo))return error('参数错误');
        $res = QuestionOrder::dataFind(['order_no' => $orderNo],'id,receipt_status',true);
        if(!empty($res)){
            if($res['receipt_status'] == 4){
                $orderNo = create_order();
                QuestionOrder::save(['order_no' =>  $orderNo,'receipt_status'   =>  1],['id'    =>  $res['id']]);
                Pro::pushSend($res['id'],20*60);
            }
        }
        return success(['order_no' =>  $orderNo]);
    }
}
相关推荐
奥顺2 小时前
PHPUnit使用指南:编写高效的单元测试
大数据·mysql·开源·php
黑客Jack3 小时前
网络安全加密
安全·web安全·php
龙哥·三年风水6 小时前
workman服务端开发模式-应用开发-后端api推送修改二
分布式·gateway·php
计算机徐师兄7 小时前
基于TP5框架的家具购物小程序的设计与实现【附源码、文档】
小程序·php·家具购物小程序·家具购物微信小程序·家具购物
希雅不是希望8 小时前
Ubuntu命令行网络配置
网络·ubuntu·php
龙哥·三年风水10 小时前
workman服务端开发模式-应用开发-后端api推送修改一
分布式·gateway·php
开心工作室_kaic18 小时前
springboot461学生成绩分析和弱项辅助系统设计(论文+源码)_kaic
开发语言·数据库·vue.js·php·apache
火³可²1 天前
PHP接入美团联盟推广
开发语言·php
奥顺1 天前
PHP与AJAX:实现动态网页的完美结合
大数据·mysql·开源·php
网络安全Jack1 天前
防火墙技术与网络安全
web安全·智能路由器·php