app.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. types:[],
  20. userData: '',
  21. token:'',
  22. // token:' eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvZGluZy5pbnN5cy5nMTA3LmNvbVwvYXBpXC9kaW5nXC9sb2dpbiIsImlhdCI6MTYwMDMxNDM0NywiZXhwIjoxNjAwOTE0MzQ3LCJuYmYiOjE2MDAzMTQzNDcsImp0aSI6IjgwMFJUS0J4RllIbnU5M3EiLCJzdWIiOjIsInBydiI6ImNhNjQ4OWQ1MGYyNDA3YTY3ODMwZTgwOTBkMDE0ODgzNTY4NTk2MmIiLCJyb2xlIjoiZW1wbG95ZWUifQ.E1RiQ6uMRICoL-2qDTeh5j5y-QLaUsGGs6PYLm-_1m8',
  23. ALIOSS_URL: 'https://integralsys.oss-cn-shenzhen.aliyuncs.com',
  24. imgHttpUrl:'https://intesys.cms.g107.com/integral.php/Api/get_signature',
  25. corpId:'',
  26. yearArr:yearArr,
  27. },
  28. //获取积分类型
  29. getTypes() {
  30. this.$get("api/integral/types", {}).then((res) => {
  31. this.globalData.types = res.data.data.list;
  32. })
  33. },
  34. routerGet(url,data={},Accept){
  35. return new Promise(function (resolve, reject) {
  36. dd.showLoading();
  37. dd.httpRequest({
  38. url: baseUrl + url,
  39. method: 'GET',
  40. headers: {
  41. 'Content-Type': 'application/x-www-form-urlencoded',
  42. 'A-Token': that.globalData.token,
  43. 'Accept': Accept ? Accept:''
  44. },
  45. data: data,
  46. dataType: 'json',
  47. success: function (res) {
  48. if (res.data.code == 1) {
  49. if (res.data.refresh_token && res.data.refresh_token != that.globalData.token) {
  50. that.globalData.token = res.data.refresh_token
  51. }
  52. resolve(res);
  53. } else if (res.data.code == 401) {
  54. that.globalData.showToast("登录已过期");
  55. // reject(res);
  56. } else {
  57. that.globalData.showToast(res.data.msg)
  58. // reject(res);
  59. }
  60. },
  61. fail: function (res) {
  62. reject(res);
  63. },
  64. complete:function(){
  65. dd.hideLoading();
  66. }
  67. });
  68. })
  69. },
  70. routerPost(url,data={}){
  71. return new Promise(function (resolve, reject) {
  72. dd.httpRequest({
  73. url: baseUrl + url,
  74. method: 'POST',
  75. headers: {
  76. 'Content-Type': 'application/x-www-form-urlencoded',
  77. 'A-Token': that.globalData.token
  78. },
  79. data: data,
  80. dataType: 'json',
  81. success: function (res) {
  82. if (res.data.code == 1) {
  83. if (res.refresh_token && res.data.refresh_token != that.globalData.token) {
  84. that.globalData.token = res.data.refresh_token
  85. }
  86. resolve(res);
  87. } else if (res.data.code == 401) {
  88. that.globalData.showToast("登录已过期");
  89. reject(res);
  90. } else {
  91. that.globalData.showToast(res.data.msg);
  92. reject(res);
  93. }
  94. },
  95. fail: function (res) {
  96. reject(res);
  97. }
  98. });
  99. })
  100. },
  101. //封装post
  102. $post(url, data={}) {
  103. if(url=='api/ding/login'){
  104. return that.routerPost(url,data);
  105. }
  106. if(!that.globalData.token){
  107. dd.navigateTo({
  108. url:'../../init/init'
  109. })
  110. return Promise.reject("登录中")
  111. }else{
  112. return that.routerPost(url,data);
  113. }
  114. },
  115. //封装get
  116. $get(url,data={},Accept) {
  117. if(url=='api/integral/types'){
  118. return that.routerGet(url,data,Accept);
  119. }
  120. if(!that.globalData.token){
  121. dd.navigateTo({
  122. url:'../../init/init'
  123. })
  124. return Promise.reject("登录中")
  125. }else{
  126. return that.routerGet(url,data,Accept);
  127. }
  128. },
  129. //免登
  130. login(corpId, callBack) {
  131. dd.getAuthCode({
  132. success: function (res) {
  133. that.$post('api/ding/login', { authCode: res.authCode, corpId: corpId }).then(res => {
  134. if (res.data.code == 1) {
  135. var { token, user } = res.data.data;
  136. if(user.is_official==1){
  137. that.globalData.token = token;
  138. that.globalData.userData = user;
  139. callBack(true);
  140. }else{
  141. callBack(false);
  142. }
  143. } else {
  144. that.globalData.showToast(res.msg)
  145. }
  146. }).catch(err=>{
  147. console.log(err);
  148. })
  149. },
  150. fail:function(err){
  151. console.log(err)
  152. }
  153. });
  154. },
  155. getTypesItem(id){
  156. return getTypeItem(this.globalData.types,id);
  157. },
  158. onLaunch(options) {
  159. that = this;
  160. that.getTypes();
  161. that.globalData.corpId = options.query.corpId;
  162. },
  163. });