app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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://app109758.eapps.dingtalkcloud.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: "app109758.eapps.dingtalkcloud.com",
  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. ALIOSS_URL: 'https://gdyapp.oss-cn-shenzhen.aliyuncs.com',
  29. imgHttpUrl: 'https://intesys.cms.g107.com/integral.php/Api/get_signature',
  30. corpId: '',
  31. yearArr: yearArr,
  32. isAdministrator: false,//是否管理员
  33. isCreator: false,//是否是创始人
  34. corpMessage: '',//企业套餐信息
  35. usersList: [],//缓存的人员列表
  36. socketApi: socketApi,//长连接
  37. },
  38. //获取积分类型
  39. getTypes(callBack) {
  40. this.$get("api/integral/types", {}).then((res) => {
  41. this.globalData.types = res.data.data.list;
  42. callBack();
  43. })
  44. },
  45. //获取套餐信息
  46. getCorp(corpId) {
  47. this.$get("api/order/corp", { corp_id: corpId }).then((res) => {
  48. this.globalData.corpMessage = res.data.data;
  49. })
  50. },
  51. routerGet2(url, data = {}, Accept) {
  52. return new Promise(function (resolve, reject) {
  53. dd.httpRequest({
  54. url: baseUrl + url,
  55. method: 'GET',
  56. headers: {
  57. 'Content-Type': 'application/x-www-form-urlencoded',
  58. 'A-Token': that.globalData.token,
  59. 'Accept': Accept ? Accept : ''
  60. },
  61. data: data,
  62. timeout: 10000,
  63. dataType: 'json',
  64. success: function (res) {
  65. if (res.data.code == 1) {
  66. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  67. that.globalData.token = res.data.refresh_token
  68. }
  69. resolve(res);
  70. } else if (res.data.code == 401) {
  71. that.globalData.showToast("授权已过期");
  72. // reject(res);
  73. } else {
  74. that.globalData.showToast(res.data.msg)
  75. // reject(res);
  76. }
  77. },
  78. fail: function (res) {
  79. if (res.errorMessage == 'NETWORK ERROR') {
  80. dd.alert({
  81. title: '网络异常',
  82. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  83. buttonText: '我知道了',
  84. success: () => { },
  85. });
  86. return;
  87. }
  88. reject(res);
  89. }
  90. });
  91. })
  92. },
  93. routerGet(url, data = {}, Accept) {
  94. return new Promise(function (resolve, reject) {
  95. dd.showLoading();
  96. dd.httpRequest({
  97. url: baseUrl + url,
  98. method: 'GET',
  99. timeout: 10000,
  100. headers: {
  101. 'Content-Type': 'application/x-www-form-urlencoded',
  102. 'A-Token': that.globalData.token,
  103. 'Accept': Accept ? Accept : ''
  104. },
  105. data: data,
  106. dataType: 'json',
  107. success: function (res) {
  108. if (res.data.code == 1) {
  109. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  110. that.globalData.token = res.data.refresh_token
  111. }
  112. resolve(res);
  113. } else if (res.data.code == 401) {
  114. that.globalData.showToast("授权已过期");
  115. // reject(res);
  116. } else {
  117. that.globalData.showToast(res.data.msg)
  118. // reject(res);
  119. }
  120. },
  121. fail: function (res) {
  122. if (res.errorMessage == 'NETWORK ERROR') {
  123. dd.alert({
  124. title: '网络异常',
  125. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  126. buttonText: '我知道了',
  127. success: () => { },
  128. });
  129. return;
  130. }
  131. reject(res);
  132. },
  133. complete: function () {
  134. dd.hideLoading();
  135. }
  136. });
  137. })
  138. },
  139. routerPost(url, data = {}, Accept) {
  140. return new Promise(function (resolve, reject) {
  141. dd.httpRequest({
  142. url: baseUrl + url,
  143. method: 'POST',
  144. headers: {
  145. 'Content-Type': 'application/x-www-form-urlencoded',
  146. 'A-Token': that.globalData.token,
  147. 'Accept': Accept ? Accept : ''
  148. },
  149. timeout: 10000,
  150. data: data,
  151. dataType: 'json',
  152. success: function (res) {
  153. if (res.data.code == 1) {
  154. if (res.refresh_token && res.data.refresh_token != that.globalData.token) {
  155. that.globalData.token = res.data.refresh_token
  156. }
  157. resolve(res);
  158. } else if (res.data.code == 401) {
  159. that.globalData.showToast("授权已过期");
  160. reject(res);
  161. } else if (res.data.code == 3000) {
  162. reject(res);
  163. } else {
  164. that.globalData.showToast(res.data.msg);
  165. reject(res);
  166. }
  167. },
  168. fail: function (res) {
  169. if (res.error == 19 || res.error == 13) {
  170. if (url == 'api/integral/review/a/entry' || url == 'api/integral/point/entry') {
  171. let sum = 0;
  172. if (data) {
  173. sum = Number(JSON.parse(data.items).length) * Number(JSON.parse(data.members).length);
  174. }
  175. that.$post('api/e', { sum: sum, message: '钉钉小程序' }).then(res => { })
  176. }
  177. }
  178. if (res.errorMessage == 'NETWORK ERROR') {
  179. dd.alert({
  180. title: '网络异常',
  181. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  182. buttonText: '我知道了',
  183. success: () => { },
  184. });
  185. return;
  186. }
  187. reject(res);
  188. }
  189. });
  190. })
  191. },
  192. //封装post
  193. $post(url, data = {}, Accept) {
  194. return that.routerPost(url, data, Accept);
  195. },
  196. //封装get
  197. $get(url, data = {}, Accept) {
  198. return that.routerGet(url, data, Accept);
  199. },
  200. //封装get
  201. $get2(url, data = {}, Accept) {
  202. return that.routerGet2(url, data, Accept);
  203. },
  204. //免登 isShowStart是否显示引导页
  205. login(corpId, callBack, errBack = function () { }, isShowStart = false) {
  206. if (that.globalData.token) {
  207. callBack(true)
  208. } else {
  209. dd.getAuthCode({
  210. success: function (res) {
  211. that.$post('api/ding/login', { authCode: res.authCode, corpId: corpId }).then(res => {
  212. if (res.data.code == 1) {
  213. let user = res.data.data;
  214. // var isStart = false;
  215. if (user.is_official == 1) {
  216. // if (user.is_creator) {
  217. // that.globalData.isCreator = true;
  218. // isStart = true;
  219. // }
  220. that.globalData.token = user.token;
  221. that.globalData.userData = user;
  222. var getIsStart = dd.getStorageSync({ key: 'isStart' });//判断是否需要打开引导页
  223. console.log(JSON.stringify(user));
  224. // that.getUserList();//缓存人员列表
  225. that.setRouters();
  226. // if (isStart && !getIsStart.data && isShowStart) {//判断是否需要打开引导
  227. // dd.reLaunch({
  228. // url: '../start/start'
  229. // })
  230. // } else {
  231. callBack(true);
  232. // }
  233. } else {
  234. callBack(false);
  235. }
  236. } else if (res.data.code == 3000) {
  237. that.globalData.showToast("员工信息同步中,请稍后进入")
  238. } else {
  239. that.globalData.showToast(res.msg)
  240. }
  241. }).catch(err => {
  242. console.log(JSON.stringify(err))
  243. errBack(err.data);
  244. return
  245. if (err.data.code == 2001) {
  246. dd.confirm({
  247. title: '尊敬的用户',
  248. content: '你当前的套餐已到期',
  249. confirmButtonText: '去续费',
  250. cancelButtonText: '暂不需要',
  251. success: (result) => {
  252. if (result.confirm) {
  253. dd.reLaunch({
  254. url: `../../deploy/addMoney/addMoney`
  255. })
  256. }
  257. },
  258. });
  259. }
  260. })
  261. },
  262. fail: function (err) {
  263. console.log(JSON.stringify(err))
  264. that.globalData.showToast("错误" + JSON.stringify(err))
  265. }
  266. });
  267. }
  268. },
  269. //返回用户Code
  270. returnCode(data) {
  271. let list=this.globalData.usersList;
  272. if(Array.isArray(data)){
  273. let result =data.map(e=>{
  274. if(list[e]){
  275. return list[e].code
  276. }
  277. })
  278. return JSON.stringify(result)
  279. }else{
  280. data=data.toString()
  281. var arr = data.split(",");
  282. let result =arr.map(e=>{
  283. if(list[e]){
  284. return list[e].code
  285. }
  286. })
  287. return result.toString()
  288. }
  289. },
  290. setRouters() {
  291. let main = this.globalData.userData.permission_info.main;
  292. let per_role = this.globalData.userData.permission_info.per_role; // 用户的身份
  293. if (per_role == 'manager') {
  294. if (main == 1) {
  295. // 主管理员
  296. this.plusStoSet('masterAdministrator');
  297. } else {
  298. // 子管理员
  299. this.plusStoSet('childAdministrator');
  300. }
  301. } else {
  302. // 员工||创始人||部门管理员
  303. // if (this.globalData.isCreator) {
  304. // this.plusStoSet('creator');
  305. // } else
  306. if (this.globalData.userData.manage_dept_list.length > 0) {
  307. this.plusStoSet('deptManager');
  308. } else {
  309. this.plusStoSet('employee');
  310. }
  311. }
  312. },
  313. plusStoSet(val) {
  314. setCache('role', val);
  315. },
  316. // 判断当前登录者是否有某项权限
  317. getPermis(type){
  318. /* type为传入的权限id*/
  319. let jurisdictions = this.globalData.userData.permission_info.permission.map((item) => {
  320. return item.id
  321. })
  322. return jurisdictions.indexOf(type) >= 0
  323. },
  324. getUserList() {
  325. this.$get("api/per/user/employee_list", { page: 0, page_size: 0 }).then((res) => {
  326. let list = res.data.data.list
  327. let data = {}
  328. for (let i in list) {
  329. if (list[i].is_scope === 0||list[i].is_official ===0) {
  330. list[i].name = list[i].name + '(未启用)';
  331. }
  332. data[list[i].id] = list[i];
  333. }
  334. that.globalData.usersList = data;
  335. })
  336. },
  337. getTypesItem(id) {
  338. return getTypeItem(this.globalData.types, id);
  339. },
  340. updateApp() {
  341. const updateManager = dd.getUpdateManager()
  342. updateManager.onCheckForUpdate(function (res) {
  343. // 请求完新版本信息的回调
  344. // console.log(res.hasUpdate) // 是否有更新
  345. })
  346. updateManager.onUpdateReady(function (ret) {
  347. // console.log(ret.version) // 更新版本号
  348. dd.confirm({
  349. title: '更新提示',
  350. content: '新版本已经准备好,是否重启应用?',
  351. success: function (res) {
  352. if (res.confirm) {
  353. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  354. updateManager.applyUpdate()
  355. }
  356. }
  357. })
  358. })
  359. updateManager.onUpdateFailed(function () {
  360. // 新版本下载失败
  361. })
  362. },
  363. onLaunch(options) {
  364. dd.globalData = this.globalData;
  365. that = this;
  366. that.updateApp();
  367. // that.getTypes(function () { });
  368. // that.getCorp(options.query.corpId);
  369. that.globalData.corpId = options.query.corpId;
  370. },
  371. });