支付宝小程序开发遇到的问题

653 阅读2分钟
  • API使用问题
    // my.canIUse 是判断当前小程序的 API、入参或返回值、组件、属性等在当前版本是否支持的 API。
    // 先做一次兼容判断
    if (my.canIUse('createWebViewContext')) {
      this.webViewContext = my.createWebViewContext('web-view-1');
    } else {
      my.alert({
        title: '提示',
        content: '当前支付宝版本过低,无法使用此功能,请升级最新版本支付宝'
      });
    }

  • 更新版本问题
    
    const updateManager = my.getUpdateManager()
    // 监听向支付宝后台请求检查更新结果事件。
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
    // 监听小程序有版本更新事件。
    updateManager.onUpdateReady(function () {
      my.confirm({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })

  • 启动方式
    
    冷启动: 用户打开未启动或者已经销毁的小程序,称为冷启动。小程序执行初始化,触发onLaunch回调函数。
    
    热启动: 用户打开已经关闭但仍处于后台运行的小程序,成为热启动。小程序不会重启,只是从后台切到前台,触发onShow回调函数,不会触发onLaunch函数。当前运行页面的的onShow函数也会被触发。
    
  • 获取启动时的参数

    // 获取本次小程序启动时的参数。如果当前是冷启动,则返回值与 App.onLaunch 的回调参数一致;如果当前是热启动,则返回值与 App.onShow 一致。
    
    const options = my.getEnterOptionsSync();
    console.log(options);

  • 全局变量、app.js

    // 通过 alipays://platformapi/startapp?appId=xxx&page=pages/index/index&query= 打开小程序
    
    // App() 代表顶层应用,管理所有页面和全局数据
    App({
        globalData: {
            params: {}
        },
        onLaunch(options){
            // 第一次打开
        },
        onShow(options){
            // 从后台被 scheme 重新打开
        }
    })
    
    // 获取全局变量
    let app = getApp();
    console.log(app.globalData);
    
  • webview组件
   
   // webview组件只支持企业账号开发使用,会覆盖掉这个页面上的其他原生的组件。
   
   my.postMessage({'sendToMiniProgram': '0'}); // H5页面向小城发送消息
   
    <web-view id="web-view-1" src="{{url}}"  onMessage="onmessage"></web-view>
    this.webViewContext = my.createWebViewContext('web-view-1');//创建webview
    // 小程序和H5通信,需要H5先发消息给小程序,然后小程序回应H5,这样H5才能无延迟的收到小程序发送出去的消息
    onmessage(e){
        if(e.detail.sendToMiniProgram === '0'){
          my.getAuthCode({
            scopes: 'auth_base',
            success: (res) => {
              // console.log(res.authCode);
              this.webViewContext.postMessage({ 'authCode': res.authCode });
            },
          });
        }
      },
    
  • 跳转小程序

    //   直接在当前页面上打开小程序
    alipays://platformapi/startapp?appId=xxx&page=pages/index/index&query=
    
    //   会跳转到支付宝的一个页面再打开
    https://ds.alipay.com/?scheme=alipays://platformapi/startapp?appId=xxx&page=pages/index/index&query=

  • 支付宝接口调用
    
    const fs = require("fs");
    const path = require('path');
    const AlipaySdk = require('alipay-sdk').default;
    
    const alipaySdk = new AlipaySdk({
        appId: 'xxxxx', // 支付宝appid
        privateKey: fs.readFileSync(path.join(__dirname, '/pem/private-key.pem'), 'ascii'),   // 应用私钥
        alipayPublicKey: fs.readFileSync(path.join(__dirname, '/pem/alipayPublic-key.pem'), 'ascii')  // 支付宝公钥
    });
    
    let options = {
        validateSign: true  // 是否对返回值验签
    };
    
    // 获取用户access_token、user_id
    let getUserInfoMethod = 'alipay.system.oauth.token'; // 支付宝接口名
    let params2 = {
        charset: 'UTF-8',
        sign_type: 'RSA2',
        timestamp: '2014-07-24 03:07:50',  // 时间戳(实时获取)
        version: '1.0',
        grantType: 'authorization_code', 
        code:'xxx'  // 支付宝auth_code
    };
    
    alipaySdk.exec(getUserInfoMethod, params2, options).then(result => {
        console.log("alipay.system.oauth.token-------");
        console.log(result);   // 接口返回数据
    }).catch(err=>{
        console.log("alipay.system.oauth.token error-------");
        console.log(err);
    })