app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.$get("api/integral/types", {}).then((res) => {
  42. this.globalData.types = res.data.data.list;
  43. callBack();
  44. })
  45. },
  46. //获取套餐信息
  47. getCorp(corpId) {
  48. this.$get("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. if (res.data.code == 1) {
  126. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  127. that.globalData.token = res.data.refresh_token
  128. }
  129. resolve(res);
  130. } else if (res.data.code == 401) {
  131. that.globalData.showToast("授权已过期");
  132. // reject(res);
  133. } else {
  134. that.globalData.showToast(res.data.msg)
  135. // reject(res);
  136. }
  137. },
  138. fail: function (res) {
  139. if (res.errorMessage == 'NETWORK ERROR') {
  140. dd.alert({
  141. title: '网络异常',
  142. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  143. buttonText: '我知道了',
  144. success: () => { },
  145. });
  146. return;
  147. }
  148. reject(res);
  149. },
  150. complete: function () {
  151. dd.hideLoading();
  152. }
  153. });
  154. })
  155. },
  156. routerPost(url, data = {}, Accept) {
  157. return new Promise(function (resolve, reject) {
  158. dd.httpRequest({
  159. url: baseUrl + url,
  160. method: 'POST',
  161. headers: {
  162. 'Content-Type': 'application/x-www-form-urlencoded',
  163. 'A-Token': that.globalData.token,
  164. 'Accept': Accept ? Accept : ''
  165. },
  166. timeout:10000,
  167. data: data,
  168. dataType: 'json',
  169. success: function (res) {
  170. if (res.data.code == 1) {
  171. if (res.refresh_token && res.data.refresh_token != that.globalData.token) {
  172. that.globalData.token = res.data.refresh_token
  173. }
  174. resolve(res);
  175. } else if (res.data.code == 401) {
  176. that.globalData.showToast("授权已过期");
  177. reject(res);
  178. } else if(res.data.code == 3000) {
  179. reject(res);
  180. }else{
  181. that.globalData.showToast(res.data.msg);
  182. reject(res);
  183. }
  184. },
  185. fail: function (res) {
  186. if(res.error==19||res.error==13){
  187. if(url=='api/integral/review/a/entry'||url=='api/integral/point/entry'){
  188. let sum=0;
  189. if(data){
  190. sum=Number(JSON.parse(data.items).length)*Number(JSON.parse(data.members).length);
  191. }
  192. that.$post('api/e', {sum:sum,message:'钉钉小程序'}).then(res => {})
  193. }
  194. }
  195. if (res.errorMessage == 'NETWORK ERROR') {
  196. dd.alert({
  197. title: '网络异常',
  198. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  199. buttonText: '我知道了',
  200. success: () => { },
  201. });
  202. return;
  203. }
  204. reject(res);
  205. }
  206. });
  207. })
  208. },
  209. //封装post
  210. $post(url, data = {}, Accept) {
  211. // if (url == 'api/ding/login') {
  212. // return that.routerPost(url, data, Accept);
  213. // }
  214. // if (!that.globalData.token) {
  215. // dd.navigateTo({
  216. // url: '../../init/init'
  217. // })
  218. // return Promise.reject("登录中")
  219. // } else {
  220. return that.routerPost(url, data, Accept);
  221. // }
  222. },
  223. //封装get
  224. $get(url, data = {}, Accept) {
  225. // if (url == 'api/integral/types' || url == 'api/order/corp') {
  226. // return that.routerGet(url, data, Accept);
  227. // }
  228. // if (!that.globalData.token) {
  229. // dd.navigateTo({
  230. // url: '../../init/init'
  231. // })
  232. // return Promise.reject("登录中")
  233. // } else {
  234. return that.routerGet(url, data, Accept);
  235. // }
  236. },
  237. //封装get
  238. $get2(url, data = {}, Accept) {
  239. // if (url == 'api/integral/types' || url == 'api/order/corp') {
  240. // return that.routerGet2(url, data, Accept);
  241. // }
  242. // if (!that.globalData.token) {
  243. // dd.navigateTo({
  244. // url: '../../init/init'
  245. // })
  246. // return Promise.reject("登录中")
  247. // } else {
  248. return that.routerGet2(url, data, Accept);
  249. // }
  250. },
  251. //免登 isShowStart是否显示引导页
  252. login(corpId, callBack, errBack=function(){},isShowStart=false) {
  253. if (that.globalData.token) {
  254. callBack(true)
  255. } else {
  256. dd.getAuthCode({
  257. success: function (res) {
  258. that.$post('api/ding/login', { authCode: res.authCode, corpId: corpId }).then(res => {
  259. if (res.data.code == 1) {
  260. var { token, user } = res.data.data;
  261. var isStart = false;
  262. if (user.is_official == 1) {
  263. user.employee_detail.role_list.forEach(item => {
  264. if (item.name == 'creator') {
  265. that.globalData.isCreator = true;
  266. isStart = true;
  267. }
  268. if (item.name == 'admin' || item.name == 'creator' || item.name == 'point_manager' || item.name == 'dept_manager') {
  269. that.globalData.isAdministrator = true;
  270. }
  271. });
  272. that.globalData.token = token;
  273. that.globalData.userData = user;
  274. var getIsStart = dd.getStorageSync({ key: 'isStart' });//判断是否需要打开引导页
  275. console.log(JSON.stringify(user));
  276. that.getUserList();//缓存人员列表
  277. if (isStart && !getIsStart.data&&isShowStart) {//判断是否需要打开引导
  278. dd.reLaunch({
  279. url: '../start/start'
  280. })
  281. } else {
  282. callBack(true);
  283. }
  284. } else {
  285. callBack(false);
  286. }
  287. } else if (res.data.code == 3000) {
  288. that.globalData.showToast("员工信息同步中,请稍后进入")
  289. } else {
  290. that.globalData.showToast(res.msg)
  291. }
  292. }).catch(err => {
  293. errBack(err.data);
  294. return
  295. if (err.data.code == 2001) {
  296. dd.confirm({
  297. title: '尊敬的用户',
  298. content: '你当前的套餐已到期',
  299. confirmButtonText: '去续费',
  300. cancelButtonText: '暂不需要',
  301. success: (result) => {
  302. if (result.confirm) {
  303. dd.reLaunch({
  304. url: `../../deploy/addMoney/addMoney`
  305. })
  306. }
  307. },
  308. });
  309. }
  310. })
  311. },
  312. fail: function (err) {
  313. console.log(JSON.stringify(err))
  314. that.globalData.showToast("错误" + JSON.stringify(err))
  315. }
  316. });
  317. }
  318. },
  319. getUserList() {
  320. this.$get("api/employee/list",{ dept_id: 0 }).then((res) => {
  321. that.globalData.usersList = res.data.data.list;
  322. })
  323. },
  324. getTypesItem(id) {
  325. return getTypeItem(this.globalData.types, id);
  326. },
  327. updateApp(){
  328. const updateManager = dd.getUpdateManager()
  329. updateManager.onCheckForUpdate(function (res) {
  330. // 请求完新版本信息的回调
  331. // console.log(res.hasUpdate) // 是否有更新
  332. })
  333. updateManager.onUpdateReady(function (ret) {
  334. // console.log(ret.version) // 更新版本号
  335. dd.confirm({
  336. title: '更新提示',
  337. content: '新版本已经准备好,是否重启应用?',
  338. success: function (res) {
  339. if (res.confirm) {
  340. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  341. updateManager.applyUpdate()
  342. }
  343. }
  344. })
  345. })
  346. updateManager.onUpdateFailed(function () {
  347. // 新版本下载失败
  348. })
  349. },
  350. onLaunch(options) {
  351. dd.globalData=this.globalData;
  352. that = this;
  353. that.updateApp();
  354. that.getTypes(function () { });
  355. that.getCorp(options.query.corpId);
  356. that.globalData.corpId = options.query.corpId;
  357. },
  358. });