index.js 31 KB

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