app.js 10 KB

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