http.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. export let api_url = null
  2. export function get(url, params, success = noop, fail = noop) {
  3. return http(url, 'GET', params, success, fail)
  4. }
  5. export function post(url, params, success = noop, fail = noop) {
  6. return http(url, 'POST', params, success, fail)
  7. }
  8. function noop() {}
  9. function http(url, method, params, success, fail) {
  10. // const type = wx.getStorageSync('env') || 'dev' // 测试环境
  11. const type = wx.getStorageSync('env') || 'ijolijoli' // 正式环境
  12. switch (type) {
  13. case 'dev':
  14. api_url = 'https://test-api.ijolijoli.com/'
  15. break
  16. default:
  17. api_url = 'https://api.ijolijoli.com/'
  18. break
  19. }
  20. return new Promise ((resolve, reject) => {
  21. wx.request({
  22. url: `${api_url}${url}`,
  23. method: method,
  24. data: params,
  25. header: {
  26. token: wx.getStorageSync('token') || ''
  27. },
  28. success: ({ data, statusCode, header }) => {
  29. console.log(`==============${url}`, data)
  30. resolve(data)
  31. if (data.code == 200) {
  32. success(data, header)
  33. } else if (data.code == 201) {
  34. wx.showToast({
  35. title: data.msg,
  36. icon: 'none'
  37. })
  38. fail(data, header)
  39. } else if (data.code == 400) {
  40. console.log('登录失败')
  41. wx.showToast({
  42. title: data.msg,
  43. icon: 'none'
  44. })
  45. let pages = getCurrentPages()
  46. // if (pages.length == 0) {
  47. // wx.reLaunch({
  48. // url: '/pages/login/login'
  49. // })
  50. // return
  51. // }
  52. if (pages[pages.length - 1].route == 'pages/deviceLogin/deviceLogin') {
  53. // wx.navigateTo({
  54. // url: '/pages/login/login?prePage=1'
  55. // })
  56. } else {
  57. // wx.reLaunch({
  58. // url: '/pages/login/login'
  59. // })
  60. }
  61. // wx.reLaunch({
  62. // url: '/pages/login/login',
  63. // })
  64. // login()
  65. }
  66. },
  67. fail(error) {
  68. reject(error)
  69. wx.showToast({
  70. title: '服务器异常 请稍后再试',
  71. icon: 'none'
  72. })
  73. }
  74. })
  75. })
  76. }
  77. export function login(opts) {
  78. return new Promise((resolve, reject) => {
  79. // wx.login({
  80. // success: (res) => {
  81. getWxLoginCode().then((wxCode) => {
  82. post(
  83. 'api/login',
  84. {
  85. js_code: wxCode,
  86. },
  87. (res) => {
  88. clearWxLoginCode()
  89. getWxLoginCode()
  90. if (res.code == 200) {
  91. wx.setStorageSync('ticket', res.data.ticket)
  92. resolve(res.data)
  93. if (res.data.status == 1) {
  94. // wx.setStorageSync('userInfo', {
  95. // avatar_url: res.data.avatar_url,
  96. // mobile: res.data.mobile,
  97. // nickname: res.data.nickname,
  98. // uid: res.data.uid,
  99. // });
  100. wx.setStorageSync('token', res.data.token)
  101. // 查询是否首次购买
  102. get('api/order/check_first', {}, (res) => {
  103. if (res.data.order_status == 1) {
  104. wx.setStorageSync('agree', true)
  105. }
  106. })
  107. getUser()
  108. }
  109. return
  110. // 此处不用再跳转登录页面
  111. if (res.data.status == 0) {
  112. let pages = getCurrentPages()
  113. if (
  114. pages[pages.length - 1].route == 'pages/deviceLogin/deviceLogin'
  115. ) {
  116. wx.navigateTo({
  117. url: '/pages/login/login?prePage=1'
  118. })
  119. } else {
  120. wx.reLaunch({
  121. url: '/pages/login/login'
  122. })
  123. }
  124. // if(opts && opts.path == 'pages/deviceLogin/deviceLogin') {
  125. // wx.navigateTo({
  126. // url: '/pages/login/login',
  127. // })
  128. // } else {
  129. // wx.reLaunch({
  130. // url: '/pages/login/login',
  131. // })
  132. // }
  133. return
  134. }
  135. }
  136. },
  137. () => {
  138. clearWxLoginCode()
  139. getWxLoginCode()
  140. }
  141. )
  142. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  143. // }
  144. })
  145. })
  146. }
  147. /**
  148. * 获取用户信息
  149. * api/user
  150. */
  151. function getUser() {
  152. get('api/user', {}, (res) => {
  153. if (res.data) {
  154. wx.setStorageSync('userInfo', res.data)
  155. }
  156. console.log(res)
  157. })
  158. }
  159. export function getWxLoginCode() {
  160. let loginConfig = getApp().loginConfig
  161. let refreshCode = function (resolve, reject) {
  162. wx.login({
  163. success: (res) => {
  164. loginConfig.code = res.code
  165. loginConfig.codeTime = new Date().getTime()
  166. resolve && resolve(res.code)
  167. },
  168. fail: (res) => {
  169. reject && reject(res)
  170. }
  171. })
  172. }
  173. //code的有效期不能超过1个小时
  174. if (
  175. !loginConfig.code ||
  176. new Date().getTime() - loginConfig.codeTime > 50 * 60 * 60 * 1000
  177. ) {
  178. return new Promise((resolve, reject) => {
  179. refreshCode(resolve, reject)
  180. })
  181. } else {
  182. return new Promise((resolve, reject) => {
  183. wx.checkSession({
  184. success: function () {
  185. resolve(getApp().loginConfig.code)
  186. },
  187. fail: function () {
  188. // session_key 已经失效,需要重新执行登录流程
  189. refreshCode(resolve, reject)
  190. }
  191. })
  192. })
  193. }
  194. }
  195. // 清理code
  196. export function clearWxLoginCode() {
  197. getApp().loginConfig = {
  198. code: '',
  199. codeTime: ''
  200. }
  201. }