app.js 11 KB

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