uni-app: Creating a web-view with plus to Support Aliyun Captcha

Tech Sharing 2023-12-11
uni-app: Creating a web-view with plus to Support Aliyun Captcha

On uni-app''s App side, use plus.webview.create to load a local verify page integrating Aliyun slider captcha, returning the result to the main app via uni.postMessage.

On uni-app''s App side, use plus to create a web-view that loads a local verify page (local HTML lives in /hybrid/html under the root) to support Aliyun captcha:

appCreateVerify() {
  // Aliyun captcha: open the local verify page
  if (this.wv) { return this.wv.show() }
  this.wv = plus.webview.create('', 'custom-webview', {
    'uni-app': 'none',
    background: 'transparent',
    webviewBGTransparent: true
  }, {
    appkey: "xxxxxxxxxxxxxx",
    scene: "nc_other_app",
    verifyTitle: this.lang.verifyTitle,
    verifyTips: this.lang.verifyTips
  })
  this.wv.loadURL('/hybrid/html/app/awscVerify.html')
  const currentWebview = this.$scope.$getAppWebview()
  // listen for parameters passed via uni.postMessage
  plus.globalEvent.addEventListener('plusMessage', msg => {
    const result = msg.data.args.data
    if (result.name == 'postMessage') {
      this.activateCard(result.arg)
    }
  })
  currentWebview.append(this.wv)
}

For the HTML page: the structure contains a verify container, popup wrapper, title, tip text and the Aliyun captcha container, and imports the Aliyun captcha SDK and the uni-app webview SDK; the styles define a transparent background, a fixed-position verify box, centered layout and a rounded white popup; the script listens for plusready, initializes the Aliyun captcha module, configures appkey and scene, defines success/fail/error callbacks, and on success returns data to the main app via uni.postMessage.