index.js 35 KB

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