const HOST = 'https://lottery-wxa-api.duowan.com' // prod // const HOST = 'https://test-lottery-wxa-api.duowan.com/' // test const HEADER = { 'content-type': 'application/x-www-form-urlencoded' } /** * 获取抽奖列表 * * @param {*} cur_page 当前页 * @param {number} [page_count=10] 每页数量 */ function getLotteries(cur_page, page_count = 10) { const { token } = wx.getStorageSync('USER_INFO') return new Promise((resolve, reject) => { wx.request({ url: HOST + '/lotteries', data: { token, page: cur_page, page_size: page_count }, header: HEADER, method: 'GET', dataType: 'json', success: result => { if (result.data.code == -401) { let pages = getCurrentPages() let pageIndex = pages.length - 1 let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index') if (pages[pageIndex].route && pages[pageIndex].options) { let querys = '' Object.keys(pages[pageIndex].options).map((key, value) => { querys += key + '~' + pages[pageIndex].options[key] + ',' }) prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1) } wx.redirectTo({ url: '/pages/login/login' + prevpage }) return false } result.data.code === 0 ? resolve(result.data) : reject(result.data) }, fail: () => { reject('请求失败') } }) }) } /** * 获取抽奖详情信息 * * @param {*} id 抽奖ID * @returns */ function getLotteryDetail(id) { const { token } = wx.getStorageSync('USER_INFO') return new Promise((resolve, reject) => { wx.request({ url: HOST + '/lotteries/' + id, data: { token }, header: HEADER, method: 'GET', dataType: 'json', success: result => { if (result.data.code == -401) { let pages = getCurrentPages() let pageIndex = pages.length - 1 let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index') if (pages[pageIndex].route && pages[pageIndex].options) { let querys = '' Object.keys(pages[pageIndex].options).map((key, value) => { querys += key + '~' + pages[pageIndex].options[key] + ',' }) prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1) } wx.redirectTo({ url: '/pages/login/login' + prevpage }) return false } result.data.code === 0 ? resolve(result.data) : reject(result.data) }, fail: () => { reject('请求失败') } }) }) } /** * 抽奖接口 * * @param {*} id 抽奖ID * @returns */ function drawLottery(id, formId) { const { token } = wx.getStorageSync('USER_INFO') return new Promise((resolve, reject) => { wx.request({ url: HOST + '/lotteries/draw/' + id , data: { token, formId }, header: HEADER, method: 'POST', dataType: 'json', success: result => { if (result.data.code == -401) { let pages = getCurrentPages() let pageIndex = pages.length - 1 let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index') if (pages[pageIndex].route && pages[pageIndex].options) { let querys = '' Object.keys(pages[pageIndex].options).map((key, value) => { querys += key + '~' + pages[pageIndex].options[key] + ',' }) prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1) } wx.redirectTo({ url: '/pages/login/login' + prevpage }) return false } result.data.code === 0 ? resolve(result.data) : reject(result.data) }, fail: () => { reject('请求失败') } }) }) } /** * 邀请接口 被邀请的用户打开分享页面时请求 * * @param {*} lottery_id 抽奖id * @param {*} user_id 用户id */ function invite(lottery_id, from_user_id){ const { token } = wx.getStorageSync('USER_INFO') return new Promise((resolve, reject) => { wx.request({ url: HOST + '/lotteries/invite/', data: { token, lottery_id, from_user_id }, header: HEADER, method: 'POST', dataType: 'json', success: result => { if (result.data.code == -401) { let pages = getCurrentPages() let pageIndex = pages.length - 1 let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index') if (pages[pageIndex].route && pages[pageIndex].options) { let querys = '' Object.keys(pages[pageIndex].options).map((key, value) => { querys += key + '~' + pages[pageIndex].options[key] + ',' }) prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1) } wx.redirectTo({ url: '/pages/login/login' + prevpage }) return false } result.data.code === 0 ? resolve(result.data) : reject(result.data) }, fail: () => { reject('请求失败') } }) }) } /** * 获取抽奖所有用户 * * @param {*} id 抽奖id * @returns */ function getAllLotteryUser(id) { const { token } = wx.getStorageSync('USER_INFO') return new Promise((resolve, reject) => { wx.request({ url: HOST + '/lotteries/avatars/' + id, data: { token }, header: HEADER, method: 'GET', dataType: 'json', success: result => { if (result.data.code == -401) { let pages = getCurrentPages() let pageIndex = pages.length - 1 let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index') if (pages[pageIndex].route && pages[pageIndex].options) { let querys = '' Object.keys(pages[pageIndex].options).map((key, value) => { querys += key + '~' + pages[pageIndex].options[key] + ',' }) prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1) } wx.redirectTo({ url: '/pages/login/login' + prevpage }) return false } result.data.code === 0 ? resolve(result.data) : reject(result.data) }, fail: () => { reject('请求失败') } }) }) } /** * 数据接口 * * @param {*} url 接口地址 * @param {*} data 数据 */ function apiRequest (url, data) { const { token } = wx.getStorageSync('USER_INFO') data = data ? data : {} if (url != '/auth/signin') data.token = token return new Promise((resolve, reject) => { wx.request({ url: HOST + url, data: { ...data }, header: HEADER, method: 'GET', dataType: 'json', success: (res) => { if (res.data.code == -401 && url !== '/auth/signin') { let pages = getCurrentPages() let pageIndex = pages.length - 1 let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index') if (pages[pageIndex].route && pages[pageIndex].options) { let querys = '' Object.keys(pages[pageIndex].options).map((key, value) => { querys += key + '~' + pages[pageIndex].options[key] + ',' }) prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1) } wx.redirectTo({ url: '/pages/login/login' + prevpage }) return false } res.data.code === 0 ? resolve(res.data) : reject(res.data) }, fail: () => { reject('请求失败') } }) }) } /** * 获取小程序码接口 * * @param {*} url 接口地址 * @param {*} data 数据 */ function getQrcode(url, data) { const { token } = wx.getStorageSync('USER_INFO') data = data ? data : {} return new Promise((resolve, reject) => { wx.request({ url: HOST + url, data: { token, ...data }, header: HEADER, method: 'POST', dataType: 'json', success: (res) => { res.data.code === 0 ? resolve(res.data) : reject(res.data) }, fail: () => { reject('请求失败') } }) }) } module.exports = { getLotteries, getLotteryDetail, drawLottery, invite, getAllLotteryUser, apiRequest, getQrcode }