我们发布啦
This commit is contained in:
62
app/libs/chat.js
Normal file
62
app/libs/chat.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import $store from "@/store";
|
||||
import { VUE_APP_WS_URL } from "@/utils/index.js";
|
||||
|
||||
const Socket = function() {
|
||||
this.ws = new WebSocket(wss(VUE_APP_WS_URL));
|
||||
this.ws.onopen = this.onOpen.bind(this);
|
||||
this.ws.onerror = this.onError.bind(this);
|
||||
this.ws.onmessage = this.onMessage.bind(this);
|
||||
this.ws.onclose = this.onClose.bind(this);
|
||||
};
|
||||
|
||||
function wss(wsSocketUrl) {
|
||||
let ishttps = document.location.protocol == 'https:';
|
||||
if (ishttps) {
|
||||
return wsSocketUrl.replace('ws:', 'wss:');
|
||||
} else {
|
||||
return wsSocketUrl.replace('wss:', 'ws:');
|
||||
}
|
||||
}
|
||||
|
||||
Socket.prototype = {
|
||||
vm(vm) {
|
||||
this.vm = vm;
|
||||
},
|
||||
close() {
|
||||
clearInterval(this.timer);
|
||||
this.ws.close();
|
||||
},
|
||||
onOpen: function() {
|
||||
console.log("ws open");
|
||||
this.init();
|
||||
this.send({
|
||||
type: "login",
|
||||
data: $store.state.app.token
|
||||
});
|
||||
this.vm.$emit("socket_open");
|
||||
},
|
||||
init: function() {
|
||||
var that = this;
|
||||
this.timer = setInterval(function() {
|
||||
that.send({ type: "ping" });
|
||||
}, 10000);
|
||||
},
|
||||
send: function(data) {
|
||||
return this.ws.send(JSON.stringify(data));
|
||||
},
|
||||
onMessage: function(res) {
|
||||
const { type, data = {} } = JSON.parse(res.data);
|
||||
this.vm.$emit(type, data);
|
||||
},
|
||||
onClose: function() {
|
||||
clearInterval(this.timer);
|
||||
},
|
||||
onError: function(e) {
|
||||
console.log(e);
|
||||
this.vm.$emit("socket_error", e);
|
||||
}
|
||||
};
|
||||
|
||||
Socket.prototype.constructor = Socket;
|
||||
|
||||
export default Socket;
|
||||
61
app/libs/login.js
Normal file
61
app/libs/login.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import store from "../store";
|
||||
import Cache from '../utils/cache';
|
||||
// #ifdef H5 || APP-PLUS
|
||||
import { isWeixin } from "../utils";
|
||||
import auth from './wechat';
|
||||
// #endif
|
||||
|
||||
import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY} from './../config/cache';
|
||||
|
||||
function prePage(){
|
||||
let pages = getCurrentPages();
|
||||
let prePage = pages[pages.length - 1];
|
||||
return prePage.route;
|
||||
}
|
||||
|
||||
export function toLogin(push, pathLogin) {
|
||||
store.commit("LOGOUT");
|
||||
let path = prePage();
|
||||
// #ifdef H5
|
||||
path = location.href;
|
||||
// #endif
|
||||
if(!pathLogin)
|
||||
pathLogin = '/page/users/login/index'
|
||||
Cache.set('login_back_url',path);
|
||||
// #ifdef H5 || APP-PLUS
|
||||
if (isWeixin()) {
|
||||
auth.oAuth();
|
||||
} else {
|
||||
if (path !== pathLogin) {
|
||||
push ? uni.navigateTo({
|
||||
url:'/pages/users/login/index'
|
||||
}) : uni.reLaunch({
|
||||
url: '/pages/users/login/index'
|
||||
});
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
}
|
||||
|
||||
|
||||
export function checkLogin()
|
||||
{
|
||||
let token = Cache.get(LOGIN_STATUS);
|
||||
let expiresTime = Cache.get(EXPIRES_TIME);
|
||||
let newTime = Math.round(new Date() / 1000);
|
||||
if (expiresTime < newTime || !token){
|
||||
Cache.clear(LOGIN_STATUS);
|
||||
Cache.clear(EXPIRES_TIME);
|
||||
Cache.clear(USER_INFO);
|
||||
Cache.clear(STATE_R_KEY);
|
||||
return false;
|
||||
}else{
|
||||
store.commit('UPDATE_LOGIN',token);
|
||||
let userInfo = Cache.get(USER_INFO,true);
|
||||
if(userInfo){
|
||||
store.commit('UPDATE_USERINFO',userInfo);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
19
app/libs/order.js
Normal file
19
app/libs/order.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export function goShopDetail(item,uid) {
|
||||
return new Promise(resolve => {
|
||||
if (item.activity && item.activity.type === "1") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
|
||||
})
|
||||
} else if (item.activity && item.activity.type === "2") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}&bargain=${uid}`
|
||||
})
|
||||
} else if (item.activity && item.activity.type === "3") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
|
||||
})
|
||||
} else {
|
||||
resolve(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
141
app/libs/routine.js
Normal file
141
app/libs/routine.js
Normal file
@@ -0,0 +1,141 @@
|
||||
import store from '../store';
|
||||
import { checkLogin } from './login';
|
||||
import { login } from '../api/public';
|
||||
import Cache from '../utils/cache';
|
||||
import { STATE_R_KEY, USER_INFO, EXPIRES_TIME, LOGIN_STATUS} from './../config/cache';
|
||||
|
||||
class Routine
|
||||
{
|
||||
|
||||
constructor()
|
||||
{
|
||||
this.scopeUserInfo = 'scope.userInfo';
|
||||
}
|
||||
|
||||
async getUserCode(){
|
||||
let isAuth = await this.isAuth(), code = '' ;
|
||||
if(isAuth)
|
||||
code = await this.getCode();
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
getUserInfo(){
|
||||
let that = this , code = this.getUserCode();
|
||||
return new Promise( (resolve,reject) => {
|
||||
uni.getUserInfo({
|
||||
lang: 'zh_CN',
|
||||
success(user) {
|
||||
if(code) user.code = code;
|
||||
resolve({userInfo:user,islogin:false});
|
||||
},
|
||||
fail(res){
|
||||
reject(res);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
authorize()
|
||||
{
|
||||
let that = this;
|
||||
return new Promise((resolve,reject)=>{
|
||||
if(checkLogin())
|
||||
return resolve({
|
||||
userInfo:Cache.get(USER_INFO,true),
|
||||
islogin:true,
|
||||
});
|
||||
uni.authorize({
|
||||
scope: that.scopeUserInfo,
|
||||
success() {
|
||||
resolve({islogin:false});
|
||||
},
|
||||
fail(res){
|
||||
reject(res);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async getCode(){
|
||||
let provider = await this.getProvider();
|
||||
return new Promise((resolve,reject)=>{
|
||||
if(Cache.has(STATE_R_KEY)){
|
||||
return resolve(Cache.get(STATE_R_KEY));
|
||||
}
|
||||
uni.login({
|
||||
provider:provider,
|
||||
success(res) {
|
||||
if (res.code) Cache.set(STATE_R_KEY, res.code ,10800);
|
||||
return resolve(res.code);
|
||||
},
|
||||
fail(){
|
||||
return reject(null);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务供应商
|
||||
*/
|
||||
getProvider()
|
||||
{
|
||||
return new Promise((resolve,reject)=>{
|
||||
uni.getProvider({
|
||||
service:'oauth',
|
||||
success(res) {
|
||||
resolve(res.provider);
|
||||
},
|
||||
fail() {
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否授权
|
||||
*/
|
||||
isAuth(){
|
||||
let that = this;
|
||||
return new Promise((resolve,reject)=>{
|
||||
uni.getSetting({
|
||||
success(res) {
|
||||
if (!res.authSetting[that.scopeUserInfo]) {
|
||||
resolve(true)
|
||||
} else {
|
||||
resolve(true);
|
||||
}
|
||||
},
|
||||
fail(){
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
authUserInfo(code,data)
|
||||
{
|
||||
return new Promise((resolve, reject)=>{
|
||||
login(code,data).then(res=>{
|
||||
// let time = res.data.expiresTime - Cache.time();
|
||||
store.commit('UPDATE_USERINFO', res.data.user);
|
||||
store.commit('LOGIN', {token:res.data.token});
|
||||
store.commit('SETUID', res.data.user.uid);
|
||||
// Cache.set(EXPIRES_TIME,res.data.expiresTime,time);
|
||||
Cache.set(USER_INFO,res.data.user);
|
||||
return resolve(res);
|
||||
}).catch(res=>{
|
||||
return reject(res);
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default new Routine();
|
||||
268
app/libs/wechat.js
Normal file
268
app/libs/wechat.js
Normal file
@@ -0,0 +1,268 @@
|
||||
// #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 = document.location.href
|
||||
}
|
||||
return /(Android)/i.test(navigator.userAgent) ? document.location.href : window.entryUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化wechat(分享配置)
|
||||
*/
|
||||
wechat() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// if (this.status && !this.isAndroid()) return resolve(this.instance);
|
||||
getWechatConfig()
|
||||
.then(res => {
|
||||
let data = res.data;
|
||||
// delete data.jsApiTicket;
|
||||
this.instance.config(data);
|
||||
this.initConfig = 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);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付
|
||||
* @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 => {
|
||||
reject(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() {
|
||||
if (uni.getStorageSync(WX_AUTH) && store.state.app.token) return;
|
||||
const {
|
||||
code
|
||||
} = parseQuery();
|
||||
if (!code) return this.toAuth();
|
||||
}
|
||||
|
||||
clearAuthStatus() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权登陆获取token
|
||||
* @param {Object} code
|
||||
*/
|
||||
auth(code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let loginType = Cache.get(LOGINTYPE);
|
||||
wechatAuth(code, parseInt(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);
|
||||
loginType && Cache.clear(LOGINTYPE);
|
||||
resolve();
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取跳转授权后的地址
|
||||
* @param {Object} appId
|
||||
*/
|
||||
getAuthUrl(appId) {
|
||||
const redirect_uri = encodeURIComponent(
|
||||
`${location.origin}/pages/auth/index?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);
|
||||
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() {
|
||||
let that = this;
|
||||
this.wechat().then(wx => {
|
||||
location.href = this.getAuthUrl(that.initConfig.appId);
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定事件
|
||||
* @param {Object} name 事件名
|
||||
* @param {Object} config 参数
|
||||
*/
|
||||
wechatEvevt(name, config) {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
let configDefault = {
|
||||
fail(res) {
|
||||
console.log('前解放后的金凤凰3');
|
||||
console.log(res);
|
||||
console.log(that.instance);
|
||||
console.log('前解放后的金凤凰2');
|
||||
if (that.instance) return reject({
|
||||
is_ready: true,
|
||||
wx: that.instance
|
||||
});
|
||||
that.verifyInstance().then(wx => {
|
||||
return reject({
|
||||
is_ready: true,
|
||||
wx: wx
|
||||
});
|
||||
console.log('么么么么1');
|
||||
console.log(wx);
|
||||
console.log('么么么么2');
|
||||
})
|
||||
},
|
||||
success(res) {
|
||||
console.log(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
|
||||
Reference in New Issue
Block a user