import axios from "axios" import { encodeStr } from "@/pass.js" import { Base64 } from "js-base64" import store from "@/store/index.js" import router from "@/router" axios.interceptors.response.use(function (response) { // 2xx 范围内的状态码都会触发该函数。 if (response.data.code === 5001 || response.data.code === 5002) { store.commit('logoutCallback') router.push({ name: 'HomeView' }) return Promise.reject('登录态过期') } return response }, function (error) { return error }) export function getUserFromStorageIfNeed() { if (!store.state.token || !store.state.userInfo) { const lastToken = localStorage.getItem('token') const lastUserInfoStr = localStorage.getItem('userInfo') if (lastToken && lastUserInfoStr) { store.commit('setLoginStatus', true) store.commit('setToken', lastToken) store.commit('setUserInfo', JSON.parse(lastUserInfoStr)) } } } // export async function reportVisit() { // const res = await axios({ // method: 'get', // url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/addVisit`, // }) // } // export async function fetchVisitInfo() { // const res = await axios({ // method: 'get', // url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/getVisit`, // }) // return res.data.data // } export async function login(account, password) { const pwdEncrypted = encodeStr(Base64.encode(password)) const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/login`, data: { userName: account, password: pwdEncrypted, }, }) if (res.data.code !== 0) { store.commit('setLoginStatus', false) throw (`登录失败:${res.data.msg}`) } else { store.commit('setLoginStatus', true) store.commit('setToken', res.data.data.token) store.commit('setUserInfo', res.data.data.user) } } export async function logout() { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/logout`, headers: { token: store.state.token, } }) if (res?.data?.code === 0) { store.commit('logoutCallback') } } export async function checkLoginStatusAndProcess() { const lastToken = localStorage.getItem('token') const lastUserInfoStr = localStorage.getItem('userInfo') if (lastToken && lastUserInfoStr) { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/checkLogin`, headers: { token: lastToken, } }) if (res?.data?.code === 0 && res?.data?.data) { store.commit('setLoginStatus', true) store.commit('setToken', lastToken) store.commit('setUserInfo', JSON.parse(lastUserInfoStr)) return true } else { store.commit('logoutCallback') return false } } else { store.commit('logoutCallback') return false } } export async function signUp(account, phone, password) { const pwdEncrypted = encodeStr(Base64.encode(password)) const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/register`, data: { password: pwdEncrypted, phone, userName: account, verifyPassword: pwdEncrypted, }, }) if (res.data.code !== 0) { throw (`注册失败:${res.data.msg}`) } else { return } } export async function findPassowrd(account, phone) { const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/findPass`, data: { phone, userName: account, }, }) if (res.data.code !== 0) { throw (`找回密码失败:${res.data.msg}`) } else { return } } export async function changePassword(newPassword, oldPassword) { const pwdNewEncrypted = encodeStr(Base64.encode(newPassword)) const pwdOldEncrypted = encodeStr(Base64.encode(oldPassword)) const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/update/pass`, data: { newPassword: pwdNewEncrypted, oldPassword: pwdOldEncrypted, }, headers: { token: store.state.token, } }) if (res.data.code !== 0) { throw (`密码修改失败:${res.data.msg}`) } else { return } } export async function getGameRuleList() { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/rule/getList?rnd=${Math.random()}`, params: { type: 'game', }, }) if (res.data.code !== 0) { throw (`获取游戏规则失败:${res.data.msg}`) } else { return res.data.data } } export async function getScoreLimit() { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/rule/getList?rnd=${Math.random()}`, params: { type: 'day', }, }) if (res.data.code !== 0) { throw (`获取积分上限失败:${res.data.msg}`) } else { return res.data.data[0].score } } export async function getScore() { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/getPoint?rnd=${Math.random()}`, headers: { token: store.state.token, } }) if (res.data.code !== 0) { throw (`获取积分失败:${res.data.msg}`) } else { return res.data.data } } export async function addScore(score, description) { const scoreEncrypted = encodeStr(Base64.encode(score)) const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/point/add`, data: { score: scoreEncrypted, type: '玩游戏', userId: store.state.userInfo.id, description, }, headers: { token: store.state.token, } }) if (res.data.code !== 0) { throw (`新增积分失败:${res.data.msg}`) } else { return res.data.data } } export async function getExamQuestionList() { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/show/question/getList`, }) if (res.data.code !== 0) { throw (`获取答题列表失败:${res.data.msg}`) } else { return res.data.data } } /* createTime: "2024-01-08 19:07:01" creatorId: 1 creatorName: "" id: 11 isEnabled: 1 name: "可乐" recordDate: null rtf: "

可乐

123

" score: 10 stock: 10 thumb: "/prize/11/thumb/20240109_0929514233.png" updateTime: "2024-01-09 09:35:10" */ export async function getPrizeList(pageNum, pageSize) { const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/prize/pageList`, data: { pageNum, pageSize, }, headers: { token: store.state.token, } }) if (res.data.code !== 0) { throw (`获取奖品列表失败:${res.data.msg}`) } else { return res.data.data } } export async function getBonusPointRecord() { const res = await axios({ method: 'get', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/point/getList`, headers: { token: store.state.token, } }) if (res.data.code !== 0) { throw (`获取积分记录失败:${res.data.msg}`) } else { return res.data.data } } export async function redeem(description, name, phone, prizeId, score, prizeName) { const res = await axios({ method: 'post', url: `${process.env.VUE_APP_DEPLOY_ORIGIN}/api/cms/game/prize/redeem`, headers: { token: store.state.token, }, data: { description, name, phone, prizeId, score: -1 * score, prizeName, } }) if (res.data.code !== 0) { throw (`奖品兑换失败:${res.data.msg}`) } else { return res.data.data } } export function notifyQuit() { console.log('小游戏:调用父窗口的goBackSceneFu方法……') window.parent?.goBackSceneFu() }