index.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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 share =
  526. {
  527. wechat : false,
  528. url : '',
  529. project : '',
  530. token : '',
  531. param : {
  532. title : '',
  533. pic : '',
  534. content : '',
  535. link : '',
  536. },
  537. wx : {},
  538. init : function(wx, project, host, param)
  539. {
  540. var self = this;
  541. self.wx = wx;
  542. self.wechat = dever.is_weixin();
  543. self.project = project;
  544. self.url = host;
  545. self.token = http.getToken();
  546. self.param = param;
  547. self.param.timelineCallback = function() {};
  548. self.param.sendMessageCallback = function() {};
  549. self.param.shareQQCallback = function() {};
  550. if (self.wechat) {
  551. self.wechatInit();
  552. }
  553. self.reflux();
  554. }
  555. ,wechatInit: function()
  556. {
  557. var self = this;
  558. var url = this.url + 'init?callback=?';
  559. var param = this.param;
  560. var project = this.project;
  561. var options = {};
  562. options.project = project;
  563. options.url = location.href.split('#')[0];
  564. options.noloading = 1;
  565. options.token = this.token;
  566. http.request('post', url, options, function(result) {
  567. var data = result.data;
  568. self.wx.config({
  569. //debug:true,
  570. appId: data.appId,
  571. timestamp: data.timestamp,
  572. nonceStr: data.nonceStr,
  573. signature: data.signature,
  574. jsApiList: [
  575. 'onMenuShareTimeline',
  576. 'onMenuShareAppMessage',
  577. 'onMenuShareQQ'
  578. ]
  579. });
  580. self.wx.ready(function() {
  581. self.wx.onMenuShareAppMessage({
  582. title: param.title,
  583. desc: param.content,
  584. link: param.link,
  585. imgUrl: param.pic,
  586. success: function(res) {
  587. param.sendMessageCallback('success', res)
  588. self.shareLog(1, 1);
  589. },
  590. cancel: function(res) {
  591. param.sendMessageCallback('cancel', res)
  592. self.shareLog(1, 2);
  593. },
  594. fail: function(res) {
  595. param.sendMessageCallback('fail', res)
  596. self.shareLog(1, 3);
  597. }
  598. });
  599. self.wx.onMenuShareTimeline({
  600. title: param.title,
  601. link: param.url,
  602. imgUrl: param.img,
  603. success: function(res) {
  604. param.timelineCallback('success', res)
  605. self.shareLog(2, 1);
  606. },
  607. cancel: function(res) {
  608. param.timelineCallback('cancel', res)
  609. self.shareLog(2, 2);
  610. },
  611. fail: function(res) {
  612. param.timelineCallback('fail', res)
  613. self.shareLog(2, 3);
  614. }
  615. });
  616. self.wx.onMenuShareQQ({
  617. title: param.title,
  618. link: param.url,
  619. imgUrl: param.img,
  620. success: function(res) {
  621. param.shareQQCallback('success', res)
  622. self.shareLog(3, 1);
  623. },
  624. cancel: function(res) {
  625. param.shareQQCallback('cancel', res)
  626. self.shareLog(3, 2);
  627. },
  628. fail: function(res) {
  629. param.shareQQCallback('fail', res)
  630. self.shareLog(3, 3);
  631. }
  632. });
  633. });
  634. });
  635. }
  636. ,reflux: function() {
  637. var refer = document.referrer,
  638. url = encodeURIComponent(document.location.href),
  639. param = location.search.substr(1),
  640. ua = encodeURIComponent(navigator.userAgent),
  641. project = this.project,
  642. token = this.token,
  643. wechat = this.wechat;
  644. if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
  645. var url = this.url + 'reflux?callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '&param=' + encodeURIComponent(param) + '&token=' + token + '&type=' + wechat;
  646. http.request('post', url, {noloading:1});
  647. }
  648. }
  649. ,shareLog: function(actType, actResult) {
  650. var project = this.project;
  651. var token = this.token;
  652. var wechat = this.wechat;
  653. var ua = encodeURIComponent(navigator.userAgent);
  654. var url = encodeURIComponent(document.location.href);
  655. var url = this.url + 'collect?callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&token=' + token + '&type=' + wechat
  656. http.request('post', url, {noloading:1});
  657. }
  658. }
  659. var dever = {
  660. login : 'user/login',
  661. web_view : '/lib/dever/pages/web_view',
  662. source : 'h5',
  663. host : '',
  664. doc : false,
  665. config : {},
  666. vue : false,
  667. cur : false,
  668. getLoadingState : {},
  669. getLoadingCall : false,
  670. postLoadingCall : false,
  671. pageLoadingCall : false,
  672. pageData : {},
  673. lastGetUrl : '',
  674. showLoad : false,
  675. btnText : '确定提交',
  676. switchTab : [],
  677. switchTabCall : {},
  678. slideValue : {},
  679. //获取当前时间戳
  680. curTime : function() {
  681. var time = Date.parse(new Date())/1000;
  682. return time;
  683. },
  684. //载入全局配置
  685. init : function(url, vue, value, set) {
  686. this.vue = vue;
  687. vue.prototype.$config = value;
  688. var self = this;
  689. var config = self.data('dever_config');
  690. var state = false;
  691. if (config && !set) {
  692. self.config = config;
  693. vue.prototype.$config = self.config;
  694. if (self.config.update_time) {
  695. var time = this.curTime();
  696. if (!self.config.update_time || time - self.config.update_time >= value.set_update) {
  697. state = true;
  698. }
  699. } else {
  700. state = false;
  701. }
  702. } else {
  703. state = true;
  704. }
  705. if (state) {
  706. this.setConfig(url, value);
  707. }
  708. return this;
  709. },
  710. //设置全局变量
  711. setConfig : function(url, config, key, value) {
  712. var self = this;
  713. if (!config && key && value) {
  714. config = self.data('dever_config');
  715. config[key] = value;
  716. }
  717. self.config = config;
  718. self.vue.prototype.$config = self.config;
  719. self.data('dever_config', self.config);
  720. if (url) {
  721. self.post(url, {noloading:1}, function(t) {
  722. self.config = t;
  723. self.config.update_time = self.curTime();
  724. self.data('dever_config', self.config);
  725. self.vue.prototype.$config = self.config;
  726. });
  727. }
  728. },
  729. //加载通用的方法
  730. common : function(func) {
  731. this.source = 'h5';
  732. this.host = '';
  733. //#ifdef H5
  734. this.source = 'h5';
  735. this.host = window.location.protocol + '//' + window.location.host + '/#';
  736. //#endif
  737. //#ifdef APP-PLUS
  738. this.source = 'app';
  739. //#endif
  740. //#ifdef MP-WEIXIN
  741. this.source = 'applet';
  742. //#endif
  743. if (func) {
  744. for (var i in func) {
  745. this.vue.prototype[i] = func[i];
  746. }
  747. }
  748. },
  749. //设置提交的按钮
  750. btnLoad : function(cur) {
  751. if (!cur) {
  752. cur = this.cur;
  753. }
  754. if (!cur) {
  755. return;
  756. }
  757. cur.btn = this.btn(cur);
  758. },
  759. //设置提交的按钮
  760. btnFinish : function() {
  761. var cur = this.cur;
  762. if (!cur) {
  763. return;
  764. }
  765. cur.btn = {
  766. disabled:false,
  767. loading:false,
  768. text:this.btnText,
  769. };
  770. },
  771. //设置提交的按钮
  772. btn : function(cur, text) {
  773. this.cur = cur;
  774. if (text) {
  775. this.btnText = text;
  776. }
  777. return {
  778. disabled:true,
  779. loading:true,
  780. text:'加载中...',
  781. };
  782. },
  783. //设置loading
  784. setGetLoading : function(yes, no) {
  785. this.getLoadingCall = {'yes': yes, 'no' : no};
  786. },
  787. setPostLoading : function(yes, no) {
  788. this.postLoadingCall = {'yes': yes, 'no' : no};
  789. },
  790. setPageLoading : function(yes, no) {
  791. this.pageLoadingCall = {'yes': yes, 'no' : no};
  792. },
  793. //获取当前route
  794. route : function() {
  795. var page = this.getPage();
  796. if (!page) {
  797. return '';
  798. }
  799. var route = page.route;
  800. return route;
  801. },
  802. //获取当前param
  803. param : function() {
  804. var page = this.getPage();
  805. if (!page) {
  806. return '';
  807. }
  808. var options = page.options;
  809. return options;
  810. },
  811. //获取当前url
  812. url : function() {
  813. var page = this.getPage();
  814. if (!page) {
  815. return '';
  816. }
  817. var route = page.route;
  818. var options = page.options;
  819. // 拼接参数
  820. let param = []
  821. if (options) {
  822. for (let key in options) {
  823. param.push(key + '=' + options[key]);
  824. }
  825. }
  826. param = param.join('&');
  827. if (param) {
  828. param = '?' + param;
  829. }
  830. return '/' + route + param;
  831. },
  832. //获取当前page
  833. getPage : function() {
  834. var pages = getCurrentPages();
  835. if (pages.length > 0) {
  836. return pages[pages.length - 1];
  837. } else {
  838. return false;
  839. }
  840. },
  841. //loading
  842. loading : function(method, options, url) {
  843. var state = false;
  844. if (method == 'POST') {
  845. if (this.postLoadingCall && this.postLoadingCall.yes) {
  846. state = true;
  847. var callback = this.postLoadingCall.yes;
  848. callback(this);
  849. }
  850. } else if (method == 'page') {
  851. //state = true;
  852. if (options.page && options.page > 1) {
  853. state = false;
  854. }
  855. if (this.pageLoadingCall && this.pageLoadingCall.yes) {
  856. state = true;
  857. var callback = this.pageLoadingCall.yes;
  858. callback(this);
  859. }
  860. } else {
  861. url = this.route();
  862. if (this.lastGetUrl && this.lastGetUrl != url && this.getLoadingState[this.lastGetUrl]) {
  863. this.getLoadingState[this.lastGetUrl] = false;
  864. }
  865. if (this.getLoadingState && this.getLoadingState[url]) {
  866. state = false;
  867. } else {
  868. if (this.getLoadingCall && this.getLoadingCall.yes) {
  869. state = true;
  870. var callback = this.getLoadingCall.yes;
  871. callback(this);
  872. }
  873. }
  874. }
  875. if (!state) {
  876. var title = '加载中';
  877. if (method == 'POST') {
  878. title = '提交中';
  879. }
  880. uni.showLoading({title: title, mask: true});
  881. //uni.showNavigationBarLoading();
  882. this.showLoad = true;
  883. }
  884. uni.stopPullDownRefresh();
  885. },
  886. //取消loading
  887. hideLoading : function(method, options, url) {
  888. var state = false;
  889. if (method == 'POST' || method == 'post') {
  890. if (this.postLoadingCall && this.postLoadingCall.no) {
  891. state = true;
  892. var callback = this.postLoadingCall.no;
  893. callback(this);
  894. }
  895. } else if (method == 'page') {
  896. //state = true;
  897. if (options.page && options.page > 1) {
  898. state = false;
  899. }
  900. if (this.pageLoadingCall && this.pageLoadingCall.no) {
  901. state = true;
  902. var callback = this.pageLoadingCall.no;
  903. callback(this);
  904. }
  905. } else {
  906. url = this.route();
  907. if (!this.lastGetUrl || (this.lastGetUrl && this.lastGetUrl != url)) {
  908. this.lastGetUrl = url;
  909. }
  910. if (this.getLoadingState && this.getLoadingState[url]) {
  911. state = false;
  912. } else {
  913. this.getLoadingState[url] = true;
  914. if (this.getLoadingCall && this.getLoadingCall.no) {
  915. state = true;
  916. var callback = this.getLoadingCall.no;
  917. callback(this);
  918. }
  919. }
  920. }
  921. if (!state && this.showLoad) {
  922. uni.hideLoading();
  923. this.showLoad = false;
  924. this.btnFinish();
  925. //uni.hideNavigationBarLoading();
  926. }
  927. },
  928. //data数据获取
  929. dataset : function(e) {
  930. if (e.currentTarget) {
  931. var dataset = e.currentTarget.dataset;
  932. } else {
  933. var dataset = e.target.dataset;
  934. }
  935. return dataset;
  936. },
  937. //view中的页面提示
  938. viewAlert : function(e) {
  939. if (e) {
  940. var dataset = this.dataset(e);
  941. var msg = dataset.msg;
  942. var icon = dataset.icon;
  943. var callback = dataset.callback;
  944. this.alert(msg, icon, callback);
  945. } else {
  946. this.alert('敬请期待');
  947. }
  948. },
  949. //view中的页面跳转
  950. viewLocation : function(e) {
  951. var page = '';
  952. var to = '';
  953. var option = '';
  954. if (e) {
  955. var dataset = this.dataset(e);
  956. page = dataset.page;
  957. to = dataset.to;
  958. option = []
  959. for (var i in dataset) {
  960. if (i.indexOf('save_') != -1) {
  961. var v = dataset[i];
  962. i = i.replace('save_', '');
  963. this.data(i, v);
  964. } else if (i != 'page' && i != 'to') {
  965. option.push(i + '=' + dataset[i]);
  966. }
  967. }
  968. if (option) {
  969. option = option.join('&');
  970. }
  971. }
  972. if (!page) {
  973. uni.navigateBack({});
  974. } else {
  975. var go = '';
  976. if (to) {
  977. go = 'to';
  978. }
  979. if (option) {
  980. page = page + '?' + option;
  981. }
  982. this.location(page, go);
  983. }
  984. },
  985. //页面跳转
  986. location : function(path, go) {
  987. var self = this;
  988. if (path.indexOf('http') > -1) {
  989. this.debug('http:' + path);
  990. this.data('web_view', path);
  991. if (this.source == 'app' || this.source == 'applet' || go == 'webview') {
  992. uni.navigateTo({
  993. url: this.web_view
  994. })
  995. } else {
  996. if (!go) {
  997. go = '';
  998. }
  999. if (go && go == 'location') {
  1000. window.location.href = path;
  1001. } else {
  1002. var url = window.location.href;
  1003. if (go.indexOf('/') != -1) {
  1004. url = this.host + go
  1005. } else {
  1006. url = url + go;
  1007. }
  1008. url = window.btoa(url);
  1009. if (path.indexOf('?') == -1) {
  1010. path = path + '?';
  1011. } else {
  1012. path = path + '&';
  1013. }
  1014. window.location.href = path + 'refer=' + url;
  1015. }
  1016. }
  1017. } else {
  1018. this.debug('navigateTo:' + go + ':' + path);
  1019. if (path.indexOf('/pages/') == -1) {
  1020. path = '/pages/' + path;
  1021. }
  1022. if (this.switchTab.indexOf(path) != -1) {
  1023. uni.switchTab({
  1024. url: path,
  1025. success: function(e) {
  1026. if (self.source != 'app' && self.switchTabCall[path]) {
  1027. var call = self.switchTabCall[path];
  1028. call(self, e);
  1029. }
  1030. }
  1031. })
  1032. } else if (go) {
  1033. uni.redirectTo({
  1034. url: path
  1035. })
  1036. } else {
  1037. uni.navigateTo({
  1038. url: path
  1039. })
  1040. }
  1041. }
  1042. },
  1043. //提示信息
  1044. alert : function(info, icon, callback) {
  1045. if (!icon) {
  1046. icon = 'none'
  1047. }
  1048. if (info) {
  1049. uni.showToast({title: info, icon: icon});
  1050. }
  1051. this.debug('alert:' + info)
  1052. if (callback) {
  1053. this.debug('callback:' + callback)
  1054. setTimeout(function(){
  1055. callback();
  1056. }, 1000)
  1057. }
  1058. },
  1059. //询问信息
  1060. confirm : function(text, callback) {
  1061. uni.showModal({
  1062. content: text,
  1063. cancelText: '取消',
  1064. confirmText: '确定',
  1065. success: res => {
  1066. if (res.confirm) {
  1067. callback();
  1068. }
  1069. }
  1070. });
  1071. },
  1072. //debug调试
  1073. debug : function(string) {
  1074. if (config.base.debug) {
  1075. this.log(string);
  1076. }
  1077. },
  1078. //记录日志
  1079. log : function(string) {
  1080. console.log(string);
  1081. },
  1082. //获取token
  1083. getToken: function() {
  1084. return http.getToken();
  1085. },
  1086. //设置token
  1087. setToken: function(value) {
  1088. return http.setToken(value);
  1089. },
  1090. //通用的提交数据的方法
  1091. post : function(url, options, callback, err_callback) {
  1092. http.request('post', url, options, callback, err_callback);
  1093. },
  1094. //通用的获取数据方法
  1095. get : function(vue, url, options, callback, err_callback) {
  1096. http.request(vue, url, options, callback, err_callback);
  1097. },
  1098. //通用的获取数据方法
  1099. page : function(config, vue, url, options, callback, err_callback) {
  1100. page.get(config, vue, url, options, callback, err_callback);
  1101. },
  1102. //选择图片并上传
  1103. upload : function(id, count, callback) {
  1104. this.getUpload().handle(id, count, callback);
  1105. },
  1106. //上传到七牛
  1107. qiniu : function(qn, self, id, count, callback, handle) {
  1108. this.getUpload().qiniu(qn, self, id, count, callback, handle);
  1109. },
  1110. //html上传文件
  1111. upfile : function(self, key, count, callback, ext, title, style) {
  1112. this.getUpload().upfile(self, key, count, callback, ext, title, style);
  1113. },
  1114. //直接上传文件
  1115. uploadFile : function(path, id, count, callback) {
  1116. this.getUpload().uploadFile(path, id, count, callback);
  1117. },
  1118. //获取upload
  1119. getUpload : function() {
  1120. return upload;
  1121. },
  1122. //im聊天
  1123. im : function(url, callback, connect) {
  1124. im.init(url, callback, connect);
  1125. },
  1126. //删除上传
  1127. uploadDel : function(id, index, callback) {
  1128. upload.uploadDel(id, index, callback);
  1129. },
  1130. //验证登录
  1131. checkLogin : function(refer) {
  1132. if (!this.getToken()) {
  1133. this.data('login_refer', this.getRefer(refer));
  1134. //this.hideLoading();
  1135. if (this.source == 'h5') {
  1136. this.location(this.login, 'go');
  1137. } else {
  1138. this.location(this.login, 'go');
  1139. }
  1140. }
  1141. },
  1142. //获取refer
  1143. getRefer : function(refer) {
  1144. if (!refer) {
  1145. refer = this.url();
  1146. }
  1147. return refer;
  1148. },
  1149. //数据存储
  1150. data : function(key, value) {
  1151. if (value) {
  1152. if (value == 'del') {
  1153. uni.removeStorageSync(key);
  1154. return true;
  1155. } else {
  1156. uni.setStorageSync(key, value);
  1157. return value;
  1158. }
  1159. } else {
  1160. return uni.getStorageSync(key);
  1161. }
  1162. },
  1163. //插入html代码,初始化
  1164. initHtml : function(doc) {
  1165. this.doc = doc.$refs.initHtml.$el;
  1166. },
  1167. //插入html代码
  1168. html : function(html) {
  1169. var self = this;
  1170. var div = document.createElement('div');
  1171. div.innerHTML = html;
  1172. this.doc.appendChild(div);
  1173. var scripts = div.querySelectorAll('script');
  1174. return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
  1175. return chain.then(() => self.runScript(script));
  1176. }, Promise.resolve());
  1177. },
  1178. //微信提醒
  1179. wxTip : function(type) {
  1180. var wx = this.is_weixin();
  1181. if (wx) {
  1182. var img = 'live_weixin';
  1183. if (type) {
  1184. img = 'weixin-guide';
  1185. }
  1186. 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/'+img+'.png" alt="微信打开" style="max-width: 100%; height: auto;"/></p></div>';
  1187. this.html(tip);
  1188. return true;
  1189. }
  1190. return false;
  1191. },
  1192. is_weixin: function() {
  1193. if (this.source != 'h5') {
  1194. return false;
  1195. }
  1196. var ua = navigator.userAgent.toLowerCase();
  1197. if(ua.match(/MicroMessenger/i)=="micromessenger") {
  1198. return true;
  1199. } else {
  1200. return false;
  1201. }
  1202. },
  1203. //执行script代码
  1204. runScript : function(script) {
  1205. return new Promise((reslove, rejected) => {
  1206. const newScript = document.createElement('script');
  1207. newScript.innerHTML = script.innerHTML;
  1208. const src = script.getAttribute('src');
  1209. if (src) newScript.setAttribute('src', src);
  1210. // script 加载完成和错误处理
  1211. newScript.onload = () => reslove();
  1212. newScript.onerror = err => rejected();
  1213. document.head.appendChild(newScript);
  1214. document.head.removeChild(newScript);
  1215. if (!src) {
  1216. // 如果是 inline script 执行是同步的
  1217. reslove();
  1218. }
  1219. })
  1220. },
  1221. //拉起支付
  1222. pay : function(result, location, error) {
  1223. var self = this;
  1224. if (!error) {
  1225. if (location.indexOf('?') == -1) {
  1226. var fix = '?';
  1227. } else {
  1228. var fix = '&';
  1229. }
  1230. location = location + fix + 'order_id=' + result.order_id;
  1231. }
  1232. if (result.type == 'test') {
  1233. self.alert('支付成功','none', function() {
  1234. self.location(location, 'to');
  1235. })
  1236. } else if (self.source == 'h5') {
  1237. uni.hideLoading();
  1238. uni.showLoading({title: '跳转中', mask: true});
  1239. self.html(result.order);
  1240. } else if (self.source == 'app') {
  1241. self.appPayment(result.type, result.order, function() {
  1242. self.location(location, 'to');
  1243. }, function() {
  1244. if (!error) {
  1245. self.location(location + '&error=1', 'to');
  1246. } else {
  1247. self.alert(error);
  1248. }
  1249. });
  1250. }
  1251. },
  1252. //app支付
  1253. appPayment : function(type, order, callback, errorCallback) {
  1254. uni.requestPayment({
  1255. provider: type,
  1256. orderInfo: order, //微信、支付宝订单数据
  1257. success: function (res) {
  1258. //console.log('success:' + JSON.stringify(res));
  1259. callback(res);
  1260. },
  1261. fail: function (err) {
  1262. //console.log('fail:' + JSON.stringify(err));
  1263. errorCallback(err)
  1264. }
  1265. });
  1266. },
  1267. //跳转到refer
  1268. jump : function() {
  1269. var refer = this.data('login_refer');
  1270. var id = this.data('invite_id');
  1271. var type = this.data('invite_type');
  1272. if (id && refer != 'index/index') {
  1273. refer = refer + '?id=' + id + '&type=' + type;
  1274. }
  1275. this.data('login_refer', 'del');
  1276. this.data('invite_id', 'del');
  1277. this.data('invite_type', 'del');
  1278. if (!refer) {
  1279. refer = 'index/index';
  1280. }
  1281. this.location(refer, 'go');
  1282. },
  1283. //转星号
  1284. xing : function(s) {
  1285. return s.replace(s, function(sMatch){
  1286. return sMatch.replace(/./g,"*");
  1287. });
  1288. },
  1289. //检查更新
  1290. checkUpdate : function() {
  1291. this.update();
  1292. },
  1293. update : function(path, call) {
  1294. var self = this;
  1295. var source = this.source;
  1296. if (source != 'app') {
  1297. return;
  1298. }
  1299. var type = plus.os.name.toLowerCase();
  1300. //var type = 'android';
  1301. //var config = this.config.version;
  1302. self.post('isUpdate', {t:1}, function(res) {
  1303. if (type == 'android') {
  1304. res.downloadUrl = res.androidUrl;
  1305. } else {
  1306. res.downloadUrl = res.iosUrl;
  1307. }
  1308. self.data('dever_update_link', res.downloadUrl);
  1309. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  1310. var version = widgetInfo.versionCode;
  1311. var state = false;
  1312. state = widgetInfo.versionCode < res.versionCode;
  1313. if (state) {
  1314. //强制更新
  1315. var txt = '正在为您下载更新,下载完成将重启应用';
  1316. if (res.forceUpdate == true) {
  1317. self.down(res, res.downloadUrl, txt, type);
  1318. } else if(call) {
  1319. call(res, txt, type);
  1320. } else {
  1321. uni.showModal({
  1322. title: '发现新版本',
  1323. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  1324. success: (t) => {
  1325. if (t.confirm) {
  1326. if (path) {
  1327. self.location(path);
  1328. } else {
  1329. self.down(res, res.downloadUrl, txt, type);
  1330. }
  1331. }
  1332. }
  1333. })
  1334. }
  1335. }
  1336. });
  1337. });
  1338. },
  1339. uploadCall : function(config, type, packgePath) {
  1340. // 保存更新记录到stroage,下次启动app时安装更新
  1341. var self = this;
  1342. self.data('dever_update', 1);
  1343. self.data('dever_update_down', packgePath);
  1344. // 任务完成,关闭下载任务,开始安装
  1345. plus.runtime.install(packgePath, {force: true}, function() {
  1346. self.data('dever_update', 2);
  1347. self.data('dever_update_down', 'del');
  1348. uni.showModal({
  1349. title: '提示',
  1350. content: '应用将重启以完成更新',
  1351. showCancel: false,
  1352. complete: () => {
  1353. plus.runtime.restart();
  1354. }
  1355. })
  1356. });
  1357. },
  1358. down : function(config, url, txt, type, call) {
  1359. if (!url) {
  1360. return false;
  1361. }
  1362. if (txt) {
  1363. this.alert(txt);
  1364. }
  1365. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  1366. plus.runtime.openURL(url);
  1367. return false;
  1368. }
  1369. var self = this;
  1370. var packgePath = self.data('dever_update_down');
  1371. if (packgePath) {
  1372. self.uploadCall(config, type, packgePath);
  1373. return false;
  1374. }
  1375. var downloadTask = uni.downloadFile({
  1376. url: url,
  1377. success: (res) => {
  1378. if (res.statusCode === 200) {
  1379. // 保存下载的安装包
  1380. uni.saveFile({
  1381. tempFilePath: res.tempFilePath,
  1382. success: (res) => {
  1383. var packgePath = res.savedFilePath;
  1384. if (call) {
  1385. call();
  1386. }
  1387. self.uploadCall(config, type, packgePath);
  1388. downloadTask.abort();
  1389. downloadTask = null;
  1390. }
  1391. })
  1392. }
  1393. }
  1394. });
  1395. return downloadTask;
  1396. },
  1397. //预览图片
  1398. viewPic : function(imgs, img, key) {
  1399. if (imgs && imgs.length > 0) {
  1400. if (key) {
  1401. var temp = [];
  1402. var i;
  1403. for(i in imgs) {
  1404. temp.push(imgs[i][key]);
  1405. }
  1406. imgs = temp;
  1407. }
  1408. uni.previewImage({
  1409. current:img,
  1410. urls: imgs,
  1411. indicator : 'default',
  1412. loop : true,
  1413. /*
  1414. longPressActions : {
  1415. itemList: ['发送给朋友', '保存图片', '收藏'],
  1416. success: function(data) {
  1417. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  1418. },
  1419. fail: function(err) {
  1420. console.log(err.errMsg);
  1421. }
  1422. }
  1423. */
  1424. });
  1425. }
  1426. },
  1427. //截取APP退出功能
  1428. quit : function(page, call) {
  1429. var self = this;
  1430. if (self.source == 'app') {
  1431. var main = plus.android.runtimeMainActivity();
  1432. //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
  1433. /*
  1434. plus.runtime.quit = function(){
  1435. main.moveTaskToBack(false);
  1436. };
  1437. */
  1438. //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
  1439. plus.nativeUI.toast = (function(str){
  1440. if(str == '再按一次退出应用') {
  1441. if (call) {
  1442. call(main);
  1443. } else {
  1444. var webview = page.$mp.page.$getAppWebview();
  1445. var child = webview.children();
  1446. child[0].back();
  1447. }
  1448. return false;
  1449. }else{
  1450. self.alert(str);
  1451. }
  1452. });
  1453. }
  1454. },
  1455. //滑动
  1456. slide : function(e) {
  1457. this.slideValue = {};
  1458. if (e.changedTouches[0].clientX) {
  1459. this.slideValue.clientX = e.changedTouches[0].clientX;
  1460. this.slideValue.clientY = e.changedTouches[0].clientY;
  1461. } else {
  1462. this.slideValue.clientX = e.changedTouches[0].screenX;
  1463. this.slideValue.clientY = e.changedTouches[0].screenY;
  1464. }
  1465. },
  1466. //滑动结束
  1467. slideEnd : function(e) {
  1468. if (e.changedTouches[0].clientX) {
  1469. var subX = e.changedTouches[0].clientX - this.slideValue.clientX;
  1470. var subY = e.changedTouches[0].clientY - this.slideValue.clientY;
  1471. } else {
  1472. var subX = e.changedTouches[0].screenX - this.slideValue.clientX;
  1473. var subY = e.changedTouches[0].screenY - this.slideValue.clientY;
  1474. }
  1475. if (subY > 20) {
  1476. //上滑
  1477. return 1;
  1478. } else if (subY < -20) {
  1479. //下滑
  1480. return 2;
  1481. } else if (subX > 20) {
  1482. //左滑
  1483. return 3;
  1484. } else if(subX < -20) {
  1485. //右滑
  1486. return 4;
  1487. }
  1488. return
  1489. },
  1490. //计算图片宽高比
  1491. getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
  1492. let [
  1493. // 用于设定图片的宽和高
  1494. tempWidth,
  1495. tempHeight,
  1496. ] = [
  1497. undefined,
  1498. undefined
  1499. ]
  1500. try {
  1501. imgWidth = parseFloat(imgWidth)
  1502. imgHeight = parseFloat(imgHeight)
  1503. containerWidth = parseFloat(containerWidth)
  1504. containerHeight = parseFloat(containerHeight)
  1505. } catch (error) {
  1506. throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
  1507. }
  1508. if (imgWidth > 0 && imgHeight > 0) {
  1509. //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
  1510. if (imgWidth / imgHeight >= containerWidth / containerHeight) {
  1511. if (imgWidth > containerWidth) {
  1512. // alert('aaaaaaaa')
  1513. tempWidth = containerWidth
  1514. // 按原图片的比例进行缩放
  1515. tempHeight = (imgHeight * containerWidth) / imgWidth
  1516. } else {
  1517. // 按照图片的大小进行缩放
  1518. tempWidth = imgWidth
  1519. tempHeight = imgHeight
  1520. }
  1521. } else { // 原图片的高度必然 > 宽度
  1522. if (imgHeight > containerHeight) {
  1523. tempHeight = containerHeight
  1524. // 按原图片的比例进行缩放
  1525. tempWidth = (imgWidth * containerHeight) / imgHeight
  1526. } else {
  1527. // 按原图片的大小进行缩放
  1528. tempWidth = imgWidth
  1529. tempHeight = imgHeight
  1530. }
  1531. }
  1532. }
  1533. return [tempWidth, tempHeight]
  1534. },
  1535. sleep : function(time) {
  1536. return new Promise((resolve) => setTimeout(resolve, time));
  1537. },
  1538. //分享功能初始化
  1539. shareInit : function(wx, project, host, data) {
  1540. share.init(wx, project, host, data);
  1541. },
  1542. //通用的分享功能
  1543. share : function(provider, scene, type, data, success, error) {
  1544. if (this.source == 'h5') {
  1545. var weixin = this.wxTip(1);
  1546. if (!weixin) {
  1547. this.alert('请点击复制按钮');
  1548. }
  1549. } else {
  1550. var config = {
  1551. provider: provider,
  1552. type: type,
  1553. scene: scene,
  1554. href: data.link,
  1555. title: data.title,
  1556. summary: data.content,
  1557. imageUrl: data.pic,
  1558. success: function(res) {
  1559. if (success) {
  1560. success(res);
  1561. } else {
  1562. console.log("success:" + JSON.stringify(res));
  1563. }
  1564. },
  1565. fail: function(err) {
  1566. if (error) {
  1567. success(err);
  1568. } else {
  1569. console.log("fail:" + JSON.stringify(err));
  1570. }
  1571. }
  1572. };
  1573. if (data.media) {
  1574. config.mediaUrl = media;
  1575. }
  1576. if (this.source == 'applet') {
  1577. if (!data.path) {
  1578. console.log('data.path未定义!');
  1579. return;
  1580. }
  1581. if (!data.applet_id) {
  1582. console.log('data.applet_id未定义!');
  1583. return;
  1584. }
  1585. config.miniProgram = {};
  1586. config.miniProgram.id = data.applet_id;
  1587. config.miniProgram.path = data.path;
  1588. config.miniProgram.type = 0;
  1589. config.miniProgram.webUrl = data.link;
  1590. }
  1591. uni.share(config);
  1592. }
  1593. },
  1594. // 获取地理位置
  1595. getLocation : function(type, text, success) {
  1596. if (!type) {
  1597. type = 'wgs84';
  1598. }
  1599. if (!text) {
  1600. text = '需要获得您的位置,请您授权';
  1601. }
  1602. var self = this;
  1603. if (self.source != 'h5') {
  1604. uni.getSetting({
  1605. success(res) {
  1606. if (res.authSetting['scope.userLocation']) {
  1607. self.getAltitude(type, success);
  1608. } else {
  1609. uni.authorize({
  1610. scope: 'scope.userLocation',
  1611. success(res) {
  1612. self.getAltitude(type, success);
  1613. },
  1614. // 授权失败
  1615. fail(err) {
  1616. uni.showModal({
  1617. title: '提示',
  1618. content: text,
  1619. showCancel: false,
  1620. confirmText: '确认授权',
  1621. success() {
  1622. uni.openSetting({
  1623. success(res) {
  1624. self.getAltitude(type, success);
  1625. },
  1626. fail(err) {
  1627. self.alert('授权失败');
  1628. }
  1629. })
  1630. }
  1631. })
  1632. }
  1633. })
  1634. }
  1635. }
  1636. })
  1637. } else {
  1638. self.getAltitude(type, success);
  1639. }
  1640. },
  1641. getAltitude : function(type, success) {
  1642. uni.getLocation({
  1643. type: type,
  1644. altitude: true,
  1645. success(res) {
  1646. if (success) {
  1647. success(res);
  1648. } else {
  1649. console.info(res);
  1650. }
  1651. }
  1652. })
  1653. }
  1654. }
  1655. module.exports = dever