App.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <div id="app">
  3. <div class="top">
  4. <p v-if="account">{{ '欢迎 ' + account.displayName }}</p>
  5. <p v-else>欢迎使用野火IM工作台</p>
  6. </div>
  7. <div v-if="!showManageFavApp" class="apps-container">
  8. <div class="title-action-container">
  9. <p class="title">我的</p>
  10. <p class="action" @click="showManageFavAppView">管理</p>
  11. </div>
  12. <div class="apps">
  13. <div v-for="(app, index) in favApps" :key="index" class="app" @click="openApp(app)">
  14. <img :src="app.portraitUrl">
  15. <p>{{ app.name }}</p>
  16. </div>
  17. </div>
  18. <p v-if="favApps.length === 0" class="empty">没有应用,请点击管理按钮进行配置</p>
  19. </div>
  20. <div v-if="showManageFavApp" class="apps-container">
  21. <div class="title-action-container">
  22. <p class="title">管理</p>
  23. <div class="action-container">
  24. <p class="action" @click="cancelManageFavApp">取消</p>
  25. <p class="action" @click="manageFavApp">确定</p>
  26. </div>
  27. </div>
  28. <div class="apps">
  29. <div v-for="(app, index) in apps" :key="index" class="app" :class="{checked: app._checked}" @click="checkApp(app)">
  30. <img :src="app.portraitUrl">
  31. <p>{{ app.name }}</p>
  32. <input type="checkbox" :value="app.targetId" v-model="checkedAppIds">
  33. </div>
  34. </div>
  35. <p v-if="apps.length === 0" class="empty">没有应用,请到开放平台创建</p>
  36. </div>
  37. <div class="apps-container">
  38. <p class="title">全员</p>
  39. <div class="apps">
  40. <div v-for="(app, index) in globalApps" :key="index" class="app" @click="openApp(app)">
  41. <img :src="app.portraitUrl">
  42. <p>{{ app.name }}</p>
  43. </div>
  44. </div>
  45. <p v-if="globalApps.length === 0" class="empty">没有应用,请到开放平台添加</p>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. import wf from "@/jssdk/wf";
  51. import api from "@/api/api";
  52. import './jssdk/bridgeClientImpl.uni'
  53. export default {
  54. name: 'App',
  55. data() {
  56. return {
  57. account: null,
  58. favApps: [],
  59. apps: [], // 非 globalApps
  60. globalApps: [],
  61. checkedAppIds: [],
  62. showManageFavApp: false,
  63. }
  64. },
  65. components: {},
  66. created() {
  67. document.title = '野火IM工作台'
  68. this.getAppList();
  69. this.getAccount();
  70. },
  71. methods: {
  72. getAccount(failToLogin = true) {
  73. api.getAccount().then(account => {
  74. this.account = account;
  75. this.getFavAppList();
  76. }).catch(reason => {
  77. wf.toast('开放平台登录中...')
  78. if (reason.code === 13 && failToLogin) {
  79. console.log('getAuthCode and login')
  80. this.login();
  81. }
  82. })
  83. },
  84. getAppList() {
  85. api.getAppList().then((apps) => {
  86. console.log('apps', apps)
  87. this.apps = apps.filter(app => app.global !== true)
  88. this.globalApps = apps.filter(app => app.global === true)
  89. });
  90. },
  91. getFavAppList() {
  92. api.getFavAppList().then(favApps => {
  93. this.favApps = favApps;
  94. this.favApps.forEach(app => {
  95. this.checkedAppIds.push(app.targetId);
  96. })
  97. }).catch(reason => {
  98. console.log('getFavAppList error', reason)
  99. })
  100. },
  101. login() {
  102. // type: 0, robot; 1, channel; 2, admin
  103. wf.biz.getAuthCode('wfcadmin', 2, (authCode) => {
  104. console.log('getAuthCode success', authCode)
  105. api.login({
  106. appId: 'wfcadmin',
  107. appType: 2,
  108. authCode: authCode,
  109. }).then(() => {
  110. this.getFavAppList(false);
  111. this.getAccount(false);
  112. }).catch(reason => {
  113. console.log('login failed', reason);
  114. wf.toast('开放平台登录失败 ' + reason);
  115. })
  116. }, err => {
  117. console.log('getAuthCode error', err)
  118. })
  119. },
  120. showManageFavAppView() {
  121. if (this.account) {
  122. this.showManageFavApp = true;
  123. } else {
  124. console.log('not login, to login')
  125. this.login();
  126. }
  127. },
  128. openApp(app) {
  129. let url = process ? app.desktopUrl : app.mobileUrl;
  130. wf.openUrl(url);
  131. },
  132. checkApp(app) {
  133. let index = this.checkedAppIds.indexOf(app.targetId);
  134. if (index >= 0) {
  135. this.checkedAppIds = this.checkedAppIds.filter(id => id !== app.targetId);
  136. } else {
  137. this.checkedAppIds.push(app.targetId);
  138. }
  139. },
  140. cancelManageFavApp() {
  141. this.showManageFavApp = false;
  142. this.checkedAppIds = [];
  143. },
  144. manageFavApp() {
  145. let toUnFavApps = [];
  146. let toFavApps = [];
  147. this.favApps.forEach(app => {
  148. if (this.checkedAppIds.indexOf(app.targetId) === -1) {
  149. toUnFavApps.push(app.targetId);
  150. }
  151. })
  152. this.checkedAppIds.forEach(targetId => {
  153. if (this.favApps.length > 0) {
  154. if (this.favApps.indexOf(targetId) === -1) {
  155. toFavApps.push(targetId);
  156. }
  157. } else {
  158. toFavApps.push(targetId);
  159. }
  160. })
  161. console.log('manageFavApp', toFavApps, toUnFavApps);
  162. let fp = api.favApps(toFavApps);
  163. let ufp = api.unFavApps(toUnFavApps);
  164. Promise.all([fp, ufp]).then(value => {
  165. console.log('manageFavApp result', value);
  166. this.getFavAppList(false);
  167. })
  168. this.showManageFavApp = false;
  169. this.checkedAppIds = [];
  170. }
  171. }
  172. }
  173. </script>
  174. <style lang="css" scoped>
  175. @import "./assets/main.css";
  176. body {
  177. margin: 0;
  178. padding: 0;
  179. }
  180. #app {
  181. font-family: Avenir, Helvetica, Arial, sans-serif;
  182. -webkit-font-smoothing: antialiased;
  183. -moz-osx-font-smoothing: grayscale;
  184. text-align: center;
  185. color: #2c3e50;
  186. background-color: #EDEDED;
  187. width: 100vw;
  188. height: 100vh;
  189. }
  190. .top {
  191. height: 60px;
  192. display: flex;
  193. flex-direction: column;
  194. justify-content: center;
  195. align-items: flex-start;
  196. background-color: white;
  197. }
  198. .top p {
  199. padding-left: 15px;
  200. font-size: 18px;
  201. font-weight: bold;
  202. }
  203. .apps-container {
  204. background-color: white;
  205. border-radius: 5px;
  206. margin: 5px;
  207. }
  208. .apps-container .title {
  209. text-align: left;
  210. padding: 5px;
  211. }
  212. .title-action-container {
  213. display: flex;
  214. justify-content: space-between;
  215. align-items: center;
  216. }
  217. .title-action-container .action-container {
  218. display: flex;
  219. flex-direction: row;
  220. }
  221. .title-action-container .action {
  222. padding: 5px 10px;
  223. border-radius: 5px;
  224. }
  225. .title-action-container .action:active {
  226. background-color: lightgrey;
  227. }
  228. .apps-container .empty {
  229. padding: 10px 0;
  230. font-size: 14px;
  231. }
  232. .apps {
  233. display: grid;
  234. grid-template-columns: repeat(auto-fill, 80px);
  235. justify-content: space-between;
  236. }
  237. .app {
  238. margin: 5px 0;
  239. width: 80px;
  240. display: flex;
  241. flex-direction: column;
  242. justify-content: center;
  243. align-items: center;
  244. padding: 5px 0;
  245. position: relative;
  246. }
  247. .app:active {
  248. background-color: lightgrey;
  249. border-radius: 5px;
  250. }
  251. .app img {
  252. width: 40px;
  253. height: 40px;
  254. }
  255. .app p {
  256. margin: 5px 0 0 0;
  257. font-size: 12px;
  258. }
  259. .app input {
  260. position: absolute;
  261. top: 5px;
  262. left: 5px;
  263. }
  264. </style>