index.js 47 KB

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