index.js 48 KB

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