index.js 47 KB

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