You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

2 years ago
  1. //1.读取日历属性
  2. function onReadCalendar() {
  3. WL.login({
  4. scope: "wl.calendars"
  5. }).then(
  6. function (response) {
  7. WL.api({
  8. path: "calendar.4fa89fa6142690ab",
  9. method: "GET"
  10. }).then(
  11. function (response) {
  12. document.getElementById("resultDiv").innerHTML =
  13. "ID: " + response.id +
  14. "<br/>Name: " + response.name;
  15. },
  16. function (responseFailed) {
  17. document.getElementById("infoArea").innerText =
  18. "Error calling API: " + responseFailed.error.message;
  19. }
  20. );
  21. },
  22. function (responseFailed) {
  23. document.getElementById("infoArea").innerText =
  24. "Error signing in: " + responseFailed.error_description;
  25. }
  26. );
  27. }
  28. //2.删除日历
  29. function onDeleteCalendar() {
  30. WL.login({
  31. scope: "wl.calendars_update"
  32. }).then(
  33. function (response) {
  34. WL.api({
  35. path: "calendar.a6b2a7e8f2515e5e.11a088a04c28495e8672ecf2bf645461",
  36. method: "DELETE"
  37. }).then(
  38. function (response) {
  39. document.getElementById("resultDiv").innerHTML = "Deleted.";
  40. },
  41. function (responseFailed) {
  42. document.getElementById("infoArea").innerText =
  43. "Error calling API: " + responseFailed.error.message;
  44. }
  45. );
  46. },
  47. function (responseFailed) {
  48. document.getElementById("infoArea").innerText =
  49. "Error signing in: " + responseFailed.error_description;
  50. }
  51. );
  52. }
  53. //3.创建日历
  54. function onCreateCalendar() {
  55. WL.login({
  56. scope: "wl.calendars_update "
  57. }).then(
  58. function (response) {
  59. debugger;
  60. WL.api({
  61. path: "me/calendars",
  62. method: "POST",
  63. body: {
  64. name: "My example calendar"
  65. }
  66. }).then(
  67. function (response) {
  68. debugger;
  69. document.getElementById("resultDiv").innerHTML =
  70. "ID: " + response.id +
  71. "<br/>Name: " + response.name;
  72. },
  73. function (responseFailed) {
  74. debugger;
  75. document.getElementById("infoArea").innerText =
  76. "Error calling API: " + responseFailed.error.message;
  77. }
  78. );
  79. },
  80. function (responseFailed) {
  81. document.getElementById("infoArea").innerText =
  82. "Error signing in: " + responseFailed.error_description;
  83. }
  84. );
  85. }
  86. //3.更新日历属性
  87. function onCreateCalendar() {
  88. WL.login({
  89. scope: "wl.calendars_update "
  90. }).then(
  91. function (response) {
  92. WL.api({
  93. path: "me/calendars",
  94. method: "POST",
  95. body: {
  96. name: "My example calendar"
  97. }
  98. }).then(
  99. function (response) {
  100. document.getElementById("resultDiv").innerHTML =
  101. "ID: " + response.id +
  102. "<br/>Name: " + response.name;
  103. },
  104. function (responseFailed) {
  105. document.getElementById("infoArea").innerText =
  106. "Error calling API: " + responseFailed.error.message;
  107. }
  108. );
  109. },
  110. function (responseFailed) {
  111. document.getElementById("infoArea").innerText =
  112. "Error signing in: " + responseFailed.error_description;
  113. }
  114. );
  115. }
  116. //4.更新日历属性
  117. function onUpdateCalendar() {
  118. WL.login({
  119. scope: "wl.calendars_update"
  120. }).then(
  121. function (response) {
  122. WL.api({
  123. path: "calendar.a6b2a7e8f2515e5e.11a088a04c28495e8672ecf2bf645461",
  124. method: "PUT",
  125. body: {
  126. name: "My example calendar updated"
  127. }
  128. }).then(
  129. function (response) {
  130. document.getElementById("resultDiv").innerHTML =
  131. "ID: " + response.id +
  132. "<br/>Name: " + response.name;
  133. },
  134. function (responseFailed) {
  135. document.getElementById("infoArea").innerText =
  136. "Error calling API: " + responseFailed.error.message;
  137. }
  138. );
  139. },
  140. function (responseFailed) {
  141. document.getElementById("infoArea").innerText =
  142. "Error signing in: " + responseFailed.error_description;
  143. }
  144. );
  145. }
  146. //读取日历事件
  147. function onReadEvent() {
  148. WL.login({
  149. scope: "wl.calendars"
  150. }).then(
  151. function (response) {
  152. debugger;
  153. WL.api({
  154. path: "me/events",
  155. method: "GET"
  156. }).then(
  157. function (response) {
  158. debugger;
  159. document.getElementById("resultDiv").innerHTML =
  160. "ID: " + response.id +
  161. "<br/>Location: " + response.location;
  162. },
  163. function (responseFailed) {
  164. debugger;
  165. document.getElementById("infoArea").innerText =
  166. "Error calling API: " + responseFailed.error.message;
  167. }
  168. );
  169. },
  170. function (responseFailed) {
  171. document.getElementById("infoArea").innerText =
  172. "Error signing in: " + responseFailed.error_description;
  173. }
  174. );
  175. }
  176. //6.删除日历事件
  177. function onDeleteEvent() {
  178. WL.login({
  179. scope: "wl.calendars_update"
  180. }).then(
  181. function (response) {
  182. WL.api({
  183. path: "event.a6b2a7e8f2515e5e.16c27f2b66ac4ffdae6d59236b692e20.21e30d8fc00841b8841dd16c7ec7a503",
  184. method: "DELETE"
  185. }).then(
  186. function (response) {
  187. document.getElementById("resultDiv").innerHTML = "Deleted";
  188. },
  189. function (responseFailed) {
  190. document.getElementById("infoArea").innerText =
  191. "Error calling API: " + responseFailed.error.message;
  192. }
  193. );
  194. },
  195. function (responseFailed) {
  196. document.getElementById("infoArea").innerText =
  197. "Error signing in: " + responseFailed.error_description;
  198. }
  199. );
  200. }
  201. //7.創建日历事件
  202. function onCreateEvent() {
  203. WL.login({
  204. scope: "wl.events_create"
  205. }).then(
  206. function (response) {
  207. debugger;
  208. WL.api({
  209. path: "me/events",
  210. method: "POST",
  211. body: {
  212. name: "Family Dinner",
  213. description: "Dinner with Cynthia's family",
  214. start_time: "2018-04-18T09:30:00-08:00",
  215. end_time: "2018-04-18T11:00:00-08:00",
  216. location: "Coho Vineyard and Winery, 123 Main St., Redmond WA 19532",
  217. is_all_day_event: "false",
  218. availability: "busy",
  219. visibility: "public"
  220. }
  221. }).then(
  222. function (response) {
  223. debugger;
  224. document.getElementById("resultDiv").innerHTML =
  225. "ID: " + response.id +
  226. "<br/>Name: " + response.name;
  227. },
  228. function (responseFailed) {
  229. debugger;
  230. document.getElementById("infoArea").innerText =
  231. "Error calling API: " + responseFailed.error.message;
  232. }
  233. );
  234. },
  235. function (responseFailed) {
  236. document.getElementById("infoArea").innerText =
  237. "Error signing in: " + responseFailed.error_description;
  238. }
  239. );
  240. }
  241. //8.更新日历事件
  242. function onUpdateEvent() {
  243. WL.login({
  244. scope: "wl.calendars_update"
  245. }).then(
  246. function (response) {
  247. WL.api({
  248. path: "event.a6b2a7e8f2515e5e.16c27f2b66ac4ffdae6d59236b692e20.21e30d8fc00841b8841dd16c7ec7a503",
  249. method: "PUT",
  250. body: {
  251. name: "My example event has changed"
  252. }
  253. }).then(
  254. function (response) {
  255. document.getElementById("resultDiv").innerHTML =
  256. "ID: " + response.id +
  257. "<br/>Name: " + response.name;
  258. },
  259. function (responseFailed) {
  260. document.getElementById("infoArea").innerText =
  261. "Error calling API: " + responseFailed.error.message;
  262. }
  263. );
  264. },
  265. function (responseFailed) {
  266. document.getElementById("infoArea").innerText =
  267. "Error signing in: " + responseFailed.error_description;
  268. }
  269. );
  270. }
  271. $(function () {
  272. $(':input[type="button"]').on('click', function () {
  273. switch (this.id) {
  274. case 'id1':
  275. onReadCalendar();//读取日历属性
  276. break;
  277. case 'id2':
  278. onDeleteCalendar();//删除日历
  279. break;
  280. case 'id3':
  281. onCreateCalendar();//创建日历
  282. break;
  283. case 'id4':
  284. onUpdateCalendar();//更新日历属性
  285. break;
  286. case 'id5':
  287. onReadEvent();//读取日历事件
  288. break;
  289. case 'id6':
  290. onDeleteEvent();//删除日历事件
  291. break;
  292. case 'id7':
  293. onCreateEvent();//建日历事件
  294. break;
  295. case 'id8':
  296. onUpdateEvent();//更新日历事件
  297. break;
  298. }
  299. });
  300. });
  301. $(function () {
  302. // Check for browser support for sessionStorage
  303. if (typeof (Storage) === 'undefined') {
  304. render('#unsupportedbrowser');
  305. return;
  306. }
  307. // Check for browser support for crypto.getRandomValues
  308. var cryptObj = window.crypto || window.msCrypto; // For IE11
  309. if (cryptObj === undefined || cryptObj.getRandomValues === 'undefined') {
  310. render('#unsupportedbrowser');
  311. return;
  312. }
  313. render(window.location.hash);
  314. $(window).on('hashchange', function () {
  315. render(window.location.hash);
  316. });
  317. function render(hash) {
  318. var action = hash.split('=')[0];
  319. // Hide everything
  320. $('.main-container .page').hide();
  321. var isAuthenticated = false;
  322. var pagemap = {
  323. // Welcome page
  324. '': function () {
  325. renderWelcome(isAuthenticated);
  326. },
  327. // Receive access token
  328. // Signout
  329. // Error display
  330. // Display inbox
  331. // Shown if browser doesn't support session storage
  332. '#unsupportedbrowser': function () {
  333. $('#unsupported').show();
  334. }
  335. }
  336. if (pagemap[action]) {
  337. pagemap[action]();
  338. } else {
  339. // Redirect to home page
  340. window.location.hash = '#';
  341. }
  342. }
  343. function setActiveNav(navId) {
  344. $('#navbar').find('li').removeClass('active');
  345. $(navId).addClass('active');
  346. }
  347. function renderWelcome(isAuthed) {
  348. if (isAuthed) {
  349. $('#username').text(sessionStorage.userDisplayName);
  350. $('#logged-in-welcome').show();
  351. setActiveNav('#home-nav');
  352. } else {
  353. $('#connect-button').attr('href', buildAuthUrl());
  354. $('#signin-prompt').show();
  355. }
  356. }
  357. // OAUTH FUNCTIONS =============================
  358. // OUTLOOK API FUNCTIONS =======================
  359. // HELPER FUNCTIONS ============================
  360. });