当前位置: 首页 > article >正文

微信支付开发-前端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]);
    }
}

http://www.kler.cn/a/308547.html

相关文章:

  • 【计算机网络】Socket编程接口
  • 机器学习——损失函数、代价函数、KL散度
  • P8680 [蓝桥杯 2019 省 B] 特别数的和
  • PostgreSQL中的COPY命令:高效数据导入与导出
  • 信捷 PLC C语言 POU 指示灯交替灭0.5秒亮0.5秒(保持型定时器)
  • WPF中MVVM工具包 CommunityToolkit.Mvvm
  • 大模型笔记03--快速体验dify
  • HTTP的强制缓存和协商缓存有什么区别和联系?
  • 《使用 LangChain 进行大模型应用开发》学习笔记(三)
  • 行人动作行为识别系统源码分享
  • LLamaindex基本使用
  • MYSQL数据库基础篇——DDL
  • 第7篇:【系统分析师】计算机网络
  • openwrt wsdd模块介绍
  • C++(重载)
  • Skytower
  • 第二百三十五节 JPA教程 - JPA Lob列示例
  • k8s(kubernetes)的PV / PVC / StorageClass(理论+实践)
  • 数据库的操作:SQL运算符(算法/比较/逻辑/位)
  • 【大模型专栏—进阶篇】智能对话全总结
  • 力扣100题——动态规划(二)
  • React Native防止重复点击
  • 详解 Pandas 的透视表函数
  • PHP智能化云端培训考试系统小程序源码
  • YOLOv5 Detect.py 改变检测框box线条的粗细,隐藏检测框的检测信息,只显示检测框box
  • PHP在现代Web开发中的高效应用与最佳实践