index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. //封装好一系列常用的方法 dever
  2. import config from './config';
  3. //http网络请求
  4. var http = {
  5. //token的key
  6. token : 'token',
  7. //获取token
  8. getToken: function() {
  9. return uni.getStorageSync(this.token);
  10. },
  11. //设置token
  12. setToken: function(value) {
  13. return uni.setStorageSync(this.token, value);
  14. },
  15. //设置http的option
  16. option: function(options) {
  17. if (!options) {
  18. options = {};
  19. }
  20. options[this.token] = this.getToken();
  21. return options;
  22. },
  23. //request请求
  24. request: function(vue, url, options, callback, err_callback) {
  25. var self = this;
  26. if (vue == 'post') {
  27. config.request.method = 'POST';
  28. vue = false;
  29. } else {
  30. config.request.method = 'GET';
  31. }
  32. if (typeof(url) == 'object') {
  33. vue = url[1];
  34. url = url[0];
  35. }
  36. if (typeof(vue) == 'object') {
  37. vue.isDisabled = true;
  38. }
  39. self.core(url, options).then((result, state) => {
  40. if (typeof(vue) == 'object') {
  41. if (!vue.fetch) {
  42. vue.fetch = {}
  43. }
  44. vue.isDisabled = false;
  45. vue.fetch = Object.assign({}, vue.fetch, result)
  46. }
  47. if (callback) {
  48. callback(result);
  49. }
  50. }).catch((result) => {
  51. if (typeof(vue) == 'object') {
  52. vue.isDisabled = false;
  53. }
  54. if (err_callback) {
  55. err_callback(result);
  56. } else {
  57. if (result.code == '2') {
  58. dever.location(dever.login);
  59. } else {
  60. dever.alert(result.msg)
  61. }
  62. }
  63. })
  64. },
  65. core : function(url, options) {
  66. var self = this;
  67. options = this.option(options);
  68. if (config.server[url]) {
  69. config.request.url = config.server[url];
  70. } else {
  71. config.request.url = url;
  72. }
  73. config.request.url = config.request.host + config.request.url;
  74. config.request.data = options;
  75. return new Promise((resolve, reject) => {
  76. config.request.complete = (response) => {
  77. dever.debug(response);
  78. uni.hideLoading()
  79. uni.hideNavigationBarLoading();
  80. if (response.statusCode === 200) {
  81. if (response.data.code == '0') {
  82. if (options.page && options.page > 0) {
  83. page.state = true;
  84. if (page.key && response.data.data[page.key].length > 0) {
  85. page.data = page.data.concat(response.data.data[page.key]);
  86. response.data.data[page.key] = page.data;
  87. } else if (response.data.data.length > 0) {
  88. page.data = page.data.concat(response.data.data);
  89. response.data.data = page.data;
  90. } else {
  91. page.state = false;
  92. resolve(response.data.data, false);
  93. return;
  94. }
  95. }
  96. resolve(response.data.data);
  97. } else {
  98. reject(response.data)
  99. }
  100. } else {
  101. // 处理catch 请求,不在本页面之外处理,统一在这里处理
  102. if (options && options.handle) {
  103. reject(response)
  104. } else {
  105. try {
  106. Promise.reject(response).catch(err => {
  107. self.error(response.statusCode || response.errMsg);
  108. });
  109. } catch (e) {
  110. dever.alert(e)
  111. }
  112. }
  113. }
  114. }
  115. //uni.request(Object.assign({}, config, options));
  116. dever.debug(config.request);
  117. uni.showLoading({title: '加载中', mask: true});
  118. uni.showNavigationBarLoading();
  119. uni.stopPullDownRefresh();
  120. uni.request(config.request);
  121. })
  122. },
  123. error : function(err) {
  124. switch (err) {
  125. case 401:
  126. // 错误码404的处理方式
  127. console.error("请求背拒绝" + err)
  128. break;
  129. case 404:
  130. // 错误码404的处理方式
  131. console.error("没有找到页面" + err)
  132. break;
  133. case 500:
  134. // 错误码404的处理方式
  135. console.error("500服务器错误" + err)
  136. break;
  137. case 405:
  138. console.error("错误的请求" + err)
  139. break;
  140. }
  141. },
  142. }
  143. var page = {
  144. //分页控制
  145. value : 1,
  146. //分页数据
  147. data : [],
  148. //分页所属的key
  149. key : '',
  150. //分页状态 true可以分页,false不能分页
  151. state : true,
  152. //通用的获取数据方法:瀑布流分页 (1,'level')
  153. get : function(config, vue, url, options, callback, err_callback) {
  154. options = http.option(options);
  155. if (typeof(config) == 'object') {
  156. var state = config[0];
  157. this.key = config[1];
  158. } else {
  159. var state = config;
  160. this.key = '';
  161. }
  162. if (state == 1) {
  163. this.value = 1;
  164. this.data = [];
  165. } else {
  166. this.value++;
  167. }
  168. if (this.state == false && state != 1) {
  169. return;
  170. }
  171. options.page = this.value;
  172. http.request(vue, url, options, callback, err_callback);
  173. },
  174. }
  175. var upload = {
  176. data : {},
  177. handle : function(key, count, callback) {
  178. if (!count) {
  179. count = 1;
  180. }
  181. var type = 1;
  182. if (count > 1) {
  183. type = 2;
  184. }
  185. var self = this;
  186. count = parseInt(count);
  187. uni.chooseImage({
  188. count: count,
  189. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  190. sourceType: ['album'], //从相册选择
  191. success: res => {
  192. uni.showLoading();
  193. res.tempFilePaths.forEach(function(item, index) {
  194. //请求上传接口
  195. uni.uploadFile({
  196. url: config.upload.url, //仅为示例,非真实的接口地址
  197. filePath: item,
  198. name: config.upload.name,
  199. formData: { token: http.getToken() },
  200. success: res => {
  201. dever.debug(res);
  202. var data = JSON.parse(res.data);
  203. uni.hideLoading();
  204. if (data.uploaded) {
  205. var backUrl = data.url;
  206. if (count > 1) {
  207. if (!self.data[key]) {
  208. self.data[key] = [];
  209. }
  210. self.data[key].push(backUrl);
  211. } else {
  212. self.data[key] = backUrl;
  213. }
  214. if (callback) {
  215. callback(type, self.data[key]);
  216. }
  217. } else if (data.uploaded == false) {
  218. uni.showToast({ title: '上传失败,请重新上传', icon: 'none' });
  219. uni.hideLoading();
  220. }
  221. }
  222. });
  223. });
  224. },
  225. fail: () => {
  226. uni.showToast({ title: '图片上传失败,请重试', icon: 'none' });
  227. uni.hideLoading();
  228. }
  229. });
  230. },
  231. uploadDel : function(key, index, callback) {
  232. var self = this;
  233. uni.showModal({
  234. content: '确定要删除吗?',
  235. cancelText: '取消',
  236. confirmText: '确定',
  237. success: res => {
  238. if (res.confirm) {
  239. self.data[key].splice(index, 1);
  240. if (callback) {
  241. callback(res, index);
  242. }
  243. }
  244. }
  245. });
  246. }
  247. }
  248. var dever = {
  249. login : 'login/login',
  250. web_view : '/components/dever/pages/web_view',
  251. //页面跳转
  252. location : function(path, go) {
  253. if (path.indexOf('http') > -1) {
  254. this.debug('http:' + path);
  255. this.data('web_view', path);
  256. /*
  257. //#ifdef APP-PLUS
  258. uni.navigateTo({
  259. url: this.web_view
  260. })
  261. //#endif
  262. //#ifdef MP-WEIXIN
  263. uni.navigateTo({
  264. url: this.web_view
  265. })
  266. //#endif
  267. //#ifndef H5
  268. window.location.href = path;
  269. //#endif
  270. */
  271. if (go) {
  272. //#ifdef APP-PLUS
  273. uni.navigateTo({
  274. url: this.web_view
  275. })
  276. //#endif
  277. //#ifdef MP-WEIXIN
  278. uni.navigateTo({
  279. url: this.web_view
  280. })
  281. //#endif
  282. if (go == 'location') {
  283. window.location.href = path;
  284. } else {
  285. var url = window.location.href;
  286. url = url.replace(go, 'paySuccess');
  287. url = window.btoa(url);
  288. window.location.href = path + '&refer=' + url + '&token=' + this.getToken();
  289. }
  290. } else {
  291. uni.navigateTo({
  292. url: this.web_view
  293. })
  294. }
  295. } else {
  296. this.debug('navigateTo:' + path);
  297. if (go) {
  298. uni.switchTab({
  299. url: '/pages/' + path
  300. })
  301. } else {
  302. uni.navigateTo({
  303. url: '/pages/' + path
  304. })
  305. }
  306. }
  307. },
  308. //提示信息
  309. alert : function(msg, icon, callback) {
  310. if (!icon) {
  311. icon = 'none'
  312. }
  313. if (msg) {
  314. uni.showToast({title: msg, icon: icon});
  315. }
  316. this.debug('alert:' + msg)
  317. if (callback) {
  318. this.debug('callback:' + callback)
  319. setTimeout(function(){
  320. callback();
  321. }, 1000)
  322. }
  323. },
  324. //debug调试
  325. debug : function(string) {
  326. if (config.base.debug) {
  327. this.log(string);
  328. }
  329. },
  330. //记录日志
  331. log : function(string) {
  332. console.log(string);
  333. },
  334. //获取token
  335. getToken: function() {
  336. return http.getToken();
  337. },
  338. //设置token
  339. setToken: function(value) {
  340. return http.setToken(value);
  341. },
  342. //通用的提交数据的方法
  343. post : function(url, options, callback, err_callback) {
  344. http.request('post', url, options, callback, err_callback);
  345. },
  346. //通用的获取数据方法
  347. get : function(vue, url, options, callback, err_callback) {
  348. http.request(vue, url, options, callback, err_callback);
  349. },
  350. //通用的获取数据方法
  351. page : function(config, vue, url, options, callback, err_callback) {
  352. page.get(config, vue, url, options, callback, err_callback);
  353. },
  354. //上传
  355. upload : function(id, count, callback) {
  356. upload.handle(id, count, callback);
  357. },
  358. //删除上传
  359. uploadDel : function(id, index, callback) {
  360. upload.uploadDel(id, index, callback);
  361. },
  362. //验证登录
  363. checkLogin : function() {
  364. if (!this.getToken()) {
  365. this.location(this.login);
  366. }
  367. },
  368. //检查更新
  369. checkUpdate : function(gotoUrl) {
  370. var version = '1.0.0';//更新版本号,每次更新需要重新更换
  371. http.request('post', 'isUpdate', {}, function(res){
  372. console.log('version5',res)
  373. if(version != res.info.app_version){
  374. if(res.info.is_update == 1){
  375. uni.showToast({
  376. title:'当前更新版本,正在为你跳转更新',
  377. icon:'none',
  378. duration:3000
  379. });
  380. setTimeout(function(){
  381. uni.navigateTo({
  382. url: gotoUrl
  383. });
  384. }, 3000);
  385. }
  386. }
  387. });
  388. },
  389. //数据存储
  390. data : function(key, value) {
  391. if (value) {
  392. uni.setStorageSync(key, value);
  393. return value;
  394. } else {
  395. return uni.getStorageSync(key);
  396. }
  397. }
  398. }
  399. module.exports = dever