微信支付开发-前端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]);
    }
}
相关推荐
残月只会敲键盘4 小时前
php代码审计--常见函数整理
开发语言·php
ac-er88885 小时前
MySQL如何实现PHP输入安全
mysql·安全·php
YUJIANYUE8 小时前
PHP将指定文件夹下多csv文件[即多表]导入到sqlite单文件
jvm·sqlite·php
陈思杰系统思考Jason11 小时前
系统思考—深层结构
百度·微信·微信公众平台·新浪微博·微信开放平台
龙哥·三年风水17 小时前
群控系统服务端开发模式-应用开发-个人资料
分布式·php·群控系统
Dingww101121 小时前
梧桐数据库中的网络地址类型使用介绍分享
数据库·oracle·php
Genius Kim1 天前
SpringCloud Sentinel 服务治理详解
spring cloud·sentinel·php
原机小子1 天前
城镇保障性住房管理:SpringBoot系统解决方案
数据库·spring boot·php
kali-Myon1 天前
NewStarCTF2024-Week5-Web&Misc-WP
前端·python·学习·mysql·web安全·php·web
DK七七1 天前
当今陪玩系统小程序趋势,陪玩系统源码搭建后的适用于哪些平台
小程序·php·uniapp