TrackTimeActions.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { API_LOCAL_PUSHES, API_MIX_CLOCKS } from "@/services/http/api";
  2. import { request } from "@/services/http/request";
  3. import { recordCheck } from "@/services/trackTimeDuration";
  4. import trackTimeService from "@/store/trackTimeMachine"
  5. import { isIOS, kIsIOS } from "@/utils/tools";
  6. // import {PushNotification} from 'react-native-push-notification'
  7. import Taro from "@tarojs/taro";
  8. import dayjs from 'dayjs'
  9. let PushNotification, Importance, NativeModules;
  10. if (process.env.TARO_ENV == 'rn') {
  11. PushNotification = require('react-native-push-notification')
  12. Importance = require('react-native-push-notification').Importance
  13. NativeModules = require('react-native').NativeModules
  14. }
  15. export const startFast = (start_time: number, duration: number) => {
  16. return new Promise((resolve, reject) => {
  17. // duration = duration < 3600 * 1000 ? 3600 * 1000 : duration;
  18. const extra = {
  19. set_time: global.set_time,
  20. confirm_time: new Date().getTime(),
  21. }
  22. recordCheck({
  23. action: 'FAST_START',
  24. real_check_time: start_time,
  25. target_duration: duration,
  26. extra: extra
  27. }).then(res => {
  28. resolve(res)
  29. trackTimeService.send({ type: 'START_FAST' });
  30. // getLocalPush()
  31. }).catch(e => {
  32. reject(e)
  33. });
  34. });
  35. }
  36. export const endFast = (start_time: number) => {
  37. return new Promise((resolve, reject) => {
  38. const extra = {
  39. set_time: global.set_time,
  40. confirm_time: new Date().getTime(),
  41. }
  42. recordCheck({
  43. action: 'FAST_END',
  44. real_check_time: start_time,
  45. extra: extra
  46. }).then(res => {
  47. trackTimeService.send({ type: 'END_FAST' });
  48. trackTimeService.send({ type: 'RESET' });
  49. trackTimeService.send({ type: global.scenario });
  50. resolve(res)
  51. // getLocalPush()
  52. }).catch(e => {
  53. reject(e)
  54. });
  55. });
  56. }
  57. export const startSleep = (start_time: number, duration: number) => {
  58. // duration = duration < 3600 * 1000 ? 3600 * 1000 : duration;
  59. const extra = {
  60. set_time: global.set_time,
  61. confirm_time: new Date().getTime(),
  62. }
  63. return new Promise((resolve, reject) => {
  64. recordCheck({
  65. action: 'SLEEP_START',
  66. real_check_time: start_time,
  67. target_duration: duration,
  68. extra: extra
  69. }).then(res => {
  70. trackTimeService.send({ type: 'START_SLEEP' });
  71. resolve(res)
  72. // getLocalPush()
  73. }).catch(e => {
  74. reject(e)
  75. });
  76. });
  77. }
  78. export const endSleep = (start_time: number) => {
  79. const extra = {
  80. set_time: global.set_time,
  81. confirm_time: new Date().getTime(),
  82. }
  83. return new Promise((resolve, reject) => {
  84. recordCheck({
  85. action: 'SLEEP_END',
  86. real_check_time: start_time,
  87. extra: extra
  88. }).then(res => {
  89. trackTimeService.send({ type: 'END_SLEEP' });
  90. resolve(res)
  91. // getLocalPush()
  92. }).catch(e => {
  93. reject(e)
  94. });
  95. });
  96. }
  97. export const batchClocks = (params: any) => {
  98. const extra = {
  99. set_time: global.set_time,
  100. confirm_time: new Date().getTime(),
  101. }
  102. return new Promise((resolve, reject) => {
  103. request({
  104. url: API_MIX_CLOCKS, method: 'POST', data: { extra: extra, ...params }
  105. }).then(res => {
  106. resolve(res);
  107. }).catch(e => {
  108. if (global.postBtnUpdateStatus)
  109. global.postBtnUpdateStatus('idle')
  110. reject(e)
  111. })
  112. })
  113. }
  114. export const uploadLocalPushInfo = (params) => {
  115. request({
  116. url: API_LOCAL_PUSHES, method: 'POST', data: {
  117. id: params.messageId,
  118. result: 'SUCCESS'
  119. }
  120. })
  121. }
  122. export const getLocalPush = () => {
  123. if (process.env.TARO_ENV == 'rn') {
  124. PushNotification.checkPermissions((res) => {
  125. console.log('notification status begin')
  126. console.log('notification status', res)
  127. //允许授权
  128. if ((kIsIOS && res.authorizationStatus == 2) || (!kIsIOS && res.alert)) {
  129. request({
  130. url: API_LOCAL_PUSHES, method: 'GET', data: {}
  131. }).then(res => {
  132. const { messages } = res as any;
  133. if (messages.length == 0) {
  134. return;
  135. }
  136. // PushNotification.configure({
  137. // onRegister: function (token) {
  138. // console.log('TOKEN:', token);
  139. // },
  140. // onNotification: function (notification) {
  141. // console.log('NOTIFICATION:', notification);
  142. // // notification.finish(PushNotificationIOS.FetchResult.NoData);
  143. // },
  144. // permissions: {
  145. // alert: true,
  146. // badge: true,
  147. // sound: true,
  148. // },
  149. // popInitialNotification: true,
  150. // // requestPermissions: true,
  151. // });
  152. // PushNotification.cancelAllLocalNotifications()
  153. if (kIsIOS) {
  154. var Jto = require('react-native').NativeModules.NativeBridge;
  155. Jto.addLocalPush(messages)
  156. return;
  157. }
  158. else {
  159. const { NativeBridge } = NativeModules
  160. NativeBridge.addLocalPush(JSON.stringify(messages))
  161. return;
  162. }
  163. messages.map((item, index) => {
  164. if (kIsIOS) {
  165. setTimeout(() => {
  166. var Jto = require('react-native').NativeModules.NativeBridge;
  167. Jto.addLocalPush(item)
  168. }, index * 1000)
  169. }
  170. else {
  171. PushNotification.createChannel({
  172. channelId: item.id, // (required)
  173. channelName: "My channel", // (required)
  174. channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
  175. playSound: true, // (optional) default: true
  176. soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
  177. importance: Importance.HIGH, // (optional) default: Importance.HIGH. Int value of the Android notification importance
  178. vibrate: true, // (optional) default: true. Creates the default vibration pattern if true.
  179. }, (created) => {
  180. if (item.mode == 'ONCE') {
  181. console.log('leon 9528')
  182. if (new Date().getTime() <= item.once) {
  183. console.log('leon 9529')
  184. PushNotification.localNotificationSchedule({
  185. //... You can use all the options from localNotifications
  186. channelId: item.id,
  187. title: item.title,
  188. message: item.body,// + '\nPress for actions >>', // (required)
  189. date: new Date(new Date().getTime() + 10 * 1000),
  190. // date: new Date(item.once), // in 60 secs
  191. allowWhileIdle: true, // (optional) set notification to work while on doze, default: false
  192. messageId: item.id,
  193. // repeatType:'minute',
  194. /* Android Only Properties */
  195. })
  196. }
  197. }
  198. else if (item.mode == 'RECURRING') {
  199. console.log('leon 9527')
  200. const { time, repeat_type } = item.recurring;
  201. const date = dayjs().format('YYYY-MM-DD')
  202. const { NativeBridge } = NativeModules
  203. NativeBridge.addLocalPush(item.title, item.body + '\nPress for actions >>', time)
  204. return;
  205. PushNotification.localNotificationSchedule({
  206. //... You can use all the options from localNotifications
  207. channelId: item.id,
  208. title: item.title,
  209. message: item.content + ' ', // (required)
  210. date: new Date(date + 'T' + time), // in 60 secs
  211. allowWhileIdle: true, // (optional) set notification to work while on doze, default: false
  212. messageId: item.id,
  213. repeatType: repeat_type,
  214. // repeatType:'minute',
  215. /* Android Only Properties */
  216. })
  217. }
  218. })
  219. }
  220. })
  221. console.log('notificatin push set', messages)
  222. })
  223. }
  224. });
  225. }
  226. }