index.js 31 KB

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