index.js 47 KB

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