index.js 30 KB

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