app.js 11 KB

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