index.js 29 KB

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