API.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. const HOST = 'https://lottery-wxa-api.duowan.com' // prod
  2. // const HOST = 'https://test-lottery-wxa-api.duowan.com/' // test
  3. const HEADER = { 'content-type': 'application/x-www-form-urlencoded' }
  4. /**
  5. * 获取抽奖列表
  6. *
  7. * @param {*} cur_page 当前页
  8. * @param {number} [page_count=10] 每页数量
  9. */
  10. function getLotteries(cur_page, page_count = 10) {
  11. const { token } = wx.getStorageSync('USER_INFO')
  12. return new Promise((resolve, reject) => {
  13. wx.request({
  14. url: HOST + '/lotteries',
  15. data: {
  16. token,
  17. page: cur_page,
  18. page_size: page_count
  19. },
  20. header: HEADER,
  21. method: 'GET',
  22. dataType: 'json',
  23. success: result => {
  24. if (result.data.code == -401) {
  25. let pages = getCurrentPages()
  26. let pageIndex = pages.length - 1
  27. let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index')
  28. if (pages[pageIndex].route && pages[pageIndex].options) {
  29. let querys = ''
  30. Object.keys(pages[pageIndex].options).map((key, value) => {
  31. querys += key + '~' + pages[pageIndex].options[key] + ','
  32. })
  33. prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1)
  34. }
  35. wx.redirectTo({
  36. url: '/pages/login/login' + prevpage
  37. })
  38. return false
  39. }
  40. result.data.code === 0 ? resolve(result.data) : reject(result.data)
  41. },
  42. fail: () => {
  43. reject('请求失败')
  44. }
  45. })
  46. })
  47. }
  48. /**
  49. * 获取抽奖详情信息
  50. *
  51. * @param {*} id 抽奖ID
  52. * @returns
  53. */
  54. function getLotteryDetail(id) {
  55. const { token } = wx.getStorageSync('USER_INFO')
  56. return new Promise((resolve, reject) => {
  57. wx.request({
  58. url: HOST + '/lotteries/' + id,
  59. data: {
  60. token
  61. },
  62. header: HEADER,
  63. method: 'GET',
  64. dataType: 'json',
  65. success: result => {
  66. if (result.data.code == -401) {
  67. let pages = getCurrentPages()
  68. let pageIndex = pages.length - 1
  69. let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index')
  70. if (pages[pageIndex].route && pages[pageIndex].options) {
  71. let querys = ''
  72. Object.keys(pages[pageIndex].options).map((key, value) => {
  73. querys += key + '~' + pages[pageIndex].options[key] + ','
  74. })
  75. prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1)
  76. }
  77. wx.redirectTo({
  78. url: '/pages/login/login' + prevpage
  79. })
  80. return false
  81. }
  82. result.data.code === 0 ? resolve(result.data) : reject(result.data)
  83. },
  84. fail: () => {
  85. reject('请求失败')
  86. }
  87. })
  88. })
  89. }
  90. /**
  91. * 抽奖接口
  92. *
  93. * @param {*} id 抽奖ID
  94. * @returns
  95. */
  96. function drawLottery(id, formId) {
  97. const { token } = wx.getStorageSync('USER_INFO')
  98. return new Promise((resolve, reject) => {
  99. wx.request({
  100. url: HOST + '/lotteries/draw/' + id ,
  101. data: {
  102. token,
  103. formId
  104. },
  105. header: HEADER,
  106. method: 'POST',
  107. dataType: 'json',
  108. success: result => {
  109. if (result.data.code == -401) {
  110. let pages = getCurrentPages()
  111. let pageIndex = pages.length - 1
  112. let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index')
  113. if (pages[pageIndex].route && pages[pageIndex].options) {
  114. let querys = ''
  115. Object.keys(pages[pageIndex].options).map((key, value) => {
  116. querys += key + '~' + pages[pageIndex].options[key] + ','
  117. })
  118. prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1)
  119. }
  120. wx.redirectTo({
  121. url: '/pages/login/login' + prevpage
  122. })
  123. return false
  124. }
  125. result.data.code === 0 ? resolve(result.data) : reject(result.data)
  126. },
  127. fail: () => {
  128. reject('请求失败')
  129. }
  130. })
  131. })
  132. }
  133. /**
  134. * 邀请接口 被邀请的用户打开分享页面时请求
  135. *
  136. * @param {*} lottery_id 抽奖id
  137. * @param {*} user_id 用户id
  138. */
  139. function invite(lottery_id, from_user_id){
  140. const { token } = wx.getStorageSync('USER_INFO')
  141. return new Promise((resolve, reject) => {
  142. wx.request({
  143. url: HOST + '/lotteries/invite/',
  144. data: {
  145. token,
  146. lottery_id,
  147. from_user_id
  148. },
  149. header: HEADER,
  150. method: 'POST',
  151. dataType: 'json',
  152. success: result => {
  153. if (result.data.code == -401) {
  154. let pages = getCurrentPages()
  155. let pageIndex = pages.length - 1
  156. let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index')
  157. if (pages[pageIndex].route && pages[pageIndex].options) {
  158. let querys = ''
  159. Object.keys(pages[pageIndex].options).map((key, value) => {
  160. querys += key + '~' + pages[pageIndex].options[key] + ','
  161. })
  162. prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1)
  163. }
  164. wx.redirectTo({
  165. url: '/pages/login/login' + prevpage
  166. })
  167. return false
  168. }
  169. result.data.code === 0 ? resolve(result.data) : reject(result.data)
  170. },
  171. fail: () => {
  172. reject('请求失败')
  173. }
  174. })
  175. })
  176. }
  177. /**
  178. * 获取抽奖所有用户
  179. *
  180. * @param {*} id 抽奖id
  181. * @returns
  182. */
  183. function getAllLotteryUser(id) {
  184. const { token } = wx.getStorageSync('USER_INFO')
  185. return new Promise((resolve, reject) => {
  186. wx.request({
  187. url: HOST + '/lotteries/avatars/' + id,
  188. data: {
  189. token
  190. },
  191. header: HEADER,
  192. method: 'GET',
  193. dataType: 'json',
  194. success: result => {
  195. if (result.data.code == -401) {
  196. let pages = getCurrentPages()
  197. let pageIndex = pages.length - 1
  198. let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index')
  199. if (pages[pageIndex].route && pages[pageIndex].options) {
  200. let querys = ''
  201. Object.keys(pages[pageIndex].options).map((key, value) => {
  202. querys += key + '~' + pages[pageIndex].options[key] + ','
  203. })
  204. prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1)
  205. }
  206. wx.redirectTo({
  207. url: '/pages/login/login' + prevpage
  208. })
  209. return false
  210. }
  211. result.data.code === 0 ? resolve(result.data) : reject(result.data)
  212. },
  213. fail: () => {
  214. reject('请求失败')
  215. }
  216. })
  217. })
  218. }
  219. /**
  220. * 数据接口
  221. *
  222. * @param {*} url 接口地址
  223. * @param {*} data 数据
  224. */
  225. function apiRequest (url, data) {
  226. const { token } = wx.getStorageSync('USER_INFO')
  227. data = data ? data : {}
  228. if (url != '/auth/signin') data.token = token
  229. return new Promise((resolve, reject) => {
  230. wx.request({
  231. url: HOST + url,
  232. data: {
  233. ...data
  234. },
  235. header: HEADER,
  236. method: 'GET',
  237. dataType: 'json',
  238. success: (res) => {
  239. if (res.data.code == -401 && url !== '/auth/signin') {
  240. let pages = getCurrentPages()
  241. let pageIndex = pages.length - 1
  242. let prevpage = '?prevpage=' + (pages[pageIndex].route ? '/' + pages[pageIndex].route : '/pages/index/index')
  243. if (pages[pageIndex].route && pages[pageIndex].options) {
  244. let querys = ''
  245. Object.keys(pages[pageIndex].options).map((key, value) => {
  246. querys += key + '~' + pages[pageIndex].options[key] + ','
  247. })
  248. prevpage = prevpage + '&querys=' + querys.slice(0, querys.length - 1)
  249. }
  250. wx.redirectTo({
  251. url: '/pages/login/login' + prevpage
  252. })
  253. return false
  254. }
  255. res.data.code === 0 ? resolve(res.data) : reject(res.data)
  256. },
  257. fail: () => {
  258. reject('请求失败')
  259. }
  260. })
  261. })
  262. }
  263. /**
  264. * 获取小程序码接口
  265. *
  266. * @param {*} url 接口地址
  267. * @param {*} data 数据
  268. */
  269. function getQrcode(url, data) {
  270. const { token } = wx.getStorageSync('USER_INFO')
  271. data = data ? data : {}
  272. return new Promise((resolve, reject) => {
  273. wx.request({
  274. url: HOST + url,
  275. data: {
  276. token,
  277. ...data
  278. },
  279. header: HEADER,
  280. method: 'POST',
  281. dataType: 'json',
  282. success: (res) => {
  283. res.data.code === 0 ? resolve(res.data) : reject(res.data)
  284. },
  285. fail: () => {
  286. reject('请求失败')
  287. }
  288. })
  289. })
  290. }
  291. module.exports = {
  292. getLotteries,
  293. getLotteryDetail,
  294. drawLottery,
  295. invite,
  296. getAllLotteryUser,
  297. apiRequest,
  298. getQrcode
  299. }