From 517728c6e4e4f0c2f4e0584047fcdb2493d7425d Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 13 Jan 2020 15:34:24 +0800 Subject: [PATCH] update Former-commit-id: 0ce0fddbd477d91bb747c4116b34b1f898cd4482 --- projects/IoTCenter/Api/DeviceController.cs | 30 ++++++++----------- projects/IoTCenter/wwwroot/nodes.html | 2 +- projects/IoTCenter/wwwroot/products.html | 2 +- .../{Controllers => }/Api/TokenController.cs | 2 +- .../{Controllers => }/Api/UserController.cs | 0 projects/WebUI/wwwroot/lib/vuex/vuex.min.js | 6 ++++ projects/WebUI/wwwroot/login.js | 3 ++ projects/WebUI/wwwroot/pc.html | 1 + projects/WebUI/wwwroot/pc.js | 25 ++++++++++++++-- projects/WebUI/wwwroot/pchome.html | 8 ++--- projects/WebUI/wwwroot/pchome.js | 11 ++++--- projects/lib/package.json | 1 + 12 files changed, 60 insertions(+), 31 deletions(-) rename projects/UserCenter/{Controllers => }/Api/TokenController.cs (98%) rename projects/UserCenter/{Controllers => }/Api/UserController.cs (100%) create mode 100644 projects/WebUI/wwwroot/lib/vuex/vuex.min.js diff --git a/projects/IoTCenter/Api/DeviceController.cs b/projects/IoTCenter/Api/DeviceController.cs index ee8e2470..8ff76d98 100644 --- a/projects/IoTCenter/Api/DeviceController.cs +++ b/projects/IoTCenter/Api/DeviceController.cs @@ -2,8 +2,10 @@ using Infrastructure.Data; using Infrastructure.Extensions; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; +using System.ComponentModel.DataAnnotations; using System.Linq; namespace UserCenter.Controllers @@ -14,33 +16,27 @@ namespace UserCenter.Controllers public class DeviceController : ControllerBase { private readonly IConfiguration _cfg; - private readonly IRepository _productRepo; - private readonly IRepository _categoryRepo; + private readonly IRepository _deviceRepo; public DeviceController(IConfiguration cfg, - IRepository productRepo, - IRepository categoryRepo) + IRepository deviceRepo) { this._cfg = cfg; - this._productRepo = productRepo; - this._categoryRepo = categoryRepo; + this._deviceRepo = deviceRepo; } [HttpPost] - public ActionResult GetProducts() + public ActionResult GetDevice([Required]string number) { try { - var model = this._productRepo.ReadOnlyTable() - .OrderBy(o => o.DisplayOrder) - .Select(o => new - { - o.Name, - o.Number, - o.Image, - o.DisplayOrder, - Count = o.Devices.Count() - }); + var model = this._deviceRepo.ReadOnlyTable() + .Include(o => o.Data) + .Include(o => o.Product) + .ThenInclude(o => o.Apis) + .ThenInclude(o => o.Parameters) + .Where(o => o.Number == number) + .FirstOrDefault(); return Ok(model); } catch (Exception ex) diff --git a/projects/IoTCenter/wwwroot/nodes.html b/projects/IoTCenter/wwwroot/nodes.html index 38fabfde..b5fc6e2e 100644 --- a/projects/IoTCenter/wwwroot/nodes.html +++ b/projects/IoTCenter/wwwroot/nodes.html @@ -11,7 +11,7 @@
-

{{node.name}}

+

{{node.name}}({{node.count}})

diff --git a/projects/IoTCenter/wwwroot/products.html b/projects/IoTCenter/wwwroot/products.html index 4eef9b54..66292130 100644 --- a/projects/IoTCenter/wwwroot/products.html +++ b/projects/IoTCenter/wwwroot/products.html @@ -11,7 +11,7 @@
-

{{product.name}}

+

{{product.name}}({{product.count}})

diff --git a/projects/UserCenter/Controllers/Api/TokenController.cs b/projects/UserCenter/Api/TokenController.cs similarity index 98% rename from projects/UserCenter/Controllers/Api/TokenController.cs rename to projects/UserCenter/Api/TokenController.cs index c12e3480..1dfab171 100644 --- a/projects/UserCenter/Controllers/Api/TokenController.cs +++ b/projects/UserCenter/Api/TokenController.cs @@ -116,7 +116,7 @@ namespace UserCenter.Controllers return Ok(new { AccessToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("AccessTokenHours", 0.5))), - RefreshToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("AccessTokenHours", 720))), + RefreshToken = Request.HttpContext.GetToken(userName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("RefreshToken", 720))), }); } catch (Exception ex) diff --git a/projects/UserCenter/Controllers/Api/UserController.cs b/projects/UserCenter/Api/UserController.cs similarity index 100% rename from projects/UserCenter/Controllers/Api/UserController.cs rename to projects/UserCenter/Api/UserController.cs diff --git a/projects/WebUI/wwwroot/lib/vuex/vuex.min.js b/projects/WebUI/wwwroot/lib/vuex/vuex.min.js new file mode 100644 index 00000000..41b878bb --- /dev/null +++ b/projects/WebUI/wwwroot/lib/vuex/vuex.min.js @@ -0,0 +1,6 @@ +/** + * vuex v3.1.2 + * (c) 2019 Evan You + * @license MIT + */ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).Vuex=e()}(this,function(){"use strict";var t=("undefined"!=typeof window?window:"undefined"!=typeof global?global:{}).__VUE_DEVTOOLS_GLOBAL_HOOK__;function e(t,e){Object.keys(t).forEach(function(n){return e(t[n],n)})}function n(t){return null!==t&&"object"==typeof t}var o=function(t,e){this.runtime=e,this._children=Object.create(null),this._rawModule=t;var n=t.state;this.state=("function"==typeof n?n():n)||{}},i={namespaced:{configurable:!0}};i.namespaced.get=function(){return!!this._rawModule.namespaced},o.prototype.addChild=function(t,e){this._children[t]=e},o.prototype.removeChild=function(t){delete this._children[t]},o.prototype.getChild=function(t){return this._children[t]},o.prototype.update=function(t){this._rawModule.namespaced=t.namespaced,t.actions&&(this._rawModule.actions=t.actions),t.mutations&&(this._rawModule.mutations=t.mutations),t.getters&&(this._rawModule.getters=t.getters)},o.prototype.forEachChild=function(t){e(this._children,t)},o.prototype.forEachGetter=function(t){this._rawModule.getters&&e(this._rawModule.getters,t)},o.prototype.forEachAction=function(t){this._rawModule.actions&&e(this._rawModule.actions,t)},o.prototype.forEachMutation=function(t){this._rawModule.mutations&&e(this._rawModule.mutations,t)},Object.defineProperties(o.prototype,i);var r,s=function(t){this.register([],t,!1)};s.prototype.get=function(t){return t.reduce(function(t,e){return t.getChild(e)},this.root)},s.prototype.getNamespace=function(t){var e=this.root;return t.reduce(function(t,n){return t+((e=e.getChild(n)).namespaced?n+"/":"")},"")},s.prototype.update=function(t){!function t(e,n,o){n.update(o);if(o.modules)for(var i in o.modules){if(!n.getChild(i))return;t(e.concat(i),n.getChild(i),o.modules[i])}}([],this.root,t)},s.prototype.register=function(t,n,i){var r=this;void 0===i&&(i=!0);var s=new o(n,i);0===t.length?this.root=s:this.get(t.slice(0,-1)).addChild(t[t.length-1],s);n.modules&&e(n.modules,function(e,n){r.register(t.concat(n),e,i)})},s.prototype.unregister=function(t){var e=this.get(t.slice(0,-1)),n=t[t.length-1];e.getChild(n).runtime&&e.removeChild(n)};var a=function(e){var n=this;void 0===e&&(e={}),!r&&"undefined"!=typeof window&&window.Vue&&m(window.Vue);var o=e.plugins;void 0===o&&(o=[]);var i=e.strict;void 0===i&&(i=!1),this._committing=!1,this._actions=Object.create(null),this._actionSubscribers=[],this._mutations=Object.create(null),this._wrappedGetters=Object.create(null),this._modules=new s(e),this._modulesNamespaceMap=Object.create(null),this._subscribers=[],this._watcherVM=new r,this._makeLocalGettersCache=Object.create(null);var a=this,c=this.dispatch,u=this.commit;this.dispatch=function(t,e){return c.call(a,t,e)},this.commit=function(t,e,n){return u.call(a,t,e,n)},this.strict=i;var f=this._modules.root.state;p(this,f,[],this._modules.root),h(this,f),o.forEach(function(t){return t(n)}),(void 0!==e.devtools?e.devtools:r.config.devtools)&&function(e){t&&(e._devtoolHook=t,t.emit("vuex:init",e),t.on("vuex:travel-to-state",function(t){e.replaceState(t)}),e.subscribe(function(e,n){t.emit("vuex:mutation",e,n)}))}(this)},c={state:{configurable:!0}};function u(t,e){return e.indexOf(t)<0&&e.push(t),function(){var n=e.indexOf(t);n>-1&&e.splice(n,1)}}function f(t,e){t._actions=Object.create(null),t._mutations=Object.create(null),t._wrappedGetters=Object.create(null),t._modulesNamespaceMap=Object.create(null);var n=t.state;p(t,n,[],t._modules.root,!0),h(t,n,e)}function h(t,n,o){var i=t._vm;t.getters={},t._makeLocalGettersCache=Object.create(null);var s=t._wrappedGetters,a={};e(s,function(e,n){a[n]=function(t,e){return function(){return t(e)}}(e,t),Object.defineProperty(t.getters,n,{get:function(){return t._vm[n]},enumerable:!0})});var c=r.config.silent;r.config.silent=!0,t._vm=new r({data:{$$state:n},computed:a}),r.config.silent=c,t.strict&&function(t){t._vm.$watch(function(){return this._data.$$state},function(){},{deep:!0,sync:!0})}(t),i&&(o&&t._withCommit(function(){i._data.$$state=null}),r.nextTick(function(){return i.$destroy()}))}function p(t,e,n,o,i){var s=!n.length,a=t._modules.getNamespace(n);if(o.namespaced&&(t._modulesNamespaceMap[a],t._modulesNamespaceMap[a]=o),!s&&!i){var c=l(e,n.slice(0,-1)),u=n[n.length-1];t._withCommit(function(){r.set(c,u,o.state)})}var f=o.context=function(t,e,n){var o=""===e,i={dispatch:o?t.dispatch:function(n,o,i){var r=d(n,o,i),s=r.payload,a=r.options,c=r.type;return a&&a.root||(c=e+c),t.dispatch(c,s)},commit:o?t.commit:function(n,o,i){var r=d(n,o,i),s=r.payload,a=r.options,c=r.type;a&&a.root||(c=e+c),t.commit(c,s,a)}};return Object.defineProperties(i,{getters:{get:o?function(){return t.getters}:function(){return function(t,e){if(!t._makeLocalGettersCache[e]){var n={},o=e.length;Object.keys(t.getters).forEach(function(i){if(i.slice(0,o)===e){var r=i.slice(o);Object.defineProperty(n,r,{get:function(){return t.getters[i]},enumerable:!0})}}),t._makeLocalGettersCache[e]=n}return t._makeLocalGettersCache[e]}(t,e)}},state:{get:function(){return l(t.state,n)}}}),i}(t,a,n);o.forEachMutation(function(e,n){!function(t,e,n,o){(t._mutations[e]||(t._mutations[e]=[])).push(function(e){n.call(t,o.state,e)})}(t,a+n,e,f)}),o.forEachAction(function(e,n){var o=e.root?n:a+n,i=e.handler||e;!function(t,e,n,o){(t._actions[e]||(t._actions[e]=[])).push(function(e){var i,r=n.call(t,{dispatch:o.dispatch,commit:o.commit,getters:o.getters,state:o.state,rootGetters:t.getters,rootState:t.state},e);return(i=r)&&"function"==typeof i.then||(r=Promise.resolve(r)),t._devtoolHook?r.catch(function(e){throw t._devtoolHook.emit("vuex:error",e),e}):r})}(t,o,i,f)}),o.forEachGetter(function(e,n){!function(t,e,n,o){if(t._wrappedGetters[e])return;t._wrappedGetters[e]=function(t){return n(o.state,o.getters,t.state,t.getters)}}(t,a+n,e,f)}),o.forEachChild(function(o,r){p(t,e,n.concat(r),o,i)})}function l(t,e){return e.length?e.reduce(function(t,e){return t[e]},t):t}function d(t,e,o){return n(t)&&t.type&&(o=e,e=t,t=t.type),{type:t,payload:e,options:o}}function m(t){r&&t===r||function(t){if(Number(t.version.split(".")[0])>=2)t.mixin({beforeCreate:n});else{var e=t.prototype._init;t.prototype._init=function(t){void 0===t&&(t={}),t.init=t.init?[n].concat(t.init):n,e.call(this,t)}}function n(){var t=this.$options;t.store?this.$store="function"==typeof t.store?t.store():t.store:t.parent&&t.parent.$store&&(this.$store=t.parent.$store)}}(r=t)}c.state.get=function(){return this._vm._data.$$state},c.state.set=function(t){},a.prototype.commit=function(t,e,n){var o=this,i=d(t,e,n),r=i.type,s=i.payload,a={type:r,payload:s},c=this._mutations[r];c&&(this._withCommit(function(){c.forEach(function(t){t(s)})}),this._subscribers.forEach(function(t){return t(a,o.state)}))},a.prototype.dispatch=function(t,e){var n=this,o=d(t,e),i=o.type,r=o.payload,s={type:i,payload:r},a=this._actions[i];if(a){try{this._actionSubscribers.filter(function(t){return t.before}).forEach(function(t){return t.before(s,n.state)})}catch(t){}return(a.length>1?Promise.all(a.map(function(t){return t(r)})):a[0](r)).then(function(t){try{n._actionSubscribers.filter(function(t){return t.after}).forEach(function(t){return t.after(s,n.state)})}catch(t){}return t})}},a.prototype.subscribe=function(t){return u(t,this._subscribers)},a.prototype.subscribeAction=function(t){return u("function"==typeof t?{before:t}:t,this._actionSubscribers)},a.prototype.watch=function(t,e,n){var o=this;return this._watcherVM.$watch(function(){return t(o.state,o.getters)},e,n)},a.prototype.replaceState=function(t){var e=this;this._withCommit(function(){e._vm._data.$$state=t})},a.prototype.registerModule=function(t,e,n){void 0===n&&(n={}),"string"==typeof t&&(t=[t]),this._modules.register(t,e),p(this,this.state,t,this._modules.get(t),n.preserveState),h(this,this.state)},a.prototype.unregisterModule=function(t){var e=this;"string"==typeof t&&(t=[t]),this._modules.unregister(t),this._withCommit(function(){var n=l(e.state,t.slice(0,-1));r.delete(n,t[t.length-1])}),f(this)},a.prototype.hotUpdate=function(t){this._modules.update(t),f(this,!0)},a.prototype._withCommit=function(t){var e=this._committing;this._committing=!0,t(),this._committing=e},Object.defineProperties(a.prototype,c);var v=w(function(t,e){var n={};return b(e).forEach(function(e){var o=e.key,i=e.val;n[o]=function(){var e=this.$store.state,n=this.$store.getters;if(t){var o=$(this.$store,"mapState",t);if(!o)return;e=o.context.state,n=o.context.getters}return"function"==typeof i?i.call(this,e,n):e[i]},n[o].vuex=!0}),n}),_=w(function(t,e){var n={};return b(e).forEach(function(e){var o=e.key,i=e.val;n[o]=function(){for(var e=[],n=arguments.length;n--;)e[n]=arguments[n];var o=this.$store.commit;if(t){var r=$(this.$store,"mapMutations",t);if(!r)return;o=r.context.commit}return"function"==typeof i?i.apply(this,[o].concat(e)):o.apply(this.$store,[i].concat(e))}}),n}),y=w(function(t,e){var n={};return b(e).forEach(function(e){var o=e.key,i=e.val;i=t+i,n[o]=function(){if(!t||$(this.$store,"mapGetters",t))return this.$store.getters[i]},n[o].vuex=!0}),n}),g=w(function(t,e){var n={};return b(e).forEach(function(e){var o=e.key,i=e.val;n[o]=function(){for(var e=[],n=arguments.length;n--;)e[n]=arguments[n];var o=this.$store.dispatch;if(t){var r=$(this.$store,"mapActions",t);if(!r)return;o=r.context.dispatch}return"function"==typeof i?i.apply(this,[o].concat(e)):o.apply(this.$store,[i].concat(e))}}),n});function b(t){return function(t){return Array.isArray(t)||n(t)}(t)?Array.isArray(t)?t.map(function(t){return{key:t,val:t}}):Object.keys(t).map(function(e){return{key:e,val:t[e]}}):[]}function w(t){return function(e,n){return"string"!=typeof e?(n=e,e=""):"/"!==e.charAt(e.length-1)&&(e+="/"),t(e,n)}}function $(t,e,n){return t._modulesNamespaceMap[n]}return{Store:a,install:m,version:"3.1.2",mapState:v,mapMutations:_,mapGetters:y,mapActions:g,createNamespacedHelpers:function(t){return{mapState:v.bind(null,t),mapGetters:y.bind(null,t),mapMutations:_.bind(null,t),mapActions:g.bind(null,t)}}}}); \ No newline at end of file diff --git a/projects/WebUI/wwwroot/login.js b/projects/WebUI/wwwroot/login.js index 74c0a49c..11832fb6 100644 --- a/projects/WebUI/wwwroot/login.js +++ b/projects/WebUI/wwwroot/login.js @@ -20,6 +20,9 @@ password: /^[^\s]+$/ } }; + }, + user: function () { + return this.$store.user; } }, methods: { diff --git a/projects/WebUI/wwwroot/pc.html b/projects/WebUI/wwwroot/pc.html index ff3394b0..ddc795ee 100644 --- a/projects/WebUI/wwwroot/pc.html +++ b/projects/WebUI/wwwroot/pc.html @@ -26,6 +26,7 @@ + diff --git a/projects/WebUI/wwwroot/pc.js b/projects/WebUI/wwwroot/pc.js index 4fd4c88f..7e15953d 100644 --- a/projects/WebUI/wwwroot/pc.js +++ b/projects/WebUI/wwwroot/pc.js @@ -1,4 +1,22 @@ -var userAgent = navigator.userAgent; +Vue.use(Vuex); +const store = new Vuex.Store({ + state: { + accessToken: localStorage.getItem("accessToken"), + refreshToken: localStorage.getItem("refreshToken"), + user: null, + products: [], + nodes: [], + }, + mutations: { + setUser(state, user) { + console.log('~~~~'); + console.log(store); + state.user = user; + } + }, +}) +/// +var userAgent = navigator.userAgent; var isApp = userAgent.indexOf('isapp') !== -1; var hubUrl = "/IoTCenter/hub?group=page"; var connection = new signalR.HubConnectionBuilder().withUrl(hubUrl).build(); @@ -60,7 +78,7 @@ axios.interceptors.response.use(function (response) { localStorage.setItem("accessToken", token); refreshToken = response.data.refreshToken; localStorage.setItem("refreshToken", refreshToken); - config.headers['Authorization'] = 'Bearer ' + token; + error.config.headers['Authorization'] = 'Bearer ' + token; console.log('刷新token后重发api请求'); axios.request(error.config); }) @@ -123,6 +141,7 @@ router.afterEach((route, redirect) => { }) /// const app = new Vue({ + store:store, router, data: { }, @@ -130,5 +149,5 @@ const app = new Vue({ console.log('mounted:app'); }, methods: { - } + }, }).$mount('#app'); diff --git a/projects/WebUI/wwwroot/pchome.html b/projects/WebUI/wwwroot/pchome.html index 2ae00533..b240b142 100644 --- a/projects/WebUI/wwwroot/pchome.html +++ b/projects/WebUI/wwwroot/pchome.html @@ -23,14 +23,14 @@
-
+
- +
-

{{model.nickName}}

-

{{model.userName}}

+

{{user.nickName}}

+

{{user.userName}}

diff --git a/projects/WebUI/wwwroot/pchome.js b/projects/WebUI/wwwroot/pchome.js index c1bbc395..0eae93a6 100644 --- a/projects/WebUI/wwwroot/pchome.js +++ b/projects/WebUI/wwwroot/pchome.js @@ -5,17 +5,15 @@ template: response.data, data() { return { - model: null }; }, mounted: function () { console.log('mounted:home'); weui.tab('#tab', { defaultIndex: 1 }); var url = '/UserCenter/api/v1/user/getUserInfo'; - var component = this; axios.get(url) .then(function (response) { - component.model = response.data; + store.commit('setUser', response.data); }) .catch(function (error) { }) @@ -28,7 +26,12 @@ localStorage.removeItem('refreshToken'); router.push('/login'); } - } + }, + computed: { + user: function () { + return store.state.user; + } + }, }) }); }); diff --git a/projects/lib/package.json b/projects/lib/package.json index 8720c584..3a14dd2a 100644 --- a/projects/lib/package.json +++ b/projects/lib/package.json @@ -32,6 +32,7 @@ "urijs": "1.19.2", "vue": "2.6.10", "vue-router": "3.1.3", + "vuex": "3.1.2", "metro4": "4.3.3", "toastr": "2.1.4", "weui": "2.1.3",