app.js 12 KB

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