app.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import { showToast } from './utils/feedback'
  2. import { formatTime, arrRemoveObj, getTypeItem } from './utils/util'
  3. import * as socketApi from './utils/websocket'
  4. import { onSubscribeAppBack} from 'dingtalk-service-window-libs/lib/openSubscribeMiniApp'
  5. // const baseUrl = "https://test-ding.g107.com/";//测试
  6. const baseUrl = "https://app55493.eapps.dingtalkcloud.com/";//正式
  7. var that;
  8. var yearArr = [];
  9. var year = formatTime(new Date()).year;
  10. for (var i = 2018; i <= year; i++) {
  11. yearArr.push(i);
  12. }
  13. App({
  14. //自定义全局变量
  15. globalData: {
  16. baseUrl:baseUrl,
  17. // webScoketUrl: "test-ding.g107.com",//webScoket测试
  18. webScoketUrl: "app55493.eapps.dingtalkcloud.com",//webScoket正式
  19. showToast: showToast,//提示框
  20. arrRemoveObj: arrRemoveObj,//删除数组的某一项
  21. year: formatTime(new Date()).year,//当前年
  22. month: formatTime(new Date()).month,//当前月
  23. day: formatTime(new Date()).day,//当前日
  24. month_tow: formatTime(new Date()).month_tow,//当前月份
  25. allDate: formatTime(new Date()).allDate,//带时分
  26. types: [],
  27. userData: '',
  28. token: '',
  29. ALIOSS_URL: 'https://integralsys.oss-cn-shenzhen.aliyuncs.com',
  30. imgHttpUrl: 'https://intesys.cms.g107.com/integral.php/Api/get_signature',
  31. corpId: '',
  32. yearArr: yearArr,
  33. isSuperAdministrator:false,
  34. isAdministrator: false,//是否管理员
  35. isCreator: false,//是否是系统管理员
  36. corpMessage: '',//企业套餐信息
  37. usersList: [],//缓存的人员列表
  38. socketApi:socketApi,//长连接
  39. config:{} //基础配置
  40. },
  41. //获取积分类型
  42. getTypes(callBack) {
  43. this.$get2("api/integral/types", {}).then((res) => {
  44. this.globalData.types = res.data.data.list;
  45. callBack();
  46. })
  47. },
  48. //获取套餐信息
  49. getCorp(corpId) {
  50. this.$get2("api/order/corp", { corp_id: corpId }).then((res) => {
  51. this.globalData.corpMessage = res.data.data;
  52. })
  53. },
  54. //将部门链接在一起
  55. returnDeptName(arr){
  56. let data=arr.map(item=>{
  57. let str='';
  58. item.dept_list.forEach((e,index)=>{
  59. if(item.dept_list.length-index>1){
  60. str+=e.dept_name+','
  61. }else{
  62. str+=e.dept_name
  63. }
  64. })
  65. item.deptName=str
  66. return item
  67. })
  68. return data
  69. },
  70. routerGet2(url, data = {}, Accept) {
  71. return new Promise(function (resolve, reject) {
  72. dd.httpRequest({
  73. url: baseUrl + url,
  74. method: 'GET',
  75. headers: {
  76. 'Content-Type': 'application/x-www-form-urlencoded',
  77. 'A-Token': that.globalData.token,
  78. 'Accept': Accept ? Accept : ''
  79. },
  80. data: data,
  81. timeout:10000,
  82. dataType: 'json',
  83. success: function (res) {
  84. if (res.data.code == 1) {
  85. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  86. that.globalData.token = res.data.refresh_token
  87. }
  88. resolve(res);
  89. } else if (res.data.code == 401) {
  90. that.globalData.showToast("授权已过期");
  91. // reject(res);
  92. } else {
  93. that.globalData.showToast(res.data.msg)
  94. // reject(res);
  95. }
  96. },
  97. fail: function (res) {
  98. if (res.errorMessage == 'NETWORK ERROR') {
  99. dd.alert({
  100. title: '网络异常',
  101. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  102. buttonText: '我知道了',
  103. success: () => { },
  104. });
  105. return;
  106. }
  107. reject(res);
  108. }
  109. });
  110. })
  111. },
  112. routerGet(url, data = {}, Accept) {
  113. return new Promise(function (resolve, reject) {
  114. dd.showLoading();
  115. dd.httpRequest({
  116. url: baseUrl + url,
  117. method: 'GET',
  118. timeout:10000,
  119. headers: {
  120. 'Content-Type': 'application/x-www-form-urlencoded',
  121. 'A-Token': that.globalData.token,
  122. 'Accept': Accept ? Accept : ''
  123. },
  124. data: data,
  125. dataType: 'json',
  126. success: function (res) {
  127. dd.hideLoading();
  128. if (res.data.code == 1) {
  129. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  130. that.globalData.token = res.data.refresh_token
  131. }
  132. resolve(res);
  133. } else if (res.data.code == 401) {
  134. that.globalData.showToast("授权已过期");
  135. // reject(res);
  136. } else {
  137. that.globalData.showToast(res.data.msg)
  138. // reject(res);
  139. }
  140. },
  141. fail: function (res) {
  142. dd.hideLoading();
  143. if (res.errorMessage == 'NETWORK ERROR') {
  144. dd.alert({
  145. title: '网络异常',
  146. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  147. buttonText: '我知道了',
  148. success: () => { },
  149. });
  150. return;
  151. }
  152. reject(res);
  153. },
  154. complete: function () {
  155. dd.hideLoading();
  156. }
  157. });
  158. })
  159. },
  160. routerPost(url, data = {}, Accept) {
  161. return new Promise(function (resolve, reject) {
  162. dd.httpRequest({
  163. url: baseUrl + url,
  164. method: 'POST',
  165. headers: {
  166. 'Content-Type': 'application/x-www-form-urlencoded',
  167. 'A-Token': that.globalData.token,
  168. 'Accept': Accept ? Accept : ''
  169. },
  170. timeout:10000,
  171. data: data,
  172. dataType: 'json',
  173. success: function (res) {
  174. if (res.data.code == 1) {
  175. if (res.refresh_token && res.data.refresh_token != that.globalData.token) {
  176. that.globalData.token = res.data.refresh_token
  177. }
  178. resolve(res);
  179. } else if (res.data.code == 401) {
  180. that.globalData.showToast("授权已过期");
  181. reject(res);
  182. } else if(res.data.code == 3000) {
  183. reject(res);
  184. }else{
  185. that.globalData.showToast(res.data.msg);
  186. reject(res);
  187. }
  188. },
  189. fail: function (res) {
  190. if(res.error==19||res.error==13){
  191. if(url=='api/integral/review/a/entry'||url=='api/integral/point/entry'){
  192. let sum=0;
  193. if(data){
  194. sum=Number(JSON.parse(data.items).length)*Number(JSON.parse(data.members).length);
  195. }
  196. that.$post('api/e', {sum:sum,message:'钉钉小程序'}).then(res => {})
  197. }
  198. }
  199. if (res.errorMessage == 'NETWORK ERROR') {
  200. dd.alert({
  201. title: '网络异常',
  202. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  203. buttonText: '我知道了',
  204. success: () => { },
  205. });
  206. return;
  207. }
  208. reject(res);
  209. }
  210. });
  211. })
  212. },
  213. //封装post
  214. $post(url, data = {}, Accept) {
  215. // if (url == 'api/ding/login') {
  216. // return that.routerPost(url, data, Accept);
  217. // }
  218. // if (!that.globalData.token) {
  219. // dd.navigateTo({
  220. // url: '../../init/init'
  221. // })
  222. // return Promise.reject("登录中")
  223. // } else {
  224. return that.routerPost(url, data, Accept);
  225. // }
  226. },
  227. //封装get
  228. $get(url, data = {}, Accept) {
  229. // if (url == 'api/integral/types' || url == 'api/order/corp') {
  230. // return that.routerGet(url, data, Accept);
  231. // }
  232. // if (!that.globalData.token) {
  233. // dd.navigateTo({
  234. // url: '../../init/init'
  235. // })
  236. // return Promise.reject("登录中")
  237. // } else {
  238. return that.routerGet(url, data, Accept);
  239. // }
  240. },
  241. //封装get
  242. $get2(url, data = {}, Accept) {
  243. // if (url == 'api/integral/types' || url == 'api/order/corp') {
  244. // return that.routerGet2(url, data, Accept);
  245. // }
  246. // if (!that.globalData.token) {
  247. // dd.navigateTo({
  248. // url: '../../init/init'
  249. // })
  250. // return Promise.reject("登录中")
  251. // } else {
  252. return that.routerGet2(url, data, Accept);
  253. // }
  254. },
  255. //免登 isShowStart是否显示引导页
  256. login(corpId, callBack, errBack=function(){},isShowStart=false) {
  257. if (that.globalData.token) {
  258. callBack(true)
  259. } else {
  260. dd.getAuthCode({
  261. success: function (res) {
  262. that.$post('api/ding/login', { authCode: res.authCode, corpId: corpId }).then(res => {
  263. if (res.data.code == 1) {
  264. var { token, user } = res.data.data;
  265. var isStart = false;
  266. // console.log(JSON.stringify(user.employee_detail.role_list))
  267. if (user.is_official == 1) {
  268. user.employee_detail.role_list.forEach(item => {
  269. if (item.name == 'creator') {
  270. that.globalData.isCreator = true;
  271. isStart = true;
  272. }
  273. if (item.name == 'admin' || item.name == 'creator' || item.name == 'point_manager') {
  274. that.globalData.isSuperAdministrator = true;
  275. }
  276. if (item.name == 'admin' || item.name == 'creator' || item.name == 'point_manager' || item.name == 'dept_manager') {
  277. that.globalData.isAdministrator = true;
  278. }
  279. });
  280. that.globalData.token = token;
  281. that.globalData.userData = user;
  282. var getIsStart = dd.getStorageSync({ key: 'isStart' });//判断是否需要打开引导页
  283. console.log(JSON.stringify(user));
  284. that.getUserList(()=>{
  285. if (isStart && !getIsStart.data&&isShowStart) {//判断是否需要打开引导
  286. dd.reLaunch({
  287. url: '../start/start'
  288. })
  289. } else {
  290. callBack(true);
  291. }
  292. });//缓存人员列表
  293. } else {
  294. callBack(false);
  295. }
  296. } else if (res.data.code == 3000) {
  297. that.globalData.showToast("员工信息同步中,请稍后进入")
  298. } else {
  299. that.globalData.showToast(res.msg)
  300. }
  301. }).catch(err => {
  302. errBack(err.data);
  303. return
  304. if (err.data.code == 2001) {
  305. dd.confirm({
  306. title: '尊敬的用户',
  307. content: '你当前的套餐已到期',
  308. confirmButtonText: '去续费',
  309. cancelButtonText: '暂不需要',
  310. success: (result) => {
  311. if (result.confirm) {
  312. dd.reLaunch({
  313. url: `../../deploy/addMoney/addMoney`
  314. })
  315. }
  316. },
  317. });
  318. }
  319. })
  320. },
  321. fail: function (err) {
  322. console.log(JSON.stringify(err))
  323. that.globalData.showToast("错误" + JSON.stringify(err))
  324. }
  325. });
  326. }
  327. },
  328. getUserList(fun) {
  329. this.$get2("api/employee/list",{ dept_id: 0 }).then((res) => {
  330. that.globalData.usersList = res.data.data.list;
  331. fun()
  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. console.log(options.query.corpId)
  366. that.getCorp(options.query.corpId);
  367. that.globalData.corpId = options.query.corpId;
  368. },
  369. onShow(options) {
  370. onSubscribeAppBack(options, (data) => {
  371. // 这里可以对返回数据做二次处理,之后需要把数据返回到page.onShow
  372. // dd.alert({
  373. // title: 'onSubscribeAppBack data :',
  374. // content: JSON.stringify(data),
  375. // });
  376. return data;
  377. });
  378. },
  379. });