123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- 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
- }
|