app.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { showToast } from './utils/feedback'
  2. import { formatTime, arrRemoveObj, getTypeItem,setCache } from './utils/util'
  3. import * as socketApi from './utils/websocket'
  4. const baseUrl = "https://dp-test.g107.com/";//测试
  5. // const baseUrl = "https://ding.insys.g107.com/";//正式
  6. var that;
  7. var yearArr = [];
  8. var year = formatTime(new Date()).year;
  9. for (var i = 2018; i <= year; i++) {
  10. yearArr.push(i);
  11. }
  12. App({
  13. //自定义全局变量
  14. globalData: {
  15. baseUrl: baseUrl,
  16. webScoketUrl: "test-ding.g107.com",//webScoket测试
  17. // webScoketUrl: "ding.insys.g107.com",//webScoket正式
  18. showToast: showToast,//提示框
  19. arrRemoveObj: arrRemoveObj,//删除数组的某一项
  20. year: formatTime(new Date()).year,//当前年
  21. month: formatTime(new Date()).month,//当前月
  22. day: formatTime(new Date()).day,//当前日
  23. month_tow: formatTime(new Date()).month_tow,//当前月份
  24. allDate: formatTime(new Date()).allDate,//带时分
  25. types: [],
  26. userData:"",
  27. token:'',
  28. // userData: { "id": 28, "user_id": "102250695436416908", "unionid": "KFnssd5hpjl3mWiinu02vCAiEiE", "manager_userid": [], "name": "郭家裕", "img_url": "https://static-legacy.dingtalk.com/media/lADPBF8a7BNICxrNAcDNAcA_448_448.jpg", "is_creator": 0, "post": "", "is_official": 1, "is_scope": 1, "dept_list": [{ "name": "总部", "path": [1, 407144118], "dept_id": 407144118 }, { "name": "挖坑", "path": [1, 407144118, 407102191], "dept_id": 407102191 }, { "name": "探测", "path": [1, 407144118, 406992333], "dept_id": 406992333 }], "site": { "id": 2, "is_personal": 0, "name": "旺旺队2", "is_official": 1, "creator_ids": [20], "user_count_max": 50, "industry": "计算机软件", "logo_url": "", "expire_time": 1676537085, "config": { "assessment": { "feedback": 0, "point_scope": { "manager": 1, "special": 1, "employee": 2, "reviewer": 2, "same_level": 1 }, "point_comment": { "manager": 2, "special": 1, "employee": 1, "reviewer": 2, "same_level": 1 }, "result_distribution": 2 }, "level_type": 1, "level_scope": { "levels": [{ "name": "C", "value": 60 }, { "name": "B", "value": 80 }, { "name": "A", "value": 100 }, { "name": "S", "value": 120 }] }, "level_enable": 1, "level_normal": { "levels": [], "distribution_type": 1 }, "calc_dimension": 1 } }, "permission_info": { "id": 28, "name": "郭家裕", "img_url": "https://static-legacy.dingtalk.com/media/lADPBF8a7BNICxrNAcDNAcA_448_448.jpg", "permission": [{ "id": 1, "name": "performance", "remark": "绩效考核" }, { "id": 2, "name": "okr", "remark": "OKR" }, { "id": 3, "name": "range_all", "remark": "全公司" }, { "id": 6, "name": "assessment_report", "remark": "考核管理 可见考核结果" }, { "id": 7, "name": "assessment_initiate", "remark": "考核管理 发起考核" }, { "id": 8, "name": "assessment_scoring", "remark": "考核管理 开始评分" }, { "id": 9, "name": "assessment_result", "remark": "考核管理 调整结果" }, { "id": 10, "name": "assessment_level", "remark": "考核管理 调整等级" }, { "id": 11, "name": "assessment_export", "remark": "考核管理 导出报表" }, { "id": 12, "name": "index", "remark": "指标库管理" }, { "id": 14, "name": "group_all", "remark": "全部考评组管理" }, { "id": 15, "name": "base", "remark": "基础设置" }, { "id": 16, "name": "performance_report", "remark": "绩效报表" }], "main": 1, "per_role": "manager", "group_ids": [] }, "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOm51bGwsImlhdCI6MTY0NTYwNzUxMSwiZXhwIjotMSwibmJmIjoxNjQ1NjA3NTExLCJqdGkiOiJra0QyTXA0Wjl3c2lEbUdEIiwic3ViIjoyOCwicHJ2IjoiY2E2NDg5ZDUwZjI0MDdhNjc4MzBlODA5MGQwMTQ4ODM1Njg1OTYyYiIsInJvbGUiOiJlbXBsb3llZSJ9.ZFRMqvGU24tsBbOIbs476fiOKWltSNntsrb4DkBc10o" },
  29. // token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOm51bGwsImlhdCI6MTY0NTYwNzUxMSwiZXhwIjotMSwibmJmIjoxNjQ1NjA3NTExLCJqdGkiOiJra0QyTXA0Wjl3c2lEbUdEIiwic3ViIjoyOCwicHJ2IjoiY2E2NDg5ZDUwZjI0MDdhNjc4MzBlODA5MGQwMTQ4ODM1Njg1OTYyYiIsInJvbGUiOiJlbXBsb3llZSJ9.ZFRMqvGU24tsBbOIbs476fiOKWltSNntsrb4DkBc10o',
  30. ALIOSS_URL: 'https://gdyapp.oss-cn-shenzhen.aliyuncs.com',
  31. imgHttpUrl: 'https://intesys.cms.g107.com/integral.php/Api/get_signature',
  32. corpId: '',
  33. yearArr: yearArr,
  34. isAdministrator: false,//是否管理员
  35. isCreator: false,//是否是创始人
  36. corpMessage: '',//企业套餐信息
  37. usersList: [],//缓存的人员列表
  38. socketApi: socketApi,//长连接
  39. },
  40. //获取积分类型
  41. getTypes(callBack) {
  42. this.$get("api/integral/types", {}).then((res) => {
  43. this.globalData.types = res.data.data.list;
  44. callBack();
  45. })
  46. },
  47. //获取套餐信息
  48. getCorp(corpId) {
  49. this.$get("api/order/corp", { corp_id: corpId }).then((res) => {
  50. this.globalData.corpMessage = res.data.data;
  51. })
  52. },
  53. routerGet2(url, data = {}, Accept) {
  54. return new Promise(function (resolve, reject) {
  55. dd.httpRequest({
  56. url: baseUrl + url,
  57. method: 'GET',
  58. headers: {
  59. 'Content-Type': 'application/x-www-form-urlencoded',
  60. 'A-Token': that.globalData.token,
  61. 'Accept': Accept ? Accept : ''
  62. },
  63. data: data,
  64. timeout: 10000,
  65. dataType: 'json',
  66. success: function (res) {
  67. if (res.data.code == 1) {
  68. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  69. that.globalData.token = res.data.refresh_token
  70. }
  71. resolve(res);
  72. } else if (res.data.code == 401) {
  73. that.globalData.showToast("授权已过期");
  74. // reject(res);
  75. } else {
  76. that.globalData.showToast(res.data.msg)
  77. // reject(res);
  78. }
  79. },
  80. fail: function (res) {
  81. if (res.errorMessage == 'NETWORK ERROR') {
  82. dd.alert({
  83. title: '网络异常',
  84. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  85. buttonText: '我知道了',
  86. success: () => { },
  87. });
  88. return;
  89. }
  90. reject(res);
  91. }
  92. });
  93. })
  94. },
  95. routerGet(url, data = {}, Accept) {
  96. return new Promise(function (resolve, reject) {
  97. dd.showLoading();
  98. dd.httpRequest({
  99. url: baseUrl + url,
  100. method: 'GET',
  101. timeout: 10000,
  102. headers: {
  103. 'Content-Type': 'application/x-www-form-urlencoded',
  104. 'A-Token': that.globalData.token,
  105. 'Accept': Accept ? Accept : ''
  106. },
  107. data: data,
  108. dataType: 'json',
  109. success: function (res) {
  110. if (res.data.code == 1) {
  111. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  112. that.globalData.token = res.data.refresh_token
  113. }
  114. resolve(res);
  115. } else if (res.data.code == 401) {
  116. that.globalData.showToast("授权已过期");
  117. // reject(res);
  118. } else {
  119. that.globalData.showToast(res.data.msg)
  120. // reject(res);
  121. }
  122. },
  123. fail: function (res) {
  124. if (res.errorMessage == 'NETWORK ERROR') {
  125. dd.alert({
  126. title: '网络异常',
  127. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  128. buttonText: '我知道了',
  129. success: () => { },
  130. });
  131. return;
  132. }
  133. reject(res);
  134. },
  135. complete: function () {
  136. dd.hideLoading();
  137. }
  138. });
  139. })
  140. },
  141. routerPost(url, data = {}, Accept) {
  142. return new Promise(function (resolve, reject) {
  143. dd.httpRequest({
  144. url: baseUrl + url,
  145. method: 'POST',
  146. headers: {
  147. 'Content-Type': 'application/x-www-form-urlencoded',
  148. 'A-Token': that.globalData.token,
  149. 'Accept': Accept ? Accept : ''
  150. },
  151. timeout: 10000,
  152. data: data,
  153. dataType: 'json',
  154. success: function (res) {
  155. if (res.data.code == 1) {
  156. if (res.refresh_token && res.data.refresh_token != that.globalData.token) {
  157. that.globalData.token = res.data.refresh_token
  158. }
  159. resolve(res);
  160. } else if (res.data.code == 401) {
  161. that.globalData.showToast("授权已过期");
  162. reject(res);
  163. } else if (res.data.code == 3000) {
  164. reject(res);
  165. } else {
  166. that.globalData.showToast(res.data.msg);
  167. reject(res);
  168. }
  169. },
  170. fail: function (res) {
  171. if (res.error == 19 || res.error == 13) {
  172. if (url == 'api/integral/review/a/entry' || url == 'api/integral/point/entry') {
  173. let sum = 0;
  174. if (data) {
  175. sum = Number(JSON.parse(data.items).length) * Number(JSON.parse(data.members).length);
  176. }
  177. that.$post('api/e', { sum: sum, message: '钉钉小程序' }).then(res => { })
  178. }
  179. }
  180. if (res.errorMessage == 'NETWORK ERROR') {
  181. dd.alert({
  182. title: '网络异常',
  183. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  184. buttonText: '我知道了',
  185. success: () => { },
  186. });
  187. return;
  188. }
  189. reject(res);
  190. }
  191. });
  192. })
  193. },
  194. //封装post
  195. $post(url, data = {}, Accept) {
  196. return that.routerPost(url, data, Accept);
  197. },
  198. //封装get
  199. $get(url, data = {}, Accept) {
  200. return that.routerGet(url, data, Accept);
  201. },
  202. //封装get
  203. $get2(url, data = {}, Accept) {
  204. return that.routerGet2(url, data, Accept);
  205. },
  206. //免登 isShowStart是否显示引导页
  207. login(corpId, callBack, errBack = function () { }, isShowStart = false) {
  208. if (that.globalData.token) {
  209. callBack(true)
  210. } else {
  211. dd.getAuthCode({
  212. success: function (res) {
  213. that.$post('api/ding/login', { authCode: res.authCode, corpId: corpId }).then(res => {
  214. if (res.data.code == 1) {
  215. let user = res.data.data;
  216. var isStart = false;
  217. if (user.is_official == 1) {
  218. if (user.is_creator) {
  219. that.globalData.isCreator = true;
  220. isStart = true;
  221. }
  222. that.globalData.token = user.token;
  223. that.globalData.userData = user;
  224. var getIsStart = dd.getStorageSync({ key: 'isStart' });//判断是否需要打开引导页
  225. console.log(JSON.stringify(user));
  226. // that.getUserList();//缓存人员列表
  227. that.setRouters();
  228. if (isStart && !getIsStart.data && isShowStart) {//判断是否需要打开引导
  229. dd.reLaunch({
  230. url: '../start/start'
  231. })
  232. } else {
  233. callBack(true);
  234. }
  235. } else {
  236. callBack(false);
  237. }
  238. } else if (res.data.code == 3000) {
  239. that.globalData.showToast("员工信息同步中,请稍后进入")
  240. } else {
  241. that.globalData.showToast(res.msg)
  242. }
  243. }).catch(err => {
  244. errBack(err.data);
  245. return
  246. if (err.data.code == 2001) {
  247. dd.confirm({
  248. title: '尊敬的用户',
  249. content: '你当前的套餐已到期',
  250. confirmButtonText: '去续费',
  251. cancelButtonText: '暂不需要',
  252. success: (result) => {
  253. if (result.confirm) {
  254. dd.reLaunch({
  255. url: `../../deploy/addMoney/addMoney`
  256. })
  257. }
  258. },
  259. });
  260. }
  261. })
  262. },
  263. fail: function (err) {
  264. console.log(JSON.stringify(err))
  265. that.globalData.showToast("错误" + JSON.stringify(err))
  266. }
  267. });
  268. }
  269. },
  270. //返回用户Code
  271. returnCode(data) {
  272. let list=this.globalData.usersList;
  273. if(Array.isArray(data)){
  274. let result =data.map(e=>{
  275. if(list[e]){
  276. return list[e].code
  277. }
  278. })
  279. return JSON.stringify(result)
  280. }else{
  281. data=data.toString()
  282. var arr = data.split(",");
  283. let result =arr.map(e=>{
  284. if(list[e]){
  285. return list[e].code
  286. }
  287. })
  288. return result.toString()
  289. }
  290. },
  291. setRouters() {
  292. let main = this.globalData.userData.permission_info.main;
  293. let per_role = this.globalData.userData.permission_info.per_role; // 用户的身份
  294. if (per_role == 'manager') {
  295. if (main == 1) {
  296. // 主管理员
  297. this.plusStoSet('masterAdministrator');
  298. } else {
  299. // 子管理员
  300. this.plusStoSet('childAdministrator');
  301. }
  302. } else {
  303. // 员工||创始人||部门管理员
  304. // if (this.globalData.isCreator) {
  305. // this.plusStoSet('creator');
  306. // } else
  307. if (this.globalData.userData.manage_dept_list.length > 0) {
  308. this.plusStoSet('deptManager');
  309. } else {
  310. this.plusStoSet('employee');
  311. }
  312. }
  313. },
  314. plusStoSet(val) {
  315. setCache('role', val);
  316. },
  317. // 判断当前登录者是否有某项权限
  318. getPermis(type){
  319. /* type为传入的权限id*/
  320. let jurisdictions = this.globalData.userData.permission_info.permission.map((item) => {
  321. return item.id
  322. })
  323. return jurisdictions.indexOf(type) >= 0
  324. },
  325. getUserList() {
  326. this.$get("api/per/user/employee_list", { page: 0, page_size: 0 }).then((res) => {
  327. let list = res.data.data.list
  328. let data = {}
  329. for (let i in list) {
  330. if (list[i].is_scope === 0||list[i].is_official ===0) {
  331. list[i].name = list[i].name + '(未启用)';
  332. }
  333. data[list[i].id] = list[i];
  334. }
  335. that.globalData.usersList = data;
  336. })
  337. },
  338. getTypesItem(id) {
  339. return getTypeItem(this.globalData.types, id);
  340. },
  341. updateApp() {
  342. const updateManager = dd.getUpdateManager()
  343. updateManager.onCheckForUpdate(function (res) {
  344. // 请求完新版本信息的回调
  345. // console.log(res.hasUpdate) // 是否有更新
  346. })
  347. updateManager.onUpdateReady(function (ret) {
  348. // console.log(ret.version) // 更新版本号
  349. dd.confirm({
  350. title: '更新提示',
  351. content: '新版本已经准备好,是否重启应用?',
  352. success: function (res) {
  353. if (res.confirm) {
  354. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  355. updateManager.applyUpdate()
  356. }
  357. }
  358. })
  359. })
  360. updateManager.onUpdateFailed(function () {
  361. // 新版本下载失败
  362. })
  363. },
  364. onLaunch(options) {
  365. dd.globalData = this.globalData;
  366. that = this;
  367. that.updateApp();
  368. // that.getTypes(function () { });
  369. // that.getCorp(options.query.corpId);
  370. that.globalData.corpId = options.query.corpId;
  371. },
  372. });