index.js 34 KB

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