Files
crmeb_java/app/libs/wechat.js
stivepeim f44c0ade12 修复内容
1. 会员等级背景图去掉校验
	2. 当查询不到会员等级是,按无会员等级展示
	3. 删除商城首页冗余配置在index中的引用
	4. 换绑推广人时,计算上一个推广人的推广人数
	5. pc后台清除推广人时,更新推广人数量
	6. 保证砍价金额最小为0.01
	7. 修复商品删除时购物车关联删除
	8. 删除商品问题修复
	9. 运费模板——指定包邮,包邮数量类型修改
	10. 签到错误修复
	11. 修复我的优惠券只查询20条的问题
	12. 文章列表修复
	13. 拼团商品详情页数据统计显示问题修复
	14. PC后台,账户详情,持有优惠券列表修复
	15. 支付查询参数修复
	16. 修复过期优惠券可以重复领取
	17. 订单邮费切换地址重复计算修复
	18. 判断是否在指定包邮区域内 必须满足件数 + 金额 才能包邮
	19. 支付页面,切换tab,金额计算问题修复
	20. 物流模板新增、编辑——修复
	21. 去除线下邮费的影响
	22. 订单运费计算重写
	23. 下单页面到店自提合计金额不应该计算商品邮费
	24. 新人券领取后,部分使用时间为空——修复
2021-03-19 18:26:43 +08:00

311 lines
6.6 KiB
JavaScript

// #ifdef H5
import WechatJSSDK from "@/plugin/jweixin-module/index.js";
import {
getWechatConfig,
wechatAuth
} from "@/api/public";
import {
WX_AUTH,
STATE_KEY,
LOGINTYPE,
BACK_URL
} from '@/config/cache';
import {
parseQuery
} from '@/utils';
import store from '@/store';
import Cache from '@/utils/cache';
class AuthWechat {
constructor() {
//微信实例化对象
this.instance = WechatJSSDK;
//是否实例化
this.status = false;
this.initConfig = {};
}
isAndroid(){
let u = navigator.userAgent;
return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
}
signLink() {
if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
window.entryUrl = location.href.split('#')[0]
}
return /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : window.entryUrl;
}
/**
* 初始化wechat(分享配置)
*/
wechat() {
return new Promise((resolve, reject) => {
// if (this.status && !this.isAndroid()) return resolve(this.instance);
getWechatConfig()
.then(res => {
this.instance.config(res.data);
this.initConfig = res.data;
this.status = true;
this.instance.ready(() => {
resolve(this.instance);
})
}).catch(err => {
console.log('微信分享配置失败',err);
this.status = false;
reject(err);
});
});
}
/**
* 验证是否初始化
*/
verifyInstance() {
let that = this;
return new Promise((resolve, reject) => {
if (that.instance === null && !that.status) {
that.wechat().then(res => {
resolve(that.instance);
}).catch(() => {
return reject();
})
} else {
return resolve(that.instance);
}
})
}
// 微信公众号的共享地址
openAddress() {
return new Promise((resolve, reject) => {
this.wechat().then(wx => {
this.toPromise(wx.openAddress).then(res => {
resolve(res);
}).catch(err => {
reject(err);
});
}).catch(err => {
reject(err);
})
});
}
// 获取经纬度;
location(){
return new Promise((resolve, reject) => {
this.wechat().then(wx => {
this.toPromise(wx.getLocation,{type: 'wgs84'}).then(res => {
resolve(res);
}).catch(err => {
reject(err);
});
}).catch(err => {
reject(err);
})
});
}
/**
* 微信支付
* @param {Object} config
*/
pay(config) {
return new Promise((resolve, reject) => {
this.wechat().then((wx) => {
this.toPromise(wx.chooseWXPay, config).then(res => {
resolve(res);
}).catch(res => {
resolve(res);
});
}).catch(res => {
reject(res);
});
});
}
toPromise(fn, config = {}) {
return new Promise((resolve, reject) => {
fn({
...config,
success(res) {
resolve(res);
},
fail(err) {
reject(err);
},
complete(err) {
reject(err);
},
cancel(err) {
reject(err);
}
});
});
}
/**
* 自动去授权
*/
oAuth(snsapiBase,url) {
if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return;
const {
code
} = parseQuery();
if (!code || code == uni.getStorageSync('snsapiCode')){
return this.toAuth(snsapiBase,url);
}else{
if(Cache.has('snsapiKey'))
return this.auth(code).catch(error=>{
uni.showToast({
title:error,
icon:'none'
})
})
}
// if (uni.getStorageSync(WX_AUTH) && store.state.app.token) return;
// const {
// code
// } = parseQuery();
// if (!code){
// return this.toAuth(snsapiBase,url);
// }else{
// if(Cache.has('snsapiKey'))
// return this.auth(code).catch(error=>{
// uni.showToast({
// title:error,
// icon:'none'
// })
// })
// }
}
clearAuthStatus() {
}
/**
* 授权登录获取token
* @param {Object} code
*/
auth(code) {
return new Promise((resolve, reject) => {
let loginType = Cache.get(LOGINTYPE);
wechatAuth(code, Cache.get("spread"), loginType)
.then(({
data
}) => {
// let expires_time = data.expires_time.substring(0, 19);
// expires_time = expires_time.replace(/-/g, '/');
// expires_time = new Date(expires_time).getTime();
// let newTime = Math.round(new Date() / 1000);
store.commit("LOGIN", {
token: data.token
// time: expires_time - newTime
});
Cache.set(WX_AUTH, code);
Cache.clear(STATE_KEY);
// Cache.clear('spread');
loginType && Cache.clear(LOGINTYPE);
resolve(data);
})
.catch(reject);
});
}
/**
* 获取跳转授权后的地址
* @param {Object} appId
*/
getAuthUrl(appId,snsapiBase,backUrl) {
let url = `${location.origin}${backUrl}`
if(url.indexOf('?') == -1){
url = url+'?'
}else{
url = url+'&'
}
const redirect_uri = encodeURIComponent(
`${url}scope=${snsapiBase}&back_url=` +
encodeURIComponent(
encodeURIComponent(
uni.getStorageSync(BACK_URL) ?
uni.getStorageSync(BACK_URL) :
location.pathname + location.search
)
)
);
uni.removeStorageSync(BACK_URL);
const state = encodeURIComponent(
("" + Math.random()).split(".")[1] + "authorizestate"
);
uni.setStorageSync(STATE_KEY, state);
if(snsapiBase==='snsapi_base'){
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${state}#wechat_redirect`;
}else{
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
}
}
/**
* 跳转自动登录
*/
toAuth(snsapiBase,backUrl) {
let that = this;
this.wechat().then(wx => {
location.href = this.getAuthUrl(that.initConfig.appId,snsapiBase,backUrl);
})
}
/**
* 绑定事件
* @param {Object} name 事件名
* @param {Object} config 参数
*/
wechatEvevt(name, config) {
let that = this;
return new Promise((resolve, reject) => {
let configDefault = {
fail(res) {
if (that.instance) return reject({
is_ready: true,
wx: that.instance
});
that.verifyInstance().then(wx => {
return reject({
is_ready: true,
wx: wx
});
})
},
success(res) {
return resolve(res,2222);
}
};
Object.assign(configDefault, config);
that.wechat().then(wx => {
if (typeof name === 'object') {
name.forEach(item => {
wx[item] && wx[item](configDefault)
})
} else {
wx[name] && wx[name](configDefault)
}
})
});
}
isWeixin() {
return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
}
}
export default new AuthWechat();
// #endif