app.js 11 KB

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