apicloud拉起小程序並傳遞參數

最近工做中遇到一個需求:App拉起微信小程序。App是用APICloud開發的。查閱APICloud文檔發現 端API->開放SDK->wx模塊下有launchMiniProgram方法能夠實現官方文檔小程序

因而在項目中添加wx模塊。
官方示例代碼:微信小程序

var wx = api.require('wx');
wx.launchMiniProgram({
    apiKey: '', // 字符串,微信開放平臺獲取的appid, 不傳則從當前widget的config.xml中讀取
    miniProgramType: 'test', // 字符串,['test', 'preview', 'release'](開發版,體驗版,正式版)默認test
    userName: '', // 字符串,小程序的原始ID
    path: '', // 字符串,拉起小程序頁面的可帶參數路徑,不填默認拉起小程序首頁
}, function(ret, err) {
    if (ret.status) {
        alert('成功');
    } else {
        alert(err.code);
    }
});

按照官方文檔配置發現只能跳轉到微信,不能拉起小程序。
apicloud App端代碼:
config.xml文件小程序有關配置以下:api

<feature name="wx">
    <param name="urlScheme" value="wx1007b2********68"/>
    <param name="apiKey" value="wx1007b2********68"/>
    <param name="apiSecret" value="3******67176****39244b********25"/>
  </feature>

拉起小程序相關代碼:微信

function launchMini () {
    var wx = api.require('wx')
    wx.launchMiniProgram({
        // apiKey: '', // 不知道是由於配置文件中已經配置過了仍是什麼緣由,帶上該項參數僅能打開微信,沒法拉起小程序,註釋後成功拉起小程序
        miniProgramType: 'preview',
        userName: 'gh_******',
        path: 'pages/index/index?from=app666' // 傳遞參數from 值爲 app666
    }, function(ret, err) {
        if (ret.status) {
            alert('成功')
        } else {
            alert(err.code)
        }
    }
}

小程序端代碼:
index.jsapp

onLoad: function (options) {
    console.log(options)
    console.log(options.from) // 'app666'
}
相關文章
相關標籤/搜索