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