第一次提交
This commit is contained in:
46
store/modules/app.js
Executable file
46
store/modules/app.js
Executable file
@@ -0,0 +1,46 @@
|
||||
import { STORE_ID, PLATFORM, REFEREE_ID } from '@/store/mutation-types'
|
||||
import storage from '@/utils/storage'
|
||||
|
||||
const app = {
|
||||
state: {
|
||||
// 当前商城的ID
|
||||
storeId: null,
|
||||
// 当前终端平台
|
||||
platform: '',
|
||||
// 推荐人ID
|
||||
refereeId: null
|
||||
},
|
||||
|
||||
mutations: {
|
||||
SET_STORE_ID: (state, value) => {
|
||||
state.storeId = value
|
||||
},
|
||||
SET_PLATFORM: (state, value) => {
|
||||
state.platform = value
|
||||
},
|
||||
SET_REFEREE_ID: (state, value) => {
|
||||
state.refereeId = value
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
|
||||
// 记录推荐人ID
|
||||
SaveRefereeId({ commit }, value) {
|
||||
const store = this
|
||||
const refereeId = parseInt(value)
|
||||
return new Promise((resolve, reject) => {
|
||||
if (refereeId > 0 && store.getters.userId != refereeId) {
|
||||
// 保存推荐人ID到缓存
|
||||
storage.set(REFEREE_ID, refereeId)
|
||||
// 记录到store全局变量
|
||||
commit('SET_REFEREE_ID', refereeId)
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default app
|
||||
273
store/modules/chat.js
Normal file
273
store/modules/chat.js
Normal file
@@ -0,0 +1,273 @@
|
||||
import storage from '@/utils/storage'
|
||||
import {
|
||||
ACCESS_TOKEN,
|
||||
USER_ID
|
||||
} from '@/store/mutation-types'
|
||||
import io from '@hyoga/uni-socket.io';
|
||||
import {
|
||||
isEmpty
|
||||
} from '@/utils/util'
|
||||
import Vue from 'vue'
|
||||
import {
|
||||
socketUrl
|
||||
} from "@/config.js"
|
||||
import {
|
||||
getConversationList,
|
||||
sendMessage,
|
||||
getFriemdMessages,
|
||||
postMarkRead,
|
||||
removeConversation
|
||||
} from '@/api/chat.js'
|
||||
import {
|
||||
getUserDetail
|
||||
} from '@/api/love-user-profile.js'
|
||||
import {getArticleCommentUnreadCount} from '@/api/article-comment.js'
|
||||
import {pageLike} from '@/api/user-like.js'
|
||||
import {pageLook} from '@/api/user-look.js'
|
||||
const theme = {
|
||||
state: {
|
||||
conversations: {},
|
||||
friends: [],
|
||||
unReadCommentNumber: 0,
|
||||
unReadLikeNumber: 0,
|
||||
unReadLikeMeNumber: 0,
|
||||
unReadLookNumber: 0,
|
||||
},
|
||||
|
||||
mutations: {
|
||||
ADD_FRIEND(state, userInfo) {
|
||||
if (!state.conversations[userInfo.userId]) {
|
||||
Vue.set(state.conversations, userInfo.userId, {
|
||||
friendInfo: userInfo,
|
||||
messages: [],
|
||||
unRead: 0,
|
||||
updateTime: new Date()
|
||||
});
|
||||
}
|
||||
},
|
||||
REMOVE_FRIEND(state, userId) {
|
||||
if (state.conversations[userId]) {
|
||||
Vue.delete(state.conversations,userId)
|
||||
}
|
||||
},
|
||||
ADD_MESSAGE(state, {
|
||||
userId,
|
||||
message,
|
||||
friendInfo
|
||||
}) {
|
||||
|
||||
if (!state.conversations[userId]) {
|
||||
|
||||
// todo getFriendInfo
|
||||
Vue.set(state.conversations, userId, {
|
||||
messages: [],
|
||||
unRead: 0,
|
||||
friendInfo,
|
||||
updateTime: new Date()
|
||||
});
|
||||
}
|
||||
state.conversations[userId].messages.push(message);
|
||||
// console.log(message.formUserId, storage.get(USER_ID));
|
||||
if (message.formUserId != storage.get(USER_ID)) {
|
||||
state.conversations[userId].unRead++
|
||||
}
|
||||
state.conversations[userId].updateTime = new Date()
|
||||
console.log(message.type);
|
||||
if (message.type == 'image') {
|
||||
state.conversations[userId].content = '[图片]'
|
||||
} else if (message.type == 'card') {
|
||||
state.conversations[userId].content = '[卡片]'
|
||||
} else {
|
||||
state.conversations[userId].content = message.content
|
||||
}
|
||||
|
||||
uni.$emit('message' + userId, message)
|
||||
},
|
||||
|
||||
SET_CONVERSATION(state, data) {
|
||||
state.conversations = data
|
||||
},
|
||||
|
||||
SET_MESSAGES(state, {
|
||||
friendId,
|
||||
messages
|
||||
}) {
|
||||
state.conversations[friendId].messages = messages
|
||||
},
|
||||
MARK_READ(state, friendId) {
|
||||
state.conversations[friendId].unRead = 0
|
||||
},
|
||||
SET_UNREAD_COMMENT_NUMBER(state, number) {
|
||||
if(number < 0) {
|
||||
return
|
||||
}
|
||||
state.unReadCommentNumber = number
|
||||
},
|
||||
SET_UNREAD_LIKE_NUMBER(state, number) {
|
||||
if(number < 0) {
|
||||
return
|
||||
}
|
||||
state.unReadLikeNumber = number
|
||||
},
|
||||
SET_UNREAD_LIKE_ME_NUMBER(state, number) {
|
||||
if(number < 0) {
|
||||
return
|
||||
}
|
||||
state.unReadLikeMeNumber = number
|
||||
},
|
||||
SET_UNREAD_LOOK_NUMBER(state, number) {
|
||||
if(number < 0) {
|
||||
return
|
||||
}
|
||||
state.unReadLookNumber = number
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
async RemoveFriend({
|
||||
state,
|
||||
commit
|
||||
}, friendId){
|
||||
removeConversation(state.conversations[friendId].id)
|
||||
commit('REMOVE_FRIEND', friendId);
|
||||
|
||||
},
|
||||
|
||||
async SendMessage({
|
||||
commit
|
||||
}, data) {
|
||||
await sendMessage(data)
|
||||
data.createTime = new Date()
|
||||
commit('ADD_MESSAGE', {
|
||||
userId: data.toUserId,
|
||||
message: data
|
||||
})
|
||||
},
|
||||
async LoadFriendMessage({
|
||||
commit
|
||||
}, friendId) {
|
||||
const res = await getFriemdMessages(friendId)
|
||||
commit('SET_MESSAGES', {
|
||||
friendId: friendId,
|
||||
messages: res.data
|
||||
})
|
||||
|
||||
},
|
||||
ConnectSocket({
|
||||
state,
|
||||
commit
|
||||
}, data) {
|
||||
console.log("ConnectSocket");
|
||||
const socket = io(socketUrl, {
|
||||
//这里是连接时所携带的数据
|
||||
query: {
|
||||
token: storage.get(ACCESS_TOKEN),
|
||||
userId: storage.get(USER_ID)
|
||||
},
|
||||
//连接方式
|
||||
transports: ['websocket', 'polling'],
|
||||
//过期时间
|
||||
timeout: 5000,
|
||||
});
|
||||
|
||||
socket.on('connect', () => {
|
||||
const {
|
||||
id
|
||||
} = socket
|
||||
console.log("socketio 已连接 clientId" + id);
|
||||
uni.socket = socket
|
||||
// 获取聊天记录
|
||||
getConversationList().then(res => {
|
||||
const data = {}
|
||||
res.data.forEach(item => {
|
||||
item.messages = []
|
||||
if(item.friendInfo){
|
||||
data[item.friendInfo.userId] = item
|
||||
}
|
||||
})
|
||||
console.log('SET_CONVERSATION', data);
|
||||
commit('SET_CONVERSATION', data)
|
||||
}).catch(err=>{
|
||||
console.log(err)
|
||||
})
|
||||
// 获取恋爱圈未读消息
|
||||
getArticleCommentUnreadCount().then(res=>{
|
||||
commit('SET_UNREAD_COMMENT_NUMBER', res.data)
|
||||
})
|
||||
// 喜欢我和我喜欢
|
||||
pageLike({
|
||||
sceneType: 'UN_READ_USER_LIKE'
|
||||
}).then(res=>{
|
||||
commit('SET_UNREAD_LIKE_NUMBER', res.data.count)
|
||||
})
|
||||
pageLike({
|
||||
sceneType: 'UN_READ_USER_LIKE_ME'
|
||||
}).then(res=>{
|
||||
commit('SET_UNREAD_LIKE_ME_NUMBER', res.data.count)
|
||||
})
|
||||
pageLook({
|
||||
sceneType: 'UN_READ_USER_LOOK'
|
||||
}).then(res=>{
|
||||
commit('SET_UNREAD_LOOK_NUMBER', res.data.count)
|
||||
})
|
||||
})
|
||||
|
||||
socket.on('message', (data) => {
|
||||
if (!state.conversations[data.formUserId]) {
|
||||
getUserDetail(data.formUserId).then(res => {
|
||||
commit('ADD_MESSAGE', {
|
||||
userId: data.formUserId,
|
||||
message: data,
|
||||
friendInfo: res.data.userInfo
|
||||
})
|
||||
})
|
||||
} else {
|
||||
commit('ADD_MESSAGE', {
|
||||
userId: data.formUserId,
|
||||
message: data
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
socket.on('pyq', (data) => {
|
||||
commit('SET_UNREAD_COMMENT_NUMBER', data)
|
||||
})
|
||||
|
||||
socket.on('likeme', (data) => {
|
||||
console.log("likeme",state.unReadLikeMeNumber , data);
|
||||
commit('SET_UNREAD_LIKE_ME_NUMBER', state.unReadLikeMeNumber + data)
|
||||
})
|
||||
|
||||
socket.on('look', (data) => {
|
||||
console.log("look",state.unReadLookNumber , data);
|
||||
commit('SET_UNREAD_LOOK_NUMBER', state.unReadLookNumber + data)
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
MarkRead({
|
||||
state,
|
||||
commit
|
||||
}, friendId) {
|
||||
postMarkRead({
|
||||
id: state.conversations[friendId].id
|
||||
})
|
||||
commit('MARK_READ', friendId)
|
||||
},
|
||||
|
||||
CloseSocket({
|
||||
state,
|
||||
commit
|
||||
}, data) {
|
||||
if (uni.socket) {
|
||||
uni.socket.close()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default theme
|
||||
6
store/modules/index.js
Executable file
6
store/modules/index.js
Executable file
@@ -0,0 +1,6 @@
|
||||
import app from './app'
|
||||
import user from './user'
|
||||
import theme from './theme'
|
||||
import chat from './chat'
|
||||
|
||||
export { app, user, theme, chat }
|
||||
37
store/modules/notice.js
Executable file
37
store/modules/notice.js
Executable file
@@ -0,0 +1,37 @@
|
||||
import storage from '@/utils/storage'
|
||||
import { getConversationList, sendMessage, getFriemdMessages,postMarkRead } from '@/api/chat.js'
|
||||
|
||||
const notice = {
|
||||
state: {
|
||||
// 未读喜欢我的
|
||||
likeMe: 10,
|
||||
// 未读我喜欢
|
||||
likes: 20,
|
||||
// 未读评论数
|
||||
unReadComments: 30,
|
||||
// 未读访客数
|
||||
look: 40
|
||||
},
|
||||
|
||||
mutations: {
|
||||
SET_LIKE_ME: (state, value) => {
|
||||
state.likeMe = value
|
||||
},
|
||||
SET_LIKES: (state, value) => {
|
||||
state.likes = value
|
||||
},
|
||||
SET_UNREAD_COMMENTS: (state, value) => {
|
||||
state.unReadComments = value
|
||||
},
|
||||
SET_LOOK: (state, value) => {
|
||||
state.look = value
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default notice
|
||||
40
store/modules/theme.js
Executable file
40
store/modules/theme.js
Executable file
@@ -0,0 +1,40 @@
|
||||
import storage from '@/utils/storage'
|
||||
import { isEmpty } from '@/utils/util'
|
||||
import { APP_THEME } from '@/store/mutation-types'
|
||||
|
||||
const theme = {
|
||||
state: {
|
||||
// 当前自定义主题
|
||||
appTheme: {
|
||||
mainBg: '#fa2209',
|
||||
mainBg2: '#ff6335',
|
||||
mainText: '#ffffff',
|
||||
viceBg: '#ffb100',
|
||||
viceBg2: '#ffb900',
|
||||
viceText: '#ffffff',
|
||||
},
|
||||
},
|
||||
|
||||
mutations: {
|
||||
SET_APP_THEME: (state, value) => {
|
||||
if (!isEmpty(value)) {
|
||||
state.appTheme = value
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
|
||||
// 记录自定义主题
|
||||
SetAppTheme({ commit }, value) {
|
||||
return new Promise((resolve, reject) => {
|
||||
storage.set(APP_THEME, value)
|
||||
commit('SET_APP_THEME', value)
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default theme
|
||||
206
store/modules/user.js
Executable file
206
store/modules/user.js
Executable file
@@ -0,0 +1,206 @@
|
||||
import {
|
||||
ACCESS_TOKEN,
|
||||
USER_ID,
|
||||
USER_INFO
|
||||
} from '@/store/mutation-types'
|
||||
import storage from '@/utils/storage'
|
||||
import * as LoginApi from '@/api/login'
|
||||
import * as UserProfileApi from '@/api/love-user-profile.js'
|
||||
import {
|
||||
alipayLogin
|
||||
} from '@/api/login.js'
|
||||
import {
|
||||
getUser,
|
||||
updateUser
|
||||
} from '@/api/user.js'
|
||||
|
||||
// 登陆成功后执行
|
||||
const loginSuccess = ({commit,dispatch}, data) => {
|
||||
const {
|
||||
access_token,
|
||||
user
|
||||
} = data
|
||||
// 过期时间30天
|
||||
const expiryTime = 30 * 86400
|
||||
// 保存tokne和userId到缓存
|
||||
storage.set(USER_ID, user.userId, expiryTime)
|
||||
storage.set(USER_INFO, user, expiryTime)
|
||||
storage.set(ACCESS_TOKEN, access_token, expiryTime)
|
||||
// 记录到store全局变量
|
||||
commit('SET_TOKEN', access_token)
|
||||
commit('SET_USER_ID', user.userId)
|
||||
commit('SET_USER', user)
|
||||
|
||||
|
||||
dispatch('ConnectSocket')
|
||||
console.log("user: ",user);
|
||||
}
|
||||
|
||||
const user = {
|
||||
state: {
|
||||
// 用户认证token
|
||||
token: '',
|
||||
// 用户ID
|
||||
userId: null,
|
||||
// 用户
|
||||
userInfo: null,
|
||||
|
||||
userProfile: null
|
||||
},
|
||||
|
||||
mutations: {
|
||||
SET_TOKEN: (state, value) => {
|
||||
state.token = value
|
||||
},
|
||||
SET_USER_ID: (state, value) => {
|
||||
state.userId = value
|
||||
},
|
||||
SET_USER: (state, value) => {
|
||||
state.userInfo = value
|
||||
storage.set(USER_INFO, value)
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
GetUserInfo({commit}, data){
|
||||
getUser().then(res => {
|
||||
if (res.code == 0 && res.data.username != 'www') {
|
||||
commit('SET_USER', res.data)
|
||||
}
|
||||
})
|
||||
|
||||
},
|
||||
GetUserProfile({state, commit}, data) {
|
||||
|
||||
},
|
||||
// 用户登录 (普通登录: 输入手机号和验证码)
|
||||
Login({
|
||||
commit,dispatch
|
||||
}, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
LoginApi.login(data)
|
||||
.then(response => {
|
||||
const result = response.data
|
||||
loginSuccess({commit,dispatch}, result)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(reject)
|
||||
})
|
||||
},
|
||||
|
||||
// 微信小程序一键授权登录 (获取用户基本信息)
|
||||
LoginMpWx({
|
||||
commit, dispatch
|
||||
}, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
LoginApi.loginMpWx({
|
||||
form: data
|
||||
}, {
|
||||
isPrompt: false
|
||||
})
|
||||
.then(response => {
|
||||
const result = response.data
|
||||
loginSuccess({commit, dispatch}, result)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(reject)
|
||||
})
|
||||
},
|
||||
|
||||
// 支付宝小程序一键授权登录 (获取用户基本信息)
|
||||
LoginMpAlipay({
|
||||
commit
|
||||
}, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
alipayLogin({
|
||||
data
|
||||
}, {
|
||||
isPrompt: false
|
||||
})
|
||||
.then(response => {
|
||||
const result = response.data
|
||||
loginSuccess(commit, result)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(reject)
|
||||
})
|
||||
},
|
||||
|
||||
// 微信公众号一键授权登录 (获取用户基本信息)
|
||||
LoginWxOfficial({
|
||||
commit
|
||||
}, data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
LoginApi.loginWxOfficial({
|
||||
form: data
|
||||
}, {
|
||||
isPrompt: false
|
||||
})
|
||||
.then(response => {
|
||||
const result = response.data
|
||||
loginSuccess(commit, result)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(reject)
|
||||
})
|
||||
},
|
||||
|
||||
// 微信小程序一键授权登录 (授权手机号)
|
||||
LoginMpWxMobile({
|
||||
commit, dispatch
|
||||
}, data) {
|
||||
console.log("data: ",data);
|
||||
return new Promise((resolve, reject) => {
|
||||
LoginApi.loginMpWxMobile(data)
|
||||
.then(response => {
|
||||
const result = response.data
|
||||
console.log("result999999: ",result);
|
||||
console.log("commit: ",commit);
|
||||
loginSuccess({commit, dispatch}, result)
|
||||
resolve(response)
|
||||
})
|
||||
.catch(reject)
|
||||
})
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
Logout({
|
||||
commit,dispatch
|
||||
}, data) {
|
||||
const store = this
|
||||
return new Promise((resolve, reject) => {
|
||||
if (store.getters.userId > 0) {
|
||||
// 删除缓存中的tokne和userId
|
||||
storage.remove(USER_ID)
|
||||
storage.remove(ACCESS_TOKEN)
|
||||
storage.remove(USER_INFO)
|
||||
// 记录到store全局变量
|
||||
commit('SET_TOKEN', '')
|
||||
commit('SET_USER_ID', null)
|
||||
commit('SET_USER', null)
|
||||
resolve()
|
||||
}
|
||||
dispatch('CloseSocket')
|
||||
})
|
||||
},
|
||||
|
||||
setUserInfo({
|
||||
commit
|
||||
}, data) {
|
||||
commit('SET_USER', data)
|
||||
},
|
||||
setToken({
|
||||
commit
|
||||
}, data) {
|
||||
commit('SET_TOKEN', data)
|
||||
},
|
||||
setUserId({
|
||||
commit
|
||||
}, data) {
|
||||
commit('SET_USER_ID', data)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default user
|
||||
Reference in New Issue
Block a user