webman/push 统计在线人数 PHP

自定义进程定时获取在线人数广播给所有在线用户
process/Status.php

<?php

namespace process;

use app\Request;
use Webman\Push\Api;
use Workerman\Timer;
use Workerman\Worker;
use think\facade\Db;

class Status
{
    public function __construct()
    {
        Timer::add(3, function () {
            $this->status();
        });
    }

    public function status()
    {
        static $user_count = 0, $page_count = 0;
        $api = new Api(
            'http://127.0.0.1:3232',
            config('plugin.webman.push.app.app_key'),
            config('plugin.webman.push.app.app_secret')
        );
        $result = $api->get('/channels', ['filter_by_prefix' => 'user', 'info' => 'subscription_count']);
        if (!$result || $result['status'] != 200) {
            return;
        }
        $channels = $result['result']['channels'];
        $user_count_now = count($channels);
        $page_count_now = 0;
        foreach ($channels as $channel) {
            $page_count_now += $channel['subscription_count'];
        }
        if ($page_count_now == $page_count && $user_count_now === $user_count) {
            return;
        }
        $user_count = $user_count_now;
        $page_count = $page_count_now;

        $api->trigger('online-page', 'update_online_status', "当前<b>$user_count</b>人在线,共打开<b>$page_count</b>个页面");
    }
}

自定义进程配置文件
config/process.php

<?php
use Workerman\Worker;
return [
    // ...其它配置...

    'status'  => [
        'handler'  => process\Status::class
    ]
];

前端js代码

var connection = new Push({
    url: 'wss://xxx.com',
    app_key: 'xxxx',
});
function update_online_status(status) {
    $('#online_status').html(status);
}
var online_channel = connection.subscribe('online-page');
online_channel.on('update_online_status', update_online_status);
var user_channel = connection.subscribe('user-<?=session()->getId()?>');
user_channel.on('update_online_status', update_online_status);

以上是实现类似workerman站点底部实时在线统计的代码


杨佳乐 发布于  2024-4-30 14:13 

站点介绍

该博客站于2024-4-30全新升级,用于记录日常开发中遇到的问题,以及一些新闻热搜

个人承接PHP二次开发、网站定制、公众号小程序定制开发、网站源码搭建、服务器维护等业务

出租宝塔专业版账号,里面包含网站监控、nginx防火墙、网站防篡改等插件,长期租用价格优惠

联系微信:admin_1998
联系QQ:1401586744


杨佳乐 发布于  2024-4-30 11:57 

宝塔定时任务访问url url内有header跳转处理 服务器

使用宝塔计划任务访问url类型不会自动跳转,需要使用shell命令

wget --spider -nv --max-redirect 100000 "http://xxxxx"


杨佳乐 发布于  2024-4-30 11:23 

uniapp 封装request请求 前端

