api.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import store from '../../store'
  2. const _config = {
  3. baseUrl: "http://www.lychwl.com.cn"
  4. }
  5. // 提示
  6. const msg = (title, duration = 3000, mask = false, icon = 'none') => {
  7. //统一提示方便全局修改
  8. if (Boolean(title) === false) {
  9. return;
  10. }
  11. uni.showToast({
  12. title,
  13. duration,
  14. mask,
  15. icon
  16. });
  17. setTimeout(function() {
  18. uni.hideToast();
  19. }, duration)
  20. }
  21. //等待框
  22. function showLoading(title='请稍候..', mask=false){
  23. // 弹出系统等待对话框
  24. // #ifdef APP-PLUS
  25. _loading = plus.nativeUI.showWaiting( title, {
  26. width: '76px',
  27. height: '76px',
  28. size: '12px',
  29. modal: mask,
  30. back: 'none', //安卓截获返回键 none:截获不做响应 close:截获后关闭等待框 none:不截获
  31. loading: {
  32. type: 'snow'
  33. }
  34. });
  35. setTimeout(()=>{
  36. hideLoading();
  37. }, 10000)
  38. return _loading;
  39. // #endif
  40. // #ifndef APP-PLUS
  41. uni.showLoading({
  42. title,
  43. mask
  44. })
  45. // #endif
  46. }
  47. function inAction(fn, that, timeout = 2000){
  48. if(that['in' + fn]){
  49. return true
  50. }
  51. that['in' + fn] = true;
  52. setTimeout(()=>{
  53. that['in' + fn] = false;
  54. }, timeout)
  55. return false;
  56. }
  57. // 返回上一页
  58. const prePage = () => {
  59. let pages = getCurrentPages();
  60. let prePage = pages[pages.length - 2];
  61. // #ifdef H5
  62. return prePage;
  63. // #endif
  64. return prePage.$vm;
  65. }
  66. // 上传图片--注按需要对res处理 JSON.parse()
  67. function uploadImg(formData){
  68. return new Promise((resolve,reject)=>{
  69. uni.chooseImage({
  70. count:1,
  71. success(chooseImageRes){
  72. // resolve(chooseImageRes.tempFilePaths[0]);
  73. const tempFilePaths = chooseImageRes.tempFilePaths[0];
  74. let token = uni.getStorageSync("token")
  75. uni.showLoading();
  76. uni.uploadFile({
  77. url:_config.consoleBaseUrl+'api/common/upload',
  78. filePath: tempFilePaths,
  79. name: 'file',
  80. formData:formData,
  81. header: {
  82. 'token':token,
  83. },
  84. success:(res)=>{
  85. resolve(res.data);
  86. },
  87. fail:(err)=>{
  88. msg('上传失败');
  89. reject();
  90. },
  91. complete:()=> {
  92. uni.hideLoading();
  93. },
  94. })
  95. },
  96. fail(err){
  97. // msg('打开相册失败');
  98. reject(err)
  99. }
  100. })
  101. })
  102. }
  103. // 深拷贝
  104. const deepCopy = (p, c) => {
  105. var c = c || {};
  106. for (var i in p) {
  107. if (typeof p[i] === "object") {
  108. c[i] = (p[i].constructor === Array) ? [] : {};
  109. deepCopy(p[i], c[i])
  110. } else {
  111. c[i] = p[i]
  112. }
  113. }
  114. return c;
  115. }
  116. function request(url, data, options, method="POST") {
  117. options = Object.assign({showLoading: false,loadingText: '加载中',loadingMask: false}, options);
  118. const header = {
  119. 'Content-Type': 'application/x-www-form-urlencoded'
  120. };
  121. const token = uni.getStorageSync('token') || '';
  122. if(token && typeof token == "string"){
  123. data={...data,token};
  124. }
  125. let params = {
  126. url: _config.consoleBaseUrl + url,
  127. //url,
  128. data,
  129. method,
  130. header,
  131. }
  132. return new Promise((resolve, reject) => {
  133. options.showLoading && showLoading(options.loadingText, options.loadingMask);
  134. params.complete = (response) => {
  135. options.showLoading && hideLoading();
  136. if (response.statusCode === 200) {
  137. if(response.data.code != 1){
  138. msg(response.data.msg);
  139. reject();
  140. }else{
  141. resolve(response.data);
  142. }
  143. }else if(response.statusCode === 401){
  144. msg("401,开发中。。。")
  145. reject();
  146. }else{
  147. msg('网络繁忙 ' + response.statusCode, 1500, true);
  148. reject();
  149. }
  150. }
  151. uni.request(params);
  152. })
  153. }
  154. function get(url, data={}, options={}) {
  155. return request(url, data, options, 'GET');
  156. }
  157. function post(url, data={}, options={}) {
  158. return request(url, data, options, 'POST');
  159. }
  160. // 处理时间
  161. const handleStrLength=(num)=> {
  162. return String(num).length < 2 ? '0' + num : num;
  163. }
  164. const handleDatetime=(date)=> {
  165. let m = handleStrLength(date.getMonth() + 1);
  166. let d = handleStrLength(date.getDate());
  167. let h = handleStrLength(date.getHours());
  168. let mm = handleStrLength(date.getMinutes());
  169. let dateStr = date.getFullYear() + "-" + m + "-" + d + " " + h + ":" + mm;
  170. return dateStr;
  171. }
  172. const getCurTime=()=> {
  173. let date = new Date();
  174. return handleDatetime(date);
  175. }
  176. const getEndTime=()=> {
  177. let date = new Date();
  178. date.setFullYear(date.getFullYear() + 1);
  179. return handleDatetime(date);
  180. }
  181. function match(str, type, showMsg=true){
  182. let exp = '';
  183. switch(type){
  184. case 'mobile':
  185. if(str === ''){
  186. showMsg && msg('请填写手机号码');
  187. return false;
  188. }
  189. exp = /(^1[3|4|5|6|7|8|9][0-9]{9}$)/; //手机号
  190. if(!exp.test(str)){
  191. showMsg && msg('手机号码格式不正确');
  192. return false;
  193. }
  194. break;
  195. case 'pwd':
  196. if(str === ''){
  197. showMsg && msg('请填写密码');
  198. return false;
  199. }
  200. case 'code':
  201. if(str === ''){
  202. showMsg && msg('请填写验证码');
  203. return false;
  204. }
  205. if(!/^[0-9]+$/.test(str)){
  206. showMsg && msg('验证码不正确');
  207. return false;
  208. }
  209. break;
  210. }
  211. return true;
  212. }
  213. ///**详情页处理富文本 details仅是变化替换部分a.replace(/<text>(abc)<\/text>/,function(match,$1) $1=abc---------------------------------------------------------------------- */
  214. const replaceDetail = (details = '') => {
  215. //newContent仅是details替换后内容;
  216. let newContent = details.replace(/<img[^>]*>/gi, function (match, capture) { //去除三标签
  217. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  218. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  219. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  220. return match;
  221. });
  222. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  223. newContent = newContent.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;"');
  224. return newContent;
  225. }
  226. export default {
  227. _config,
  228. msg,
  229. prePage,
  230. get,
  231. post,
  232. deepCopy,
  233. inAction,
  234. replaceDetail,
  235. getCurTime,
  236. getEndTime,
  237. match,
  238. uploadImg,
  239. };