index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. //封装好一系列常用的方法 dever
  2. import config from './config';
  3. //http网络请求
  4. var http = {
  5. //token的key
  6. token : 'token',
  7. //获取token
  8. getToken: function() {
  9. return uni.getStorageSync('prev1_' + this.token);
  10. },
  11. //设置token
  12. setToken: function(value) {
  13. return uni.setStorageSync('prev1_' + this.token, value);
  14. },
  15. //设置http的option
  16. option: function(options) {
  17. if (!options) {
  18. options = {};
  19. }
  20. options[this.token] = this.getToken();
  21. options['json'] = 1;
  22. return options;
  23. },
  24. //request请求
  25. request: function(vue, url, options, callback, err_callback) {
  26. var self = this;
  27. if (vue == 'post') {
  28. config.request.method = 'POST';
  29. config.request.method = 'GET';
  30. vue = false;
  31. } else {
  32. config.request.method = 'GET';
  33. }
  34. if (typeof(url) == 'object') {
  35. vue = url[1];
  36. url = url[0];
  37. }
  38. if (typeof(vue) == 'object') {
  39. vue.isDisabled = true;
  40. }
  41. self.core(url, options).then((result, state) => {
  42. if (typeof(vue) == 'object') {
  43. if (!vue.fetch) {
  44. vue.fetch = {}
  45. }
  46. vue.isDisabled = false;
  47. vue.fetch = Object.assign({}, vue.fetch, result)
  48. }
  49. if (callback) {
  50. callback(result, state);
  51. }
  52. }).catch((result) => {
  53. if (typeof(vue) == 'object') {
  54. vue.isDisabled = false;
  55. }
  56. if (err_callback) {
  57. err_callback(result);
  58. } else {
  59. if (result.code == '2') {
  60. dever.location(dever.login);
  61. } else {
  62. dever.alert(result.msg)
  63. }
  64. }
  65. })
  66. },
  67. core : function(url, options) {
  68. var self = this;
  69. options = this.option(options);
  70. if (!options.noconcat) {
  71. options.noconcat = false;
  72. }
  73. if (config.server[url]) {
  74. config.request.url = config.server[url];
  75. } else {
  76. config.request.url = url;
  77. }
  78. var loading_method = config.request.method;
  79. if (options && options.page && options.page > 0) {
  80. options.pg = options.page;
  81. loading_method = 'page';
  82. }
  83. if (config.request.url.indexOf('http') == -1) {
  84. config.request.url = config.request.host + config.request.url;
  85. }
  86. config.request.data = options;
  87. return new Promise((resolve, reject) => {
  88. config.request.complete = (response) => {
  89. dever.debug(response);
  90. if (response.statusCode === 200) {
  91. if (response.data.code == '0') {
  92. if (response.data.page) {
  93. dever.pageData = response.data.page;
  94. } else {
  95. dever.pageData = {};
  96. }
  97. if (options.page && options.page > 0 && !options.noconcat) {
  98. page.state = true;
  99. if (page.key && response.data.data[page.key].length > 0) {
  100. if (options && options.concat && options.concat == -1) {
  101. page.data = response.data.data[page.key].concat(page.data);
  102. } else {
  103. page.data = page.data.concat(response.data.data[page.key]);
  104. }
  105. response.data.data[page.key] = page.data;
  106. } else if (response.data.data.length > 0) {
  107. if (options && options.concat && options.concat == -1) {
  108. response.data.data = response.data.data.reverse();
  109. page.data = response.data.data.concat(page.data);
  110. } else {
  111. page.data = page.data.concat(response.data.data);
  112. }
  113. response.data.data = page.data;
  114. } else {
  115. page.state = false;
  116. resolve(false);
  117. /*
  118. if (page.key) {
  119. resolve(response.data.data[page.key], false);
  120. } else {
  121. resolve(response.data.data, false);
  122. }
  123. */
  124. if (options && options.noloading) {
  125. } else {
  126. dever.hideLoading(loading_method, options, url);
  127. }
  128. return;
  129. }
  130. }
  131. resolve(response.data.data);
  132. } else if (response.data.code == '-1') {
  133. //退出登录
  134. self.setToken('');
  135. dever.location('index/index', 'go');
  136. reject(response.data)
  137. } else {
  138. reject(response.data)
  139. }
  140. if (options && options.noloading) {
  141. } else {
  142. dever.hideLoading(loading_method, options, url);
  143. }
  144. } else {
  145. // 处理catch 请求,不在本页面之外处理,统一在这里处理
  146. if (options && options.handle) {
  147. reject(response)
  148. } else {
  149. try {
  150. Promise.reject(response).catch(err => {
  151. self.error(response.statusCode || response.errMsg);
  152. });
  153. } catch (e) {
  154. dever.alert(e)
  155. }
  156. }
  157. if (options && options.noloading) {
  158. } else {
  159. dever.hideLoading(loading_method, options, url);
  160. }
  161. }
  162. }
  163. //uni.request(Object.assign({}, config, options));
  164. dever.debug(config.request);
  165. if (options.noloading) {
  166. uni.stopPullDownRefresh();
  167. } else {
  168. dever.loading(loading_method, options, url);
  169. }
  170. uni.request(config.request);
  171. })
  172. },
  173. error : function(err) {
  174. //console.error("请求背拒绝" + err)
  175. /*
  176. dever.data('web_error', err);
  177. var path = '/components/dever/pages/web_error';
  178. uni.navigateTo({
  179. url: path
  180. })
  181. return;
  182. */
  183. switch (err) {
  184. case 401:
  185. // 错误码404的处理方式
  186. console.error("请求背拒绝" + err)
  187. break;
  188. case 404:
  189. // 错误码404的处理方式
  190. console.error("没有找到页面" + err)
  191. break;
  192. case 500:
  193. // 错误码404的处理方式
  194. console.error("500服务器错误" + err)
  195. break;
  196. case 405:
  197. console.error("错误的请求" + err)
  198. break;
  199. }
  200. },
  201. }
  202. var im = {
  203. socket : false,
  204. init : function() {
  205. this.connect();
  206. this.info();
  207. },
  208. connect : function() {
  209. uni.connectSocket({
  210. url: 'text://me.5dev.cn:8282',
  211. complete: (res)=> {
  212. console.log(res);
  213. }
  214. });
  215. uni.onSocketOpen(function (res) {
  216. console.log('WebSocket连接已打开!');
  217. });
  218. uni.onSocketError(function (res) {
  219. console.log('WebSocket连接打开失败,请检查!');
  220. });
  221. },
  222. info : function() {
  223. console.info(222);
  224. uni.onSocketMessage(function (res) {
  225. console.log('收到服务器内容:' + res.data);
  226. });
  227. }
  228. }
  229. var page = {
  230. //分页控制
  231. value : 1,
  232. //分页数据
  233. data : [],
  234. //分页所属的key
  235. key : '',
  236. //分页状态 true可以分页,false不能分页
  237. state : true,
  238. //通用的获取数据方法:瀑布流分页 (1,'level')
  239. get : function(config, vue, url, options, callback, err_callback) {
  240. options = http.option(options);
  241. if (typeof(config) == 'object') {
  242. var state = config[0];
  243. this.key = config[1];
  244. } else {
  245. var state = config;
  246. this.key = '';
  247. }
  248. if (state == 1) {
  249. this.value = 1;
  250. this.data = [];
  251. } else {
  252. this.value++;
  253. }
  254. if (this.state == false && state != 1) {
  255. return;
  256. }
  257. options.page = this.value;
  258. http.request(vue, url, options, callback, err_callback);
  259. },
  260. }
  261. var upload = {
  262. data : {},
  263. formatDate : function(date) {
  264. var date = new Date(date);
  265. var YY = date.getFullYear() + '-';
  266. var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  267. var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  268. var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  269. var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  270. var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  271. return YY + MM + DD +" "+hh + mm + ss;
  272. },
  273. handle : function(key, count, callback) {
  274. if (!count) {
  275. count = 1;
  276. }
  277. var type = 1;
  278. if (count > 1) {
  279. type = 2;
  280. }
  281. var self = this;
  282. count = parseInt(count);
  283. uni.chooseImage({
  284. count: count,
  285. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  286. sourceType: ['album'], //从相册选择
  287. success: res => {
  288. uni.showLoading();
  289. res.tempFilePaths.forEach(function(item, index) {
  290. //请求上传接口
  291. uni.uploadFile({
  292. url: config.upload.url, //仅为示例,非真实的接口地址
  293. filePath: item,
  294. name: config.upload.name,
  295. formData: { token: http.getToken() },
  296. success: res => {
  297. dever.debug(res);
  298. var data = JSON.parse(res.data);
  299. uni.hideLoading();
  300. if (data.uploaded) {
  301. var backUrl = data.url;
  302. if (count > 1) {
  303. if (!self.data[key]) {
  304. self.data[key] = [];
  305. }
  306. if (self.data[key].length < count) {
  307. self.data[key].push(backUrl);
  308. }
  309. } else {
  310. self.data[key] = backUrl;
  311. }
  312. if (callback) {
  313. callback(type, self.data[key]);
  314. }
  315. } else if (data.uploaded == false) {
  316. uni.showToast({ title: data.error.message, icon: 'none' });
  317. }
  318. }
  319. });
  320. });
  321. },
  322. fail: () => {
  323. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  324. uni.hideLoading();
  325. }
  326. });
  327. },
  328. upfile : function(e, key, count, callback, ext, title, style) {
  329. if (!count) {
  330. count = 1;
  331. }
  332. var type = 1;
  333. if (count > 1) {
  334. type = 2;
  335. }
  336. if (!title) {
  337. title = '点击这里上传文件';
  338. }
  339. var self = this;
  340. count = parseInt(count);
  341. var a = document.createElement('button');
  342. a.className = 'file';
  343. if (style) {
  344. a.style.cssText = style;
  345. } else {
  346. a.style.cssText = 'padding: 4px 10px;height: 20px;line-height: 20px;position: relative;cursor: pointer;color: #888;background: #fafafa;border: 1px solid #ddd;border-radius: 4px;overflow: hidden;display: inline-block;*display: inline;*zoom: 1;';
  347. }
  348. var style = 'position: absolute;opacity: 0;filter: alpha(opacity=0);cursor: pointer';
  349. a.innerHTML = '<input style="'+style+'" type="file" name="upload_file" id="upload_file" />' + title;
  350. e.$refs.input.$el.appendChild(a)
  351. var input = document.getElementById('upload_file');
  352. input.onchange = (event) => {
  353. uni.showLoading();
  354. var reader = new FileReader();
  355. var type = input.files[0].type;
  356. var temp = type.split('/');
  357. type = temp[1];
  358. //console.info(input.files[0]);
  359. if (ext && ext.indexOf(type) == -1) {
  360. uni.showToast({ title: '您上传的文件类型错误,请上传带有' + ext + '这些后缀的文件', icon: 'none' });
  361. } else {
  362. reader.readAsDataURL(input.files[0]);
  363. reader.onload = function(){
  364. //读取完成后,数据保存在对象的result属性中
  365. var pic = this.result;
  366. var temp = pic.split('base64,');
  367. pic = temp[1];
  368. dever.debug(pic);
  369. var url = "http://up.qiniu.com/putb64/-1";
  370. var xhr = new XMLHttpRequest();
  371. xhr.onreadystatechange=function(){
  372. if (xhr.readyState==4){
  373. dever.debug(xhr.responseText);
  374. var data = JSON.parse(xhr.responseText);
  375. uni.hideLoading();
  376. if (data.uploaded) {
  377. var backUrl = data.url;
  378. if (count > 1) {
  379. if (!self.data[key]) {
  380. self.data[key] = [];
  381. }
  382. self.data[key].push(backUrl);
  383. } else {
  384. self.data[key] = backUrl;
  385. }
  386. if (callback) {
  387. callback(type, self.data[key], input.files[0]);
  388. }
  389. } else if (data.uploaded == false) {
  390. uni.showToast({ title: data.error.message, icon: 'none' });
  391. }
  392. }
  393. }
  394. xhr.open("POST", url, true);
  395. xhr.setRequestHeader("Content-Type", "application/octet-stream");
  396. xhr.setRequestHeader("Authorization", "UpToken " + e.token);
  397. xhr.send(pic);
  398. }
  399. }
  400. }
  401. },
  402. qnUpload : function(qn, config, key, count, callback) {
  403. if (!count) {
  404. count = 1;
  405. }
  406. var type = 1;
  407. if (count > 1) {
  408. type = 2;
  409. }
  410. var self = this;
  411. count = parseInt(count);
  412. var call = function(data) {
  413. var backUrl = data.url;
  414. if (count > 1) {
  415. if (!self.data[key]) {
  416. self.data[key] = [];
  417. }
  418. self.data[key].push(backUrl);
  419. } else {
  420. self.data[key] = backUrl;
  421. }
  422. if (callback) {
  423. callback(type, self.data[key]);
  424. }
  425. }
  426. uni.chooseImage({
  427. count: count,
  428. //sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  429. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  430. sourceType: ['album'], //从相册选择
  431. success: res => {
  432. uni.showLoading();
  433. res.tempFilePaths.forEach(function(item, index) {
  434. self.qiniu(qn, item, config.domain, config.token, call);
  435. });
  436. },
  437. fail: () => {
  438. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  439. uni.hideLoading();
  440. }
  441. });
  442. },
  443. qiniu : function(qn, file, domain, token, call, handle) {
  444. //请求上传接口
  445. var config = {};
  446. config = {
  447. region: 'ECN',
  448. domain: domain,
  449. //key: 'test.mp4',
  450. };
  451. if (token.indexOf('http') == -1) {
  452. config.uptoken = token;
  453. } else {
  454. config.uptokenURL = token;
  455. }
  456. uni.showLoading({title: '上传中', mask: true});
  457. qn.upload(
  458. file,
  459. res => {
  460. dever.debug(res);
  461. var data = res;
  462. uni.hideLoading();
  463. if (data.uploaded) {
  464. call(data);
  465. } else {
  466. uni.showToast({title: data.error.message, icon: 'none' });
  467. }
  468. },
  469. error => {
  470. uni.showToast({title: '上传失败,请重试', icon: 'none' });
  471. uni.hideLoading();
  472. },
  473. config,
  474. res => {
  475. //上传进度
  476. if (handle) {
  477. handle(res.progress);
  478. } else {
  479. uni.showToast({title: '上传进度:%' + res.progress, icon: 'none' });
  480. }
  481. }
  482. );
  483. },
  484. uploadDel : function(key, index, callback) {
  485. var self = this;
  486. uni.showModal({
  487. content: '确定要删除吗?',
  488. cancelText: '取消',
  489. confirmText: '确定',
  490. success: res => {
  491. if (res.confirm) {
  492. // self.data[key].splice(index, 1);
  493. if (callback) {
  494. callback(res, index);
  495. }
  496. }
  497. }
  498. });
  499. }
  500. }
  501. var dever = {
  502. login : 'user/login',
  503. web_view : '/lib/dever/pages/web_view',
  504. source : 'h5',
  505. host : '',
  506. doc : false,
  507. config : {},
  508. vue : false,
  509. cur : false,
  510. getLoadingState : {},
  511. getLoadingCall : false,
  512. postLoadingCall : false,
  513. pageLoadingCall : false,
  514. pageData : {},
  515. lastGetUrl : '',
  516. showLoad : false,
  517. btnText : '确定提交',
  518. switchTab : [],
  519. switchTabCall : {},
  520. slideData : {},
  521. //获取当前时间戳
  522. curTime : function() {
  523. var time = Date.parse(new Date())/1000;
  524. return time;
  525. },
  526. //载入全局配置
  527. init : function(url, vue, value, set) {
  528. this.vue = vue;
  529. vue.prototype.$config = value;
  530. var self = this;
  531. var config = self.data('dever_config');
  532. var state = false;
  533. if (config && !set) {
  534. self.config = config;
  535. vue.prototype.$config = self.config;
  536. if (self.config.update_time) {
  537. var time = this.curTime();
  538. if (!self.config.update_time || time - self.config.update_time >= value.set_update) {
  539. state = true;
  540. }
  541. } else {
  542. state = false;
  543. }
  544. } else {
  545. state = true;
  546. }
  547. if (state) {
  548. this.setConfig(url, value);
  549. }
  550. return this;
  551. },
  552. //设置全局变量
  553. setConfig : function(url, config, key, value) {
  554. var self = this;
  555. if (!config && key && value) {
  556. config = self.data('dever_config');
  557. config[key] = value;
  558. }
  559. self.config = config;
  560. self.vue.prototype.$config = self.config;
  561. self.data('dever_config', self.config);
  562. if (url) {
  563. self.post(url, {noloading:1}, function(t) {
  564. self.config = t;
  565. self.config.update_time = self.curTime();
  566. self.data('dever_config', self.config);
  567. self.vue.prototype.$config = self.config;
  568. });
  569. }
  570. },
  571. //加载通用的方法
  572. common : function(func) {
  573. this.source = 'h5';
  574. this.host = '';
  575. //#ifdef H5
  576. this.source = 'h5';
  577. this.host = window.location.protocol + '//' + window.location.host + '/#';
  578. //#endif
  579. //#ifdef APP-PLUS
  580. this.source = 'app';
  581. //#endif
  582. //#ifdef MP-WEIXIN
  583. this.source = 'applet';
  584. //#endif
  585. if (func) {
  586. for (var i in func) {
  587. this.vue.prototype[i] = func[i];
  588. }
  589. }
  590. },
  591. //设置提交的按钮
  592. btnLoad : function(cur) {
  593. if (!cur) {
  594. cur = this.cur;
  595. }
  596. if (!cur) {
  597. return;
  598. }
  599. cur.btn = this.btn(cur);
  600. },
  601. //设置提交的按钮
  602. btnFinish : function() {
  603. var cur = this.cur;
  604. if (!cur) {
  605. return;
  606. }
  607. cur.btn = {
  608. disabled:false,
  609. loading:false,
  610. text:this.btnText,
  611. };
  612. },
  613. //设置提交的按钮
  614. btn : function(cur, text) {
  615. this.cur = cur;
  616. if (text) {
  617. this.btnText = text;
  618. }
  619. return {
  620. disabled:true,
  621. loading:true,
  622. text:'加载中...',
  623. };
  624. },
  625. //设置loading
  626. setGetLoading : function(yes, no) {
  627. this.getLoadingCall = {'yes': yes, 'no' : no};
  628. },
  629. setPostLoading : function(yes, no) {
  630. this.postLoadingCall = {'yes': yes, 'no' : no};
  631. },
  632. setPageLoading : function(yes, no) {
  633. this.pageLoadingCall = {'yes': yes, 'no' : no};
  634. },
  635. //获取当前route
  636. route : function() {
  637. var page = this.getPage();
  638. if (!page) {
  639. return '';
  640. }
  641. var route = page.route;
  642. return route;
  643. },
  644. //获取当前param
  645. param : function() {
  646. var page = this.getPage();
  647. if (!page) {
  648. return '';
  649. }
  650. var options = page.options;
  651. return options;
  652. },
  653. //获取当前url
  654. url : function() {
  655. var page = this.getPage();
  656. if (!page) {
  657. return '';
  658. }
  659. var route = page.route;
  660. var options = page.options;
  661. // 拼接参数
  662. let param = []
  663. if (options) {
  664. for (let key in options) {
  665. param.push(key + '=' + options[key]);
  666. }
  667. }
  668. param = param.join('&');
  669. if (param) {
  670. param = '?' + param;
  671. }
  672. return '/' + route + param;
  673. },
  674. //获取当前page
  675. getPage : function() {
  676. var pages = getCurrentPages();
  677. if (pages.length > 0) {
  678. return pages[pages.length - 1];
  679. } else {
  680. return false;
  681. }
  682. },
  683. //loading
  684. loading : function(method, options, url) {
  685. var state = false;
  686. if (method == 'POST') {
  687. if (this.postLoadingCall && this.postLoadingCall.yes) {
  688. state = true;
  689. var callback = this.postLoadingCall.yes;
  690. callback(this);
  691. }
  692. } else if (method == 'page') {
  693. //state = true;
  694. if (options.page && options.page > 1) {
  695. state = false;
  696. }
  697. if (this.pageLoadingCall && this.pageLoadingCall.yes) {
  698. state = true;
  699. var callback = this.pageLoadingCall.yes;
  700. callback(this);
  701. }
  702. } else {
  703. url = this.route();
  704. if (this.lastGetUrl && this.lastGetUrl != url && this.getLoadingState[this.lastGetUrl]) {
  705. this.getLoadingState[this.lastGetUrl] = false;
  706. }
  707. if (this.getLoadingState && this.getLoadingState[url]) {
  708. state = false;
  709. } else {
  710. if (this.getLoadingCall && this.getLoadingCall.yes) {
  711. state = true;
  712. var callback = this.getLoadingCall.yes;
  713. callback(this);
  714. }
  715. }
  716. }
  717. if (!state) {
  718. uni.showLoading({title: '加载中', mask: true});
  719. //uni.showNavigationBarLoading();
  720. this.showLoad = true;
  721. }
  722. uni.stopPullDownRefresh();
  723. },
  724. //取消loading
  725. hideLoading : function(method, options, url) {
  726. var state = false;
  727. if (method == 'POST' || method == 'post') {
  728. if (this.postLoadingCall && this.postLoadingCall.no) {
  729. state = true;
  730. var callback = this.postLoadingCall.no;
  731. callback(this);
  732. }
  733. } else if (method == 'page') {
  734. //state = true;
  735. if (options.page && options.page > 1) {
  736. state = false;
  737. }
  738. if (this.pageLoadingCall && this.pageLoadingCall.no) {
  739. state = true;
  740. var callback = this.pageLoadingCall.no;
  741. callback(this);
  742. }
  743. } else {
  744. url = this.route();
  745. if (!this.lastGetUrl || (this.lastGetUrl && this.lastGetUrl != url)) {
  746. this.lastGetUrl = url;
  747. }
  748. if (this.getLoadingState && this.getLoadingState[url]) {
  749. state = false;
  750. } else {
  751. this.getLoadingState[url] = true;
  752. if (this.getLoadingCall && this.getLoadingCall.no) {
  753. state = true;
  754. var callback = this.getLoadingCall.no;
  755. callback(this);
  756. }
  757. }
  758. }
  759. if (!state && this.showLoad) {
  760. uni.hideLoading();
  761. this.showLoad = false;
  762. this.btnFinish();
  763. //uni.hideNavigationBarLoading();
  764. }
  765. },
  766. //data数据获取
  767. dataset : function(e) {
  768. if (e.currentTarget) {
  769. var dataset = e.currentTarget.dataset;
  770. } else {
  771. var dataset = e.target.dataset;
  772. }
  773. return dataset;
  774. },
  775. //view中的页面提示
  776. viewAlert : function(e) {
  777. if (e) {
  778. var dataset = this.dataset(e);
  779. var msg = dataset.msg;
  780. var icon = dataset.icon;
  781. var callback = dataset.callback;
  782. this.alert(msg, icon, callback);
  783. } else {
  784. this.alert('敬请期待');
  785. }
  786. },
  787. //view中的页面跳转
  788. viewLocation : function(e) {
  789. var page = '';
  790. var to = '';
  791. var option = '';
  792. if (e) {
  793. var dataset = this.dataset(e);
  794. page = dataset.page;
  795. to = dataset.to;
  796. option = []
  797. for (var i in dataset) {
  798. if (i.indexOf('save_') != -1) {
  799. var v = dataset[i];
  800. i = i.replace('save_', '');
  801. this.data(i, v);
  802. } else if (i != 'page' && i != 'to') {
  803. option.push(i + '=' + dataset[i]);
  804. }
  805. }
  806. if (option) {
  807. option = option.join('&');
  808. }
  809. }
  810. if (!page) {
  811. uni.navigateBack({});
  812. } else {
  813. var go = '';
  814. if (to) {
  815. go = 'to';
  816. }
  817. if (option) {
  818. page = page + '?' + option;
  819. }
  820. this.location(page, go);
  821. }
  822. },
  823. //页面跳转
  824. location : function(path, go) {
  825. var self = this;
  826. if (path.indexOf('http') > -1) {
  827. this.debug('http:' + path);
  828. this.data('web_view', path);
  829. if (this.source == 'app' || this.source == 'applet' || go == 'webview') {
  830. uni.navigateTo({
  831. url: this.web_view
  832. })
  833. } else {
  834. if (!go) {
  835. go = '';
  836. }
  837. if (go && go == 'location') {
  838. window.location.href = path;
  839. } else {
  840. var url = window.location.href;
  841. if (go.indexOf('/') != -1) {
  842. url = this.host + go
  843. } else {
  844. url = url + go;
  845. }
  846. url = window.btoa(url);
  847. if (path.indexOf('?') == -1) {
  848. path = path + '?';
  849. } else {
  850. path = path + '&';
  851. }
  852. window.location.href = path + 'refer=' + url;
  853. }
  854. }
  855. } else {
  856. this.debug('navigateTo:' + go + ':' + path);
  857. if (path.indexOf('/pages/') == -1) {
  858. path = '/pages/' + path;
  859. }
  860. if (this.switchTab.indexOf(path) != -1) {
  861. uni.switchTab({
  862. url: path,
  863. success: function(e) {
  864. if (self.source != 'app' && self.switchTabCall[path]) {
  865. var call = self.switchTabCall[path];
  866. call(self, e);
  867. }
  868. }
  869. })
  870. } else if (go) {
  871. uni.redirectTo({
  872. url: path
  873. })
  874. } else {
  875. uni.navigateTo({
  876. url: path
  877. })
  878. }
  879. }
  880. },
  881. //提示信息
  882. alert : function(info, icon, callback) {
  883. if (!icon) {
  884. icon = 'none'
  885. }
  886. if (info) {
  887. uni.showToast({title: info, icon: icon});
  888. }
  889. this.debug('alert:' + info)
  890. if (callback) {
  891. this.debug('callback:' + callback)
  892. setTimeout(function(){
  893. callback();
  894. }, 1000)
  895. }
  896. },
  897. //debug调试
  898. debug : function(string) {
  899. if (config.base.debug) {
  900. this.log(string);
  901. }
  902. },
  903. //记录日志
  904. log : function(string) {
  905. console.log(string);
  906. },
  907. //获取token
  908. getToken: function() {
  909. return http.getToken();
  910. },
  911. //设置token
  912. setToken: function(value) {
  913. return http.setToken(value);
  914. },
  915. //通用的提交数据的方法
  916. post : function(url, options, callback, err_callback) {
  917. http.request('post', url, options, callback, err_callback);
  918. },
  919. //通用的获取数据方法
  920. get : function(vue, url, options, callback, err_callback) {
  921. http.request(vue, url, options, callback, err_callback);
  922. },
  923. //通用的获取数据方法
  924. page : function(config, vue, url, options, callback, err_callback) {
  925. page.get(config, vue, url, options, callback, err_callback);
  926. },
  927. //上传
  928. upload : function(id, count, callback) {
  929. this.getUpload().handle(id, count, callback);
  930. },
  931. //上传
  932. qiniu : function(qn, self, id, count, callback, handle) {
  933. this.getUpload().qiniu(qn, self, id, count, callback, handle);
  934. },
  935. //上传文件
  936. upfile : function(self, key, count, callback, ext, title, style) {
  937. this.getUpload().upfile(self, key, count, callback, ext, title, style);
  938. },
  939. //获取upload
  940. getUpload : function() {
  941. return upload;
  942. },
  943. //im聊天
  944. im : function() {
  945. im.init();
  946. },
  947. //删除上传
  948. uploadDel : function(id, index, callback) {
  949. upload.uploadDel(id, index, callback);
  950. },
  951. //验证登录
  952. checkLogin : function(refer) {
  953. if (!this.getToken()) {
  954. this.data('login_refer', this.getRefer(refer));
  955. //this.hideLoading();
  956. if (this.source == 'h5') {
  957. this.location(this.login, 'go');
  958. } else {
  959. this.location(this.login, 'go');
  960. }
  961. }
  962. },
  963. //获取refer
  964. getRefer : function(refer) {
  965. if (!refer) {
  966. refer = this.url();
  967. }
  968. return refer;
  969. },
  970. //数据存储
  971. data : function(key, value) {
  972. if (value) {
  973. if (value == 'del') {
  974. uni.removeStorageSync(key);
  975. return true;
  976. } else {
  977. uni.setStorageSync(key, value);
  978. return value;
  979. }
  980. } else {
  981. return uni.getStorageSync(key);
  982. }
  983. },
  984. //插入html代码,初始化
  985. initHtml : function(doc) {
  986. this.doc = doc.$refs.initHtml.$el;
  987. },
  988. //插入html代码
  989. html : function(html) {
  990. var self = this;
  991. var div = document.createElement('div');
  992. div.innerHTML = html;
  993. this.doc.appendChild(div);
  994. var scripts = div.querySelectorAll('script');
  995. return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
  996. return chain.then(() => self.runScript(script));
  997. }, Promise.resolve());
  998. },
  999. //微信提醒
  1000. wxTip : function() {
  1001. var wx = this.is_weixin();
  1002. if (wx) {
  1003. var tip = '<div id="weixin-tip" style="position: fixed; left:0; top:0; background: rgba(0,0,0,0.8); filter:alpha(opacity=80); width: 100%; height:100%; z-index: 100;" onclick="document.getElementById(\'weixin-tip\').remove()"><p style="text-align: center; margin-top: 10%; padding:0 5%;"><img src="static/dever/live_weixin.png" alt="微信打开" style="max-width: 100%; height: auto;"/></p></div>';
  1004. this.html(tip);
  1005. return true;
  1006. }
  1007. return false;
  1008. },
  1009. is_weixin: function() {
  1010. if (this.source != 'h5') {
  1011. return false;
  1012. }
  1013. var ua = navigator.userAgent.toLowerCase();
  1014. if(ua.match(/MicroMessenger/i)=="micromessenger") {
  1015. return true;
  1016. } else {
  1017. return false;
  1018. }
  1019. },
  1020. //执行script代码
  1021. runScript : function(script) {
  1022. return new Promise((reslove, rejected) => {
  1023. const newScript = document.createElement('script');
  1024. newScript.innerHTML = script.innerHTML;
  1025. const src = script.getAttribute('src');
  1026. if (src) newScript.setAttribute('src', src);
  1027. // script 加载完成和错误处理
  1028. newScript.onload = () => reslove();
  1029. newScript.onerror = err => rejected();
  1030. document.head.appendChild(newScript);
  1031. document.head.removeChild(newScript);
  1032. if (!src) {
  1033. // 如果是 inline script 执行是同步的
  1034. reslove();
  1035. }
  1036. })
  1037. },
  1038. //app支付
  1039. appPayment : function(type, order, callback, errorCallback) {
  1040. uni.requestPayment({
  1041. provider: type,
  1042. orderInfo: order, //微信、支付宝订单数据
  1043. success: function (res) {
  1044. //console.log('success:' + JSON.stringify(res));
  1045. callback(res);
  1046. },
  1047. fail: function (err) {
  1048. //console.log('fail:' + JSON.stringify(err));
  1049. errorCallback(err)
  1050. }
  1051. });
  1052. },
  1053. //跳转到refer
  1054. jump : function() {
  1055. var refer = this.data('login_refer');
  1056. var id = this.data('invite_id');
  1057. var type = this.data('invite_type');
  1058. if (id && refer != 'index/index') {
  1059. refer = refer + '?id=' + id + '&type=' + type;
  1060. }
  1061. this.data('login_refer', 'del');
  1062. this.data('invite_id', 'del');
  1063. this.data('invite_type', 'del');
  1064. if (!refer) {
  1065. refer = 'index/index';
  1066. }
  1067. this.location(refer, 'go');
  1068. },
  1069. //转星号
  1070. xing : function(s) {
  1071. return s.replace(s, function(sMatch){
  1072. return sMatch.replace(/./g,"*");
  1073. });
  1074. },
  1075. //检查更新
  1076. checkUpdate : function() {
  1077. this.update();
  1078. },
  1079. update : function(path, call) {
  1080. var self = this;
  1081. var source = this.source;
  1082. if (source != 'app') {
  1083. return;
  1084. }
  1085. var type = plus.os.name.toLowerCase();
  1086. //var type = 'android';
  1087. //var config = this.config.version;
  1088. self.post('isUpdate', {t:1}, function(res) {
  1089. if (type == 'android') {
  1090. res.downloadUrl = res.androidUrl;
  1091. } else {
  1092. res.downloadUrl = res.iosUrl;
  1093. }
  1094. self.data('dever_update_link', res.downloadUrl);
  1095. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  1096. var version = widgetInfo.versionCode;
  1097. var state = false;
  1098. state = widgetInfo.versionCode < res.versionCode;
  1099. if (state) {
  1100. //强制更新
  1101. var txt = '正在为您下载更新,下载完成将重启应用';
  1102. if (res.forceUpdate == true) {
  1103. self.down(res, res.downloadUrl, txt, type);
  1104. } else if(call) {
  1105. call(res, txt, type);
  1106. } else {
  1107. uni.showModal({
  1108. title: '发现新版本',
  1109. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  1110. success: (t) => {
  1111. if (t.confirm) {
  1112. if (path) {
  1113. self.location(path);
  1114. } else {
  1115. self.down(res, res.downloadUrl, txt, type);
  1116. }
  1117. }
  1118. }
  1119. })
  1120. }
  1121. }
  1122. });
  1123. });
  1124. },
  1125. uploadCall : function(config, type, packgePath) {
  1126. // 保存更新记录到stroage,下次启动app时安装更新
  1127. var self = this;
  1128. self.data('dever_update', 1);
  1129. self.data('dever_update_down', packgePath);
  1130. // 任务完成,关闭下载任务,开始安装
  1131. plus.runtime.install(packgePath, {force: true}, function() {
  1132. self.data('dever_update', 2);
  1133. self.data('dever_update_down', 'del');
  1134. uni.showModal({
  1135. title: '提示',
  1136. content: '应用将重启以完成更新',
  1137. showCancel: false,
  1138. complete: () => {
  1139. plus.runtime.restart();
  1140. }
  1141. })
  1142. });
  1143. },
  1144. down : function(config, url, txt, type, call) {
  1145. if (!url) {
  1146. return false;
  1147. }
  1148. if (txt) {
  1149. this.alert(txt);
  1150. }
  1151. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  1152. plus.runtime.openURL(url);
  1153. return false;
  1154. }
  1155. var self = this;
  1156. var packgePath = self.data('dever_update_down');
  1157. if (packgePath) {
  1158. self.uploadCall(config, type, packgePath);
  1159. return false;
  1160. }
  1161. var downloadTask = uni.downloadFile({
  1162. url: url,
  1163. success: (res) => {
  1164. if (res.statusCode === 200) {
  1165. // 保存下载的安装包
  1166. uni.saveFile({
  1167. tempFilePath: res.tempFilePath,
  1168. success: (res) => {
  1169. var packgePath = res.savedFilePath;
  1170. if (call) {
  1171. call();
  1172. }
  1173. self.uploadCall(config, type, packgePath);
  1174. downloadTask.abort();
  1175. downloadTask = null;
  1176. }
  1177. })
  1178. }
  1179. }
  1180. });
  1181. return downloadTask;
  1182. },
  1183. //预览图片
  1184. viewPic : function(imgs, img, key) {
  1185. if (imgs && imgs.length > 0) {
  1186. if (key) {
  1187. var temp = [];
  1188. var i;
  1189. for(i in imgs) {
  1190. temp.push(imgs[i][key]);
  1191. }
  1192. imgs = temp;
  1193. }
  1194. uni.previewImage({
  1195. current:img,
  1196. urls: imgs,
  1197. indicator : 'default',
  1198. loop : true,
  1199. /*
  1200. longPressActions : {
  1201. itemList: ['发送给朋友', '保存图片', '收藏'],
  1202. success: function(data) {
  1203. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  1204. },
  1205. fail: function(err) {
  1206. console.log(err.errMsg);
  1207. }
  1208. }
  1209. */
  1210. });
  1211. }
  1212. },
  1213. //截取APP退出功能
  1214. quit : function(page, call) {
  1215. var self = this;
  1216. if (self.source == 'app') {
  1217. var main = plus.android.runtimeMainActivity();
  1218. //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
  1219. /*
  1220. plus.runtime.quit = function(){
  1221. main.moveTaskToBack(false);
  1222. };
  1223. */
  1224. //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
  1225. plus.nativeUI.toast = (function(str){
  1226. if(str == '再按一次退出应用') {
  1227. if (call) {
  1228. call(main);
  1229. } else {
  1230. var webview = page.$mp.page.$getAppWebview();
  1231. var child = webview.children();
  1232. child[0].back();
  1233. }
  1234. return false;
  1235. }else{
  1236. self.alert(str);
  1237. }
  1238. });
  1239. }
  1240. },
  1241. //滑动
  1242. slide : function(e) {
  1243. this.slideData.clientX = e.changedTouches[0].clientX;
  1244. this.slideData.clientY = e.changedTouches[0].clientY;
  1245. },
  1246. //滑动结束
  1247. slideEnd : function(e) {
  1248. var subX = e.changedTouches[0].clientX - this.slideData.clientX;
  1249. var subY = e.changedTouches[0].clientY - this.slideData.clientY;
  1250. if (subY > 50) {
  1251. //上滑
  1252. return 1;
  1253. } else if (subY < -50) {
  1254. //下滑
  1255. return 2;
  1256. } else if (subX > 50) {
  1257. //左滑
  1258. return 3;
  1259. } else if(subX < -50) {
  1260. //右滑
  1261. return 4;
  1262. }
  1263. return
  1264. },
  1265. //计算图片宽高比
  1266. getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
  1267. let [
  1268. // 用于设定图片的宽和高
  1269. tempWidth,
  1270. tempHeight,
  1271. ] = [
  1272. undefined,
  1273. undefined
  1274. ]
  1275. try {
  1276. imgWidth = parseFloat(imgWidth)
  1277. imgHeight = parseFloat(imgHeight)
  1278. containerWidth = parseFloat(containerWidth)
  1279. containerHeight = parseFloat(containerHeight)
  1280. } catch (error) {
  1281. throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
  1282. }
  1283. if (imgWidth > 0 && imgHeight > 0) {
  1284. //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
  1285. if (imgWidth / imgHeight >= containerWidth / containerHeight) {
  1286. if (imgWidth > containerWidth) {
  1287. // alert('aaaaaaaa')
  1288. tempWidth = containerWidth
  1289. // 按原图片的比例进行缩放
  1290. tempHeight = (imgHeight * containerWidth) / imgWidth
  1291. } else {
  1292. // 按照图片的大小进行缩放
  1293. tempWidth = imgWidth
  1294. tempHeight = imgHeight
  1295. }
  1296. } else { // 原图片的高度必然 > 宽度
  1297. if (imgHeight > containerHeight) {
  1298. tempHeight = containerHeight
  1299. // 按原图片的比例进行缩放
  1300. tempWidth = (imgWidth * containerHeight) / imgHeight
  1301. } else {
  1302. // 按原图片的大小进行缩放
  1303. tempWidth = imgWidth
  1304. tempHeight = imgHeight
  1305. }
  1306. }
  1307. }
  1308. return [tempWidth, tempHeight]
  1309. }
  1310. }
  1311. module.exports = dever