From 67f72bb4d898a65ae99c3ba8f87a06c50d6b86d5 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 26 Jun 2020 11:52:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eutil,=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E9=9D=9E=E9=81=97=E5=92=8C=E6=96=B0=E9=97=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 10 ++ pages/index/index.js | 109 ++++++++++++++------ pages/index/index.wxml | 100 +++++++++--------- project.config.json | 26 ++--- utils/dialog.js | 8 ++ utils/restAjax.js | 226 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 388 insertions(+), 91 deletions(-) create mode 100644 utils/dialog.js create mode 100644 utils/restAjax.js diff --git a/app.js b/app.js index b545190..c6ba268 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,15 @@ //app.js +var restAjax = require('utils/restAjax.js'); +var dialog = require('utils/dialog.js'); App({ + loginUrl: 'http://192.168.0.113:7001/usercenter', + usercenterUrl: 'http://192.168.0.113:7001/usesrcenter', + newsUrl: 'http://192.168.0.113:8082/news', + libraryUrl: 'http://192.168.0.113:8081/library', + activityUrl: '', + venueUrl: '', + restAjax: restAjax, + dialog: dialog, onLaunch: function () { // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] diff --git a/pages/index/index.js b/pages/index/index.js index 949f80c..90e2d9c 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -1,42 +1,91 @@ +var app = getApp(); Page({ - /** * 页面的初始数据 */ data: { indicatorDots: true, autoplay: true, - baseUrl: 'http://192.168.0.111:8888/volunteer/', token: '', - key: '' + library: { + libraryUrl: app.libraryUrl, + bannerLibrary: [], + }, + news: { + newsUrl: app.newsUrl, + newsList: [] + }, + venue: { + venueUrl: app.venueUrl + }, + activity: { + activityUrl: app.activityUrl + } }, - doLogin: function() { + doLogin: function () { var self = this; wx.login({ success(res) { - console.log(res) - if(res.code) { - var WechatMiniAppLoginVO = {jsCode: res.code}; - wx.request({ - url: self.data.baseUrl + 'wxminiapp/auth/login', - method: 'post', - data: WechatMiniAppLoginVO, - success: function(res) { - self.setData({ - token: res.data.data - }) - wx.setStorage({ - data: res.data.data, - key: 'token' - }) - } - }) - }else { + if (res.code) { + app.restAjax.post(app.restAjax.path('{url}/wxminiapp/auth/login', [app.loginUrl]), { + jsCode: res.code + }, null, function (code, data) { + var tokenArray = data.data.split('_'); + var token = tokenArray[0]; + var isRandomUsername = tokenArray[1]; + self.setData({ + token: token + }) + wx.setStorageSync('token', token); + wx.setStorageSync('isRandomUsername', isRandomUsername) + }, function (code, data) { + app.dialog.msg(data.msg); + }); + } else { console.log('登陆失败!' + res.errMsg); } } }) }, + doGetLibrary: function() { + var self = this; + app.restAjax.get(app.restAjax.path('{libraryUrl}/app/intangiblelibrary/listpageintangiblelibraryrelease', [self.data.library.libraryUrl]), { + page: 1, + rows: 5 + }, null, function(code, data) { + self.setData({ + 'library.bannerLibrary': data.rows + }); + }, function(code, data){ + app.dialog.msg(data.msg); + }); + }, + doGetVenue: function() { + app.restAjax.get(app.restAjax.path('', []), { + + }, null, function(code, data) { + + }, function(code, data) { + app.dialog.msg(data.msg); + }); + }, + doGetActivity: function() { + + }, + doGetNewsList: function() { + var self = this; + app.restAjax.get(app.restAjax.path('{newsUrl}/app/newscontent/listpagenewscontentrelease', [self.data.news.newsUrl]), { + page: 1, + rows: 5 + }, null, function(code, data) { + console.log(data); + self.setData({ + 'news.newsList': data.rows + }) + }, function(code, data) { + app.dialog.msg(data.msg); + }); + }, goNewsDetail: function () { wx.navigateTo({ url: '../newsDetail/newsDetail', @@ -78,54 +127,56 @@ Page({ onLoad: function (options) { var self = this; self.doLogin(); + self.doGetLibrary(); + self.doGetNewsList(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { - + }, /** * 生命周期函数--监听页面显示 */ onShow: function () { - + }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { - + }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { - + }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { - + }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { - + }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { - + } }) \ No newline at end of file diff --git a/pages/index/index.wxml b/pages/index/index.wxml index ab76d25..f4f042d 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -1,13 +1,7 @@ - - - - - - - - + + @@ -67,48 +61,56 @@ - - 银保监会印发《关于加快推进意外险改革的意见》 - - 来源:百度 - 10评论 - 2020-03-02 - - - - 银保监会印发《关于加快推进意外险改革的意见》 - - - - - 来源:百度 - 10评论 - 2020-03-02 - - - - 银保监会印发《关于加快推进意外险改革的意见》 - - - - - - - 来源:百度 - 10评论 - 2020-03-02 - - - - - - - - 银保监会印发《关于加快推进意外险改革的意见》 + + + {{item.newsContentTitle}} + + + - 来源:百度 - 10评论 - 2020-03-02 + 来源:{{item.newsContentResource}} + {{item.newsContentPublishTime}} + + + + + + + + {{item.newsContentTitle}} + + 来源:{{item.newsContentResource}} + {{item.newsContentPublishTime}} + + + + + + {{item.newsContentTitle}} + + 来源:{{item.newsContentResource}} + {{item.newsContentPublishTime}} + + + + + + + + {{item.newsContentTitle}} + + + + + 来源:{{item.newsContentResource}} + {{item.newsContentPublishTime}} + + + + {{item.newsContentTitle}} + + 来源:{{item.newsContentResource}} + {{item.newsContentPublishTime}} diff --git a/project.config.json b/project.config.json index 43b81a9..451ba67 100644 --- a/project.config.json +++ b/project.config.json @@ -5,35 +5,35 @@ }, "setting": { "urlCheck": false, - "scopeDataCheck": false, - "coverView": true, "es6": true, + "enhance": false, "postcss": true, - "compileHotReLoad": false, "preloadBackgroundData": false, "minified": true, - "autoAudits": false, "newFeature": true, - "uglifyFileName": false, - "uploadWithSourceMap": true, - "useIsolateContext": true, + "coverView": true, "nodeModules": false, - "enhance": false, - "useCompilerModule": true, - "userConfirmedUseCompilerModuleSwitch": false, + "autoAudits": false, "showShadowRootInWxmlPanel": true, + "scopeDataCheck": false, + "uglifyFileName": false, "checkInvalidKey": true, "checkSiteMap": true, + "uploadWithSourceMap": true, + "compileHotReLoad": false, "babelSetting": { "ignore": [], "disablePlugins": [], "outputPath": "" - } + }, + "useIsolateContext": true, + "useCompilerModule": true, + "userConfirmedUseCompilerModuleSwitch": false }, "compileType": "miniprogram", "libVersion": "2.11.1", - "appid": "wxe7f135356342a070", - "projectname": "miniprogram-2", + "appid": "wxb9c87f3dcf05852f", + "projectname": "xz_mini", "debugOptions": { "hidedInDevtools": [] }, diff --git a/utils/dialog.js b/utils/dialog.js new file mode 100644 index 0000000..dda9142 --- /dev/null +++ b/utils/dialog.js @@ -0,0 +1,8 @@ +function msg(text) { + wx.showToast({ + title: text, + icon: 'none', + duration: 1000 + }) +} +module.exports.msg = msg; \ No newline at end of file diff --git a/utils/restAjax.js b/utils/restAjax.js new file mode 100644 index 0000000..24ea122 --- /dev/null +++ b/utils/restAjax.js @@ -0,0 +1,226 @@ +var methods = { + POST_METHOD: 'POST', + DELETE_METHOD: 'DELETE', + PUT_METHOD: 'PUT', + GET_METHOD: 'GET' +}; + +/** + * 新增 + * @param url + * @param dataObj + * @param args + * @param successCallback + * @param errorCallback + * @param completeCallback + */ +function postJson(url, dataObj, args, successCallback, errorCallback, completeCallback) { + doAjax(url, methods.POST_METHOD, dataObj, args, successCallback, errorCallback, completeCallback); +} + +/** + * 删除 + * @param url + * @param dataObj + * @param args + * @param successCallback + * @param errorCallback + * @param completeCallback + */ +function deleteForm(url, dataObj, args, successCallback, errorCallback, completeCallback) { + doAjax(url, methods.DELETE_METHOD, dataObj, args, successCallback, errorCallback, completeCallback); +} + +/** + * 修改 + * @param url + * @param dataObj + * @param args + * @param successCallback + * @param errorCallback + * @param completeCallback + */ +function putJson(url, dataObj, args, successCallback, errorCallback, completeCallback) { + doAjax(url, methods.PUT_METHOD, dataObj, args, successCallback, errorCallback, completeCallback); +} + +/** + * 查询 + * @param url + * @param dataObj + * @param args + * @param successCallback + * @param errorCallback + * @param completeCallback + */ +function getForm(url, dataObj, args, successCallback, errorCallback, completeCallback) { + doAjax(url, methods.GET_METHOD, dataObj, args, successCallback, errorCallback, completeCallback); +} + +/** + * 执行上传 + * @param url + * @param method + * @param dataObj + * @param args + * @param successCallback + * @param errorCallback + * @param completeCallback + */ +function doAjax(url, method, dataObj, args, successCallback, errorCallback, completeCallback) { + var headers = {}; + if (args != null && typeof (args.headers) != 'undefined' && args.headers != null) { + headers = args.headers; + } + wx.request({ + url: url, + method: method, + data: dataObj, + header: headers, + dataType: 'json', + success: function (response) { + if (response.statusCode == 200) { + successCallback(response.statusCode, response.data, args); + } else { + if (errorCallback && typeof (errorCallback) == 'function') { + errorCallback(response.statusCode, response.data); + } + } + }, + fail: function (response) { + console.log(response); + }, + complete: function () { + if (completeCallback && typeof (completeCallback) == 'function') { + completeCallback(); + } + } + }) +} + +/** + * 检测是路径参数有重复值 + * @param pathArgArray + * @returns {boolean} + */ +function pathArgsHasSameValue(pathArgArray) { + var tempArgIndex = 0; + var tempArgs = pathArgArray[tempArgIndex]; + for (var i = (tempArgIndex + 1), item; item = pathArgArray[i]; i++) { + if (tempArgs == item) { + throw new Error('参数' + item + '有重复值!!!'); + } + if (i == pathArgArray.length - 1) { + tempArgs = pathArgArray[++tempArgIndex]; + i = tempArgIndex; + continue; + } + } +} + +/** + * 获取页面间传递的参数 + * @param url + */ +function getParamsArg(url) { + var params = url.split('?')[1]; + var paramsObj = {}; + if (typeof (params) == 'undefined' || params == null) { + return paramsObj; + } + var paramsKVs = params.split('&'); + for (var i = 0, item = null; item = paramsKVs[i++];) { + var kvs = item.split('='); + if (kvs.length == 1) { + paramsObj[kvs[0]] = null; + } + if (kvs.length == 2) { + paramsObj[kvs[0]] = decodeURI(kvs[1]); + } + } + return paramsObj; +} + +/** + * 构建路径 + * @param basePath 请求路径,{参数}, + * @param pathArgs 替换的路径参数,不能重复 + * @returns {*} + */ +function buildPath(basePath, pathArgs) { + var path = basePath; + if (!basePath || !(typeof (basePath) == 'string')) { + throw new Error('basePath必须为字符串!!!'); + } + if (!pathArgs || !Array.isArray(pathArgs)) { + throw new Error('pathArgs必须为数组!!!'); + } + var pathArgArray = basePath.match(/\{\w+\}/g); + if (!pathArgArray) { + return path; + } + pathArgsHasSameValue(pathArgArray); + for (var i = 0, item; item = pathArgArray[i]; i++) { + path = path.replace(item, pathArgs[i]); + } + return path; +} + +/** + * 通过form对象上传文件 + * @param url + * @param formData + * @param args + * @param successCallback + * @param errorCallback + * @param beforeCallback + * @param completeCallback + */ +function postFile(url, path, name, args, successCallback, errorCallback, completeCallback) { + var headers = {}; + if (args != null && typeof (args.headers) != 'undefined' && args.headers != null) { + headers = args.headers; + } + wx.uploadFile({ + filePath: path, + name: name, + url: url, + header: headers, + success: function (response) { + if (response.statusCode == 200) { + successCallback(response.statusCode, response.data, args); + } else { + if (errorCallback && typeof (errorCallback) == 'function') { + errorCallback(response.statusCode, response.data); + } + } + }, + fail: function (response) { + console.log(response); + }, + complete: function () { + if (completeCallback && typeof (completeCallback) == 'function') { + completeCallback(); + } + } + }) +}; + +/** + * xss 转义 + * @param html + * @returns {string} + */ +function escape(html) { + return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&') + .replace(//g, '>') + .replace(/'/g, ''').replace(/"/g, '"'); +} + +module.exports.post = postJson; +module.exports.delete = deleteForm; +module.exports.put = putJson; +module.exports.get = getForm; +module.exports.params = getParamsArg; +module.exports.path = buildPath; +module.exports.escape = escape; \ No newline at end of file