app.js 12 KB

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