Commit 0e81ca23 authored by 俞嘉婷's avatar 俞嘉婷

feat: 免登+webview分享

parent da2529d9
{
"pages": [
"pages/index/index",
"pages/logs/logs"
"pages/dbindex/dbindex",
"pages/webview/webview"
],
"window": {
"navigationBarTextStyle": "black",
......
const app = getApp();
/**
* 樱花节
*/
Page({
data: {
},
options: {},
async onLoad(options) {
console.log('页面参数', options)
this.options = options;
this.goAct();
},
onShow() {
},
goAct() {
// wx.showToast({
// title: "活动暂未开始,敬请期待~",
// icon: 'none',
// duration: 1000
// });
const { env, redirect, isNeedLogin } = this.options
let url = `/pages/webview/webview?isNeedLogin=${isNeedLogin || 1}`;;
if (env) url += `&env=${env}`;
if (redirect) url += `&redirect=${redirect}`;
console.log('跳转链接', url);
wx.redirectTo({ url });
},
})
\ No newline at end of file
{
"navigationBarTitleText": "",
"usingComponents": {
}
}
\ No newline at end of file
<view class="container">
<!-- <image class="bg" src="https://yun.duiba.com.cn/polaris/mid_bg0402.8066da5ae610adaa8bf3aacfb0c99b5aa39d7bde.png" bindtap="goAct" /> -->
</view>
\ No newline at end of file
.container {
min-height: 1448rpx;
background-size: 100% 100%;
background-repeat: no-repeat;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
}
.bg {
width: 750rpx;
height: 1624rpx;
position: absolute;
top: 0rpx;
left: 0rpx;
}
// webview.js
// 获取应用实例
const DEFAULT_PROJECT = {
'test': {
redirectUrl: 'https://activity.m.duibatest.com.cn/projectx/p94791461/index.html?appID=19487',
loginDomain: "https://activity.m.duibatest.com.cn"
},
'pre': {
redirectUrl: 'https://98655-activity.m.dexfu.cn/projectx/p86defdb9/index.html?appID=98655',
loginDomain: "TODO正式域名"
},
'prod': {
redirectUrl: 'https://98655-activity.m.dexfu.cn/projectx/pad0649b4/index.html?appID=98655',
loginDomain: "TODO正式域名"
},
}
Page({
data: {
// 分享信息
h5ShareInfo: {
title: '',
imgUrl: '',
link: ''
},
},
env: 'test', // TODO 'test', 'pre', 'prod'
redirectUrl: '',
options: {},
onLoad(options) {
this.initWebView(options)
},
async initWebView(options) {
console.log("options:", options)
this.options = options;
let {
env,
redirect,
isNeedLogin = '1',
} = this.options || {}
if (env) this.env = env;
console.log('DEFAULT_PROJECT[env]', this.env, DEFAULT_PROJECT[this.env])
let redirectUrl = redirect ? decodeURIComponent(redirect) : DEFAULT_PROJECT[this.env]?.redirectUrl;
this.redirectUrl = redirectUrl
if (isNeedLogin == '1') {
wx.login({
success: async (res) => {
console.log(111, res)
if (res.code) {
const loginUrl = `${DEFAULT_PROJECT[this.env]?.loginDomain}/wechat/wine/autoLogin?code=${res.code}&redirect=${encodeURIComponent(redirectUrl)}`
console.log('免登接口:', loginUrl)
// this.setData({
// path: loginUrl,
// })
wx.request({
url: loginUrl,
method: 'GET',
success: res => {
console.log('login', res)
if (res.statusCode == 200) {
this.setData({
path: res.data,
})
} else {
wx.showToast({
title: '登录失败',
icon: 'none',
duration: 4000
})
}
},
fail: err => {
// console.log(err);
},
complete: res => {
// console.log(res);
}
});
}
}
})
} else if (redirectUrl) {
this.setData({
path: redirectUrl,
})
}
},
getMessage(e) {
const {
data
} = e.detail;
console.log("getMessage----", data)
if (data) {
this.setData({
h5ShareInfo: data[data.length - 1]
})
}
},
onShareAppMessage: function (options) { //分享
const { h5ShareInfo } = this.data;
console.log("onShareAppMessage--------", JSON.stringify(options))
const shareUrl = `pages/dbindex/dbindex?env=${this.env}isNeedLogin=1&redirect=${encodeURIComponent(
h5ShareInfo.link || this.redirectUrl
)}`;
return {
title: h5ShareInfo.title,
path: shareUrl,
imageUrl: h5ShareInfo.imgUrl,
success: function (res) {
console.log(res, '成功')
// wx.showToast({
// title: '分享成功',
// icon: 'none'
// })
// 转发成功
},
fail: function (res) {
console.log(res + '失败');
// 转发失败
},
complete: function (res) {
// 不管成功失败都会执行
console.log(res, '成功或失败');
}
}
},
})
{
"usingComponents": {
},
"navigationBarTitleText": ""
}
\ No newline at end of file
<!-- webview.wxml -->
<view class="webcontainer">
<view class="webview">
<web-view src="{{path}}" bindmessage="getMessage"></web-view>
</view>
<!-- <view class="tabContainer">
<tab></tab>
</view> -->
</view>
\ No newline at end of file
/**webview.wxss**/
.webcontainer {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment