app.js 12 KB

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