index.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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 (url) {
  556. self.post(url, {noloading:1}, function(t) {
  557. self.config = t;
  558. self.config.update_time = self.curTime();
  559. self.data('dever_config', self.config);
  560. vue.prototype.$config = self.config;
  561. });
  562. } else {
  563. if (!config && key && value) {
  564. config = self.data('dever_config');
  565. config[key] = value;
  566. }
  567. self.config = config;
  568. vue.prototype.$config = self.config;
  569. self.data('dever_config', self.config);
  570. }
  571. },
  572. //加载通用的方法
  573. common : function(func) {
  574. this.source = 'h5';
  575. this.host = '';
  576. //#ifdef H5
  577. this.source = 'h5';
  578. this.host = window.location.protocol + '//' + window.location.host + '/#';
  579. //#endif
  580. //#ifdef APP-PLUS
  581. this.source = 'app';
  582. //#endif
  583. //#ifdef MP-WEIXIN
  584. this.source = 'applet';
  585. //#endif
  586. if (func) {
  587. for (var i in func) {
  588. this.vue.prototype[i] = func[i];
  589. }
  590. }
  591. },
  592. //设置提交的按钮
  593. btnLoad : function(cur) {
  594. if (!cur) {
  595. cur = this.cur;
  596. }
  597. if (!cur) {
  598. return;
  599. }
  600. cur.btn = this.btn(cur);
  601. },
  602. //设置提交的按钮
  603. btnFinish : function() {
  604. var cur = this.cur;
  605. if (!cur) {
  606. return;
  607. }
  608. cur.btn = {
  609. disabled:false,
  610. loading:false,
  611. text:this.btnText,
  612. };
  613. },
  614. //设置提交的按钮
  615. btn : function(cur, text) {
  616. this.cur = cur;
  617. if (text) {
  618. this.btnText = text;
  619. }
  620. return {
  621. disabled:true,
  622. loading:true,
  623. text:'加载中...',
  624. };
  625. },
  626. //设置loading
  627. setGetLoading : function(yes, no) {
  628. this.getLoadingCall = {'yes': yes, 'no' : no};
  629. },
  630. setPostLoading : function(yes, no) {
  631. this.postLoadingCall = {'yes': yes, 'no' : no};
  632. },
  633. setPageLoading : function(yes, no) {
  634. this.pageLoadingCall = {'yes': yes, 'no' : no};
  635. },
  636. //获取当前route
  637. route : function() {
  638. var page = this.getPage();
  639. if (!page) {
  640. return '';
  641. }
  642. var route = page.route;
  643. return route;
  644. },
  645. //获取当前param
  646. param : function() {
  647. var page = this.getPage();
  648. if (!page) {
  649. return '';
  650. }
  651. var options = page.options;
  652. return options;
  653. },
  654. //获取当前url
  655. url : function() {
  656. var page = this.getPage();
  657. if (!page) {
  658. return '';
  659. }
  660. var route = page.route;
  661. var options = page.options;
  662. // 拼接参数
  663. let param = ''
  664. if (options && options.length > 0) {
  665. for (let key in options) {
  666. param += '&' + key + '=' + curParam[key]
  667. }
  668. param = '?' + param;
  669. }
  670. return '/' + route + param;
  671. },
  672. //获取当前page
  673. getPage : function() {
  674. var pages = getCurrentPages();
  675. if (pages.length > 0) {
  676. return pages[pages.length - 1];
  677. } else {
  678. return false;
  679. }
  680. },
  681. //loading
  682. loading : function(method, options, url) {
  683. var state = false;
  684. if (method == 'POST') {
  685. if (this.postLoadingCall && this.postLoadingCall.yes) {
  686. state = true;
  687. var callback = this.postLoadingCall.yes;
  688. callback(this);
  689. }
  690. } else if (method == 'page') {
  691. //state = true;
  692. if (options.page && options.page > 1) {
  693. state = false;
  694. }
  695. if (this.pageLoadingCall && this.pageLoadingCall.yes) {
  696. state = true;
  697. var callback = this.pageLoadingCall.yes;
  698. callback(this);
  699. }
  700. } else {
  701. url = this.route();
  702. if (this.lastGetUrl && this.lastGetUrl != url && this.getLoadingState[this.lastGetUrl]) {
  703. this.getLoadingState[this.lastGetUrl] = false;
  704. }
  705. if (this.getLoadingState && this.getLoadingState[url]) {
  706. state = false;
  707. } else {
  708. if (this.getLoadingCall && this.getLoadingCall.yes) {
  709. state = true;
  710. var callback = this.getLoadingCall.yes;
  711. callback(this);
  712. }
  713. }
  714. }
  715. if (!state) {
  716. uni.showLoading({title: '加载中', mask: true});
  717. //uni.showNavigationBarLoading();
  718. this.showLoad = true;
  719. }
  720. uni.stopPullDownRefresh();
  721. },
  722. //取消loading
  723. hideLoading : function(method, options, url) {
  724. var state = false;
  725. if (method == 'POST' || method == 'post') {
  726. if (this.postLoadingCall && this.postLoadingCall.no) {
  727. state = true;
  728. var callback = this.postLoadingCall.no;
  729. callback(this);
  730. }
  731. } else if (method == 'page') {
  732. //state = true;
  733. if (options.page && options.page > 1) {
  734. state = false;
  735. }
  736. if (this.pageLoadingCall && this.pageLoadingCall.no) {
  737. state = true;
  738. var callback = this.pageLoadingCall.no;
  739. callback(this);
  740. }
  741. } else {
  742. url = this.route();
  743. if (!this.lastGetUrl || (this.lastGetUrl && this.lastGetUrl != url)) {
  744. this.lastGetUrl = url;
  745. }
  746. if (this.getLoadingState && this.getLoadingState[url]) {
  747. state = false;
  748. } else {
  749. this.getLoadingState[url] = true;
  750. if (this.getLoadingCall && this.getLoadingCall.no) {
  751. state = true;
  752. var callback = this.getLoadingCall.no;
  753. callback(this);
  754. }
  755. }
  756. }
  757. if (!state && this.showLoad) {
  758. uni.hideLoading();
  759. this.showLoad = false;
  760. this.btnFinish();
  761. //uni.hideNavigationBarLoading();
  762. }
  763. },
  764. //data数据获取
  765. dataset : function(e) {
  766. if (e.currentTarget) {
  767. var dataset = e.currentTarget.dataset;
  768. } else {
  769. var dataset = e.target.dataset;
  770. }
  771. return dataset;
  772. },
  773. //view中的页面提示
  774. viewAlert : function(e) {
  775. if (e) {
  776. var dataset = this.dataset(e);
  777. var msg = dataset.msg;
  778. var icon = dataset.icon;
  779. var callback = dataset.callback;
  780. this.alert(msg, icon, callback);
  781. } else {
  782. this.alert('敬请期待');
  783. }
  784. },
  785. //view中的页面跳转
  786. viewLocation : function(e) {
  787. var page = '';
  788. var to = '';
  789. var option = '';
  790. if (e) {
  791. var dataset = this.dataset(e);
  792. page = dataset.page;
  793. to = dataset.to;
  794. option = []
  795. for (var i in dataset) {
  796. if (i.indexOf('save_') != -1) {
  797. var v = dataset[i];
  798. i = i.replace('save_', '');
  799. this.data(i, v);
  800. } else if (i != 'page' && i != 'to') {
  801. option.push(i + '=' + dataset[i]);
  802. }
  803. }
  804. if (option) {
  805. option = option.join('&');
  806. }
  807. }
  808. if (!page) {
  809. uni.navigateBack({});
  810. } else {
  811. var go = '';
  812. if (to) {
  813. go = 'to';
  814. }
  815. if (option) {
  816. page = page + '?' + option;
  817. }
  818. this.location(page, go);
  819. }
  820. },
  821. //页面跳转
  822. location : function(path, go) {
  823. var self = this;
  824. if (path.indexOf('http') > -1) {
  825. this.debug('http:' + path);
  826. this.data('web_view', path);
  827. if (this.source == 'app' || this.source == 'applet' || go == 'webview') {
  828. uni.navigateTo({
  829. url: this.web_view
  830. })
  831. } else {
  832. if (!go) {
  833. go = '';
  834. }
  835. if (go && go == 'location') {
  836. window.location.href = path;
  837. } else {
  838. var url = window.location.href;
  839. if (go.indexOf('/') != -1) {
  840. url = this.host + go
  841. } else {
  842. url = url + go;
  843. }
  844. url = window.btoa(url);
  845. if (path.indexOf('?') == -1) {
  846. path = path + '?';
  847. } else {
  848. path = path + '&';
  849. }
  850. window.location.href = path + 'refer=' + url;
  851. }
  852. }
  853. } else {
  854. this.debug('navigateTo:' + go + ':' + path);
  855. if (path.indexOf('/pages/') == -1) {
  856. path = '/pages/' + path;
  857. }
  858. if (this.switchTab.indexOf(path) != -1) {
  859. uni.switchTab({
  860. url: path,
  861. success: function(e) {
  862. if (self.source != 'app' && self.switchTabCall[path]) {
  863. var call = self.switchTabCall[path];
  864. call(self, e);
  865. }
  866. }
  867. })
  868. } else if (go) {
  869. uni.redirectTo({
  870. url: path
  871. })
  872. } else {
  873. uni.navigateTo({
  874. url: path
  875. })
  876. }
  877. }
  878. },
  879. //提示信息
  880. alert : function(info, icon, callback) {
  881. if (!icon) {
  882. icon = 'none'
  883. }
  884. if (info) {
  885. uni.showToast({title: info, icon: icon});
  886. }
  887. this.debug('alert:' + info)
  888. if (callback) {
  889. this.debug('callback:' + callback)
  890. setTimeout(function(){
  891. callback();
  892. }, 1000)
  893. }
  894. },
  895. //debug调试
  896. debug : function(string) {
  897. if (config.base.debug) {
  898. this.log(string);
  899. }
  900. },
  901. //记录日志
  902. log : function(string) {
  903. console.log(string);
  904. },
  905. //获取token
  906. getToken: function() {
  907. return http.getToken();
  908. },
  909. //设置token
  910. setToken: function(value) {
  911. return http.setToken(value);
  912. },
  913. //通用的提交数据的方法
  914. post : function(url, options, callback, err_callback) {
  915. http.request('post', url, options, callback, err_callback);
  916. },
  917. //通用的获取数据方法
  918. get : function(vue, url, options, callback, err_callback) {
  919. http.request(vue, url, options, callback, err_callback);
  920. },
  921. //通用的获取数据方法
  922. page : function(config, vue, url, options, callback, err_callback) {
  923. page.get(config, vue, url, options, callback, err_callback);
  924. },
  925. //上传
  926. upload : function(id, count, callback) {
  927. this.getUpload().handle(id, count, callback);
  928. },
  929. //上传
  930. qiniu : function(qn, self, id, count, callback, handle) {
  931. this.getUpload().qiniu(qn, self, id, count, callback, handle);
  932. },
  933. //上传文件
  934. upfile : function(self, key, count, callback, ext, title, style) {
  935. this.getUpload().upfile(self, key, count, callback, ext, title, style);
  936. },
  937. //获取upload
  938. getUpload : function() {
  939. return upload;
  940. },
  941. //im聊天
  942. im : function() {
  943. im.init();
  944. },
  945. //删除上传
  946. uploadDel : function(id, index, callback) {
  947. upload.uploadDel(id, index, callback);
  948. },
  949. //验证登录
  950. checkLogin : function(refer) {
  951. if (!this.getToken()) {
  952. this.data('login_refer', this.getRefer(refer));
  953. //this.hideLoading();
  954. if (this.source == 'h5') {
  955. this.location(this.login, 'go');
  956. } else {
  957. this.location(this.login, 'go');
  958. }
  959. }
  960. },
  961. //获取refer
  962. getRefer : function(refer) {
  963. if (!refer) {
  964. refer = this.url();
  965. }
  966. return refer;
  967. },
  968. //数据存储
  969. data : function(key, value) {
  970. if (value) {
  971. if (value == 'del') {
  972. uni.removeStorageSync(key);
  973. return true;
  974. } else {
  975. uni.setStorageSync(key, value);
  976. return value;
  977. }
  978. } else {
  979. return uni.getStorageSync(key);
  980. }
  981. },
  982. //插入html代码,初始化
  983. initHtml : function(doc) {
  984. this.doc = doc.$refs.initHtml.$el;
  985. },
  986. //插入html代码
  987. html : function(html) {
  988. var self = this;
  989. var div = document.createElement('div');
  990. div.innerHTML = html;
  991. this.doc.appendChild(div);
  992. var scripts = div.querySelectorAll('script');
  993. return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
  994. return chain.then(() => self.runScript(script));
  995. }, Promise.resolve());
  996. },
  997. //微信提醒
  998. wxTip : function() {
  999. var wx = this.is_weixin();
  1000. if (wx) {
  1001. 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>';
  1002. this.html(tip);
  1003. return true;
  1004. }
  1005. return false;
  1006. },
  1007. is_weixin: function() {
  1008. if (this.source != 'h5') {
  1009. return false;
  1010. }
  1011. var ua = navigator.userAgent.toLowerCase();
  1012. if(ua.match(/MicroMessenger/i)=="micromessenger") {
  1013. return true;
  1014. } else {
  1015. return false;
  1016. }
  1017. },
  1018. //执行script代码
  1019. runScript : function(script) {
  1020. return new Promise((reslove, rejected) => {
  1021. const newScript = document.createElement('script');
  1022. newScript.innerHTML = script.innerHTML;
  1023. const src = script.getAttribute('src');
  1024. if (src) newScript.setAttribute('src', src);
  1025. // script 加载完成和错误处理
  1026. newScript.onload = () => reslove();
  1027. newScript.onerror = err => rejected();
  1028. document.head.appendChild(newScript);
  1029. document.head.removeChild(newScript);
  1030. if (!src) {
  1031. // 如果是 inline script 执行是同步的
  1032. reslove();
  1033. }
  1034. })
  1035. },
  1036. //app支付
  1037. appPayment : function(type, order, callback, errorCallback) {
  1038. uni.requestPayment({
  1039. provider: type,
  1040. orderInfo: order, //微信、支付宝订单数据
  1041. success: function (res) {
  1042. //console.log('success:' + JSON.stringify(res));
  1043. callback(res);
  1044. },
  1045. fail: function (err) {
  1046. //console.log('fail:' + JSON.stringify(err));
  1047. errorCallback(err)
  1048. }
  1049. });
  1050. },
  1051. //跳转到refer
  1052. jump : function() {
  1053. var refer = this.data('login_refer');
  1054. var id = this.data('invite_id');
  1055. var type = this.data('invite_type');
  1056. if (id && refer != 'index/index') {
  1057. refer = refer + '?id=' + id + '&type=' + type;
  1058. }
  1059. this.data('login_refer', 'del');
  1060. this.data('invite_id', 'del');
  1061. this.data('invite_type', 'del');
  1062. if (!refer) {
  1063. refer = 'index/index';
  1064. }
  1065. this.location(refer, 'go');
  1066. },
  1067. //转星号
  1068. xing : function(s) {
  1069. return s.replace(s, function(sMatch){
  1070. return sMatch.replace(/./g,"*");
  1071. });
  1072. },
  1073. //检查更新
  1074. checkUpdate : function() {
  1075. this.update();
  1076. },
  1077. update : function(path, call) {
  1078. var self = this;
  1079. var source = this.source;
  1080. if (source != 'app') {
  1081. return;
  1082. }
  1083. var type = plus.os.name.toLowerCase();
  1084. //var type = 'android';
  1085. //var config = this.config.version;
  1086. self.post('isUpdate', {t:1}, function(res) {
  1087. if (type == 'android') {
  1088. res.downloadUrl = res.androidUrl;
  1089. } else {
  1090. res.downloadUrl = res.iosUrl;
  1091. }
  1092. self.data('dever_update_link', res.downloadUrl);
  1093. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  1094. var version = widgetInfo.versionCode;
  1095. var state = false;
  1096. state = widgetInfo.versionCode < res.versionCode;
  1097. if (state) {
  1098. //强制更新
  1099. var txt = '正在为您下载更新,下载完成将重启应用';
  1100. if (res.forceUpdate == true) {
  1101. self.down(res, res.downloadUrl, txt, type);
  1102. } else if(call) {
  1103. call(res, txt, type);
  1104. } else {
  1105. uni.showModal({
  1106. title: '发现新版本',
  1107. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  1108. success: (t) => {
  1109. if (t.confirm) {
  1110. if (path) {
  1111. self.location(path);
  1112. } else {
  1113. self.down(res, res.downloadUrl, txt, type);
  1114. }
  1115. }
  1116. }
  1117. })
  1118. }
  1119. }
  1120. });
  1121. });
  1122. },
  1123. uploadCall : function(config, type, packgePath) {
  1124. // 保存更新记录到stroage,下次启动app时安装更新
  1125. var self = this;
  1126. self.data('dever_update', 1);
  1127. self.data('dever_update_down', packgePath);
  1128. // 任务完成,关闭下载任务,开始安装
  1129. plus.runtime.install(packgePath, {force: true}, function() {
  1130. self.data('dever_update', 2);
  1131. self.data('dever_update_down', 'del');
  1132. uni.showModal({
  1133. title: '提示',
  1134. content: '应用将重启以完成更新',
  1135. showCancel: false,
  1136. complete: () => {
  1137. plus.runtime.restart();
  1138. }
  1139. })
  1140. });
  1141. },
  1142. down : function(config, url, txt, type, call) {
  1143. if (!url) {
  1144. return false;
  1145. }
  1146. if (txt) {
  1147. this.alert(txt);
  1148. }
  1149. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  1150. plus.runtime.openURL(url);
  1151. return false;
  1152. }
  1153. var self = this;
  1154. var packgePath = self.data('dever_update_down');
  1155. if (packgePath) {
  1156. self.uploadCall(config, type, packgePath);
  1157. return false;
  1158. }
  1159. var downloadTask = uni.downloadFile({
  1160. url: url,
  1161. success: (res) => {
  1162. if (res.statusCode === 200) {
  1163. // 保存下载的安装包
  1164. uni.saveFile({
  1165. tempFilePath: res.tempFilePath,
  1166. success: (res) => {
  1167. var packgePath = res.savedFilePath;
  1168. if (call) {
  1169. call();
  1170. }
  1171. self.uploadCall(config, type, packgePath);
  1172. downloadTask.abort();
  1173. downloadTask = null;
  1174. }
  1175. })
  1176. }
  1177. }
  1178. });
  1179. return downloadTask;
  1180. },
  1181. //预览图片
  1182. viewPic : function(imgs, img, key) {
  1183. if (imgs && imgs.length > 0) {
  1184. if (key) {
  1185. var temp = [];
  1186. var i;
  1187. for(i in imgs) {
  1188. temp.push(imgs[i][key]);
  1189. }
  1190. imgs = temp;
  1191. }
  1192. uni.previewImage({
  1193. current:img,
  1194. urls: imgs,
  1195. indicator : 'default',
  1196. loop : true,
  1197. /*
  1198. longPressActions : {
  1199. itemList: ['发送给朋友', '保存图片', '收藏'],
  1200. success: function(data) {
  1201. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  1202. },
  1203. fail: function(err) {
  1204. console.log(err.errMsg);
  1205. }
  1206. }
  1207. */
  1208. });
  1209. }
  1210. },
  1211. //截取APP退出功能
  1212. quit : function(page, call) {
  1213. var self = this;
  1214. if (self.source == 'app') {
  1215. var main = plus.android.runtimeMainActivity();
  1216. //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
  1217. /*
  1218. plus.runtime.quit = function(){
  1219. main.moveTaskToBack(false);
  1220. };
  1221. */
  1222. //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
  1223. plus.nativeUI.toast = (function(str){
  1224. if(str == '再按一次退出应用') {
  1225. if (call) {
  1226. call(main);
  1227. } else {
  1228. var webview = page.$mp.page.$getAppWebview();
  1229. var child = webview.children();
  1230. child[0].back();
  1231. }
  1232. return false;
  1233. }else{
  1234. self.alert(str);
  1235. }
  1236. });
  1237. }
  1238. },
  1239. //滑动
  1240. slide : function(e) {
  1241. this.slideData.clientX = e.changedTouches[0].clientX;
  1242. this.slideData.clientY = e.changedTouches[0].clientY;
  1243. },
  1244. //滑动结束
  1245. slideEnd : function(e) {
  1246. var subX = e.changedTouches[0].clientX - this.slideData.clientX;
  1247. var subY = e.changedTouches[0].clientY - this.slideData.clientY;
  1248. if (subY > 50) {
  1249. //上滑
  1250. return 1;
  1251. } else if (subY < -50) {
  1252. //下滑
  1253. return 2;
  1254. } else if (subX > 50) {
  1255. //左滑
  1256. return 3;
  1257. } else if(subX < -50) {
  1258. //右滑
  1259. return 4;
  1260. }
  1261. return
  1262. },
  1263. //计算图片宽高比
  1264. getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
  1265. let [
  1266. // 用于设定图片的宽和高
  1267. tempWidth,
  1268. tempHeight,
  1269. ] = [
  1270. undefined,
  1271. undefined
  1272. ]
  1273. try {
  1274. imgWidth = parseFloat(imgWidth)
  1275. imgHeight = parseFloat(imgHeight)
  1276. containerWidth = parseFloat(containerWidth)
  1277. containerHeight = parseFloat(containerHeight)
  1278. } catch (error) {
  1279. throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
  1280. }
  1281. if (imgWidth > 0 && imgHeight > 0) {
  1282. //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
  1283. if (imgWidth / imgHeight >= containerWidth / containerHeight) {
  1284. if (imgWidth > containerWidth) {
  1285. // alert('aaaaaaaa')
  1286. tempWidth = containerWidth
  1287. // 按原图片的比例进行缩放
  1288. tempHeight = (imgHeight * containerWidth) / imgWidth
  1289. } else {
  1290. // 按照图片的大小进行缩放
  1291. tempWidth = imgWidth
  1292. tempHeight = imgHeight
  1293. }
  1294. } else { // 原图片的高度必然 > 宽度
  1295. if (imgHeight > containerHeight) {
  1296. tempHeight = containerHeight
  1297. // 按原图片的比例进行缩放
  1298. tempWidth = (imgWidth * containerHeight) / imgHeight
  1299. } else {
  1300. // 按原图片的大小进行缩放
  1301. tempWidth = imgWidth
  1302. tempHeight = imgHeight
  1303. }
  1304. }
  1305. }
  1306. return [tempWidth, tempHeight]
  1307. }
  1308. }
  1309. module.exports = dever