App.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <script>
  2. const _ = require('lodash');
  3. import jwt from 'jsonwebtoken';
  4. import {
  5. mapState,
  6. mapMutations,
  7. createNamespacedHelpers,
  8. } from 'vuex';
  9. const {
  10. mapActions: companyBaoanWork
  11. } = createNamespacedHelpers('company_baoan_work');
  12. const {
  13. mapActions: companyBaoanDispatch
  14. } = createNamespacedHelpers('company_baoan_dispatch');
  15. const {
  16. mapActions: securityGuardBase
  17. } = createNamespacedHelpers('security_guard_base');
  18. export default {
  19. globalData: {
  20. version: plus.runtime.version
  21. },
  22. onLaunch: function () {
  23. this.checkVersion();
  24. if (process.env.VUE_APP_PLATFORM === 'app-plus') {
  25. // plus.screen.lockOrientation('portrait-secondary');
  26. plus.screen.lockOrientation('portrait-primary');
  27. }
  28. // 定时器
  29. setInterval(() => {
  30. this.toPrepare();
  31. }, 10000);
  32. },
  33. onShow: function () {
  34. this.checkUser();
  35. // console.log('App Show')
  36. },
  37. onHide: function () {
  38. // console.log('App Hide')
  39. },
  40. methods: {
  41. ...mapMutations(['setUser']),
  42. ...companyBaoanWork(['fetch']),
  43. ...companyBaoanDispatch({ getDispatch: 'fetch' }),
  44. ...securityGuardBase({ getSGB: 'fetch' }),
  45. // 准备数据
  46. async toPrepare() {
  47. if (!this.user.id) return; // 没有用户信息,不继续进行
  48. // 组织数据,获取定位,上传定位
  49. const { id } = this.user;
  50. let obj = _.pick(this.user, ['name', 'card']);
  51. obj.user_id = this.user.id;
  52. // 1.查询这个人是否在岗
  53. const sgb = await this.getSGB({ id })
  54. if (this.$checkRes(sgb)) {
  55. obj.is_class = _.get(sgb, 'data.is_class'); // 是文字,不是码
  56. }
  57. if (obj.is_class === '下班' || obj.is_class === '1') return; // 没打卡,不需要继续进行(如果24小时定位,则把这个判断隐藏)
  58. // 2.查询入职信息,填充进去
  59. const cbw = await this.fetch({ security_guard_id: id, is_quit: '0' })
  60. if (this.$checkRes(cbw)) {
  61. obj.company_name = _.get(cbw, 'data.company_name');
  62. obj.company_id = _.get(cbw, 'data.company_id');
  63. }
  64. // 3.查询派遣信息,填充进去
  65. const dispatch = await this.getDispatch({ security_guard_id: id, status: '0' });
  66. if (this.$checkRes(dispatch)) {
  67. obj.company_service_object_id = _.get(dispatch, 'data.service_target_id');
  68. obj.company_service_object_name = _.get(dispatch, 'data.service_target');
  69. }
  70. // 获取定位信息
  71. const position = await this.toGetLocation();
  72. obj = { ...obj, ...position };
  73. // 上传
  74. this.sendPosition(obj);
  75. },
  76. // 获取定位
  77. async toGetLocation() {
  78. const [err, posRes] = await uni.getLocation({ type: 'gcj02' });
  79. if (err) return;
  80. return { latitude: posRes.latitude, longitude: posRes.longitude };
  81. },
  82. // 发送定位
  83. async sendPosition(data) {
  84. const [err, res] = await uni.request({ url: 'http://baoan.fwedzgc.com:8090/api/position', method: 'post', data })
  85. if (err) {
  86. console.error(err);
  87. } else console.log('已上传定位');
  88. },
  89. // 检查用户
  90. async checkUser() {
  91. if (this.user.id) return;
  92. const token = uni.getStorageSync('token');
  93. if (!token) {
  94. uni.redirectTo({
  95. url: '/pages/login/index'
  96. })
  97. } else {
  98. const tokenObject = jwt.decode(token);
  99. const res = await this.getSGB({
  100. id: tokenObject.id
  101. })
  102. if (this.$checkRes(res)) {
  103. this.setUser(res.data);
  104. uni.redirectTo({
  105. url: '/pages/home/index'
  106. })
  107. }
  108. }
  109. },
  110. //检查版本
  111. checkVersion() {
  112. uni.request({
  113. method: 'get',
  114. url: 'http://baoan.fwedzgc.com:8090/files/baoanbase/appVersion.json',
  115. success: (res) => {
  116. const { version, url } = res.data;
  117. if (getApp().globalData.version === version) return;
  118. uni.showModal({
  119. title: '更新',
  120. content: "有新版本,是否下载?",
  121. success: (res) => {
  122. if (res.confirm) {
  123. // 下载app
  124. this.toUpdateAPK(url);
  125. }
  126. }
  127. })
  128. }
  129. })
  130. },
  131. // 更新
  132. toUpdateAPK(url) {
  133. if (!url) return;
  134. uni.showLoading({
  135. title: '更新中……'
  136. })
  137. uni.downloadFile({
  138. url,
  139. success: res => {
  140. uni.hideLoading();
  141. console.log(res);
  142. if (res.statusCode == 200) {
  143. uni.showModal({
  144. title: '',
  145. content: '更新成功,确定现在重启吗?',
  146. confirmText: '重启',
  147. confirmColor: '#EE8F57',
  148. success: modalRes => {
  149. if (modalRes.confirm) {
  150. plus.runtime.install(
  151. res.tempFilePath,
  152. { force: true },
  153. function (res) {
  154. plus.runtime.restart();
  155. })
  156. }
  157. }
  158. })
  159. }
  160. },
  161. })
  162. // const task = plus.downloader.createDownload(url, {}, function (d, status) {
  163. // console.log(d, status);
  164. // if (status === 200) {
  165. // // 安装
  166. // plus.runtime.install(
  167. // plus.io.convertLocalFileSystemURL(d.filename, {}, {}, function (error) {
  168. // uni.showToast({
  169. // title: '更新失败',
  170. // mask: false,
  171. // })
  172. // })
  173. // )
  174. // }
  175. // });
  176. // task.start();
  177. }
  178. },
  179. computed: {
  180. ...mapState(['user']),
  181. },
  182. }
  183. </script>
  184. <style lang="scss">
  185. @import 'uview-ui/index.scss';
  186. @import '@/static/icon/iconfont.css';
  187. /*每个页面公共css */
  188. </style>