index.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  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. uni.showLoading({title: '加载中', mask: true});
  877. //uni.showNavigationBarLoading();
  878. this.showLoad = true;
  879. }
  880. uni.stopPullDownRefresh();
  881. },
  882. //取消loading
  883. hideLoading : function(method, options, url) {
  884. var state = false;
  885. if (method == 'POST' || method == 'post') {
  886. if (this.postLoadingCall && this.postLoadingCall.no) {
  887. state = true;
  888. var callback = this.postLoadingCall.no;
  889. callback(this);
  890. }
  891. } else if (method == 'page') {
  892. //state = true;
  893. if (options.page && options.page > 1) {
  894. state = false;
  895. }
  896. if (this.pageLoadingCall && this.pageLoadingCall.no) {
  897. state = true;
  898. var callback = this.pageLoadingCall.no;
  899. callback(this);
  900. }
  901. } else {
  902. url = this.route();
  903. if (!this.lastGetUrl || (this.lastGetUrl && this.lastGetUrl != url)) {
  904. this.lastGetUrl = url;
  905. }
  906. if (this.getLoadingState && this.getLoadingState[url]) {
  907. state = false;
  908. } else {
  909. this.getLoadingState[url] = true;
  910. if (this.getLoadingCall && this.getLoadingCall.no) {
  911. state = true;
  912. var callback = this.getLoadingCall.no;
  913. callback(this);
  914. }
  915. }
  916. }
  917. if (!state && this.showLoad) {
  918. uni.hideLoading();
  919. this.showLoad = false;
  920. this.btnFinish();
  921. //uni.hideNavigationBarLoading();
  922. }
  923. },
  924. //data数据获取
  925. dataset : function(e) {
  926. if (e.currentTarget) {
  927. var dataset = e.currentTarget.dataset;
  928. } else {
  929. var dataset = e.target.dataset;
  930. }
  931. return dataset;
  932. },
  933. //view中的页面提示
  934. viewAlert : function(e) {
  935. if (e) {
  936. var dataset = this.dataset(e);
  937. var msg = dataset.msg;
  938. var icon = dataset.icon;
  939. var callback = dataset.callback;
  940. this.alert(msg, icon, callback);
  941. } else {
  942. this.alert('敬请期待');
  943. }
  944. },
  945. //view中的页面跳转
  946. viewLocation : function(e) {
  947. var page = '';
  948. var to = '';
  949. var option = '';
  950. if (e) {
  951. var dataset = this.dataset(e);
  952. page = dataset.page;
  953. to = dataset.to;
  954. option = []
  955. for (var i in dataset) {
  956. if (i.indexOf('save_') != -1) {
  957. var v = dataset[i];
  958. i = i.replace('save_', '');
  959. this.data(i, v);
  960. } else if (i != 'page' && i != 'to') {
  961. option.push(i + '=' + dataset[i]);
  962. }
  963. }
  964. if (option) {
  965. option = option.join('&');
  966. }
  967. }
  968. if (!page) {
  969. uni.navigateBack({});
  970. } else {
  971. var go = '';
  972. if (to) {
  973. go = 'to';
  974. }
  975. if (option) {
  976. page = page + '?' + option;
  977. }
  978. this.location(page, go);
  979. }
  980. },
  981. //页面跳转
  982. location : function(path, go) {
  983. var self = this;
  984. if (path.indexOf('http') > -1) {
  985. this.debug('http:' + path);
  986. this.data('web_view', path);
  987. if (this.source == 'app' || this.source == 'applet' || go == 'webview') {
  988. uni.navigateTo({
  989. url: this.web_view
  990. })
  991. } else {
  992. if (!go) {
  993. go = '';
  994. }
  995. if (go && go == 'location') {
  996. window.location.href = path;
  997. } else {
  998. var url = window.location.href;
  999. if (go.indexOf('/') != -1) {
  1000. url = this.host + go
  1001. } else {
  1002. url = url + go;
  1003. }
  1004. url = window.btoa(url);
  1005. if (path.indexOf('?') == -1) {
  1006. path = path + '?';
  1007. } else {
  1008. path = path + '&';
  1009. }
  1010. window.location.href = path + 'refer=' + url;
  1011. }
  1012. }
  1013. } else {
  1014. this.debug('navigateTo:' + go + ':' + path);
  1015. if (path.indexOf('/pages/') == -1) {
  1016. path = '/pages/' + path;
  1017. }
  1018. if (this.switchTab.indexOf(path) != -1) {
  1019. uni.switchTab({
  1020. url: path,
  1021. success: function(e) {
  1022. if (self.source != 'app' && self.switchTabCall[path]) {
  1023. var call = self.switchTabCall[path];
  1024. call(self, e);
  1025. }
  1026. }
  1027. })
  1028. } else if (go) {
  1029. uni.redirectTo({
  1030. url: path
  1031. })
  1032. } else {
  1033. uni.navigateTo({
  1034. url: path
  1035. })
  1036. }
  1037. }
  1038. },
  1039. //提示信息
  1040. alert : function(info, icon, callback) {
  1041. if (!icon) {
  1042. icon = 'none'
  1043. }
  1044. if (info) {
  1045. uni.showToast({title: info, icon: icon});
  1046. }
  1047. this.debug('alert:' + info)
  1048. if (callback) {
  1049. this.debug('callback:' + callback)
  1050. setTimeout(function(){
  1051. callback();
  1052. }, 1000)
  1053. }
  1054. },
  1055. //询问信息
  1056. confirm : function(text, callback) {
  1057. uni.showModal({
  1058. content: text,
  1059. cancelText: '取消',
  1060. confirmText: '确定',
  1061. success: res => {
  1062. if (res.confirm) {
  1063. callback();
  1064. }
  1065. }
  1066. });
  1067. },
  1068. //debug调试
  1069. debug : function(string) {
  1070. if (config.base.debug) {
  1071. this.log(string);
  1072. }
  1073. },
  1074. //记录日志
  1075. log : function(string) {
  1076. console.log(string);
  1077. },
  1078. //获取token
  1079. getToken: function() {
  1080. return http.getToken();
  1081. },
  1082. //设置token
  1083. setToken: function(value) {
  1084. return http.setToken(value);
  1085. },
  1086. //通用的提交数据的方法
  1087. post : function(url, options, callback, err_callback) {
  1088. http.request('post', url, options, callback, err_callback);
  1089. },
  1090. //通用的获取数据方法
  1091. get : function(vue, url, options, callback, err_callback) {
  1092. http.request(vue, url, options, callback, err_callback);
  1093. },
  1094. //通用的获取数据方法
  1095. page : function(config, vue, url, options, callback, err_callback) {
  1096. page.get(config, vue, url, options, callback, err_callback);
  1097. },
  1098. //选择图片并上传
  1099. upload : function(id, count, callback) {
  1100. this.getUpload().handle(id, count, callback);
  1101. },
  1102. //上传到七牛
  1103. qiniu : function(qn, self, id, count, callback, handle) {
  1104. this.getUpload().qiniu(qn, self, id, count, callback, handle);
  1105. },
  1106. //html上传文件
  1107. upfile : function(self, key, count, callback, ext, title, style) {
  1108. this.getUpload().upfile(self, key, count, callback, ext, title, style);
  1109. },
  1110. //直接上传文件
  1111. uploadFile : function(path, id, count, callback) {
  1112. this.getUpload().uploadFile(path, id, count, callback);
  1113. },
  1114. //获取upload
  1115. getUpload : function() {
  1116. return upload;
  1117. },
  1118. //im聊天
  1119. im : function(url, callback, connect) {
  1120. im.init(url, callback, connect);
  1121. },
  1122. //删除上传
  1123. uploadDel : function(id, index, callback) {
  1124. upload.uploadDel(id, index, callback);
  1125. },
  1126. //验证登录
  1127. checkLogin : function(refer) {
  1128. if (!this.getToken()) {
  1129. this.data('login_refer', this.getRefer(refer));
  1130. //this.hideLoading();
  1131. if (this.source == 'h5') {
  1132. this.location(this.login, 'go');
  1133. } else {
  1134. this.location(this.login, 'go');
  1135. }
  1136. }
  1137. },
  1138. //获取refer
  1139. getRefer : function(refer) {
  1140. if (!refer) {
  1141. refer = this.url();
  1142. }
  1143. return refer;
  1144. },
  1145. //数据存储
  1146. data : function(key, value) {
  1147. if (value) {
  1148. if (value == 'del') {
  1149. uni.removeStorageSync(key);
  1150. return true;
  1151. } else {
  1152. uni.setStorageSync(key, value);
  1153. return value;
  1154. }
  1155. } else {
  1156. return uni.getStorageSync(key);
  1157. }
  1158. },
  1159. //插入html代码,初始化
  1160. initHtml : function(doc) {
  1161. this.doc = doc.$refs.initHtml.$el;
  1162. },
  1163. //插入html代码
  1164. html : function(html) {
  1165. var self = this;
  1166. var div = document.createElement('div');
  1167. div.innerHTML = html;
  1168. this.doc.appendChild(div);
  1169. var scripts = div.querySelectorAll('script');
  1170. return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
  1171. return chain.then(() => self.runScript(script));
  1172. }, Promise.resolve());
  1173. },
  1174. //微信提醒
  1175. wxTip : function(type) {
  1176. var wx = this.is_weixin();
  1177. if (wx) {
  1178. var img = 'live_weixin';
  1179. if (type) {
  1180. img = 'weixin-guide';
  1181. }
  1182. 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>';
  1183. this.html(tip);
  1184. return true;
  1185. }
  1186. return false;
  1187. },
  1188. is_weixin: function() {
  1189. if (this.source != 'h5') {
  1190. return false;
  1191. }
  1192. var ua = navigator.userAgent.toLowerCase();
  1193. if(ua.match(/MicroMessenger/i)=="micromessenger") {
  1194. return true;
  1195. } else {
  1196. return false;
  1197. }
  1198. },
  1199. //执行script代码
  1200. runScript : function(script) {
  1201. return new Promise((reslove, rejected) => {
  1202. const newScript = document.createElement('script');
  1203. newScript.innerHTML = script.innerHTML;
  1204. const src = script.getAttribute('src');
  1205. if (src) newScript.setAttribute('src', src);
  1206. // script 加载完成和错误处理
  1207. newScript.onload = () => reslove();
  1208. newScript.onerror = err => rejected();
  1209. document.head.appendChild(newScript);
  1210. document.head.removeChild(newScript);
  1211. if (!src) {
  1212. // 如果是 inline script 执行是同步的
  1213. reslove();
  1214. }
  1215. })
  1216. },
  1217. //拉起支付
  1218. pay : function(result, location, error) {
  1219. var self = this;
  1220. if (!error) {
  1221. if (location.indexOf('?') == -1) {
  1222. var fix = '?';
  1223. } else {
  1224. var fix = '&';
  1225. }
  1226. location = location + fix + 'order_id=' + result.order_id;
  1227. }
  1228. if (result.type == 'test') {
  1229. self.location(location, 'to');
  1230. } else if (self.source == 'h5') {
  1231. uni.hideLoading();
  1232. uni.showLoading({title: '跳转中', mask: true});
  1233. self.html(result.order);
  1234. } else if (self.source == 'app') {
  1235. self.appPayment(result.type, result.order, function() {
  1236. self.location(location, 'to');
  1237. }, function() {
  1238. if (!error) {
  1239. self.location(location + '&error=1', 'to');
  1240. } else {
  1241. self.alert(error);
  1242. }
  1243. });
  1244. }
  1245. },
  1246. //app支付
  1247. appPayment : function(type, order, callback, errorCallback) {
  1248. uni.requestPayment({
  1249. provider: type,
  1250. orderInfo: order, //微信、支付宝订单数据
  1251. success: function (res) {
  1252. //console.log('success:' + JSON.stringify(res));
  1253. callback(res);
  1254. },
  1255. fail: function (err) {
  1256. //console.log('fail:' + JSON.stringify(err));
  1257. errorCallback(err)
  1258. }
  1259. });
  1260. },
  1261. //跳转到refer
  1262. jump : function() {
  1263. var refer = this.data('login_refer');
  1264. var id = this.data('invite_id');
  1265. var type = this.data('invite_type');
  1266. if (id && refer != 'index/index') {
  1267. refer = refer + '?id=' + id + '&type=' + type;
  1268. }
  1269. this.data('login_refer', 'del');
  1270. this.data('invite_id', 'del');
  1271. this.data('invite_type', 'del');
  1272. if (!refer) {
  1273. refer = 'index/index';
  1274. }
  1275. this.location(refer, 'go');
  1276. },
  1277. //转星号
  1278. xing : function(s) {
  1279. return s.replace(s, function(sMatch){
  1280. return sMatch.replace(/./g,"*");
  1281. });
  1282. },
  1283. //检查更新
  1284. checkUpdate : function() {
  1285. this.update();
  1286. },
  1287. update : function(path, call) {
  1288. var self = this;
  1289. var source = this.source;
  1290. if (source != 'app') {
  1291. return;
  1292. }
  1293. var type = plus.os.name.toLowerCase();
  1294. //var type = 'android';
  1295. //var config = this.config.version;
  1296. self.post('isUpdate', {t:1}, function(res) {
  1297. if (type == 'android') {
  1298. res.downloadUrl = res.androidUrl;
  1299. } else {
  1300. res.downloadUrl = res.iosUrl;
  1301. }
  1302. self.data('dever_update_link', res.downloadUrl);
  1303. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  1304. var version = widgetInfo.versionCode;
  1305. var state = false;
  1306. state = widgetInfo.versionCode < res.versionCode;
  1307. if (state) {
  1308. //强制更新
  1309. var txt = '正在为您下载更新,下载完成将重启应用';
  1310. if (res.forceUpdate == true) {
  1311. self.down(res, res.downloadUrl, txt, type);
  1312. } else if(call) {
  1313. call(res, txt, type);
  1314. } else {
  1315. uni.showModal({
  1316. title: '发现新版本',
  1317. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  1318. success: (t) => {
  1319. if (t.confirm) {
  1320. if (path) {
  1321. self.location(path);
  1322. } else {
  1323. self.down(res, res.downloadUrl, txt, type);
  1324. }
  1325. }
  1326. }
  1327. })
  1328. }
  1329. }
  1330. });
  1331. });
  1332. },
  1333. uploadCall : function(config, type, packgePath) {
  1334. // 保存更新记录到stroage,下次启动app时安装更新
  1335. var self = this;
  1336. self.data('dever_update', 1);
  1337. self.data('dever_update_down', packgePath);
  1338. // 任务完成,关闭下载任务,开始安装
  1339. plus.runtime.install(packgePath, {force: true}, function() {
  1340. self.data('dever_update', 2);
  1341. self.data('dever_update_down', 'del');
  1342. uni.showModal({
  1343. title: '提示',
  1344. content: '应用将重启以完成更新',
  1345. showCancel: false,
  1346. complete: () => {
  1347. plus.runtime.restart();
  1348. }
  1349. })
  1350. });
  1351. },
  1352. down : function(config, url, txt, type, call) {
  1353. if (!url) {
  1354. return false;
  1355. }
  1356. if (txt) {
  1357. this.alert(txt);
  1358. }
  1359. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  1360. plus.runtime.openURL(url);
  1361. return false;
  1362. }
  1363. var self = this;
  1364. var packgePath = self.data('dever_update_down');
  1365. if (packgePath) {
  1366. self.uploadCall(config, type, packgePath);
  1367. return false;
  1368. }
  1369. var downloadTask = uni.downloadFile({
  1370. url: url,
  1371. success: (res) => {
  1372. if (res.statusCode === 200) {
  1373. // 保存下载的安装包
  1374. uni.saveFile({
  1375. tempFilePath: res.tempFilePath,
  1376. success: (res) => {
  1377. var packgePath = res.savedFilePath;
  1378. if (call) {
  1379. call();
  1380. }
  1381. self.uploadCall(config, type, packgePath);
  1382. downloadTask.abort();
  1383. downloadTask = null;
  1384. }
  1385. })
  1386. }
  1387. }
  1388. });
  1389. return downloadTask;
  1390. },
  1391. //预览图片
  1392. viewPic : function(imgs, img, key) {
  1393. if (imgs && imgs.length > 0) {
  1394. if (key) {
  1395. var temp = [];
  1396. var i;
  1397. for(i in imgs) {
  1398. temp.push(imgs[i][key]);
  1399. }
  1400. imgs = temp;
  1401. }
  1402. uni.previewImage({
  1403. current:img,
  1404. urls: imgs,
  1405. indicator : 'default',
  1406. loop : true,
  1407. /*
  1408. longPressActions : {
  1409. itemList: ['发送给朋友', '保存图片', '收藏'],
  1410. success: function(data) {
  1411. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  1412. },
  1413. fail: function(err) {
  1414. console.log(err.errMsg);
  1415. }
  1416. }
  1417. */
  1418. });
  1419. }
  1420. },
  1421. //截取APP退出功能
  1422. quit : function(page, call) {
  1423. var self = this;
  1424. if (self.source == 'app') {
  1425. var main = plus.android.runtimeMainActivity();
  1426. //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
  1427. /*
  1428. plus.runtime.quit = function(){
  1429. main.moveTaskToBack(false);
  1430. };
  1431. */
  1432. //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
  1433. plus.nativeUI.toast = (function(str){
  1434. if(str == '再按一次退出应用') {
  1435. if (call) {
  1436. call(main);
  1437. } else {
  1438. var webview = page.$mp.page.$getAppWebview();
  1439. var child = webview.children();
  1440. child[0].back();
  1441. }
  1442. return false;
  1443. }else{
  1444. self.alert(str);
  1445. }
  1446. });
  1447. }
  1448. },
  1449. //滑动
  1450. slide : function(e) {
  1451. this.slideValue = {};
  1452. if (e.changedTouches[0].clientX) {
  1453. this.slideValue.clientX = e.changedTouches[0].clientX;
  1454. this.slideValue.clientY = e.changedTouches[0].clientY;
  1455. } else {
  1456. this.slideValue.clientX = e.changedTouches[0].screenX;
  1457. this.slideValue.clientY = e.changedTouches[0].screenY;
  1458. }
  1459. },
  1460. //滑动结束
  1461. slideEnd : function(e) {
  1462. if (e.changedTouches[0].clientX) {
  1463. var subX = e.changedTouches[0].clientX - this.slideValue.clientX;
  1464. var subY = e.changedTouches[0].clientY - this.slideValue.clientY;
  1465. } else {
  1466. var subX = e.changedTouches[0].screenX - this.slideValue.clientX;
  1467. var subY = e.changedTouches[0].screenY - this.slideValue.clientY;
  1468. }
  1469. if (subY > 20) {
  1470. //上滑
  1471. return 1;
  1472. } else if (subY < -20) {
  1473. //下滑
  1474. return 2;
  1475. } else if (subX > 20) {
  1476. //左滑
  1477. return 3;
  1478. } else if(subX < -20) {
  1479. //右滑
  1480. return 4;
  1481. }
  1482. return
  1483. },
  1484. //计算图片宽高比
  1485. getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
  1486. let [
  1487. // 用于设定图片的宽和高
  1488. tempWidth,
  1489. tempHeight,
  1490. ] = [
  1491. undefined,
  1492. undefined
  1493. ]
  1494. try {
  1495. imgWidth = parseFloat(imgWidth)
  1496. imgHeight = parseFloat(imgHeight)
  1497. containerWidth = parseFloat(containerWidth)
  1498. containerHeight = parseFloat(containerHeight)
  1499. } catch (error) {
  1500. throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
  1501. }
  1502. if (imgWidth > 0 && imgHeight > 0) {
  1503. //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
  1504. if (imgWidth / imgHeight >= containerWidth / containerHeight) {
  1505. if (imgWidth > containerWidth) {
  1506. // alert('aaaaaaaa')
  1507. tempWidth = containerWidth
  1508. // 按原图片的比例进行缩放
  1509. tempHeight = (imgHeight * containerWidth) / imgWidth
  1510. } else {
  1511. // 按照图片的大小进行缩放
  1512. tempWidth = imgWidth
  1513. tempHeight = imgHeight
  1514. }
  1515. } else { // 原图片的高度必然 > 宽度
  1516. if (imgHeight > containerHeight) {
  1517. tempHeight = containerHeight
  1518. // 按原图片的比例进行缩放
  1519. tempWidth = (imgWidth * containerHeight) / imgHeight
  1520. } else {
  1521. // 按原图片的大小进行缩放
  1522. tempWidth = imgWidth
  1523. tempHeight = imgHeight
  1524. }
  1525. }
  1526. }
  1527. return [tempWidth, tempHeight]
  1528. },
  1529. sleep : function(time) {
  1530. return new Promise((resolve) => setTimeout(resolve, time));
  1531. },
  1532. //分享功能初始化
  1533. shareInit : function(wx, project, host, data) {
  1534. share.init(wx, project, host, data);
  1535. },
  1536. //通用的分享功能
  1537. share : function(provider, scene, type, data, success, error) {
  1538. if (this.source == 'h5') {
  1539. var weixin = this.wxTip(1);
  1540. if (!weixin) {
  1541. this.alert('请点击复制按钮');
  1542. }
  1543. } else {
  1544. var config = {
  1545. provider: provider,
  1546. type: type,
  1547. scene: scene,
  1548. href: data.link,
  1549. title: data.title,
  1550. summary: data.content,
  1551. imageUrl: data.pic,
  1552. success: function(res) {
  1553. if (success) {
  1554. success(res);
  1555. } else {
  1556. console.log("success:" + JSON.stringify(res));
  1557. }
  1558. },
  1559. fail: function(err) {
  1560. if (error) {
  1561. success(err);
  1562. } else {
  1563. console.log("fail:" + JSON.stringify(err));
  1564. }
  1565. }
  1566. };
  1567. if (data.media) {
  1568. config.mediaUrl = media;
  1569. }
  1570. if (this.source == 'applet') {
  1571. if (!data.path) {
  1572. console.log('data.path未定义!');
  1573. return;
  1574. }
  1575. if (!data.applet_id) {
  1576. console.log('data.applet_id未定义!');
  1577. return;
  1578. }
  1579. config.miniProgram = {};
  1580. config.miniProgram.id = data.applet_id;
  1581. config.miniProgram.path = data.path;
  1582. config.miniProgram.type = 0;
  1583. config.miniProgram.webUrl = data.link;
  1584. }
  1585. uni.share(config);
  1586. }
  1587. },
  1588. // 获取地理位置
  1589. getLocation : function(type, text, success) {
  1590. if (!type) {
  1591. type = 'wgs84';
  1592. }
  1593. if (!text) {
  1594. text = '需要获得您的位置,请您授权';
  1595. }
  1596. var self = this;
  1597. if (self.source != 'h5') {
  1598. uni.getSetting({
  1599. success(res) {
  1600. if (res.authSetting['scope.userLocation']) {
  1601. self.getAltitude(type, success);
  1602. } else {
  1603. uni.authorize({
  1604. scope: 'scope.userLocation',
  1605. success(res) {
  1606. self.getAltitude(type, success);
  1607. },
  1608. // 授权失败
  1609. fail(err) {
  1610. uni.showModal({
  1611. title: '提示',
  1612. content: text,
  1613. showCancel: false,
  1614. confirmText: '确认授权',
  1615. success() {
  1616. uni.openSetting({
  1617. success(res) {
  1618. self.getAltitude(type, success);
  1619. },
  1620. fail(err) {
  1621. self.alert('授权失败');
  1622. }
  1623. })
  1624. }
  1625. })
  1626. }
  1627. })
  1628. }
  1629. }
  1630. })
  1631. } else {
  1632. self.getAltitude(type, success);
  1633. }
  1634. },
  1635. getAltitude : function(type, success) {
  1636. uni.getLocation({
  1637. type: type,
  1638. altitude: true,
  1639. success(res) {
  1640. if (success) {
  1641. success(res);
  1642. } else {
  1643. console.info(res);
  1644. }
  1645. }
  1646. })
  1647. }
  1648. }
  1649. module.exports = dever