index.js 34 KB

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