分享一个网页跳转微信小程序代码,可以在网页端直接跳转小程序

<?php
/**
 * 生成小程序 URL Scheme 链接
 * 直接浏览器访问即可生成 weixin://dl/business/?t= 链接
 *
 * 用法:
 *   1. 直接访问 gen_scheme.php(默认跳首页,有效期30天)
 *   2. 带参数访问:
 *      gen_scheme.php?path=/pages/goal/goal&query=id=123&expire_days=7
 */

header('Content-Type: application/json; charset=utf-8');

// ---- 微信配置 ----
$APP_ID = '你的APPID';
$APP_SECRET = '你的secret';

// ---- 获取参数 ----
$path = $_GET['path'] ?? '/pages/index/index';
$query = $_GET['query'] ?? ''
;
$expireDays = intval($_GET['expire_days'] ?? 30);

if ($expireDays < 1 || $expireDays > 30) {
    $expireDays = 30;
}

// ---- 获取 access_token(带文件缓存)----
function getAccessToken($appId, $appSecret) {
    $cacheFile = sys_get_temp_dir() . '/wx_access_token.json';
    if (file_exists($cacheFile)) {
        $cache = json_decode(file_get_contents($cacheFile), true);
        if ($cache && isset($cache['expires_at']) && time() < $cache['expires_at'] - 300) {
            return $cache['access_token'];
        }
    }

    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $resp = curl_exec($ch);
    curl_close($ch);

    if (!$resp) return false;

    $data = json_decode($resp, true);
    if (!isset($data['access_token'])) return false;

    $cache = [
        'access_token' => $data['access_token'],
        'expires_at'   => time() + intval($data['expires_in'] ?? 7200),
    ];
    @file_put_contents($cacheFile, json_encode($cache));

    return $data['access_token'];
}

// ---- 主逻辑 ----
$accessToken = getAccessToken($APP_ID, $APP_SECRET);
if (!$accessToken) {
    echo json_encode(['code' => 0, 'msg' => '获取access_token失败'], JSON_UNESCAPED_UNICODE);
    exit;
}

$postData = json_encode([
    'jump_wxa' => [
        'path'  => $path,
        'query' => $query,
    ],
    'is_expire' => true,
    'expire_type' => 1,
    'expire_interval' => $expireDays,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

$url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token=' . $accessToken;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$resp = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (!$resp) {
    echo json_encode(['code' => 0, 'msg' => '请求微信接口失败'], JSON_UNESCAPED_UNICODE);
    exit;
}

$data = json_decode($resp, true);
if (!isset($data['openlink'])) {
    $errmsg = $data['errmsg'] ?? '未知错误';
    $errcode = $data['errcode'] ?? -1;
    echo json_encode(['code' => 0, 'msg' => "微信接口错误: [{$errcode}] {$errmsg}"], JSON_UNESCAPED_UNICODE);
    exit;
}

echo json_encode([
    'code' => 1,
    'msg' => '生成成功',
    'openlink' => $data['openlink'],
    'path' => $path,
    'query' => $query,
    'expire_days' => $expireDays,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
点击展开完整内容 ↓

回复 0

倒序
登录后回复