index.js 47 KB

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