common.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. var common_vendor = require("../common/vendor.js");
  3. var api_user = require("../api/user.js");
  4. function isEmpty(obj) {
  5. if (obj == "undefined" || obj == null || obj == "") {
  6. return true;
  7. } else {
  8. return false;
  9. }
  10. }
  11. function isLogin() {
  12. return new Promise((resolve, reject) => {
  13. api_user.checkLogin().then((res) => {
  14. if (res.code == 200) {
  15. resolve(true);
  16. } else {
  17. resolve(false);
  18. }
  19. }, (rej) => {
  20. });
  21. });
  22. }
  23. function isLoginCourse() {
  24. return new Promise((resolve, reject) => {
  25. let token = common_vendor.index.getStorageSync("AppTokenmini_RTCourse");
  26. if (token == null || token == void 0 || token == "") {
  27. resolve(false);
  28. } else {
  29. resolve(true);
  30. }
  31. });
  32. }
  33. function getDictLabelName(dicts, dictValue) {
  34. if (dictValue == null) {
  35. return "";
  36. }
  37. var name = "";
  38. dicts.forEach(function(item, index, array) {
  39. if (dictValue.toString() == item.dictValue.toString()) {
  40. name = item.dictLabel;
  41. }
  42. });
  43. return name;
  44. }
  45. function parseText(txt, len) {
  46. if (txt.length > len) {
  47. var text = txt.substr(0, len) + "...";
  48. return text;
  49. }
  50. return txt;
  51. }
  52. function parseIDCardInfo(idCard) {
  53. var reg = /^\d{17}[\dXx]$/;
  54. if (reg.test(idCard)) {
  55. var birthday = idCard.substring(6, 14);
  56. var year = birthday.substring(0, 4);
  57. var month = birthday.substring(4, 6);
  58. var day = birthday.substring(6, 8);
  59. var currentYear = new Date().getFullYear();
  60. var age = currentYear - parseInt(year);
  61. var genderCode = parseInt(idCard.charAt(16));
  62. var gender = genderCode % 2 === 0 ? "\u5973" : "\u7537";
  63. return {
  64. birthday: year + "-" + month + "-" + day,
  65. age,
  66. gender
  67. };
  68. }
  69. return null;
  70. }
  71. function getProvider(service) {
  72. return new Promise((resolve, reject) => {
  73. common_vendor.index.getProvider({
  74. service: service || "oauth",
  75. success: function(res) {
  76. if (res.provider) {
  77. resolve(res.provider[0]);
  78. }
  79. },
  80. fail() {
  81. reject("\u83B7\u53D6\u73AF\u5883\u670D\u52A1\u5546\u5931\u8D25");
  82. }
  83. });
  84. }).catch((error) => {
  85. console.log("167", error);
  86. });
  87. }
  88. function parsePhone(mobile) {
  89. console.log(mobile);
  90. if (mobile != null) {
  91. var str = mobile.substr(0, 3) + "****" + mobile.substr(7);
  92. return str;
  93. }
  94. }
  95. function getAge(strBirthday) {
  96. var returnAge, strBirthdayArr = strBirthday.split("-"), birthYear = strBirthdayArr[0], birthMonth = strBirthdayArr[1], birthDay = strBirthdayArr[2], d = new Date(), nowYear = d.getFullYear(), nowMonth = d.getMonth() + 1, nowDay = d.getDate();
  97. if (nowYear == birthYear) {
  98. returnAge = 0;
  99. } else {
  100. var ageDiff = nowYear - birthYear;
  101. if (ageDiff > 0) {
  102. if (nowMonth == birthMonth) {
  103. var dayDiff = nowDay - birthDay;
  104. if (dayDiff < 0) {
  105. returnAge = ageDiff - 1;
  106. } else {
  107. returnAge = ageDiff;
  108. }
  109. } else {
  110. var monthDiff = nowMonth - birthMonth;
  111. if (monthDiff < 0) {
  112. returnAge = ageDiff - 1;
  113. } else {
  114. returnAge = ageDiff;
  115. }
  116. }
  117. } else {
  118. returnAge = -1;
  119. }
  120. }
  121. return returnAge;
  122. }
  123. function parseIdCard(idCard) {
  124. var str = idCard.substr(0, 4) + "****" + idCard.substr(8);
  125. return str;
  126. }
  127. function urlToObj(url) {
  128. let obj = {};
  129. let str = url.slice(url.indexOf("?") + 1);
  130. let arr = str.split("&");
  131. for (let j = arr.length, i = 0; i < j; i++) {
  132. let arr_temp = arr[i].split("=");
  133. obj[arr_temp[0]] = arr_temp[1];
  134. }
  135. return obj;
  136. }
  137. function logout() {
  138. common_vendor.index.setStorageSync("AppToken", null);
  139. }
  140. function clearHisSearch() {
  141. var searchList = [];
  142. common_vendor.index.setStorageSync("hisSearch", JSON.stringify(searchList));
  143. }
  144. function getHisSearch() {
  145. var search = common_vendor.index.getStorageSync("hisSearch");
  146. if (search != null && search != void 0 && search != "") {
  147. var search = JSON.parse(search);
  148. return search;
  149. } else {
  150. var data = [];
  151. return data;
  152. }
  153. }
  154. function addHisSearch(searchVal) {
  155. var search = common_vendor.index.getStorageSync("hisSearch");
  156. var searchList = [];
  157. console.log(searchList);
  158. if (search != null && search != void 0 && search != "") {
  159. searchList = JSON.parse(search);
  160. }
  161. searchList.push(searchVal);
  162. const uniqueArr = [...new Set(searchList)];
  163. common_vendor.index.setStorageSync("hisSearch", JSON.stringify(uniqueArr));
  164. }
  165. function generateRandomString(length) {
  166. let result = "";
  167. const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  168. const charactersLength = characters.length;
  169. for (let i = 0; i < length; i++) {
  170. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  171. }
  172. return result;
  173. }
  174. exports.addHisSearch = addHisSearch;
  175. exports.clearHisSearch = clearHisSearch;
  176. exports.generateRandomString = generateRandomString;
  177. exports.getAge = getAge;
  178. exports.getDictLabelName = getDictLabelName;
  179. exports.getHisSearch = getHisSearch;
  180. exports.getProvider = getProvider;
  181. exports.isEmpty = isEmpty;
  182. exports.isLogin = isLogin;
  183. exports.isLoginCourse = isLoginCourse;
  184. exports.logout = logout;
  185. exports.parseIDCardInfo = parseIDCardInfo;
  186. exports.parseIdCard = parseIdCard;
  187. exports.parsePhone = parsePhone;
  188. exports.parseText = parseText;
  189. exports.urlToObj = urlToObj;