金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.6 KiB

  1. import config from './config';
  2. var gbConfig = {
  3. appid: 'wx43759fe438b7b066'
  4. };
  5. /*获取当前页带参数的url*/
  6. function getCurrentPageUrlWithArgs(){
  7. var pages = getCurrentPages() //获取加载的页面
  8. var currentPage = pages[pages.length-1] //获取当前页面的对象
  9. var url = currentPage.route //当前页面url
  10. var options = currentPage.options //如果要获取url中所带的参数可以查看options
  11. //拼接url的参数
  12. var urlWithArgs = url + '?'
  13. for(var key in options){
  14. var value = options[key]
  15. urlWithArgs += key + '=' + value + '&'
  16. }
  17. urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length-1)
  18. return encodeURIComponent(urlWithArgs)
  19. };
  20. export const sandBox = {
  21. get({api, data, header}){
  22. let extConfig = {};
  23. if (extConfig.appid) {
  24. gbConfig = extConfig
  25. }
  26. if (header) {
  27. header.appid = gbConfig.appid
  28. } else {
  29. header = {};
  30. header.appid = gbConfig.appid
  31. }
  32. return new Promise((resolve, reject) => {
  33. wx.request({
  34. url:`${config.GLOBAL.baseUrl}${api}`,
  35. header:header,
  36. data:data,
  37. method:'GET',
  38. success:res => {
  39. sandBox.error(res).then(()=>{
  40. resolve(res)
  41. })
  42. },
  43. fail:rej => {
  44. reject(rej)
  45. }
  46. })
  47. })
  48. },
  49. post({api, data, header}){
  50. let extConfig = '';
  51. if (extConfig.appid) {
  52. gbConfig = extConfig
  53. }
  54. if (header) {
  55. header.appid = gbConfig.appid
  56. } else {
  57. header = {};
  58. header.appid = gbConfig.appid
  59. }
  60. return new Promise((resolve, reject) => {
  61. wx.request({
  62. url:`${config.GLOBAL.baseUrl}${api}`,
  63. data:data,
  64. header:header,
  65. method:'POST',
  66. success:res => {
  67. sandBox.error(res).then(()=>{
  68. resolve(res)
  69. })
  70. },
  71. fail:rej => {
  72. reject(rej)
  73. }
  74. })
  75. })
  76. },
  77. error(res){
  78. return new Promise((resolve,reject)=>{
  79. var url = getCurrentPageUrlWithArgs();
  80. if (res.data.message == 'Unauthenticated.') {
  81. uni.removeStorageSync('user_token');
  82. wx.showModal({
  83. content:'请重新登录',
  84. duration:1500,
  85. showCancel: false,
  86. success:(res)=>{
  87. if (res.confirm) {
  88. // wx.navigateTo 邓平艺 于2021.09.24修改,
  89. // 原因:未登录点击后跳转到登录页,当登录成功,
  90. // 再次跳转回来时,用户点击页面返回,原有页面和弹窗还存在
  91. uni.redirectTo({
  92. url:`/pages/user/register/register?url=${url}`
  93. })
  94. return;
  95. }
  96. },
  97. cancel:()=>{
  98. uni.redirectTo({
  99. url:`/pages/user/register/register?url=${url}`
  100. })
  101. return;
  102. }
  103. })
  104. reject()
  105. return
  106. }
  107. resolve();
  108. return
  109. })
  110. },
  111. ajax({api, data, method, header}) {
  112. let extConfig = '';
  113. if (extConfig.appid) {
  114. gbConfig = extConfig
  115. }
  116. if (header) {
  117. header.appid = gbConfig.appid
  118. } else {
  119. header = {};
  120. header.appid = gbConfig.appid
  121. }
  122. return new Promise((resolve,reject) => {
  123. wx.request({
  124. url:`${config.GLOBAL.baseUrl}${api}`,
  125. data,
  126. header,
  127. method:method.toUpperCase(),
  128. success:res => {
  129. sandBox.error(res).then(()=>{
  130. resolve(res)
  131. })
  132. },
  133. fail:rej => {
  134. reject(rej)
  135. }
  136. })
  137. })
  138. },
  139. uploadFile({api,filePath,header,name}){
  140. let extConfig = '';
  141. if (extConfig.appid) {
  142. gbConfig = extConfig
  143. }
  144. if (header) {
  145. header.appid = gbConfig.appid
  146. } else {
  147. header = {};
  148. header.appid = gbConfig.appid
  149. }
  150. return new Promise((resolve,reject) =>{
  151. wx.uploadFile({
  152. url:`${config.GLOBAL.baseUrl}${api}`,
  153. header,
  154. filePath,
  155. name,
  156. success:res => {
  157. resolve(res)
  158. },
  159. fail:rej => {
  160. reject(rej)
  161. }
  162. })
  163. })
  164. },
  165. dowloadFile({api, filePath, header, name}) {
  166. let extConfig = '';
  167. if (extConfig.appid) {
  168. gbConfig = extConfig
  169. }
  170. if (header) {
  171. header.appid = gbConfig.appid
  172. } else {
  173. header = {};
  174. header.appid = gbConfig.appid
  175. }
  176. return new Promise((resolve, reject) => {
  177. wx.downloadFile({
  178. url:api,
  179. header,
  180. filePath,
  181. name,
  182. success:res => {
  183. resolve(res)
  184. },
  185. fail:rej => {
  186. reject(rej)
  187. }
  188. })
  189. })
  190. }
  191. };