index.js 47 KB

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