index.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. //封装好一系列常用的方法 dever
  2. import config from './config';
  3. //http网络请求
  4. var http = {
  5. //token的key
  6. token : 'signature',
  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, type) {
  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. var method = config.request.method;
  35. if (typeof(url) == 'object') {
  36. vue = url[1];
  37. url = url[0];
  38. }
  39. if (typeof(vue) == 'object') {
  40. vue.isDisabled = true;
  41. }
  42. self.core(url, options, type, method).then((result, state) => {
  43. if (typeof(vue) == 'object') {
  44. if (!vue.fetch) {
  45. vue.fetch = {}
  46. }
  47. vue.isDisabled = false;
  48. vue.fetch = Object.assign({}, vue.fetch, result)
  49. }
  50. if (callback) {
  51. callback(result, state);
  52. }
  53. }).catch((result) => {
  54. if (typeof(vue) == 'object') {
  55. vue.isDisabled = false;
  56. }
  57. if (err_callback) {
  58. err_callback(result);
  59. } else {
  60. if (result.code == '2') {
  61. dever.location(dever.login);
  62. } else {
  63. dever.alert(result.msg)
  64. }
  65. }
  66. })
  67. },
  68. core : function(url, options, type, method) {
  69. var self = this;
  70. options = this.option(options);
  71. if (!options.noconcat) {
  72. options.noconcat = false;
  73. }
  74. if (config.server[url]) {
  75. config.request.url = config.server[url];
  76. } else {
  77. config.request.url = url;
  78. }
  79. var loading_method = config.request.method;
  80. if (options && options.page && options.page > 0) {
  81. options.pg = options.page;
  82. loading_method = 'page';
  83. }
  84. if (config.request.url.indexOf('http') == -1) {
  85. if (type == 'im') {
  86. config.request.url = config.im.api + config.request.url;
  87. } else {
  88. config.request.url = config.request.host + config.request.url;
  89. }
  90. }
  91. config.request.data = options;
  92. return new Promise((resolve, reject) => {
  93. config.request.complete = (response) => {
  94. dever.debug(response);
  95. if (response.statusCode === 200) {
  96. if (response.data.code == '0') {
  97. if (response.data.page) {
  98. dever.pageData = response.data.page;
  99. } else {
  100. dever.pageData = {};
  101. }
  102. if (options.page && options.page > 0 && !options.noconcat) {
  103. page.state = true;
  104. if (page.key && response.data.data[page.key].length > 0) {
  105. if (options && options.concat && options.concat == -1) {
  106. page.data = response.data.data[page.key].concat(page.data);
  107. } else {
  108. page.data = page.data.concat(response.data.data[page.key]);
  109. }
  110. response.data.data[page.key] = page.data;
  111. } else if (response.data.data.length > 0) {
  112. if (options && options.concat && options.concat == -1) {
  113. response.data.data = response.data.data.reverse();
  114. page.data = response.data.data.concat(page.data);
  115. } else {
  116. page.data = page.data.concat(response.data.data);
  117. }
  118. response.data.data = page.data;
  119. } else {
  120. page.state = false;
  121. if (page.key) {
  122. if (options && options.concat && options.concat == -1) {
  123. response.data.data[page.key] = page.data;
  124. resolve(response.data.data, false);
  125. } else {
  126. resolve(response.data.data[page.key], false);
  127. }
  128. } else {
  129. if (options && options.concat && options.concat == -1) {
  130. response.data.data = page.data;
  131. }
  132. resolve(response.data.data, false);
  133. }
  134. if (options && options.noloading) {
  135. } else {
  136. dever.hideLoading(loading_method, options, url);
  137. }
  138. return;
  139. }
  140. }
  141. resolve(response.data.data);
  142. } else if (response.data.code == '-1') {
  143. //退出登录
  144. self.setToken('');
  145. dever.location('index/index', 'go');
  146. reject(response.data)
  147. } else {
  148. reject(response.data)
  149. }
  150. if (options && options.noloading) {
  151. } else {
  152. dever.hideLoading(loading_method, options, url);
  153. }
  154. } else {
  155. // 处理catch 请求,不在本页面之外处理,统一在这里处理
  156. if (options && options.handle) {
  157. reject(response)
  158. } else {
  159. try {
  160. Promise.reject(response).catch(err => {
  161. self.error(response.statusCode || response.errMsg);
  162. });
  163. } catch (e) {
  164. dever.alert(e)
  165. }
  166. }
  167. if (options && options.noloading) {
  168. } else {
  169. dever.hideLoading(loading_method, options, url);
  170. }
  171. }
  172. }
  173. //uni.request(Object.assign({}, config, options));
  174. dever.debug(config.request);
  175. if (options.noloading) {
  176. uni.stopPullDownRefresh();
  177. } else {
  178. dever.loading(loading_method, options, url);
  179. }
  180. uni.request(config.request);
  181. })
  182. },
  183. error : function(err) {
  184. //console.error("请求背拒绝" + err)
  185. /*
  186. dever.data('web_error', err);
  187. var path = '/components/dever/pages/web_error';
  188. uni.navigateTo({
  189. url: path
  190. })
  191. return;
  192. */
  193. switch (err) {
  194. case 401:
  195. // 错误码404的处理方式
  196. console.error("请求拒绝" + err)
  197. break;
  198. case 404:
  199. // 错误码404的处理方式
  200. console.error("没有找到页面" + err)
  201. break;
  202. case 500:
  203. // 错误码404的处理方式
  204. console.error("500服务器错误" + err)
  205. break;
  206. case 405:
  207. console.error("错误的请求" + err)
  208. break;
  209. }
  210. },
  211. }
  212. var im = {
  213. socket : {},
  214. status : false,
  215. //host
  216. url : 'ws://me.5dev.cn:8282',
  217. //推送路径
  218. push_path : '',
  219. init : function(key, url, callback, connect) {
  220. if (url) {
  221. this.url = url;
  222. } else {
  223. this.url = config.im.host;
  224. }
  225. this.connect(key, callback, connect);
  226. },
  227. connect : function(key, callback, connect) {
  228. var self = this;
  229. this.close(key);
  230. this.socket[key] = uni.connectSocket({
  231. url: this.url,
  232. complete: (res)=> {
  233. }
  234. });
  235. this.socket[key].onOpen(function (res) {
  236. if (connect) {
  237. connect(true, res);
  238. }
  239. console.info('open');
  240. self.status = true;
  241. self.socket[key].onMessage(function (res) {
  242. if (res.data.indexOf('{') != -1) {
  243. var data = eval("("+res.data+")");
  244. } else {
  245. var data = res.data;
  246. }
  247. if (callback) {
  248. callback(data);
  249. }
  250. });
  251. });
  252. this.socket[key].onClose(function (res) {
  253. if (connect) {
  254. connect(false, res);
  255. }
  256. console.info('close');
  257. self.status = false;
  258. });
  259. this.socket[key].onError(function (res) {
  260. console.info('error');
  261. self.connect(callback, connect);
  262. });
  263. },
  264. close : function(key) {
  265. var self = this;
  266. if (this.socket[key]) {
  267. this.socket[key].close({
  268. success(res) {
  269. self.status = false;
  270. },
  271. fail(err) {
  272. self.status = true;
  273. }
  274. })
  275. }
  276. },
  277. get : function(vue, url, options, callback, err_callback) {
  278. this.request(vue, url, options, callback, err_callback);
  279. },
  280. post : function(url, options, callback, err_callback) {
  281. this.request('post', url, options, callback, err_callback);
  282. },
  283. request : function(vue, url, options, callback, err_callback) {
  284. if (!options) {
  285. options = {};
  286. }
  287. var param = options;
  288. param.noloading = 1;
  289. dever.post(this.push_path, param, function(t) {
  290. url += '?timestamp=' + t.timestamp + '&pop_token=' + t.token + '&login_uid=' + t.login_uid;
  291. dever.config.im = t;
  292. options.login_uid = t.login_uid;
  293. if (t.friend_uid) {
  294. options.friend_uid = t.friend_uid;
  295. }
  296. delete options.noloading;
  297. http.request(vue, url, options, callback, err_callback, 'im');
  298. });
  299. }
  300. }
  301. var page = {
  302. //分页控制
  303. value : 1,
  304. //分页数据
  305. data : [],
  306. //分页所属的key
  307. key : '',
  308. //分页状态 true可以分页,false不能分页
  309. state : true,
  310. //通用的获取数据方法:瀑布流分页 (1,'level')
  311. get : function(config, vue, url, options, callback, err_callback) {
  312. options = http.option(options);
  313. if (typeof(config) == 'object') {
  314. var state = config[0];
  315. this.key = config[1];
  316. } else {
  317. var state = config;
  318. this.key = '';
  319. }
  320. if (state == 1) {
  321. this.value = 1;
  322. this.data = [];
  323. } else {
  324. this.value++;
  325. }
  326. if (this.state == false && state != 1) {
  327. return;
  328. }
  329. options.page = this.value;
  330. http.request(vue, url, options, callback, err_callback);
  331. },
  332. }
  333. var upload = {
  334. data : {},
  335. formatDate : function(date) {
  336. var date = new Date(date);
  337. var YY = date.getFullYear() + '-';
  338. var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  339. var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  340. var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  341. var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  342. var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  343. return YY + MM + DD +" "+hh + mm + ss;
  344. },
  345. handle : function(key, count, callback) {
  346. var self = this;
  347. count = parseInt(count);
  348. uni.chooseImage({
  349. count: count,
  350. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  351. sourceType: ['album'], //从相册选择
  352. success: res => {
  353. uni.showLoading();
  354. res.tempFilePaths.forEach(function(item, index) {
  355. //请求上传接口
  356. self.uploadFile(item, key, count, callback);
  357. });
  358. },
  359. fail: () => {
  360. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  361. uni.hideLoading();
  362. }
  363. });
  364. },
  365. uploadFile : function(item, key, count, callback) {
  366. var self = this;
  367. if (!count) {
  368. count = 1;
  369. }
  370. var type = 1;
  371. if (count > 1) {
  372. type = 2;
  373. }
  374. uni.uploadFile({
  375. url: config.upload.url,
  376. filePath: item,
  377. name: config.upload.name,
  378. formData: { token: http.getToken() },
  379. success: res => {
  380. dever.debug(res);
  381. var data = JSON.parse(res.data);
  382. uni.hideLoading();
  383. if (data.uploaded) {
  384. var backUrl = data.url;
  385. if (count > 1) {
  386. if (!self.data[key]) {
  387. self.data[key] = [];
  388. }
  389. if (self.data[key].length < count) {
  390. self.data[key].push(backUrl);
  391. }
  392. } else {
  393. self.data[key] = backUrl;
  394. }
  395. if (callback) {
  396. callback(type, self.data[key]);
  397. }
  398. } else if (data.uploaded == false) {
  399. uni.showToast({ title: data.error.message, icon: 'none' });
  400. }
  401. }
  402. });
  403. },
  404. upfile : function(e, key, count, callback, ext, title, style) {
  405. if (!count) {
  406. count = 1;
  407. }
  408. var type = 1;
  409. if (count > 1) {
  410. type = 2;
  411. }
  412. if (!title) {
  413. title = '点击这里上传文件';
  414. }
  415. var self = this;
  416. count = parseInt(count);
  417. var a = document.createElement('button');
  418. a.className = 'file';
  419. if (style) {
  420. a.style.cssText = style;
  421. } else {
  422. 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;';
  423. }
  424. var style = 'position: absolute;opacity: 0;filter: alpha(opacity=0);cursor: pointer';
  425. a.innerHTML = '<input style="'+style+'" type="file" name="upload_file" id="upload_file" />' + title;
  426. e.$refs.input.$el.appendChild(a)
  427. var input = document.getElementById('upload_file');
  428. input.onchange = (event) => {
  429. uni.showLoading();
  430. var reader = new FileReader();
  431. var type = input.files[0].type;
  432. var temp = type.split('/');
  433. type = temp[1];
  434. //console.info(input.files[0]);
  435. if (ext && ext.indexOf(type) == -1) {
  436. uni.showToast({ title: '您上传的文件类型错误,请上传带有' + ext + '这些后缀的文件', icon: 'none' });
  437. } else {
  438. reader.readAsDataURL(input.files[0]);
  439. reader.onload = function(){
  440. //读取完成后,数据保存在对象的result属性中
  441. var pic = this.result;
  442. var temp = pic.split('base64,');
  443. pic = temp[1];
  444. dever.debug(pic);
  445. var url = "http://up.qiniu.com/putb64/-1";
  446. var xhr = new XMLHttpRequest();
  447. xhr.onreadystatechange=function(){
  448. if (xhr.readyState==4){
  449. dever.debug(xhr.responseText);
  450. var data = JSON.parse(xhr.responseText);
  451. uni.hideLoading();
  452. if (data.uploaded) {
  453. var backUrl = data.url;
  454. if (count > 1) {
  455. if (!self.data[key]) {
  456. self.data[key] = [];
  457. }
  458. self.data[key].push(backUrl);
  459. } else {
  460. self.data[key] = backUrl;
  461. }
  462. if (callback) {
  463. callback(type, self.data[key], input.files[0]);
  464. }
  465. } else if (data.uploaded == false) {
  466. uni.showToast({ title: data.error.message, icon: 'none' });
  467. }
  468. }
  469. }
  470. xhr.open("POST", url, true);
  471. xhr.setRequestHeader("Content-Type", "application/octet-stream");
  472. xhr.setRequestHeader("Authorization", "UpToken " + e.token);
  473. xhr.send(pic);
  474. }
  475. }
  476. }
  477. },
  478. qnUpload : function(qn, config, key, count, callback) {
  479. if (!count) {
  480. count = 1;
  481. }
  482. var type = 1;
  483. if (count > 1) {
  484. type = 2;
  485. }
  486. var self = this;
  487. count = parseInt(count);
  488. var call = function(data) {
  489. var backUrl = data.url;
  490. if (count > 1) {
  491. if (!self.data[key]) {
  492. self.data[key] = [];
  493. }
  494. self.data[key].push(backUrl);
  495. } else {
  496. self.data[key] = backUrl;
  497. }
  498. if (callback) {
  499. callback(type, self.data[key]);
  500. }
  501. }
  502. uni.chooseImage({
  503. count: count,
  504. //sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  505. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  506. sourceType: ['album'], //从相册选择
  507. success: res => {
  508. uni.showLoading();
  509. res.tempFilePaths.forEach(function(item, index) {
  510. self.qiniu(qn, item, config.domain, config.token, call);
  511. });
  512. },
  513. fail: () => {
  514. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  515. uni.hideLoading();
  516. }
  517. });
  518. },
  519. qiniu : function(qn, file, domain, token, call, handle) {
  520. //请求上传接口
  521. var config = {};
  522. config = {
  523. region: 'ECN',
  524. domain: domain,
  525. //key: 'test.mp4',
  526. };
  527. if (token.indexOf('http') == -1) {
  528. config.uptoken = token;
  529. } else {
  530. config.uptokenURL = token;
  531. }
  532. uni.showLoading({title: '上传中', mask: true});
  533. qn.upload(
  534. file,
  535. res => {
  536. dever.debug(res);
  537. var data = res;
  538. uni.hideLoading();
  539. if (data.uploaded) {
  540. call(data);
  541. } else {
  542. uni.showToast({title: data.error.message, icon: 'none' });
  543. }
  544. },
  545. error => {
  546. uni.showToast({title: '上传失败,请重试', icon: 'none' });
  547. uni.hideLoading();
  548. },
  549. config,
  550. res => {
  551. //上传进度
  552. if (handle) {
  553. handle(res.progress);
  554. } else {
  555. uni.showToast({title: '上传进度:%' + res.progress, icon: 'none' });
  556. }
  557. }
  558. );
  559. },
  560. uploadDel : function(key, index, callback) {
  561. var self = this;
  562. uni.showModal({
  563. content: '确定要删除吗?',
  564. cancelText: '取消',
  565. confirmText: '确定',
  566. success: res => {
  567. if (res.confirm) {
  568. // self.data[key].splice(index, 1);
  569. if (callback) {
  570. callback(res, index);
  571. }
  572. }
  573. }
  574. });
  575. }
  576. }
  577. var share =
  578. {
  579. wechat : false,
  580. url : '',
  581. project : '',
  582. token : '',
  583. param : {
  584. title : '',
  585. pic : '',
  586. content : '',
  587. link : '',
  588. },
  589. wx : {},
  590. init : function(wx, project, host, param)
  591. {
  592. var self = this;
  593. self.wx = wx;
  594. self.wechat = dever.is_weixin();
  595. self.project = project;
  596. self.url = host;
  597. self.token = http.getToken();
  598. self.param = param;
  599. self.param.timelineCallback = function() {};
  600. self.param.sendMessageCallback = function() {};
  601. self.param.shareQQCallback = function() {};
  602. if (self.wechat) {
  603. self.wechatInit();
  604. }
  605. self.reflux();
  606. }
  607. ,wechatInit: function()
  608. {
  609. var self = this;
  610. var url = this.url + 'init?callback=?';
  611. var param = this.param;
  612. var project = this.project;
  613. var options = {};
  614. options.project = project;
  615. options.url = location.href.split('#')[0];
  616. options.noloading = 1;
  617. options.token = this.token;
  618. http.request('post', url, options, function(result) {
  619. var data = result.data;
  620. self.wx.config({
  621. //debug:true,
  622. appId: data.appId,
  623. timestamp: data.timestamp,
  624. nonceStr: data.nonceStr,
  625. signature: data.signature,
  626. jsApiList: [
  627. 'onMenuShareTimeline',
  628. 'onMenuShareAppMessage',
  629. 'onMenuShareQQ'
  630. ]
  631. });
  632. self.wx.ready(function() {
  633. self.wx.onMenuShareAppMessage({
  634. title: param.title,
  635. desc: param.content,
  636. link: param.link,
  637. imgUrl: param.pic,
  638. success: function(res) {
  639. param.sendMessageCallback('success', res)
  640. self.shareLog(1, 1);
  641. },
  642. cancel: function(res) {
  643. param.sendMessageCallback('cancel', res)
  644. self.shareLog(1, 2);
  645. },
  646. fail: function(res) {
  647. param.sendMessageCallback('fail', res)
  648. self.shareLog(1, 3);
  649. }
  650. });
  651. self.wx.onMenuShareTimeline({
  652. title: param.title,
  653. link: param.url,
  654. imgUrl: param.img,
  655. success: function(res) {
  656. param.timelineCallback('success', res)
  657. self.shareLog(2, 1);
  658. },
  659. cancel: function(res) {
  660. param.timelineCallback('cancel', res)
  661. self.shareLog(2, 2);
  662. },
  663. fail: function(res) {
  664. param.timelineCallback('fail', res)
  665. self.shareLog(2, 3);
  666. }
  667. });
  668. self.wx.onMenuShareQQ({
  669. title: param.title,
  670. link: param.url,
  671. imgUrl: param.img,
  672. success: function(res) {
  673. param.shareQQCallback('success', res)
  674. self.shareLog(3, 1);
  675. },
  676. cancel: function(res) {
  677. param.shareQQCallback('cancel', res)
  678. self.shareLog(3, 2);
  679. },
  680. fail: function(res) {
  681. param.shareQQCallback('fail', res)
  682. self.shareLog(3, 3);
  683. }
  684. });
  685. });
  686. });
  687. }
  688. ,reflux: function() {
  689. var refer = document.referrer,
  690. url = encodeURIComponent(document.location.href),
  691. param = location.search.substr(1),
  692. ua = encodeURIComponent(navigator.userAgent),
  693. project = this.project,
  694. token = this.token,
  695. wechat = this.wechat;
  696. if (param.indexOf("tsina-") > -1 || param.indexOf("timeline") > -1 || param.indexOf("singlemessage") > -1 || param.indexOf("groupmessage") > -1) {
  697. var url = this.url + 'reflux?callback=?' + '&project='+project+'&url=' + url + '&ua=' + ua + '&param=' + encodeURIComponent(param) + '&token=' + token + '&type=' + wechat;
  698. http.request('post', url, {noloading:1});
  699. }
  700. }
  701. ,shareLog: function(actType, actResult) {
  702. var project = this.project;
  703. var token = this.token;
  704. var wechat = this.wechat;
  705. var ua = encodeURIComponent(navigator.userAgent);
  706. var url = encodeURIComponent(document.location.href);
  707. var url = this.url + 'collect?callback=?' + '&project='+project+'&url=' + url + '&actType=' + actType + '&actResult=' + actResult + '&ua=' + ua + '&token=' + token + '&type=' + wechat
  708. http.request('post', url, {noloading:1});
  709. }
  710. }
  711. var dever = {
  712. login : 'user/login',
  713. web_view : '/lib/dever/pages/web_view',
  714. source : 'h5',
  715. source_id : 1,
  716. host : '',
  717. doc : false,
  718. config : {},
  719. vue : false,
  720. cur : false,
  721. getLoadingState : {},
  722. getLoadingCall : false,
  723. postLoadingCall : false,
  724. pageLoadingCall : false,
  725. pageData : {},
  726. lastGetUrl : '',
  727. showLoad : false,
  728. btnText : '确定提交',
  729. switchTab : [],
  730. switchTabCall : {},
  731. slideValue : {},
  732. //获取当前时间戳
  733. curTime : function() {
  734. var time = Date.parse(new Date())/1000;
  735. return time;
  736. },
  737. //载入全局配置
  738. init : function(url, vue, value, set) {
  739. this.vue = vue;
  740. vue.prototype.$config = value;
  741. var self = this;
  742. var config = self.data('dever_config');
  743. var state = false;
  744. if (config && !set) {
  745. self.config = config;
  746. vue.prototype.$config = self.config;
  747. if (self.config.update_time) {
  748. var time = this.curTime();
  749. if (!self.config.update_time || time - self.config.update_time >= value.set_update) {
  750. state = true;
  751. }
  752. } else {
  753. state = false;
  754. }
  755. } else {
  756. state = true;
  757. }
  758. if (state) {
  759. this.setConfig(url, value);
  760. }
  761. return this;
  762. },
  763. //设置全局变量
  764. setConfig : function(url, config, key, value) {
  765. var self = this;
  766. if (!config && key && value) {
  767. config = self.data('dever_config');
  768. config[key] = value;
  769. }
  770. self.config = config;
  771. self.vue.prototype.$config = self.config;
  772. self.data('dever_config', self.config);
  773. if (url) {
  774. self.post(url, {noloading:1}, function(t) {
  775. self.config = t;
  776. self.config.update_time = self.curTime();
  777. self.data('dever_config', self.config);
  778. self.vue.prototype.$config = self.config;
  779. });
  780. }
  781. },
  782. //加载通用的方法
  783. common : function(func) {
  784. this.source = 'h5';
  785. this.source_id = 1;
  786. this.host = '';
  787. //#ifdef H5
  788. this.source = 'h5';
  789. this.source_id = 1;
  790. this.host = window.location.protocol + '//' + window.location.host + '/#';
  791. //#endif
  792. //#ifdef APP-PLUS
  793. this.source = uni.getSystemInfoSync().platform;
  794. if (this.source == 'ios') {
  795. this.source_id = 3;
  796. } else {
  797. this.source_id = 2;
  798. }
  799. //#endif
  800. //#ifdef MP-WEIXIN
  801. this.source = 'wx_applet';
  802. this.source_id = 5;
  803. //#endif
  804. if (func) {
  805. for (var i in func) {
  806. this.vue.prototype[i] = func[i];
  807. }
  808. }
  809. },
  810. //设置提交的按钮
  811. btnLoad : function(cur) {
  812. if (!cur) {
  813. cur = this.cur;
  814. }
  815. if (!cur) {
  816. return;
  817. }
  818. cur.btn = this.btn(cur);
  819. },
  820. //设置提交的按钮
  821. btnFinish : function() {
  822. var cur = this.cur;
  823. if (!cur) {
  824. return;
  825. }
  826. cur.btn = {
  827. disabled:false,
  828. loading:false,
  829. text:this.btnText,
  830. };
  831. },
  832. //设置提交的按钮
  833. btn : function(cur, text) {
  834. this.cur = cur;
  835. if (text) {
  836. this.btnText = text;
  837. }
  838. return {
  839. disabled:true,
  840. loading:true,
  841. text:'加载中...',
  842. };
  843. },
  844. //设置loading
  845. setGetLoading : function(yes, no) {
  846. this.getLoadingCall = {'yes': yes, 'no' : no};
  847. },
  848. setPostLoading : function(yes, no) {
  849. this.postLoadingCall = {'yes': yes, 'no' : no};
  850. },
  851. setPageLoading : function(yes, no) {
  852. this.pageLoadingCall = {'yes': yes, 'no' : no};
  853. },
  854. //获取当前route
  855. route : function() {
  856. var page = this.getPage();
  857. if (!page) {
  858. return '';
  859. }
  860. var route = page.route;
  861. return route;
  862. },
  863. //获取当前param
  864. param : function() {
  865. var page = this.getPage();
  866. if (!page) {
  867. return '';
  868. }
  869. var options = page.options;
  870. return options;
  871. },
  872. //获取当前url
  873. url : function() {
  874. var page = this.getPage();
  875. if (!page) {
  876. return '';
  877. }
  878. var route = page.route;
  879. var options = page.options;
  880. // 拼接参数
  881. let param = []
  882. if (options) {
  883. for (let key in options) {
  884. param.push(key + '=' + options[key]);
  885. }
  886. }
  887. param = param.join('&');
  888. if (param) {
  889. param = '?' + param;
  890. }
  891. return '/' + route + param;
  892. },
  893. //获取当前page
  894. getPage : function() {
  895. var pages = getCurrentPages();
  896. if (pages.length > 0) {
  897. return pages[pages.length - 1];
  898. } else {
  899. return false;
  900. }
  901. },
  902. //loading
  903. loading : function(method, options, url) {
  904. var state = false;
  905. if (method == 'POST') {
  906. if (this.postLoadingCall && this.postLoadingCall.yes) {
  907. state = true;
  908. var callback = this.postLoadingCall.yes;
  909. callback(this);
  910. }
  911. } else if (method == 'page') {
  912. //state = true;
  913. if (options.page && options.page > 1) {
  914. state = false;
  915. }
  916. if (this.pageLoadingCall && this.pageLoadingCall.yes) {
  917. state = true;
  918. var callback = this.pageLoadingCall.yes;
  919. callback(this);
  920. }
  921. } else {
  922. url = this.route();
  923. if (this.lastGetUrl && this.lastGetUrl != url && this.getLoadingState[this.lastGetUrl]) {
  924. this.getLoadingState[this.lastGetUrl] = false;
  925. }
  926. if (this.getLoadingState && this.getLoadingState[url]) {
  927. state = false;
  928. } else {
  929. if (this.getLoadingCall && this.getLoadingCall.yes) {
  930. state = true;
  931. var callback = this.getLoadingCall.yes;
  932. callback(this);
  933. }
  934. }
  935. }
  936. if (!state) {
  937. var title = '加载中';
  938. if (method == 'POST') {
  939. title = '提交中';
  940. }
  941. uni.showLoading({title: title, mask: true});
  942. //uni.showNavigationBarLoading();
  943. this.showLoad = true;
  944. }
  945. uni.stopPullDownRefresh();
  946. },
  947. //取消loading
  948. hideLoading : function(method, options, url) {
  949. var state = false;
  950. if (method == 'POST' || method == 'post') {
  951. if (this.postLoadingCall && this.postLoadingCall.no) {
  952. state = true;
  953. var callback = this.postLoadingCall.no;
  954. callback(this);
  955. }
  956. } else if (method == 'page') {
  957. //state = true;
  958. if (options.page && options.page > 1) {
  959. state = false;
  960. }
  961. if (this.pageLoadingCall && this.pageLoadingCall.no) {
  962. state = true;
  963. var callback = this.pageLoadingCall.no;
  964. callback(this);
  965. }
  966. } else {
  967. url = this.route();
  968. if (!this.lastGetUrl || (this.lastGetUrl && this.lastGetUrl != url)) {
  969. this.lastGetUrl = url;
  970. }
  971. if (this.getLoadingState && this.getLoadingState[url]) {
  972. state = false;
  973. } else {
  974. this.getLoadingState[url] = true;
  975. if (this.getLoadingCall && this.getLoadingCall.no) {
  976. state = true;
  977. var callback = this.getLoadingCall.no;
  978. callback(this);
  979. }
  980. }
  981. }
  982. if (!state && this.showLoad) {
  983. uni.hideLoading();
  984. this.showLoad = false;
  985. this.btnFinish();
  986. //uni.hideNavigationBarLoading();
  987. }
  988. },
  989. //data数据获取
  990. dataset : function(e) {
  991. if (e.currentTarget) {
  992. var dataset = e.currentTarget.dataset;
  993. } else {
  994. var dataset = e.target.dataset;
  995. }
  996. return dataset;
  997. },
  998. //view中的页面提示
  999. viewAlert : function(e) {
  1000. if (e) {
  1001. var dataset = this.dataset(e);
  1002. var msg = dataset.msg;
  1003. var icon = dataset.icon;
  1004. var callback = dataset.callback;
  1005. this.alert(msg, icon, callback);
  1006. } else {
  1007. this.alert('敬请期待');
  1008. }
  1009. },
  1010. //view中的页面跳转
  1011. viewLocation : function(e) {
  1012. var page = '';
  1013. var to = '';
  1014. var option = '';
  1015. if (e) {
  1016. var dataset = this.dataset(e);
  1017. page = dataset.page;
  1018. to = dataset.to;
  1019. option = []
  1020. for (var i in dataset) {
  1021. if (i.indexOf('save_') != -1) {
  1022. var v = dataset[i];
  1023. i = i.replace('save_', '');
  1024. this.data(i, v);
  1025. } else if (i != 'page' && i != 'to') {
  1026. option.push(i + '=' + dataset[i]);
  1027. }
  1028. }
  1029. if (option) {
  1030. option = option.join('&');
  1031. }
  1032. }
  1033. if (!page) {
  1034. uni.navigateBack({});
  1035. } else {
  1036. var go = '';
  1037. if (to) {
  1038. go = 'to';
  1039. }
  1040. if (option) {
  1041. page = page + '?' + option;
  1042. }
  1043. this.location(page, go);
  1044. }
  1045. },
  1046. //页面跳转
  1047. location : function(path, go) {
  1048. var self = this;
  1049. if (path.indexOf('http') > -1) {
  1050. this.debug('http:' + path);
  1051. this.data('web_view', path);
  1052. if (this.source == 'app' || this.source == 'applet' || go == 'webview') {
  1053. uni.navigateTo({
  1054. url: this.web_view
  1055. })
  1056. } else {
  1057. if (!go) {
  1058. go = '';
  1059. }
  1060. if (go && go == 'location') {
  1061. window.location.href = path;
  1062. } else {
  1063. var url = window.location.href;
  1064. if (go.indexOf('/') != -1) {
  1065. url = this.host + go
  1066. } else {
  1067. url = url + go;
  1068. }
  1069. url = window.btoa(url);
  1070. if (path.indexOf('?') == -1) {
  1071. path = path + '?';
  1072. } else {
  1073. path = path + '&';
  1074. }
  1075. window.location.href = path + 'refer=' + url;
  1076. }
  1077. }
  1078. } else {
  1079. this.debug('navigateTo:' + go + ':' + path);
  1080. if (path.indexOf('/pages/') == -1) {
  1081. path = '/pages/' + path;
  1082. }
  1083. if (this.switchTab.indexOf(path) != -1) {
  1084. uni.switchTab({
  1085. url: path,
  1086. success: function(e) {
  1087. if (self.source != 'app' && self.switchTabCall[path]) {
  1088. var call = self.switchTabCall[path];
  1089. call(self, e);
  1090. }
  1091. }
  1092. })
  1093. } else if (go) {
  1094. uni.redirectTo({
  1095. url: path
  1096. })
  1097. } else {
  1098. uni.navigateTo({
  1099. url: path
  1100. })
  1101. }
  1102. }
  1103. },
  1104. //提示信息
  1105. alert : function(info, icon, callback) {
  1106. if (!icon) {
  1107. icon = 'none'
  1108. }
  1109. if (info) {
  1110. uni.showToast({title: info, icon: icon});
  1111. }
  1112. this.debug('alert:' + info)
  1113. if (callback) {
  1114. this.debug('callback:' + callback)
  1115. setTimeout(function(){
  1116. callback();
  1117. }, 1000)
  1118. }
  1119. },
  1120. //询问信息
  1121. confirm : function(text, callback) {
  1122. uni.showModal({
  1123. content: text,
  1124. cancelText: '取消',
  1125. confirmText: '确定',
  1126. success: res => {
  1127. if (res.confirm) {
  1128. callback();
  1129. }
  1130. }
  1131. });
  1132. },
  1133. //debug调试
  1134. debug : function(string) {
  1135. if (config.base.debug) {
  1136. this.log(string);
  1137. }
  1138. },
  1139. //记录日志
  1140. log : function(string) {
  1141. console.log(string);
  1142. },
  1143. //获取token
  1144. getToken: function() {
  1145. return http.getToken();
  1146. },
  1147. //设置token
  1148. setToken: function(value) {
  1149. return http.setToken(value);
  1150. },
  1151. //通用的提交数据的方法
  1152. post : function(url, options, callback, err_callback) {
  1153. http.request('post', url, options, callback, err_callback);
  1154. },
  1155. //通用的获取数据方法
  1156. get : function(vue, url, options, callback, err_callback) {
  1157. http.request(vue, url, options, callback, err_callback);
  1158. },
  1159. //通用的获取数据方法
  1160. page : function(config, vue, url, options, callback, err_callback) {
  1161. page.get(config, vue, url, options, callback, err_callback);
  1162. },
  1163. //选择图片并上传
  1164. upload : function(id, count, callback) {
  1165. this.getUpload().handle(id, count, callback);
  1166. },
  1167. //上传到七牛
  1168. qiniu : function(qn, self, id, count, callback, handle) {
  1169. this.getUpload().qiniu(qn, self, id, count, callback, handle);
  1170. },
  1171. //html上传文件
  1172. upfile : function(self, key, count, callback, ext, title, style) {
  1173. this.getUpload().upfile(self, key, count, callback, ext, title, style);
  1174. },
  1175. //直接上传文件
  1176. uploadFile : function(path, id, count, callback) {
  1177. this.getUpload().uploadFile(path, id, count, callback);
  1178. },
  1179. //获取upload
  1180. getUpload : function() {
  1181. return upload;
  1182. },
  1183. //im聊天
  1184. im : function(key, url, callback, connect) {
  1185. im.init(key, url, callback, connect);
  1186. },
  1187. //im聊天关闭
  1188. im_close : function(key) {
  1189. im.close(key);
  1190. },
  1191. //im聊天 get请求
  1192. im_get : function(vue, url, options, callback, err_callback) {
  1193. im.get(vue, url, options, callback, err_callback);
  1194. },
  1195. //im聊天 post请求
  1196. im_post : function(url, options, callback, err_callback) {
  1197. im.post(url, options, callback, err_callback);
  1198. },
  1199. //删除上传
  1200. uploadDel : function(id, index, callback) {
  1201. upload.uploadDel(id, index, callback);
  1202. },
  1203. //验证登录
  1204. checkLogin : function(refer) {
  1205. if (!this.getToken()) {
  1206. this.data('login_refer', this.getRefer(refer));
  1207. //this.hideLoading();
  1208. if (this.source == 'h5') {
  1209. this.location(this.login, 'go');
  1210. } else {
  1211. this.location(this.login, 'go');
  1212. }
  1213. }
  1214. },
  1215. //获取refer
  1216. getRefer : function(refer) {
  1217. if (!refer) {
  1218. refer = this.url();
  1219. }
  1220. return refer;
  1221. },
  1222. //数据存储
  1223. data : function(key, value) {
  1224. if (value) {
  1225. if (value == 'del') {
  1226. uni.removeStorageSync(key);
  1227. return true;
  1228. } else {
  1229. uni.setStorageSync(key, value);
  1230. return value;
  1231. }
  1232. } else {
  1233. return uni.getStorageSync(key);
  1234. }
  1235. },
  1236. //插入html代码,初始化
  1237. initHtml : function(doc) {
  1238. this.doc = doc.$refs.initHtml.$el;
  1239. },
  1240. //插入html代码
  1241. html : function(html) {
  1242. var self = this;
  1243. var div = document.createElement('div');
  1244. div.innerHTML = html;
  1245. this.doc.appendChild(div);
  1246. var scripts = div.querySelectorAll('script');
  1247. return Array.prototype.slice.apply(scripts).reduce((chain, script) => {
  1248. return chain.then(() => self.runScript(script));
  1249. }, Promise.resolve());
  1250. },
  1251. //微信提醒
  1252. wxTip : function(type) {
  1253. var wx = this.is_weixin();
  1254. if (wx) {
  1255. var img = 'live_weixin';
  1256. if (type) {
  1257. img = 'weixin-guide';
  1258. }
  1259. 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>';
  1260. this.html(tip);
  1261. return true;
  1262. }
  1263. return false;
  1264. },
  1265. is_weixin: function() {
  1266. if (this.source != 'h5') {
  1267. return false;
  1268. }
  1269. var ua = navigator.userAgent.toLowerCase();
  1270. if(ua.match(/MicroMessenger/i)=="micromessenger") {
  1271. return true;
  1272. } else {
  1273. return false;
  1274. }
  1275. },
  1276. //执行script代码
  1277. runScript : function(script) {
  1278. return new Promise((reslove, rejected) => {
  1279. const newScript = document.createElement('script');
  1280. newScript.innerHTML = script.innerHTML;
  1281. const src = script.getAttribute('src');
  1282. if (src) newScript.setAttribute('src', src);
  1283. // script 加载完成和错误处理
  1284. newScript.onload = () => reslove();
  1285. newScript.onerror = err => rejected();
  1286. document.head.appendChild(newScript);
  1287. document.head.removeChild(newScript);
  1288. if (!src) {
  1289. // 如果是 inline script 执行是同步的
  1290. reslove();
  1291. }
  1292. })
  1293. },
  1294. //拉起支付
  1295. pay : function(result, location, error) {
  1296. var self = this;
  1297. if (!error) {
  1298. if (location.indexOf('?') == -1) {
  1299. var fix = '?';
  1300. } else {
  1301. var fix = '&';
  1302. }
  1303. location = location + fix + 'order_id=' + result.order_id;
  1304. }
  1305. if (result.type == 'test') {
  1306. self.alert('支付成功','none', function() {
  1307. self.location(location, 'to');
  1308. })
  1309. } else if (self.source == 'h5') {
  1310. uni.hideLoading();
  1311. uni.showLoading({title: '跳转中', mask: true});
  1312. self.html(result.order);
  1313. } else if (self.source == 'app') {
  1314. self.appPayment(result.type, result.order, function() {
  1315. self.location(location, 'to');
  1316. }, function() {
  1317. if (!error) {
  1318. self.location(location + '&error=1', 'to');
  1319. } else {
  1320. self.alert(error);
  1321. }
  1322. });
  1323. }
  1324. },
  1325. //app支付
  1326. appPayment : function(type, order, callback, errorCallback) {
  1327. uni.requestPayment({
  1328. provider: type,
  1329. orderInfo: order, //微信、支付宝订单数据
  1330. success: function (res) {
  1331. //console.log('success:' + JSON.stringify(res));
  1332. callback(res);
  1333. },
  1334. fail: function (err) {
  1335. //console.log('fail:' + JSON.stringify(err));
  1336. errorCallback(err)
  1337. }
  1338. });
  1339. },
  1340. //跳转到refer
  1341. jump : function() {
  1342. var refer = this.data('login_refer');
  1343. var id = this.data('invite_id');
  1344. var type = this.data('invite_type');
  1345. if (id && refer != 'index/index') {
  1346. refer = refer + '?id=' + id + '&type=' + type;
  1347. }
  1348. this.data('login_refer', 'del');
  1349. this.data('invite_id', 'del');
  1350. this.data('invite_type', 'del');
  1351. if (!refer) {
  1352. refer = 'index/index';
  1353. }
  1354. this.location(refer, 'go');
  1355. },
  1356. //转星号
  1357. xing : function(s) {
  1358. return s.replace(s, function(sMatch){
  1359. return sMatch.replace(/./g,"*");
  1360. });
  1361. },
  1362. //检查更新
  1363. checkUpdate : function() {
  1364. this.update();
  1365. },
  1366. update : function(path, call) {
  1367. var self = this;
  1368. var source = this.source;
  1369. if (source != 'app') {
  1370. return;
  1371. }
  1372. var type = plus.os.name.toLowerCase();
  1373. //var type = 'android';
  1374. //var config = this.config.version;
  1375. self.post('isUpdate', {t:1}, function(res) {
  1376. if (type == 'android') {
  1377. res.downloadUrl = res.androidUrl;
  1378. } else {
  1379. res.downloadUrl = res.iosUrl;
  1380. }
  1381. self.data('dever_update_link', res.downloadUrl);
  1382. plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
  1383. var version = widgetInfo.versionCode;
  1384. var state = false;
  1385. state = widgetInfo.versionCode < res.versionCode;
  1386. if (state) {
  1387. //强制更新
  1388. var txt = '正在为您下载更新,下载完成将重启应用';
  1389. if (res.forceUpdate == true) {
  1390. self.down(res, res.downloadUrl, txt, type);
  1391. } else if(call) {
  1392. call(res, txt, type);
  1393. } else {
  1394. uni.showModal({
  1395. title: '发现新版本',
  1396. content: '有新版本可用 (版本号:' + res.versionName + '),请问您是否更新?',
  1397. success: (t) => {
  1398. if (t.confirm) {
  1399. if (path) {
  1400. self.location(path);
  1401. } else {
  1402. self.down(res, res.downloadUrl, txt, type);
  1403. }
  1404. }
  1405. }
  1406. })
  1407. }
  1408. }
  1409. });
  1410. });
  1411. },
  1412. uploadCall : function(config, type, packgePath) {
  1413. // 保存更新记录到stroage,下次启动app时安装更新
  1414. var self = this;
  1415. self.data('dever_update', 1);
  1416. self.data('dever_update_down', packgePath);
  1417. // 任务完成,关闭下载任务,开始安装
  1418. plus.runtime.install(packgePath, {force: true}, function() {
  1419. self.data('dever_update', 2);
  1420. self.data('dever_update_down', 'del');
  1421. uni.showModal({
  1422. title: '提示',
  1423. content: '应用将重启以完成更新',
  1424. showCancel: false,
  1425. complete: () => {
  1426. plus.runtime.restart();
  1427. }
  1428. })
  1429. });
  1430. },
  1431. down : function(config, url, txt, type, call) {
  1432. if (!url) {
  1433. return false;
  1434. }
  1435. if (txt) {
  1436. this.alert(txt);
  1437. }
  1438. if (type != 'android' && !url.match(RegExp(/.wgt/))) {
  1439. plus.runtime.openURL(url);
  1440. return false;
  1441. }
  1442. var self = this;
  1443. var packgePath = self.data('dever_update_down');
  1444. if (packgePath) {
  1445. self.uploadCall(config, type, packgePath);
  1446. return false;
  1447. }
  1448. var downloadTask = uni.downloadFile({
  1449. url: url,
  1450. success: (res) => {
  1451. if (res.statusCode === 200) {
  1452. // 保存下载的安装包
  1453. uni.saveFile({
  1454. tempFilePath: res.tempFilePath,
  1455. success: (res) => {
  1456. var packgePath = res.savedFilePath;
  1457. if (call) {
  1458. call();
  1459. }
  1460. self.uploadCall(config, type, packgePath);
  1461. downloadTask.abort();
  1462. downloadTask = null;
  1463. }
  1464. })
  1465. }
  1466. }
  1467. });
  1468. return downloadTask;
  1469. },
  1470. //预览图片
  1471. viewPic : function(imgs, img, key) {
  1472. if (imgs && imgs.length > 0) {
  1473. if (key) {
  1474. var temp = [];
  1475. var i;
  1476. for(i in imgs) {
  1477. temp.push(imgs[i][key]);
  1478. }
  1479. imgs = temp;
  1480. }
  1481. // 检测图片是否原图
  1482. if (img.indexOf('?') != -1) {
  1483. var t = img.split('?');
  1484. img = t[0];
  1485. for (i in imgs) {
  1486. t = imgs[i].split('?');
  1487. imgs[i] = t[0];
  1488. }
  1489. } else if (img.indexOf('_t') != -1) {
  1490. var t = img.split('.');
  1491. var ext = t[1];
  1492. t = img.split('_t');
  1493. img = t[0] + '.' + ext;
  1494. for (i in imgs) {
  1495. t = imgs[i].split('_t');
  1496. imgs[i] = t[0] + '.' + ext;
  1497. }
  1498. }
  1499. uni.previewImage({
  1500. current:img,
  1501. urls: imgs,
  1502. indicator : 'default',
  1503. loop : true,
  1504. /*
  1505. longPressActions : {
  1506. itemList: ['发送给朋友', '保存图片', '收藏'],
  1507. success: function(data) {
  1508. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  1509. },
  1510. fail: function(err) {
  1511. console.log(err.errMsg);
  1512. }
  1513. }
  1514. */
  1515. });
  1516. }
  1517. },
  1518. //截取APP退出功能
  1519. quit : function(page, call) {
  1520. var self = this;
  1521. if (self.source == 'app') {
  1522. var main = plus.android.runtimeMainActivity();
  1523. //为了防止快速点按返回键导致程序退出重写quit方法改为隐藏至后台
  1524. /*
  1525. plus.runtime.quit = function(){
  1526. main.moveTaskToBack(false);
  1527. };
  1528. */
  1529. //重写toast方法如果内容为 ‘再按一次退出应用’ 就隐藏应用,其他正常toast
  1530. plus.nativeUI.toast = (function(str){
  1531. if(str == '再按一次退出应用') {
  1532. if (call) {
  1533. call(main);
  1534. } else {
  1535. var webview = page.$mp.page.$getAppWebview();
  1536. var child = webview.children();
  1537. child[0].back();
  1538. }
  1539. return false;
  1540. }else{
  1541. self.alert(str);
  1542. }
  1543. });
  1544. }
  1545. },
  1546. //滑动
  1547. slide : function(e) {
  1548. this.slideValue = {};
  1549. if (e.changedTouches[0].clientX) {
  1550. this.slideValue.clientX = e.changedTouches[0].clientX;
  1551. this.slideValue.clientY = e.changedTouches[0].clientY;
  1552. } else {
  1553. this.slideValue.clientX = e.changedTouches[0].screenX;
  1554. this.slideValue.clientY = e.changedTouches[0].screenY;
  1555. }
  1556. },
  1557. //滑动结束
  1558. slideEnd : function(e) {
  1559. if (e.changedTouches[0].clientX) {
  1560. var subX = e.changedTouches[0].clientX - this.slideValue.clientX;
  1561. var subY = e.changedTouches[0].clientY - this.slideValue.clientY;
  1562. } else {
  1563. var subX = e.changedTouches[0].screenX - this.slideValue.clientX;
  1564. var subY = e.changedTouches[0].screenY - this.slideValue.clientY;
  1565. }
  1566. if (subY > 20) {
  1567. //上滑
  1568. return 1;
  1569. } else if (subY < -20) {
  1570. //下滑
  1571. return 2;
  1572. } else if (subX > 20) {
  1573. //左滑
  1574. return 3;
  1575. } else if(subX < -20) {
  1576. //右滑
  1577. return 4;
  1578. }
  1579. return
  1580. },
  1581. //计算图片宽高比
  1582. getImage : function(imgWidth, imgHeight, containerWidth, containerHeight) {
  1583. let [
  1584. // 用于设定图片的宽和高
  1585. tempWidth,
  1586. tempHeight,
  1587. ] = [
  1588. undefined,
  1589. undefined
  1590. ]
  1591. try {
  1592. imgWidth = parseFloat(imgWidth)
  1593. imgHeight = parseFloat(imgHeight)
  1594. containerWidth = parseFloat(containerWidth)
  1595. containerHeight = parseFloat(containerHeight)
  1596. } catch (error) {
  1597. throw new Error('抱歉,我只接收数值类型或者可以转成数值类型的参数')
  1598. }
  1599. if (imgWidth > 0 && imgHeight > 0) {
  1600. //原图片宽高比例 大于 指定的宽高比例,这就说明了原图片的宽度必然 > 高度
  1601. if (imgWidth / imgHeight >= containerWidth / containerHeight) {
  1602. if (imgWidth > containerWidth) {
  1603. // alert('aaaaaaaa')
  1604. tempWidth = containerWidth
  1605. // 按原图片的比例进行缩放
  1606. tempHeight = (imgHeight * containerWidth) / imgWidth
  1607. } else {
  1608. // 按照图片的大小进行缩放
  1609. tempWidth = imgWidth
  1610. tempHeight = imgHeight
  1611. }
  1612. } else { // 原图片的高度必然 > 宽度
  1613. if (imgHeight > containerHeight) {
  1614. tempHeight = containerHeight
  1615. // 按原图片的比例进行缩放
  1616. tempWidth = (imgWidth * containerHeight) / imgHeight
  1617. } else {
  1618. // 按原图片的大小进行缩放
  1619. tempWidth = imgWidth
  1620. tempHeight = imgHeight
  1621. }
  1622. }
  1623. }
  1624. return [tempWidth, tempHeight]
  1625. },
  1626. sleep : function(time) {
  1627. return new Promise((resolve) => setTimeout(resolve, time));
  1628. },
  1629. //分享功能初始化
  1630. shareInit : function(wx, project, host, data) {
  1631. share.init(wx, project, host, data);
  1632. },
  1633. //通用的分享功能
  1634. share : function(provider, scene, type, data, success, error) {
  1635. if (this.source == 'h5') {
  1636. var weixin = this.wxTip(1);
  1637. if (!weixin) {
  1638. this.alert('请点击复制按钮');
  1639. }
  1640. } else {
  1641. var config = {
  1642. provider: provider,
  1643. type: type,
  1644. scene: scene,
  1645. href: data.link,
  1646. title: data.title,
  1647. summary: data.content,
  1648. imageUrl: data.pic,
  1649. success: function(res) {
  1650. if (success) {
  1651. success(res);
  1652. } else {
  1653. console.log("success:" + JSON.stringify(res));
  1654. }
  1655. },
  1656. fail: function(err) {
  1657. if (error) {
  1658. success(err);
  1659. } else {
  1660. console.log("fail:" + JSON.stringify(err));
  1661. }
  1662. }
  1663. };
  1664. if (data.media) {
  1665. config.mediaUrl = media;
  1666. }
  1667. if (this.source == 'applet') {
  1668. if (!data.path) {
  1669. console.log('data.path未定义!');
  1670. return;
  1671. }
  1672. if (!data.applet_id) {
  1673. console.log('data.applet_id未定义!');
  1674. return;
  1675. }
  1676. config.miniProgram = {};
  1677. config.miniProgram.id = data.applet_id;
  1678. config.miniProgram.path = data.path;
  1679. config.miniProgram.type = 0;
  1680. config.miniProgram.webUrl = data.link;
  1681. }
  1682. uni.share(config);
  1683. }
  1684. },
  1685. // 获取地理位置
  1686. getLocation : function(type, text, success) {
  1687. if (!type) {
  1688. type = 'wgs84';
  1689. }
  1690. if (!text) {
  1691. text = '需要获得您的位置,请您授权';
  1692. }
  1693. var self = this;
  1694. if (self.source != 'h5') {
  1695. uni.getSetting({
  1696. success(res) {
  1697. if (res.authSetting['scope.userLocation']) {
  1698. self.getAltitude(type, success);
  1699. } else {
  1700. uni.authorize({
  1701. scope: 'scope.userLocation',
  1702. success(res) {
  1703. self.getAltitude(type, success);
  1704. },
  1705. // 授权失败
  1706. fail(err) {
  1707. uni.showModal({
  1708. title: '提示',
  1709. content: text,
  1710. showCancel: false,
  1711. confirmText: '确认授权',
  1712. success() {
  1713. uni.openSetting({
  1714. success(res) {
  1715. self.getAltitude(type, success);
  1716. },
  1717. fail(err) {
  1718. self.alert('授权失败');
  1719. }
  1720. })
  1721. }
  1722. })
  1723. }
  1724. })
  1725. }
  1726. }
  1727. })
  1728. } else {
  1729. self.getAltitude(type, success);
  1730. }
  1731. },
  1732. getAltitude : function(type, success) {
  1733. uni.getLocation({
  1734. type: type,
  1735. altitude: true,
  1736. success(res) {
  1737. if (success) {
  1738. success(res);
  1739. } else {
  1740. console.info(res);
  1741. }
  1742. }
  1743. })
  1744. }
  1745. }
  1746. module.exports = dever