index.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. var app = getApp()
  2. var api = require('../../utils/api.js');
  3. const SEC = 1000
  4. const MIN = 60 * SEC
  5. const HOUR = 60 * MIN
  6. Page({
  7. data: {
  8. currentLatitude: 0, //用户当前纬度
  9. currentLongitude: 0, //用户当前经度
  10. is_btn_disabled: true, //开始按钮状态
  11. locationAccuracy: 0, //gps 误差
  12. isLearning: false, //是否在学习中?
  13. startTime: null, //开始时间
  14. clock: '00:00:00', //计时
  15. shareCanvasWidth: 200,
  16. shareCanvasHeight: 280,
  17. shareImage: '',
  18. shareText: '',
  19. shareTextPosX: '',
  20. shareTextPosY: '',
  21. shareTempFilePath: '',
  22. showPopup: false
  23. },
  24. onLoad: function() {
  25. wx.getSystemInfo({
  26. success: (res) => {
  27. this.setData({
  28. screenWidth: res.screenWidth,
  29. screenHeight: res.screenHeight,
  30. bannerWidth: res.screenWidth,
  31. bannerHeight: res.screenWidth * 363 / 543
  32. })
  33. }
  34. })
  35. var pt_student = wx.getStorageSync('pt_student')
  36. if (!pt_student) {
  37. var we_chat_user = wx.getStorageSync('we_chat_user')
  38. if (we_chat_user) {
  39. wx.redirectTo({
  40. url: '/pages/bind-phone/index',
  41. })
  42. } else {
  43. wx.redirectTo({
  44. url: '/pages/login/index',
  45. })
  46. }
  47. } else {
  48. app.globalData.ptStudent = pt_student;
  49. }
  50. wx.getLocation({
  51. success: (res) => {
  52. // let validLocation = this.validLocation(res.latitude, res.longitude)
  53. wx.request({
  54. url: api.checkPositionUrl,
  55. method: 'GET',
  56. data: {
  57. latitude: res.latitude,
  58. longitude: res.longitude
  59. },
  60. success: res => {
  61. let validLocation = false;
  62. if (res.data.status == 'success' && res.data.result == 'ok') {
  63. validLocation = true;
  64. } else {
  65. validLocation = api.isTest ? true : false;
  66. }
  67. this.setData({
  68. is_btn_disabled: !validLocation
  69. })
  70. if (validLocation) {
  71. var start_time = wx.getStorageSync('check_card_start_time')
  72. var check_card_id = wx.getStorageSync('check_card_id')
  73. if (start_time && check_card_id) {
  74. wx.request({
  75. url: api.checkCardIsEndUrl,
  76. method: 'GET',
  77. data: {
  78. check_card_id: check_card_id
  79. },
  80. success: res => {
  81. if (res.data.status == 'success' && res.data.result == 'no') {
  82. this.setData({
  83. startTime: start_time,
  84. isLearning: true
  85. })
  86. this.startClock()
  87. }
  88. }
  89. })
  90. }
  91. }
  92. },
  93. });
  94. this.setData({
  95. currentLatitude: res.latitude,
  96. currentLongitude: res.longitude,
  97. accuracy: res.accuracy
  98. })
  99. }
  100. })
  101. wx.request({
  102. url: api.getShareInfoUrl,
  103. method: 'GET',
  104. data: {
  105. 'student_id': pt_student.id
  106. },
  107. success: res => {
  108. if (res.data.status == 'success') {
  109. this.setData({
  110. shareCanvasWidth: res.data.width,
  111. shareCanvasHeight: res.data.height,
  112. shareImage: res.data.shareImage,
  113. shareText: res.data.shareText,
  114. shareTextPosX: res.data.shareTextPosX,
  115. shareTextPosY: res.data.shareTextPosY,
  116. })
  117. wx.downloadFile({
  118. url: res.data.shareImage,
  119. success: res => {
  120. this.setData({
  121. shareImage: res.tempFilePath
  122. })
  123. }
  124. })
  125. }
  126. }
  127. })
  128. },
  129. validLocation(latitude, longitude) {
  130. let res = api.isTest ? true : false;
  131. wx.request({
  132. url: api.checkPositionUrl,
  133. method: 'GET',
  134. data: {
  135. latitude: latitude,
  136. longitude: longitude
  137. },
  138. success: res => {
  139. if (res.data.status == 'success' && res.data.result == 'ok') {
  140. res = true;
  141. }
  142. }
  143. });
  144. return res;
  145. },
  146. hidePhotoPopup() {
  147. this.setData({
  148. showPopup: false,
  149. clock: '00:00:00'
  150. })
  151. },
  152. handleBtnClick() {
  153. wx.getLocation({
  154. success: (res) => {
  155. // let validLocation = this.validLocation(res.latitude, res.longitude)
  156. wx.request({
  157. url: api.checkPositionUrl,
  158. method: 'GET',
  159. data: {
  160. latitude: res.latitude,
  161. longitude: res.longitude
  162. },
  163. success: res => {
  164. let validLocation = false;
  165. if (res.data.status == 'success' && res.data.result == 'ok') {
  166. validLocation = true;
  167. } else {
  168. validLocation = api.isTest ? true : false;
  169. }
  170. this.setData({
  171. is_btn_disabled: !validLocation
  172. })
  173. this.checkCard()
  174. },
  175. });
  176. }
  177. })
  178. },
  179. checkCard: function() {
  180. let isLearning = this.data.isLearning
  181. let is_btn_disabled = this.data.is_btn_disabled
  182. let that = this;
  183. if (is_btn_disabled) {
  184. wx.showToast({
  185. title: '请到学校之后再打卡',
  186. icon: 'none',
  187. duration: 800
  188. })
  189. return;
  190. }
  191. if (isLearning) {
  192. let check_card_id = wx.getStorageSync('check_card_id');
  193. wx.request({
  194. url: api.endCheckCardUrl,
  195. method: 'GET',
  196. data: {
  197. 'check_card_id': check_card_id
  198. },
  199. success: res => {
  200. if (res.data.status == 'success') {
  201. that.setData({
  202. isLearning: !isLearning
  203. })
  204. that.endClock()
  205. } else {
  206. wx.showToast({
  207. title: res.data.info,
  208. icon: 'none',
  209. duration: 800
  210. })
  211. }
  212. }
  213. })
  214. } else {
  215. wx.request({
  216. url: api.startCheckCardUrl,
  217. method: 'GET',
  218. data: {
  219. 'student_id': app.globalData.ptStudent.id
  220. },
  221. success: res => {
  222. if (res.data.status == 'success') {
  223. wx.setStorageSync('check_card_id', res.data.check_card_id)
  224. var start_time = new Date().getTime()
  225. wx.setStorageSync('check_card_start_time', start_time)
  226. that.setData({
  227. isLearning: !isLearning,
  228. startTime: start_time
  229. })
  230. that.startClock()
  231. } else {
  232. wx.showToast({
  233. title: res.data.info,
  234. icon: 'none',
  235. duration: 800
  236. })
  237. }
  238. }
  239. })
  240. }
  241. },
  242. startClock() {
  243. let interval = setInterval(() => {
  244. let now = new Date().getTime()
  245. let startTime = this.data.startTime
  246. let diff = now - startTime
  247. let hours = Math.floor(diff / HOUR)
  248. hours = (hours < 10 ? '0' + hours : hours)
  249. diff = diff % HOUR
  250. let mins = Math.floor(diff / MIN)
  251. mins = (mins < 10 ? '0' + mins : mins)
  252. diff = diff % MIN
  253. let sec = Math.ceil(diff / SEC)
  254. sec = (sec < 10 ? '0' + sec : sec)
  255. this.setData({
  256. clock: "" + hours + ":" + mins + ":" + sec
  257. })
  258. }, SEC)
  259. this.interval = interval
  260. },
  261. endClock() {
  262. clearInterval(this.interval)
  263. this.setData({
  264. isLearning: false,
  265. showPopup: true
  266. })
  267. var pt_student = wx.getStorageSync('pt_student')
  268. if(pt_student) {
  269. wx.request({
  270. url: api.getShareTextUrl,
  271. method: 'GET',
  272. data: {
  273. 'student_id': app.globalData.ptStudent.id
  274. },
  275. success: res => {
  276. if (res.data.status == 'success') {
  277. wx.setStorageSync('check_card_start_time', '')
  278. const ctx = wx.createCanvasContext('shareCanvas')
  279. let width = this.data.shareCanvasWidth
  280. let height = this.data.shareCanvasHeight
  281. let image = this.data.shareImage
  282. console.log(image)
  283. let text = res.data.shareText
  284. let text_x = this.data.shareTextPosX
  285. let text_y = this.data.shareTextPosY
  286. // ctx.fillStyle = "#fff"
  287. // ctx.fillRect(0, 0, width, height)
  288. ctx.drawImage(image)
  289. ctx.fillStyle = "#fff"
  290. ctx.setFontSize(18)
  291. ctx.textAlign = 'center'
  292. // ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  293. ctx.fillText(text, text_x, text_y)
  294. ctx.draw(false, this.getTempFilePath)
  295. }
  296. }
  297. })
  298. }
  299. },
  300. togglePopup() {
  301. this.setData({
  302. showPopup: !this.data.showPopup
  303. });
  304. },
  305. getTempFilePath: function() {
  306. wx.canvasToTempFilePath({
  307. canvasId: 'shareCanvas',
  308. success: (res) => {
  309. this.setData({
  310. shareTempFilePath: res.tempFilePath
  311. })
  312. }
  313. })
  314. },
  315. handleShare() {
  316. // wx.canvasToTempFilePath({
  317. // canvasId: 'shareCanvas',
  318. // success: (res) => {
  319. // wx.saveImageToPhotosAlbum({
  320. // filePath: res.tempFilePath,
  321. // success: () => {
  322. // wx.showToast({
  323. // title: '已保存到相册'
  324. // })
  325. // }
  326. // })
  327. // }
  328. // }, this)
  329. if (!this.data.shareTempFilePath) {
  330. wx.showModal({
  331. title: '提示',
  332. content: '图片绘制中,请稍后重试',
  333. showCancel: false
  334. })
  335. }
  336. wx.saveImageToPhotosAlbum({
  337. filePath: this.data.shareTempFilePath,
  338. success: (res) => {
  339. // this.setData({
  340. // showPopup: false
  341. // })
  342. },
  343. complete: res => {
  344. this.setData({
  345. showPopup: false,
  346. clock: '00:00:00'
  347. })
  348. }
  349. })
  350. },
  351. onShareAppMessage: function(t) {
  352. var a = this;
  353. return {
  354. path: "/pages/index/index",
  355. success: function(t) {},
  356. title: "钢琴时间打卡"
  357. };
  358. },
  359. })