Vue.prototype.sendRequest = function(param,backpage, backtype){
    var _self = this, 
        url = param.url,
        method = param.method,
        header = {},
        data = param.data || {}, 
        token = "",
        hideLoading = param.hideLoading || false;

    //拼接完整请求地址
    var requestUrl = this.siteBaseUrl + url;
    //固定参数:仅仅在小程序绑定页面通过code获取token的接口默认传递了参数token = login
    if(!data.token){//其他业务接口传递过来的参数中无token
        token = uni.getStorageSync(this.sessionKey);//参数中无token时在本地缓存中获取
        console.log("当前token:" + token);
        if(!token){//本地无token需重新登录(退出时清缓存token)
            _self.login(backpage, backtype);
            return;
        }else{
            data.token = token;
        }
    }
    var timestamp = Date.parse(new Date());//时间戳
    data["timestamp"] = timestamp;
    // #ifdef MP-WEIXIN
    data["device"] = "wxapp";
    data["ver"] = "1.1.30";
    // #endif
    // #ifdef APP-PLUS || H5
    data["device"] = "iosapp";
    data["ver"] = "1.0.0";
    // #endif

    //请求方式:GET或POST(POST需配置header: {'content-type' : "application/x-www-form-urlencoded"},)
    if(method){
        method = method.toUpperCase();//小写改为大写
        if(method=="POST"){
            header = {'content-type' : "application/x-www-form-urlencoded"};
        }else{
            header = {'content-type' : "application/json"};
        }
    }else{
        method = "GET";
        header = {'content-type' : "application/json"};
    }
    //用户交互:加载圈
    if (!hideLoading) {
        uni.showLoading({title:'加载中...'});
    }

    console.log("网络请求start");
    //网络请求
    uni.request({
        url: requestUrl,
        method: method,
        header: header,
        data: data,
        success: res => {
            console.log("网络请求success:" + JSON.stringify(res));
            if (res.statusCode && res.statusCode != 200) {//api错误
                uni.showModal({
                    content:"" + res.errMsg
                });
                return;
            }
            if (res.data.code) {//返回结果码code判断:0成功,1错误,-1未登录(未绑定/失效/被解绑)
                if (res.data.code == "-1") {
                    _self.login(backpage, backtype);
                    return;
                }
                if (res.data.code != "0") {
                    uni.showModal({
                        showCancel:false,
                        content:"" + res.data.msg
                    });
                    return;
                }
            } else{
                uni.showModal({
                    showCancel:false,
                    content:"No ResultCode:" + res.data.msg
                });
                return;
            }
            typeof param.success == "function" && param.success(res.data);
        },
        fail: (e) => {
            console.log("网络请求fail:" + JSON.stringify(e));
            uni.showModal({
                content:"" + e.errMsg
            });
            typeof param.fail == "function" && param.fail(e.data);
        },
        complete: () => {
            console.log("网络请求complete");
            if (!hideLoading) {
                uni.hideLoading();
            }
            typeof param.complete == "function" && param.complete();
            return;
        }
    });
}

杨佳乐 发布于  2024-4-30 11:22 

微擎企业付款到用户 PHP

