index.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. //debug调试
  917. debug : function(string) {
  918. if (config.base.debug) {
  919. this.log(string);
  920. }
  921. },
  922. //记录日志
  923. log : function(string) {
  924. console.log(string);
  925. },
  926. //获取token
  927. getToken: function() {
  928. return http.getToken();
  929. },
  930. //设置token
  931. setToken: function(value) {
  932. return http.setToken(value);
  933. },
  934. //通用的提交数据的方法
  935. post : function(url, options, callback, err_callback) {
  936. http.request('post', url, options, callback, err_callback);
  937. },
  938. //通用的获取数据方法
  939. get : function(vue, url, options, callback, err_callback) {
  940. http.request(vue, url, options, callback, err_callback);
  941. },
  942. //通用的获取数据方法
  943. page : function(config, vue, url, options, callback, err_callback) {
  944. page.get(config, vue, url, options, callback, err_callback);
  945. },
  946. //选择图片并上传
  947. upload : function(id, count, callback) {
  948. this.getUpload().handle(id, count, callback);
  949. },
  950. //上传到七牛
  951. qiniu : function(qn, self, id, count, callback, handle) {
  952. this.getUpload().qiniu(qn, self, id, count, callback, handle);
  953. },
  954. //html上传文件
  955. upfile : function(self, key, count, callback, ext, title, style) {
  956. this.getUpload().upfile(self, key, count, callback, ext, title, style);
  957. },
  958. //直接上传文件
  959. uploadFile : function(path, id, count, callback) {
  960. this.getUpload().uploadFile(path, id, count, callback);
  961. },
  962. //获取upload
  963. getUpload : function() {
  964. return upload;
  965. },
  966. //im聊天
  967. im : function(url, callback, connect) {
  968. im.init(url, callback, connect);
  969. },
  970. //删除上传
  971. uploadDel : function(id, index, callback) {
  972. upload.uploadDel(id, index, callback);
  973. },
  974. //验证登录
  975. checkLogin : function(refer) {
  976. if (!this.getToken()) {
  977. this.data('login_refer', this.getRefer(refer));
  978. //this.hideLoading();
  979. if (this.source == 'h5') {
  980. this.location(this.login, 'go');
  981. } else {
  982. this.location(this.login, 'go');
  983. }
  984. }
  985. },
  986. //获取refer
  987. getRefer : function(refer) {
  988. if (!refer) {
  989. refer = this.url();
  990. }
  991. return refer;
  992. },
  993. //数据存储
  994. data : function(key, value) {
  995. if (value) {
  996. if (value == 'del') {
  997. uni.removeStorageSync(key);
  998. return true;
  999. } else {
  1000. uni.setStorageSync(key, value);
  1001. return value;
  1002. }
  1003. } else {
  1004. return uni.getStorageSync(key);
  1005. }
  1006. },
  1007. //插入html代码,初始化
  1008. initHtml : function(doc) {
  1009. this.doc = doc.$refs.initHtml.$el;
  1010. },
  1011. //插入html代码
  1012. html : function(html) {
  1013. var self = this;
  1014. var div = document.createElement('div');
  1015. div.innerHTML = html;
  1016. this.doc.appendChild(div);
  1017. var scripts = div.querySelectorAll('script');
  1018. return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
  1019. return chain.then(() => self.runScript(script));
  1020. }, Promise.resolve());
  1021. },
  1022. //微信提醒
  1023. wxTip : function() {
  1024. var wx = this.is_weixin();
  1025. if (wx) {
  1026. 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>';
  1027. this.html(tip);
  1028. return true;
  1029. }
  1030. return false;
  1031. },
  1032. is_weixin: function() {
  1033. if (this.source != 'h5') {
  1034. return false;
  1035. }
  1036. var ua = navigator.userAgent.toLowerCase();
  1037. if(ua.match(/MicroMessenger/i)=="micromessenger") {
  1038. return true;
  1039. } else {
  1040. return false;
  1041. }
  1042. },
  1043. //执行script代码
  1044. runScript : function(script) {
  1045. return new Promise((reslove, rejected) => {
  1046. const newScript = document.createElement('script');
  1047. newScript.innerHTML = script.innerHTML;
  1048. const src = script.getAttribute('src');
  1049. if (src) newScript.setAttribute('src', src);
  1050. // script 加载完成和错误处理
  1051. newScript.onload = () => reslove();
  1052. newScript.onerror = err => rejected();
  1053. document.head.appendChild(newScript);
  1054. document.head.removeChild(newScript);
  1055. if (!src) {
  1056. // 如果是 inline script 执行是同步的
  1057. reslove();
  1058. }
  1059. })
  1060. },
  1061. //app支付
  1062. appPayment : function(type, order, callback, errorCallback) {
  1063. uni.requestPayment({
  1064. provider: type,
  1065. orderInfo: order, //微信、支付宝订单数据
  1066. success: function (res) {
  1067. //console.log('success:' + JSON.stringify(res));
  1068. callback(res);
  1069. },
  1070. fail: function (err) {
  1071. //console.log('fail:' + JSON.stringify(err));
  1072. errorCallback(err)
  1073. }
  1074. });
  1075. },
  1076. //跳转到refer
  1077. jump : function() {
  1078. var refer = this.data('login_refer');
  1079. var id = this.data('invite_id');
  1080. var type = this.data('invite_type');
  1081. if (id && refer != 'index/index') {
  1082. refer = refer + '?id=' + id + '&type=' + type;
  1083. }
  1084. this.data('login_refer', 'del');
  1085. this.data('invite_id', 'del');
  1086. this.data('invite_type', 'del');
  1087. if (!refer) {
  1088. refer = 'index/index';
  1089. }
  1090. this.location(refer, 'go');
  1091. },
  1092. //转星号
  1093. xing : function(s) {
  1094. return s.replace(s, function(sMatch){
  1095. return sMatch.replace(/./g,"*");
  1096. });
  1097. },
  1098. //检查更新
  1099. checkUpdate : function() {
  1100. this.update();
  1101. },
  1102. update : function(path, call) {
  1103. var self = this;
  1104. var source = this.source;
  1105. if (source != 'app') {
  1106. return;
  1107. }
  1108. var type = plus.os.name.toLowerCase();
  1109. //var type = 'android';
  1110. //var config = this.config.version;
  1111. self.post('isUpdate', {t:1}, function(res) {
  1112. if (type == 'android') {
  1113. res.downloadUrl = res.androidUrl;
  1114. } else {
  1115. res.downloadUrl = res.iosUrl;
  1116. }
  1117. self.data('dever_update_link', res.downloadUrl);
  1118. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  1119. var version = widgetInfo.versionCode;
  1120. var state = false;
  1121. state = widgetInfo.versionCode < res.versionCode;
  1122. if (state) {
  1123. //强制更新
  1124. var txt = '正在为您下载更新,下载完成将重启应用';
  1125. if (res.forceUpdate == true) {
  1126. self.down(res, res.downloadUrl, txt, type);
  1127. } else if(call) {
  1128. call(res, txt, type);
  1129. } else {
  1130. uni.showModal({
  1131. title: '发现新版本',
  1132. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  1133. success: (t) => {
  1134. if (t.confirm) {
  1135. if (path) {
  1136. self.location(path);
  1137. } else {
  1138. self.down(res, res.downloadUrl, txt, type);
  1139. }
  1140. }
  1141. }
  1142. })
  1143. }
  1144. }
  1145. });
  1146. });
  1147. },
  1148. uploadCall : function(config, type, packgePath) {
  1149. // 保存更新记录到stroage,下次启动app时安装更新
  1150. var self = this;
  1151. self.data('dever_update', 1);
  1152. self.data('dever_update_down', packgePath);
  1153. // 任务完成,关闭下载任务,开始安装
  1154. plus.runtime.install(packgePath, {force: true}, function() {
  1155. self.data('dever_update', 2);
  1156. self.data('dever_update_down', 'del');
  1157. uni.showModal({
  1158. title: '提示',
  1159. content: '应用将重启以完成更新',
  1160. showCancel: false,
  1161. complete: () => {
  1162. plus.runtime.restart();
  1163. }
  1164. })
  1165. });
  1166. },
  1167. down : function(config, url, txt, type, call) {
  1168. if (!url) {
  1169. return false;
  1170. }
  1171. if (txt) {
  1172. this.alert(txt);
  1173. }
  1174. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  1175. plus.runtime.openURL(url);
  1176. return false;
  1177. }
  1178. var self = this;
  1179. var packgePath = self.data('dever_update_down');
  1180. if (packgePath) {
  1181. self.uploadCall(config, type, packgePath);
  1182. return false;
  1183. }
  1184. var downloadTask = uni.downloadFile({
  1185. url: url,
  1186. success: (res) => {
  1187. if (res.statusCode === 200) {
  1188. // 保存下载的安装包
  1189. uni.saveFile({
  1190. tempFilePath: res.tempFilePath,
  1191. success: (res) => {
  1192. var packgePath = res.savedFilePath;
  1193. if (call) {
  1194. call();
  1195. }
  1196. self.uploadCall(config, type, packgePath);
  1197. downloadTask.abort();
  1198. downloadTask = null;
  1199. }
  1200. })
  1201. }
  1202. }
  1203. });
  1204. return downloadTask;
  1205. },
  1206. //预览图片
  1207. viewPic : function(imgs, img, key) {
  1208. if (imgs && imgs.length > 0) {
  1209. if (key) {
  1210. var temp = [];
  1211. var i;
  1212. for(i in imgs) {
  1213. temp.push(imgs[i][key]);
  1214. }
  1215. imgs = temp;
  1216. }
  1217. uni.previewImage({
  1218. current:img,
  1219. urls: imgs,
  1220. indicator : 'default',
  1221. loop : true,
  1222. /*
  1223. longPressActions : {
  1224. itemList: ['发送给朋友', '保存图片', '收藏'],
  1225. success: function(data) {
  1226. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  1227. },
  1228. fail: function(err) {
  1229. console.log(err.errMsg);
  1230. }
  1231. }
  1232. */
  1233. });
  1234. }
  1235. },
  1236. //截取APP退出功能
  1237. quit : function(page, call) {
  1238. var self = this;
  1239. if (self.source == 'app') {
  1240. var main = plus.android.runtimeMainActivity();
  1241. //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
  1242. /*
  1243. plus.runtime.quit = function(){
  1244. main.moveTaskToBack(false);
  1245. };
  1246. */
  1247. //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
  1248. plus.nativeUI.toast = (function(str){
  1249. if(str == '再按一次退出应用') {
  1250. if (call) {
  1251. call(main);
  1252. } else {
  1253. var webview = page.$mp.page.$getAppWebview();
  1254. var child = webview.children();
  1255. child[0].back();
  1256. }
  1257. return false;
  1258. }else{
  1259. self.alert(str);
  1260. }
  1261. });
  1262. }
  1263. },
  1264. //滑动
  1265. slide : function(e) {
  1266. this.slideData.clientX = e.changedTouches[0].clientX;
  1267. this.slideData.clientY = e.changedTouches[0].clientY;
  1268. },
  1269. //滑动结束
  1270. slideEnd : function(e) {
  1271. var subX = e.changedTouches[0].clientX - this.slideData.clientX;
  1272. var subY = e.changedTouches[0].clientY - this.slideData.clientY;
  1273. if (subY > 50) {
  1274. //上滑
  1275. return 1;
  1276. } else if (subY < -50) {
  1277. //下滑
  1278. return 2;
  1279. } else if (subX > 50) {
  1280. //左滑
  1281. return 3;
  1282. } else if(subX < -50) {
  1283. //右滑
  1284. return 4;
  1285. }
  1286. return
  1287. },
  1288. //计算图片宽高比
  1289. getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
  1290. let [
  1291. // 用于设定图片的宽和高
  1292. tempWidth,
  1293. tempHeight,
  1294. ] = [
  1295. undefined,
  1296. undefined
  1297. ]
  1298. try {
  1299. imgWidth = parseFloat(imgWidth)
  1300. imgHeight = parseFloat(imgHeight)
  1301. containerWidth = parseFloat(containerWidth)
  1302. containerHeight = parseFloat(containerHeight)
  1303. } catch (error) {
  1304. throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
  1305. }
  1306. if (imgWidth > 0 && imgHeight > 0) {
  1307. //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
  1308. if (imgWidth / imgHeight >= containerWidth / containerHeight) {
  1309. if (imgWidth > containerWidth) {
  1310. // alert('aaaaaaaa')
  1311. tempWidth = containerWidth
  1312. // 按原图片的比例进行缩放
  1313. tempHeight = (imgHeight * containerWidth) / imgWidth
  1314. } else {
  1315. // 按照图片的大小进行缩放
  1316. tempWidth = imgWidth
  1317. tempHeight = imgHeight
  1318. }
  1319. } else { // 原图片的高度必然 > 宽度
  1320. if (imgHeight > containerHeight) {
  1321. tempHeight = containerHeight
  1322. // 按原图片的比例进行缩放
  1323. tempWidth = (imgWidth * containerHeight) / imgHeight
  1324. } else {
  1325. // 按原图片的大小进行缩放
  1326. tempWidth = imgWidth
  1327. tempHeight = imgHeight
  1328. }
  1329. }
  1330. }
  1331. return [tempWidth, tempHeight]
  1332. }
  1333. }
  1334. module.exports = dever