index.js 28 KB

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