前端小程序
这个提交包含在:
@@ -0,0 +1,170 @@
|
||||
var jweixin = require("jweixin-module");
|
||||
import http from "@/shopro/request/index";
|
||||
import $platform from "@/shopro/platform";
|
||||
export default {
|
||||
//判断是否在微信中
|
||||
isWechat() {
|
||||
var ua = window.navigator.userAgent.toLowerCase();
|
||||
if (ua.match(/micromessenger/i) == "micromessenger") {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
// 鉴权页面
|
||||
initJssdk(callback) {
|
||||
http("common.wxJssdk", {
|
||||
uri: encodeURIComponent($platform.entry())
|
||||
}).then(res => {
|
||||
jweixin.config({
|
||||
debug: res.data.debug,
|
||||
appId: res.data.appId,
|
||||
timestamp: res.data.timestamp,
|
||||
nonceStr: res.data.nonceStr,
|
||||
signature: res.data.signature,
|
||||
jsApiList: res.data.jsApiList,
|
||||
openTagList: res.data.openTagList
|
||||
});
|
||||
if (callback) {
|
||||
callback(res.data);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
//在需要定位页面调用
|
||||
getLocation(callback) {
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
jweixin.getLocation({
|
||||
type: "gcj02", // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
|
||||
success: function(res) {
|
||||
callback(res);
|
||||
},
|
||||
fail: function(res) {
|
||||
console.log("%c微信H5sdk,getLocation失败:",
|
||||
"color:green;background:yellow");
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
//获取微信收货地址
|
||||
openAddress(callback) {
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
jweixin.openAddress({
|
||||
success: function(res) {
|
||||
callback(res);
|
||||
},
|
||||
fail: function(err) {
|
||||
console.log("%c微信H5sdk,openAddress失败:",
|
||||
"color:green;background:yellow");
|
||||
},
|
||||
complete: function(msg) {}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// 微信扫码
|
||||
scanQRCode(callback) {
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
jweixin.scanQRCode({
|
||||
needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
|
||||
scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
|
||||
success: function(res) {
|
||||
callback(res);
|
||||
},
|
||||
fail: function(res) {
|
||||
console.log("%c微信H5sdk,scanQRCode失败:",
|
||||
"color:green;background:yellow");
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// 微信分享
|
||||
share(data, callback) {
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
var shareData = {
|
||||
title: data.title,
|
||||
desc: data.desc,
|
||||
link: data.path,
|
||||
imgUrl: data.image,
|
||||
success: function(res) {
|
||||
callback(res);
|
||||
// 分享后的一些操作,比如分享统计等等
|
||||
},
|
||||
cancel: function(res) {}
|
||||
};
|
||||
|
||||
jweixin.updateAppMessageShareData(shareData); //新版接口
|
||||
//分享到朋友圈接口
|
||||
// jweixin.updateTimelineShareData(shareData);
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
|
||||
// 打开坐标位置
|
||||
openLocation(data, callback) { //打开位置
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
jweixin.openLocation({ //根据传入的坐标打开地图
|
||||
latitude: data.latitude,
|
||||
longitude: data.longitude
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
// 选择图片
|
||||
chooseImage(callback) { //选择图片
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
jweixin.chooseImage({
|
||||
count: 1,
|
||||
sizeType: ["compressed"],
|
||||
sourceType: ["album"],
|
||||
success: function(rs) {
|
||||
callback(rs);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
//微信支付
|
||||
wxpay(data, callback) {
|
||||
let that = this;
|
||||
this.isWechat() && this.initJssdk(function(res) {
|
||||
jweixin.ready(function() {
|
||||
jweixin.chooseWXPay({
|
||||
timestamp: data
|
||||
.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
|
||||
nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
|
||||
package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
|
||||
signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
|
||||
paySign: data.paySign, // 支付签名
|
||||
success: function(res) {
|
||||
callback(res);
|
||||
},
|
||||
fail: function(res) {
|
||||
console.log("%c微信H5sdk,chooseWXPay失败:",
|
||||
"color:green;background:yellow");
|
||||
callback(res);
|
||||
},
|
||||
cancel: function(res) {
|
||||
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,265 @@
|
||||
/**
|
||||
* Wechat v1.1.0
|
||||
* @Class Wechat
|
||||
* @description shopro-wechat 1.1.0 wehcat第三方登录组件
|
||||
* @Author lidongtony
|
||||
* @Date 2020-05-20
|
||||
* @Email lidongtony@qq.com
|
||||
*/
|
||||
import api from "@/shopro/request/index";
|
||||
import $platform from "@/shopro/platform";
|
||||
import store from "@/shopro/store";
|
||||
import {
|
||||
API_URL
|
||||
} from "@/env";
|
||||
|
||||
export default {
|
||||
eventMap(event) {
|
||||
let map = "";
|
||||
switch (event) {
|
||||
case "login":
|
||||
map = "登录中...";
|
||||
break;
|
||||
case "refresh":
|
||||
map = "更新中...";
|
||||
break;
|
||||
case "bind":
|
||||
map = "绑定中...";
|
||||
break;
|
||||
}
|
||||
return map;
|
||||
},
|
||||
|
||||
async login(param) {
|
||||
let token = "";
|
||||
// #ifdef MP-WEIXIN
|
||||
token = await this.wxMiniProgramOauth("login", param);
|
||||
return token;
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
this.wxOfficialAccountOauth("login");
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
token = await this.wxOpenPlatformOauth("login");
|
||||
return token;
|
||||
// #endif
|
||||
},
|
||||
async refresh() {
|
||||
let token = "";
|
||||
// #ifdef MP-WEIXIN
|
||||
token = await this.wxMiniProgramOauth("refresh");
|
||||
return token;
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
this.wxOfficialAccountOauth("refresh");
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
token = await this.wxOpenPlatformOauth("refresh");
|
||||
return token;
|
||||
// #endif
|
||||
},
|
||||
async bind() {
|
||||
let token = "";
|
||||
// #ifdef MP-WEIXIN
|
||||
token = await this.wxMiniProgramOauth("bind");
|
||||
return token;
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
this.wxOfficialAccountOauth("bind");
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
token = await this.wxOpenPlatformOauth("bind");
|
||||
return token;
|
||||
// #endif
|
||||
},
|
||||
|
||||
// #ifdef H5
|
||||
// 微信公众号网页登录&刷新头像昵称&绑定
|
||||
wxOfficialAccountOauth(event = "login") {
|
||||
if ($platform.get() !== "wxOfficialAccount") {
|
||||
uni.showToast({
|
||||
title: "请在微信浏览器中打开",
|
||||
icon: "none"
|
||||
});
|
||||
throw false;
|
||||
}
|
||||
let host = $platform.host();
|
||||
let payloadObject = {
|
||||
host: host,
|
||||
event,
|
||||
token: (event !== "login" && store.getters.isLogin) ? uni.getStorageSync("token") : ""
|
||||
};
|
||||
let payload = encodeURIComponent(JSON.stringify(payloadObject));
|
||||
let redirect_uri = encodeURIComponent(`${API_URL}user/wxOfficialAccountOauth?payload=${payload}`);
|
||||
let oauthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + store.getters.initWechat.appid +
|
||||
`&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=1`;
|
||||
uni.setStorageSync("lastPage", window.location.href);
|
||||
window.location = oauthUrl;
|
||||
},
|
||||
|
||||
// 微信公众号网页静默登录:临时登录获取OpenId 不入库不绑定用户
|
||||
wxOfficialAccountBaseLogin() {
|
||||
let state = encodeURIComponent(window.location.href);
|
||||
window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + store.getters.initWechat
|
||||
.appid +
|
||||
`&redirect_uri=${API_URL}user/wxOfficialAccountBaseLogin&response_type=code&scope=snsapi_base&state=${state}`;
|
||||
throw "stop";
|
||||
},
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
// 微信开放平台登录
|
||||
wxOpenPlatformOauth(event = "login") {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.login({
|
||||
provider: "weixin",
|
||||
success: function(loginRes) {
|
||||
if (loginRes.errMsg === "login:ok") {
|
||||
let authResult = loginRes.authResult;
|
||||
api("user.wxOpenPlatformOauth", {
|
||||
authResult,
|
||||
event
|
||||
}, that.eventMap(event)).then(res => {
|
||||
if (res.code === 1) {
|
||||
resolve(res.data.token);
|
||||
} else {
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: function(res) {
|
||||
uni.showToast({
|
||||
title: "登录失败,请稍后再试"
|
||||
});
|
||||
resolve(false);
|
||||
api("common.debug", {
|
||||
info: res
|
||||
});
|
||||
},
|
||||
complete: function(res) {}
|
||||
});
|
||||
});
|
||||
},
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 微信小程序静默登录
|
||||
async getWxMiniProgramSessionKey(autoLogin = true) {
|
||||
let sessionStatus = false;
|
||||
let session_key = "";
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.checkSession({
|
||||
success(res) {
|
||||
if (res.errMsg === "checkSession:ok") sessionStatus = true;
|
||||
},
|
||||
complete() {
|
||||
if (uni.getStorageSync("session_key") && sessionStatus && !autoLogin) {
|
||||
resolve(uni.getStorageSync("session_key"));
|
||||
} else {
|
||||
uni.login({
|
||||
success: function(info) {
|
||||
let code = info.code;
|
||||
api("user.getWxMiniProgramSessionKey", {
|
||||
code: code,
|
||||
autoLogin: autoLogin
|
||||
}).then(res => {
|
||||
if (res.code === 1) {
|
||||
uni.setStorageSync("session_key", res
|
||||
.data.session_key);
|
||||
if (autoLogin) {
|
||||
if (res.data.token) {
|
||||
resolve(res.data.token);
|
||||
} else {
|
||||
resolve(false);
|
||||
}
|
||||
}
|
||||
resolve(res.data.session_key);
|
||||
} else {
|
||||
reject(res.msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
// 微信小程序获取用户信息登录
|
||||
wxMiniProgramOauth(event = "login", param = {}) {
|
||||
let that = this;
|
||||
let session_key = uni.getStorageSync("session_key");
|
||||
uni.showLoading({
|
||||
title: that.eventMap(event)
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.getUserProfile({ // 必须手动确认触发
|
||||
desc: "完善会员资料", // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
||||
success: res => {
|
||||
if (res.errMsg === "getUserProfile:ok") {
|
||||
api("user.wxMiniProgramOauth", {
|
||||
event,
|
||||
session_key,
|
||||
encryptedData: res.encryptedData,
|
||||
iv: res.iv,
|
||||
signature: res.signature,
|
||||
nickname: param.nickname,
|
||||
avatar: param.avatar
|
||||
}).then(res => {
|
||||
if (res.code === 1) {
|
||||
uni.setStorageSync("nickname", param.nickname)
|
||||
uni.setStorageSync("avatar", param.avatar)
|
||||
resolve(res.data.token);
|
||||
} else {
|
||||
uni.removeStorageSync("session_key");
|
||||
that.getWxMiniProgramSessionKey(false);
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
complete: res => {
|
||||
uni.hideLoading();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
// 小程序更新
|
||||
checkMiniProgramUpdate() {
|
||||
if (uni.canIUse("getUpdateManager")) {
|
||||
const updateManager = uni.getUpdateManager();
|
||||
updateManager.onCheckForUpdate(function(res) {
|
||||
// 请求完新版本信息的回调
|
||||
if (res.hasUpdate) {
|
||||
updateManager.onUpdateReady(function() {
|
||||
uni.showModal({
|
||||
title: "更新提示",
|
||||
content: "新版本已经准备好,是否重启应用?",
|
||||
success: function(res) {
|
||||
if (res.confirm) {
|
||||
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
||||
updateManager.applyUpdate();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
updateManager.onUpdateFailed(function() {
|
||||
// 新的版本下载失败
|
||||
uni.showModal({
|
||||
title: "已经有新版本了哟~",
|
||||
content: "新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~"
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
// #endif
|
||||
|
||||
|
||||
};
|
||||
在新工单中引用
屏蔽一个用户