index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import http from './interface'
  2. import { platform } from '../utils/index'
  3. import { webLogin, mini_env, replaceStringInObject } from '@/common/utils/util'
  4. import store from '@/store/index.js'
  5. import { goLoginPage } from '@/mixins/index.js'
  6. import { setStorage, getStorage, removeStorage } from '@/common/utils/storage.js'
  7. import { navToPage } from 'common/utils/navPag'
  8. var chongxinjiazai = true
  9. const program = {
  10. H5: 'wap',
  11. APP: 'app',
  12. APPNVUE: 'app',
  13. WEIXIN: 'miniProgram'
  14. }
  15. const whiteCodes = [200, 201, 4444]
  16. export const $http = (url, method, data, json, loadingBool = false, isExecute = true, isForm = false) => {
  17. //设置请求前拦截器
  18. http.interceptor.request = config => {
  19. return new Promise(r => {
  20. mini_env(bool => {
  21. if (loadingBool && url !== '/homepage/study') {
  22. uni.showLoading({
  23. title: '加载中...'
  24. })
  25. }
  26. config.header = {
  27. 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
  28. // "x-token": getStorage('token'),
  29. 'x-token': store.getters.token,
  30. APPID: store.getters.appId,
  31. Program: program[platform()],
  32. source: bool ? 'A' : 'B',
  33. miniOpenId: store.getters.miniOpenId,
  34. sharerOpenId: store.getters.sharerOpenId
  35. }
  36. r(true)
  37. })
  38. })
  39. }
  40. //设置请求结束后拦截器
  41. http.interceptor.response = async response => {
  42. //判断返回状态 执行相应操作
  43. if (loadingBool) {
  44. uni.hideLoading()
  45. }
  46. const res = replaceStringInObject(
  47. response.data || {},
  48. ['https://zfire-jsm-h.oss-cn-shenzhen.aliyuncs.com/', 'https://zfire-jsm-h.oss-cn-shenzhen.aliyuncs.com/'],
  49. process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API + '/common/img/get?key='
  50. )
  51. if (!isExecute) {
  52. return res
  53. }
  54. if (whiteCodes.indexOf(res.code) < 0) {
  55. if (~res.message.indexOf('USERPAYING')) {
  56. return Promise.reject(new Error(res.message || 'Error'))
  57. }
  58. if (res.code === 601) {
  59. if (chongxinjiazai) {
  60. chongxinjiazai = false
  61. setTimeout(() => {
  62. uni.showModal({
  63. title: '提示',
  64. content: '请重新加载小程序!',
  65. confirmText: '知道了',
  66. success: function (res) {
  67. chongxinjiazai = true
  68. }
  69. })
  70. }, 200)
  71. }
  72. } else if (res.code === 1001) {
  73. webLogin(true)
  74. return {}
  75. } else if (res.code === 4001) {
  76. uni.showModal({
  77. title: '提示',
  78. content: '你尚未绑定手机号,无法下单!',
  79. confirmText: '立即绑定',
  80. success: function (res) {
  81. if (res.confirm) {
  82. navToPage({
  83. url: '/packageMine/pages/phone'
  84. })
  85. } else if (res.cancel) {
  86. console.log('用户点击取消')
  87. }
  88. }
  89. })
  90. } else {
  91. if (isForm) {
  92. uni.showModal({
  93. title: '温馨提示',
  94. content: res.message || 'Error',
  95. showCancel: false,
  96. success: function (res) {
  97. if (res.confirm) {
  98. } else if (res.cancel) {
  99. }
  100. }
  101. })
  102. } else {
  103. uni.showModal({
  104. title: '温馨提示',
  105. content: res.message || 'Error',
  106. showCancel: false,
  107. confirmText: '知道了',
  108. success: function (res) {
  109. if (res.confirm) {
  110. } else if (res.cancel) {
  111. }
  112. }
  113. })
  114. }
  115. }
  116. return Promise.reject(new Error(res.message || 'Error'))
  117. } else {
  118. return res
  119. }
  120. }
  121. if (store.getters.token) {
  122. return http.request({
  123. method: method,
  124. url: url,
  125. dataType: 'json',
  126. data
  127. })
  128. } else if (!!~['/user/auth2', '/goods/detail'].indexOf(url)) {
  129. return http.request({
  130. method: method,
  131. url: url,
  132. dataType: 'json',
  133. data
  134. })
  135. } else {
  136. return new Promise((resolve, reject) => {
  137. var i = 0
  138. var digui = function () {
  139. i++
  140. if ((store?.getters?.token && store?.getters?.userId) || i == 60) {
  141. http
  142. .request({
  143. method: method,
  144. url: url,
  145. dataType: 'json',
  146. data: {
  147. ...data,
  148. userId: store.getters.userId
  149. }
  150. })
  151. .then(resolve)
  152. .catch(reject)
  153. } else {
  154. setTimeout(digui, 50)
  155. }
  156. }
  157. digui()
  158. })
  159. }
  160. }
  161. async function login() {
  162. //返回环宇token所需的login code
  163. return new Promise(resolve => {
  164. uni.login({
  165. provider: 'weixin',
  166. success(loginRes) {
  167. resolve(loginRes.code)
  168. },
  169. fail() {}
  170. })
  171. })
  172. }
  173. async function doRequest(response, url) {
  174. var code = await login()
  175. var res = await get('/v1/oauth/refreshToken/code/' + code, {})
  176. if (res && res.data.data.token) {
  177. let config = response.config
  178. setStorage('token', res.data.data.token)
  179. config.header['Authorization'] = res.data.data.token
  180. let json = config.header['Content-Type'] === 'application/json'
  181. const resold = await $http(
  182. url,
  183. config.method,
  184. {
  185. ...config.data
  186. },
  187. json
  188. )
  189. return resold
  190. } else {
  191. uni.clearStorage()
  192. uni.showToast({
  193. title: '授权失效,请重新登录',
  194. duration: 1000
  195. })
  196. this.$navToPage({
  197. url: '/pages/login/auth'
  198. })
  199. return false
  200. }
  201. }
  202. function postJson(url, data, loadingBool, isExecute, isForm) {
  203. return $http(url, 'POST', data, true, loadingBool, isExecute, isForm)
  204. }
  205. function post(url, data, loadingBool, isExecute, isForm) {
  206. return $http(url, 'POST', data, false, loadingBool, isExecute, isForm)
  207. }
  208. function get(url, data, loadingBool) {
  209. return $http(url, 'GET', data, false, loadingBool)
  210. }
  211. function put(url, data, loadingBool) {
  212. return $http(url, 'PUT', data, true, loadingBool)
  213. }
  214. function del(url, data, loadingBool) {
  215. return $http(url, 'DELETE', data, true, loadingBool)
  216. }
  217. //封装的通用请求方法
  218. export const axios = function (obj = {}) {
  219. if (typeof obj !== 'object') {
  220. throw new Error('arguments must be object')
  221. }
  222. if (!obj.url) {
  223. throw new Error('url not defined')
  224. }
  225. return new Promise((resolve, reject) => {
  226. if (obj.isLoading) {
  227. uni.showLoading({
  228. title: '加载中',
  229. mask: true
  230. })
  231. }
  232. let _url = process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API + obj.url
  233. let isGet = obj.method && obj.method.toLowerCase() == 'get'
  234. let isQuery = obj.isQuery
  235. if (isGet || isQuery) {
  236. _url +=
  237. '?' +
  238. Object.entries(obj.params || {})
  239. .map(item => item.join('='))
  240. .join('&')
  241. }
  242. let _type = 'application/x-www-form-urlencoded'
  243. if (obj.type == 'json') {
  244. _type = 'application/json'
  245. }
  246. uni.request({
  247. url: _url,
  248. data: isGet || isQuery ? '' : obj.params || '',
  249. header: {
  250. 'content-type': _type,
  251. 'x-token': store.getters.token
  252. },
  253. method: obj.method ? obj.method : 'POST',
  254. success: function (ress) {
  255. if (obj.isLoading) {
  256. uni.hideLoading()
  257. }
  258. const res = replaceStringInObject(
  259. ress.data,
  260. ['https://zfire-jsm-h.oss-cn-shenzhen.aliyuncs.com/', 'https://zfire-jsm-h.oss-cn-shenzhen.aliyuncs.com/'],
  261. process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API + '/common/img/get?key='
  262. )
  263. if (whiteCodes.indexOf(res.code) < 0) {
  264. if (res.code === 1001) {
  265. webLogin(true)
  266. reject(new Error(res.message || 'Error'))
  267. return {}
  268. }
  269. if (res.message || res.code) {
  270. uni.showToast({
  271. title: res.message || 'Error',
  272. icon: 'none',
  273. duration: 1500
  274. })
  275. }
  276. reject(new Error(res.message || 'Error'))
  277. } else {
  278. resolve(res)
  279. }
  280. },
  281. fail: function (err) {
  282. uni.hideLoading()
  283. uni.showModal({
  284. title: '提示',
  285. content: '网络连接失败' + JSON.stringify(err)
  286. })
  287. reject(err)
  288. }
  289. })
  290. })
  291. }
  292. export default {
  293. postJson,
  294. get,
  295. post,
  296. put,
  297. del
  298. }