微信小程序之短连接 分享转发 点击短连接就可以直接打开小程序某个页面
首先,小程序官网地址:https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/qrcode-link/short-link/generateShortLink.html#%E8%B0%83%E7%94%A8%E6%96%B9%E5%BC%8F
官网有个错误:  我就随便试了试 pages前面不加 / 居然成功了!!! 这个官网示例竟然有/ 害我一上午!!!
话不多说,上代码: 实现效果图:需要分享的页面,复制链接功能  html:我这里是个弹层
<view class="infoboxtan sharetanbox" wx:if="{{issharetan}}">
<view class="tanmain">
<view class="sharetopbox">
<view class="sharetxt">分享到</view>
<view class="shareicobox">
<view class="wximgbox">
<image mode="aspectFill" src="http://www.meitang.cn/static/img/images/share_weixin.png" class="wximg" alt="" />
<view class="wximgtxt">微信好友</view>
<button open-type="share" class="sharetap" />
</view>
<view class="wximgbox" bindtap="getaccess_token">
<image mode="aspectFill" src="http://www.meitang.cn/static/img/images/share_fuzhi.png" class="wximg" alt="" />
<view class="wximgtxt">复制链接</view>
</view>
</view>
</view>
<view class="sharequxiao" bindtap="sharequxiaotap">取消</view>
</view>
</view>
css: less格式
.infoboxtan {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.6);
z-index: 999;
.close {
display: block;
position: absolute;
width: 30rpx;
height: 30rpx;
top: 20rpx;
right: 20rpx;
z-index: 2
}
.tanmain {
background: #fff;
position: absolute;
top: 50%;
left: 50%;
width: 680rpx;
height: 490rpx;
margin: -200rpx 0 0 -340rpx;
border-radius: 10rpx;
}
}
.sharetanbox{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
.tanmain{
position:fixed;
height: 400rpx;
background: #f7f7f7;
overflow: hidden;
.sharetopbox{
position: relative;
margin: 0 100rpx;
.sharetxt{
font-size: 40rpx;
color: #333;
font-weight: bold;
line-height: 50rpx;
margin-top: 40rpx;
}
.shareicobox{
position: relative;
width: 400rpx;
margin: 30rpx auto 0;
display: flex;
flex-direction: row;
justify-content: space-between;
.wximgbox{
position: relative;
width: 150rpx;
height: 150rpx;
.sharetap{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
z-index: 2;
}
.wximg{
display: block;
margin: 0 auto;
width: 100rpx;
height: 100rpx;
}
.wximgtxt{
font-size: 30rpx;
color: #333;
line-height: 50rpx;
text-align: center;
}
}
}
}
.sharequxiao{
position: relative;
width: 100%;
height: 100rpx;
background: #fff;
font-size: 30rpx;
color: #333;
line-height: 100rpx;
text-align: center;
margin-top: 35rpx;
}
}
}
js:
const myUrl = require("../../../../utils/requst/url.js");
import { getRentInfo, loupanSign, getRentList, addcollection, delCollection, setzhuanfa, setLiulan } from '../../../../utils/requst/api.js'
data: {
isdenglutan: false,
issharetan: false,
access_token: '',
},
onLoad: function (options) {
this.getaccess_token();
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1]
const url = currentPage.route
let urlWithArgs = `/${url}?`
let canshu = '';
for (let key in options) {
const value = options[key]
urlWithArgs += `${key}=${value}&`
canshu += `${key}=${value}&`
}
urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
canshu = canshu.substring(0, canshu.length - 1)
this.setData({
urlWithArgs: urlWithArgs,
canshu: canshu
})
console.log(urlWithArgs);
console.log(canshu);
}
getaccess_token: function () {
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=写自己小程序的appid&secret=写自己小程序的secret (查找方式同appid)',
data: {
},
method: "get",
header: { 'content-type': 'application/json' },
dataType: 'json',
success: (res) => {
console.log(res);
this.setData({
access_token: res.data.access_token
})
},
fail: (err) => {
}
})
},
sharecopylink: function () {
let _this = this;
let pagelink = this.data.urlWithArgs + '&sharewxid=' + this.data.wxid;
pagelink = pagelink.slice(1);
console.log(pagelink);
wx.request({
url: 'https://api.weixin.qq.com/wxa/genwxashortlink?access_token=' + this.data.access_token,
data: {
'page_url': pagelink,
'page_title': this.data.r_title,
'is_permanent': false,
},
header: { 'content-type': 'application/json' },
method: 'POST',
dataType: 'json',
responseType: 'text',
success: (res) => {
console.log(res);
if (res.statusCode == 200) {
wx.setClipboardData({
data: res.data.link,
success: function success () {
wx.showToast({
title: '复制成功!'
});
_this.setData({
issharetan: false,
})
}
});
}
},
fail: () => {
wx.showToast({
title: '系统错误,请联系攻城狮!',
icon: 'none',
durtion: 2000
})
},
complete: () => { }
});
},
其实这就完事了,得到短连接了!!! 成功了!  但是吧。。。。。。。!!!你不能上线啊,腾讯不让你这样上线啊!!! 小程序后台-开发管理-开发设置里面需要写这个,相信做过成功上线的小程序都知道这个吧!  为啥呢?小程序有人家的理由啊!  那咋办呢?请求开发加入吧。。。。。。。 开发人员怎么做就让他们百度去吧~ 加油!!!
开发人员的解决办法来啦!!!
首先接口:我这里是封装过的接口,至于怎么封装接口自己查哦。。
getaccess_token: function () {
let _this = this;
let pagelink = this.data.urlWithArgs + '&sharewxid=' + this.data.wxid;
pagelink = pagelink.slice(1);
console.log(pagelink);
let params = {
page_url: pagelink,
title: this.data.title,
}
console.log(params);
access_token(params).then((res) => {
console.log(res);
if (res.statusCode == 200) {
wx.setClipboardData({
data: res.data.data.link,
success: function success () {
wx.showToast({
title: '复制成功!'
});
_this.setData({
issharetan: false,
})
}
});
}
})
},
 完美!perfect!
|