public function sendMoney($openid, $money, $re_user_name = '', $desc = '', $trade_no = '')
    {
        global $_W;
        $paysetting = uni_setting($_W['uniacid'], array('payment'));
        $wechatpay = $paysetting['payment']['wechat'];
        $account_info = $_W['account'];
        $mchid = $wechatpay['mchid'];
        $key = $wechatpay['apikey'];
        $appid = $account_info['key'];

        $desc = isset($desc) ? $desc : '余额提现';
        $money = $money * 100;

        $pars = array();
        $pars['mch_appid'] = $appid;
        $pars['mchid'] = $mchid;
        $pars['nonce_str'] = random(32);
        $pars['partner_trade_no'] = empty($trade_no) ? $mchid . date('Ymd') . rand(1000000000, 9999999999) : $trade_no;
        $pars['openid'] = $openid;
        if (empty($re_user_name)) {
            $pars['check_name'] = 'NO_CHECK';
        } else {
            $pars['check_name'] = 'FORCE_CHECK';
            $pars['re_user_name'] = $re_user_name;
        }
        // NO_CHECK:不校验真实姓名
        // FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)
        // OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)

        $pars['amount'] = $money;
        $pars['desc'] = $desc;
//        $pars['spbill_create_ip'] = isset($wechat['ip']) ? $wechat['ip'] : $_SERVER['SERVER_ADDR'];

        $pars['spbill_create_ip'] = gethostbyname($_SERVER["SERVER_NAME"]);
//        $pars['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];

        ksort($pars, SORT_STRING);
        $string1 = '';
        foreach ($pars as $k => $v) {
            $string1 .= "{$k}={$v}&";
        }
        $string1 .= "key={$key}";
        $pars['sign'] = strtoupper(md5($string1));
        $xml = array2xml($pars);
        $extras = array();

        $path_cert = IA_ROOT . '/addons/weisrc_dish/cert/apiclient_cert_' . $_W['uniacid'] . '.pem';
        $path_key = IA_ROOT . '/addons/weisrc_dish/cert/apiclient_key_' . $_W['uniacid'] . '.pem';
        $path_rootca = IA_ROOT . '/addons/weisrc_dish/cert/rootca_' . $_W['uniacid'] . '.pem';
        $extras['CURLOPT_CAINFO'] = $path_rootca;
        $extras['CURLOPT_SSLCERT'] = $path_cert;
        $extras['CURLOPT_SSLKEY'] = $path_key;

        load()->func('communication');
        $procResult = null;
        $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        $response = ihttp_request($url, $xml, $extras);

        if ($response['code'] == 200) {
            $responseObj = simplexml_load_string($response['content'], 'SimpleXMLElement', LIBXML_NOCDATA);
            $responseObj = (array)$responseObj;
            $return['code'] = $responseObj['return_code'];
            $return['result_code'] = $responseObj['result_code'];
            $return['err_code'] = $responseObj['err_code'];
            $return['msg'] = $responseObj['return_msg'];
            $return['trade_no'] = $pars['partner_trade_no'];
            $return['payment_no'] = $responseObj['payment_no'];

            if ($responseObj['result_code'] != 'SUCCESS') {
                print_r($responseObj);
                exit;
            }
            return $return;
        } else {
            echo '证书错误:';
            print_r($response);
            exit;
        }
    }

    //发放红包
    public function sendRedPack($openid, $money, $send_name = '余额提现', $act_name = '余额提现', $wishing = '祝您生活愉快', $trade_no
    = '')
    {
        global $_W;
        $paysetting = uni_setting($_W['uniacid'], array('payment'));
        $wechatpay = $paysetting['payment']['wechat'];
        $account_info = $_W['account'];
        $mchid = $wechatpay['mchid'];
        $key = $wechatpay['apikey'];
        $appid = $account_info['key'];

        $money = $money * 100;
        $num = 1;
        $url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
        $pars = array();
        $pars['wxappid'] = $appid;
        $pars['mch_id'] = $mchid;
        $pars['nonce_str'] = random(32);
        $pars['mch_billno'] = empty($trade_no) ? $mchid . date('Ymd') . rand(1000000000, 9999999999) : $trade_no;
        $pars['send_name'] = $send_name;
        $pars['re_openid'] = $openid;
        $pars['total_amount'] = $money;
        $pars['total_num'] = $num;
        $pars['wishing'] = $wishing;
//        $pars['client_ip'] = isset($wechat['ip']) ? $wechat['ip'] : $_SERVER['SERVER_ADDR'];
        $pars['client_ip'] = $_SERVER['SERVER_ADDR'];
        $pars['act_name'] = $act_name;
        $pars['remark'] = $act_name;
        ksort($pars, SORT_STRING);
        $string1 = '';
        foreach ($pars as $k => $v) {
            $string1 .= "{$k}={$v}&";
        }
        $string1 .= "key={$key}";
        $pars['sign'] = strtoupper(md5($string1));
        $xml = array2xml($pars);
        $extras = array();

        $path_cert = IA_ROOT . '/addons/weisrc_dish/cert/apiclient_cert_' . $_W['uniacid'] . '.pem';
        $path_key = IA_ROOT . '/addons/weisrc_dish/cert/apiclient_key_' . $_W['uniacid'] . '.pem';
        $path_rootca = IA_ROOT . '/addons/weisrc_dish/cert/rootca_' . $_W['uniacid'] . '.pem';
        $extras['CURLOPT_CAINFO'] = $path_rootca;
        $extras['CURLOPT_SSLCERT'] = $path_cert;
        $extras['CURLOPT_SSLKEY'] = $path_key;

        load()->func('communication');
        $procResult = null;
        $response = ihttp_request($url, $xml, $extras);
        if ($response['code'] == 200) {
            $responseObj = simplexml_load_string($response['content'], 'SimpleXMLElement', LIBXML_NOCDATA);
            $responseObj = (array)$responseObj;
            $return['code'] = $responseObj['return_code'];
            $return['result_code'] = $responseObj['result_code'];
            $return['err_code'] = $responseObj['err_code'];
            $return['msg'] = $responseObj['return_msg'];
            $return['trade_no'] = $pars['mch_billno']; //返回订单号 用于重试
            return $return;
        }
    }

杨佳乐 发布于  2024-4-30 11:22