app.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 {
  154. that.globalData.showToast(res.data.msg);
  155. reject(res);
  156. }
  157. },
  158. fail: function (res) {
  159. if (res.errorMessage == 'NETWORK ERROR') {
  160. dd.alert({
  161. title: '网络异常',
  162. content: '网络异常,请检查网络是否畅通,再重新进入应用',
  163. buttonText: '我知道了',
  164. success: () => { },
  165. });
  166. return;
  167. }
  168. reject(res);
  169. }
  170. });
  171. })
  172. },
  173. //封装post
  174. $post(url, data = {}, Accept) {
  175. // if (url == 'api/ding/login') {
  176. // return that.routerPost(url, data, Accept);
  177. // }
  178. // if (!that.globalData.token) {
  179. // dd.navigateTo({
  180. // url: '../../init/init'
  181. // })
  182. // return Promise.reject("登录中")
  183. // } else {
  184. return that.routerPost(url, data, Accept);
  185. // }
  186. },
  187. //封装get
  188. $get(url, data = {}, Accept) {
  189. // if (url == 'api/integral/types' || url == 'api/order/corp') {
  190. // return that.routerGet(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.routerGet(url, data, Accept);
  199. // }
  200. },
  201. //封装get
  202. $get2(url, data = {}, Accept) {
  203. // if (url == 'api/integral/types' || url == 'api/order/corp') {
  204. // return that.routerGet2(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.routerGet2(url, data, Accept);
  213. // }
  214. },
  215. //免登
  216. login(corpId, callBack, errBack=function(){}) {
  217. if (that.globalData.token) {
  218. callBack(true)
  219. } else {
  220. dd.getAuthCode({
  221. success: function (res) {
  222. that.$post('api/ding/login', { authCode: res.authCode, corpId: corpId }).then(res => {
  223. if (res.data.code == 1) {
  224. var { token, user } = res.data.data;
  225. var isStart = false;
  226. if (user.is_official == 1) {
  227. user.employee_detail.role_list.forEach(item => {
  228. if (item.name == 'creator') {
  229. that.globalData.isCreator = true;
  230. isStart = true;
  231. }
  232. if (item.name == 'admin' || item.name == 'creator' || item.name == 'point_manager' || item.name == 'dept_manager') {
  233. that.globalData.isAdministrator = true;
  234. }
  235. });
  236. that.globalData.token = token;
  237. that.globalData.userData = user;
  238. var getIsStart = dd.getStorageSync({ key: 'isStart' });//判断是否需要打开引导页
  239. // console.log(JSON.stringify(user));
  240. that.getUserList();//缓存人员列表
  241. if (isStart && !getIsStart.data) {//判断是否需要打开引导
  242. dd.reLaunch({
  243. url: '../../start/start'
  244. })
  245. } else {
  246. callBack(true);
  247. }
  248. } else {
  249. callBack(false);
  250. }
  251. } else if (res.data.code == 3000) {
  252. that.globalData.showToast("员工信息同步中,请稍后进入")
  253. } else {
  254. that.globalData.showToast(res.msg)
  255. }
  256. }).catch(err => {
  257. // console.log(err)
  258. errBack(err.data);
  259. return
  260. if (err.data.code == 2001) {
  261. dd.confirm({
  262. title: '尊敬的用户',
  263. content: '您当前的套餐已到期',
  264. confirmButtonText: '去续费',
  265. cancelButtonText: '暂不需要',
  266. success: (result) => {
  267. if (result.confirm) {
  268. dd.reLaunch({
  269. url: `../../deploy/addMoney/addMoney`
  270. })
  271. }
  272. },
  273. });
  274. }
  275. })
  276. },
  277. fail: function (err) {
  278. console.log(JSON.stringify(err))
  279. that.globalData.showToast("错误" + JSON.stringify(err))
  280. }
  281. });
  282. }
  283. },
  284. getUserList() {
  285. this.$get("api/employee/list", { dept_id: 0 }).then((res) => {
  286. that.globalData.usersList = res.data.data.list;
  287. })
  288. },
  289. getTypesItem(id) {
  290. return getTypeItem(this.globalData.types, id);
  291. },
  292. updateApp(){
  293. const updateManager = dd.getUpdateManager()
  294. updateManager.onCheckForUpdate(function (res) {
  295. // 请求完新版本信息的回调
  296. console.log(res.hasUpdate) // 是否有更新
  297. })
  298. updateManager.onUpdateReady(function (ret) {
  299. console.log(ret.version) // 更新版本号
  300. dd.confirm({
  301. title: '更新提示',
  302. content: '新版本已经准备好,是否重启应用?',
  303. success: function (res) {
  304. if (res.confirm) {
  305. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  306. updateManager.applyUpdate()
  307. }
  308. }
  309. })
  310. })
  311. updateManager.onUpdateFailed(function () {
  312. // 新版本下载失败
  313. })
  314. },
  315. onLaunch(options) {
  316. that = this;
  317. // that.updateApp();
  318. that.getTypes(function () { });
  319. that.getCorp(options.query.corpId);
  320. that.globalData.corpId = options.query.corpId;
  321. },
  322. });