index.js 45 KB

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