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