app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //app.js
  2. App({
  3. globalData: {
  4. title: '晋爱人才',
  5. // http_host: 'http://bd.jarc.com/',
  6. http_host: 'https://jarc.jucai.gov.cn/',
  7. // api_host: 'http://bd.jarc.com/api/',
  8. api_host: 'https://jarc.jucai.gov.cn/api/',
  9. },
  10. onLaunch: function() {
  11. var self = this;
  12. // 登录
  13. if (!this.checkLogin()) {
  14. wx.login({
  15. success: res => {
  16. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  17. self.post('applet/applet/login',{code:res.code},function(res){
  18. wx.setStorageSync('token', res.token);
  19. wx.setStorageSync('token_exp', res.expires_time);
  20. self.get('user/profile/userInfo',function(res){
  21. self.globalData.userInfo = res;
  22. },false)
  23. },false);
  24. }
  25. })
  26. } else {
  27. self.get('user/profile/userInfo',function(res){
  28. self.globalData.userInfo = res;
  29. },false)
  30. }
  31. // 获取系统状态栏信息
  32. wx.getSystemInfo({
  33. success: e => {
  34. this.globalData.StatusBar = e.statusBarHeight;
  35. let capsule = wx.getMenuButtonBoundingClientRect();
  36. if (capsule) {
  37. this.globalData.Custom = capsule;
  38. this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
  39. } else {
  40. this.globalData.CustomBar = e.statusBarHeight + 50;
  41. }
  42. }
  43. });
  44. },
  45. /**
  46. * 弹出框
  47. */
  48. msg(title, url = '', time = 2000, mask = false) {
  49. wx.showToast({
  50. title: title,
  51. icon: 'none',
  52. mask: mask,
  53. duration: time
  54. });
  55. if (url) {
  56. if (typeof url == 'function') {
  57. setTimeout(url,time);
  58. } else {
  59. setTimeout(function(){
  60. wx.navigateTo({url: url});
  61. },time);
  62. }
  63. }
  64. },
  65. loading(title) {
  66. wx.showLoading({
  67. title: title,
  68. icon: 'none',
  69. mask: true
  70. })
  71. },
  72. //服务器请求
  73. post: function (domain, param, success, is_load=true, error='') {
  74. if (is_load) {
  75. wx.showLoading({
  76. title: '加载中',
  77. mask: true,
  78. });
  79. }
  80. var that = this;
  81. var token = wx.getStorageSync('token');
  82. wx.request({
  83. url: that.globalData.api_host + domain,
  84. data : param,
  85. header: { 'XX-Device-Type': 'wxapp' , 'XX-Token': token},
  86. method: 'post',
  87. success: function (res) {
  88. if (is_load) {
  89. wx.hideLoading();
  90. }
  91. if (res.data.code == 1) {
  92. if (success != undefined) {
  93. success.call(this, res.data.data);
  94. }
  95. } else if (res.data.code == 10001) {
  96. wx.clearStorageSync();
  97. wx.showToast({
  98. title: '小程序异常,请退出后重新进入',
  99. icon: 'none',
  100. duration: 2000
  101. })
  102. } else {
  103. if (typeof error === 'function') {
  104. error(res.data);
  105. } else {
  106. wx.showToast({
  107. title: res.data.msg,
  108. icon: 'none',
  109. duration: 2000
  110. })
  111. }
  112. }
  113. },
  114. error : function () {
  115. if (is_load) {
  116. wx.hideLoading();
  117. }
  118. wx.showToast({
  119. title: '网络异常,请退出小程序重新登录',
  120. icon: 'loading'
  121. })
  122. }
  123. })
  124. },
  125. //服务器请求
  126. get: function (domain, success, is_load=true, error='') {
  127. if (is_load) {
  128. wx.showLoading({
  129. title: '加载中',
  130. mask: true,
  131. });
  132. }
  133. var that = this;
  134. var token = wx.getStorageSync('token');
  135. wx.request({
  136. url: that.globalData.api_host + domain,
  137. header: { 'XX-Device-Type': 'wxapp' , 'XX-Token': token},
  138. success: function (res) {
  139. if (is_load) {
  140. wx.hideLoading();
  141. }
  142. if (res.data.code == 1) {
  143. if (success != undefined) {
  144. success.call(this, res.data.data);
  145. }
  146. } else if (res.data.code == 10001) {
  147. wx.clearStorageSync();
  148. wx.showToast({
  149. title: '小程序异常,请退出后重新进入',
  150. icon: 'none',
  151. duration: 2000
  152. })
  153. } else {
  154. if (typeof error === 'function') {
  155. error(res.data);
  156. } else {
  157. wx.showToast({
  158. title: res.data.msg,
  159. icon: 'none',
  160. duration: 2000
  161. })
  162. }
  163. }
  164. },
  165. error : function () {
  166. if (is_load) {
  167. wx.hideLoading();
  168. }
  169. wx.showToast({
  170. title: '网络异常,请退出小程序重新登录',
  171. icon: 'loading'
  172. })
  173. }
  174. })
  175. },
  176. /**
  177. * 登录验证
  178. */
  179. checkLogin: function () {
  180. let token = wx.getStorageSync('token');
  181. if (!token) {
  182. return false;
  183. }
  184. let token_exp = wx.getStorageSync('token_exp');
  185. let now = Date.parse(new Date())/1000
  186. if (token_exp < now) {
  187. return false;
  188. }
  189. return true;
  190. },
  191. loading: function (title) {
  192. wx.showLoading({
  193. title: title,
  194. icon: 'none',
  195. mask: true
  196. })
  197. },
  198. confirm: function (content,success) {
  199. wx.showModal({
  200. title: '提示',
  201. content: content,
  202. success(res) {
  203. if (res.confirm) {
  204. wx.hideToast();
  205. success(res);
  206. } else {
  207. wx.hideToast();
  208. }
  209. }
  210. })
  211. }
  212. })