index.js 28 KB

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