index.js 47 KB

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