first commit
This commit is contained in:
24
libs/apps.js
Normal file
24
libs/apps.js
Normal file
@@ -0,0 +1,24 @@
|
||||
import { appAuth } from '../api/public';
|
||||
|
||||
class Apps{
|
||||
/**
|
||||
* 授权登录获取token
|
||||
* @param {Object} code
|
||||
*/
|
||||
authApp(code) {
|
||||
return new Promise((resolve, reject) => {
|
||||
appAuth(code,{'spread_spid': 0})
|
||||
.then(({
|
||||
data
|
||||
}) => {
|
||||
resolve(data);
|
||||
Cache.set(WX_AUTH, code);
|
||||
Cache.clear(STATE_KEY);
|
||||
loginType && Cache.clear(LOGINTYPE);
|
||||
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
export default new Apps();
|
||||
62
libs/chat.js
Normal file
62
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;
|
||||
104
libs/login.js
Normal file
104
libs/login.js
Normal file
@@ -0,0 +1,104 @@
|
||||
import store from "../store";
|
||||
import Cache from '../utils/cache';
|
||||
import { Debounce } from '@/utils/validate.js'
|
||||
// #ifdef H5
|
||||
import { isWeixin } from "../utils";
|
||||
// import auth from './wechat';
|
||||
// #endif
|
||||
|
||||
import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY, BACK_URL} from './../config/cache';
|
||||
|
||||
function prePage(){
|
||||
let pages = getCurrentPages();
|
||||
let prePage = pages[pages.length - 1];
|
||||
return prePage.route;
|
||||
}
|
||||
|
||||
export const toLogin = Debounce(_toLogin,800)
|
||||
|
||||
export function _toLogin(push, pathLogin) {
|
||||
|
||||
console.log(123456);
|
||||
|
||||
|
||||
store.commit("LOGOUT");
|
||||
let path = prePage();
|
||||
let login_back_url = Cache.get(BACK_URL);
|
||||
console.log(login_back_url);
|
||||
// #ifdef APP
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/login/index'
|
||||
})
|
||||
return
|
||||
// #endif
|
||||
|
||||
|
||||
// #ifdef H5
|
||||
// path = location.href;
|
||||
path = location.pathname + location.search;
|
||||
// #endif
|
||||
if(!pathLogin){
|
||||
pathLogin = '/page/users/login/index'
|
||||
Cache.set('login_back_url',path);
|
||||
}
|
||||
|
||||
// #ifdef H5
|
||||
if (isWeixin()) {
|
||||
let urlData = location.pathname + location.search
|
||||
if (urlData.indexOf('?') !== -1) {
|
||||
urlData += '&go_longin=1';
|
||||
} else {
|
||||
urlData += '?go_longin=1';
|
||||
}
|
||||
if (!Cache.has('snsapiKey')) {
|
||||
auth.oAuth('snsapi_base', urlData);
|
||||
} else {
|
||||
if (['/pages/user/index'].indexOf(login_back_url) == -1) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/wechat_login/index'
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (['/pages/user/index'].indexOf(login_back_url) == -1) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/login/index'
|
||||
})
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
|
||||
if (['pages/user/index','/pages/user/index'].indexOf(login_back_url) == -1) {
|
||||
// #ifdef MP
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/login/index'
|
||||
})
|
||||
// uni.navigateTo({
|
||||
// url: '/pages/users/wechat_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;
|
||||
}
|
||||
|
||||
}
|
||||
47
libs/order.js
Normal file
47
libs/order.js
Normal file
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
preOrderApi
|
||||
} from '@/api/order.js';
|
||||
import util from 'utils/util'
|
||||
|
||||
/**
|
||||
* 去商品详情
|
||||
*/
|
||||
export function goShopDetail(item, uid) {
|
||||
return new Promise(resolve => {
|
||||
if (item.activityH5 && item.activityH5.type === "1") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_seckill_details/index?id=${item.activityH5.id}`
|
||||
})
|
||||
} else if (item.activityH5 && item.activityH5.type === "2") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_bargain_details/index?id=${item.activityH5.id}&startBargainUid=${uid}`
|
||||
})
|
||||
} else if (item.activityH5 && item.activityH5.type === "3") {
|
||||
uni.navigateTo({
|
||||
url: `/pages/activity/goods_combination_details/index?id=${item.activityH5.id}`
|
||||
})
|
||||
} else {
|
||||
resolve(item);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动商品、普通商品、购物车、再次购买预下单
|
||||
*/
|
||||
export function getPreOrder(preOrderType, orderDetails) {
|
||||
return new Promise((resolve, reject) => {
|
||||
preOrderApi({
|
||||
"preOrderType": preOrderType,
|
||||
"orderDetails": orderDetails
|
||||
}).then(res => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/users/order_confirm/index?preOrderNo=' + res.data.preOrderNo
|
||||
});
|
||||
}).catch(err => {
|
||||
return util.Tips({
|
||||
title: err
|
||||
});
|
||||
})
|
||||
});
|
||||
}
|
||||
140
libs/routine.js
Normal file
140
libs/routine.js
Normal file
@@ -0,0 +1,140 @@
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
getUserProfile(){
|
||||
let that = this , code = this.getUserCode();
|
||||
return new Promise( (resolve,reject) => {
|
||||
uni.getUserProfile({
|
||||
lang: 'zh_CN',
|
||||
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
||||
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)=>{
|
||||
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=>{
|
||||
if(res.data.type==='login'){
|
||||
store.commit('LOGIN', {
|
||||
token: res.data.token
|
||||
});
|
||||
store.commit("SETUID", res.data.uid);
|
||||
}
|
||||
return resolve(res);
|
||||
}).catch(res=>{
|
||||
return reject(res);
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default new Routine();
|
||||
Reference in New Issue
Block a user