index.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. student_id: pt_student.id
  60. },
  61. success: res => {
  62. let validLocation = false;
  63. if (res.data.status == 'success' && res.data.result == 'ok') {
  64. validLocation = true;
  65. } else {
  66. validLocation = api.isTest ? true : false;
  67. }
  68. this.setData({
  69. is_btn_disabled: !validLocation
  70. })
  71. if(validLocation) {
  72. var start_time = wx.getStorageSync('check_card_start_time')
  73. if(start_time) {
  74. this.setData({
  75. startTime: start_time,
  76. isLearning: true
  77. })
  78. this.startClock()
  79. }
  80. }
  81. },
  82. });
  83. this.setData({
  84. currentLatitude: res.latitude,
  85. currentLongitude: res.longitude,
  86. accuracy: res.accuracy
  87. })
  88. }
  89. })
  90. wx.request({
  91. url: api.getShareInfoUrl,
  92. method: 'GET',
  93. data: {
  94. 'student_id': pt_student.id
  95. },
  96. success: res => {
  97. if (res.data.status == 'success') {
  98. this.setData({
  99. shareCanvasWidth: res.data.width,
  100. shareCanvasHeight: res.data.height,
  101. shareImage: res.data.shareImage,
  102. shareText: res.data.shareText,
  103. shareTextPosX: res.data.shareTextPosX,
  104. shareTextPosY: res.data.shareTextPosY,
  105. })
  106. wx.downloadFile({
  107. url: res.data.shareImage,
  108. success: res => {
  109. this.setData({
  110. shareImage: res.tempFilePath
  111. })
  112. }
  113. })
  114. }
  115. }
  116. })
  117. },
  118. validLocation(latitude, longitude) {
  119. let res = api.isTest ? true : false;
  120. wx.request({
  121. url: api.checkPositionUrl,
  122. method: 'GET',
  123. data: {
  124. latitude: latitude,
  125. longitude: longitude
  126. },
  127. success: res => {
  128. if(res.data.status == 'success' && res.data.result == 'ok') {
  129. res = true;
  130. }
  131. }
  132. });
  133. return res;
  134. },
  135. hidePhotoPopup() {
  136. this.setData({
  137. showPopup: false,
  138. clock: '00:00:00'
  139. })
  140. },
  141. handleBtnClick() {
  142. wx.getLocation({
  143. success: (res) => {
  144. // let validLocation = this.validLocation(res.latitude, res.longitude)
  145. wx.request({
  146. url: api.checkPositionUrl,
  147. method: 'GET',
  148. data: {
  149. latitude: res.latitude,
  150. longitude: res.longitude
  151. },
  152. success: res => {
  153. let validLocation = false;
  154. if (res.data.status == 'success' && res.data.result == 'ok') {
  155. validLocation = true;
  156. } else {
  157. validLocation = api.isTest ? true : false;
  158. }
  159. this.setData({
  160. is_btn_disabled: !validLocation
  161. })
  162. this.checkCard()
  163. },
  164. });
  165. }
  166. })
  167. },
  168. checkCard: function() {
  169. let isLearning = this.data.isLearning
  170. let is_btn_disabled = this.data.is_btn_disabled
  171. let that = this;
  172. if (is_btn_disabled) {
  173. wx.showToast({
  174. title: '请到学校之后再打卡',
  175. icon: 'none',
  176. duration: 800
  177. })
  178. return;
  179. }
  180. if (isLearning) {
  181. let check_card_id = wx.getStorageSync('check_card_id');
  182. wx.request({
  183. url: api.endCheckCardUrl,
  184. method: 'GET',
  185. data: {
  186. 'check_card_id': check_card_id
  187. },
  188. success: res => {
  189. if (res.data.status == 'success') {
  190. that.setData({
  191. isLearning: !isLearning
  192. })
  193. that.endClock()
  194. } else {
  195. wx.showToast({
  196. title: res.data.info,
  197. icon: 'none',
  198. duration: 800
  199. })
  200. }
  201. }
  202. })
  203. } else {
  204. wx.request({
  205. url: api.startCheckCardUrl,
  206. method: 'GET',
  207. data: {
  208. 'student_id': app.globalData.ptStudent.id
  209. },
  210. success: res => {
  211. if (res.data.status == 'success') {
  212. wx.setStorageSync('check_card_id', res.data.check_card_id)
  213. var start_time = new Date().getTime()
  214. wx.setStorageSync('check_card_start_time', start_time)
  215. that.setData({
  216. isLearning: !isLearning,
  217. startTime: start_time
  218. })
  219. that.startClock()
  220. } else {
  221. wx.showToast({
  222. title: res.data.info,
  223. icon: 'none',
  224. duration: 800
  225. })
  226. }
  227. }
  228. })
  229. }
  230. },
  231. startClock() {
  232. let interval = setInterval(() => {
  233. let now = new Date().getTime()
  234. let startTime = this.data.startTime
  235. let diff = now - startTime
  236. let hours = Math.floor(diff / HOUR)
  237. hours = (hours < 10 ? '0' + hours : hours)
  238. diff = diff % HOUR
  239. let mins = Math.floor(diff / MIN)
  240. mins = (mins < 10 ? '0' + mins : mins)
  241. diff = diff % MIN
  242. let sec = Math.ceil(diff / SEC)
  243. sec = (sec < 10 ? '0' + sec : sec)
  244. this.setData({
  245. clock: "" + hours + ":" + mins + ":" + sec
  246. })
  247. }, SEC)
  248. this.interval = interval
  249. },
  250. endClock() {
  251. clearInterval(this.interval)
  252. this.setData({
  253. isLearning: false,
  254. showPopup: true
  255. })
  256. wx.setStorageSync('check_card_start_time', '')
  257. const ctx = wx.createCanvasContext('shareCanvas')
  258. let width = this.data.shareCanvasWidth
  259. let height = this.data.shareCanvasHeight
  260. let image = this.data.shareImage
  261. let text = this.data.shareText
  262. let text_x = this.data.shareTextPosX
  263. let text_y = this.data.shareTextPosY
  264. // ctx.fillStyle = "#fff"
  265. // ctx.fillRect(0, 0, width, height)
  266. ctx.drawImage(image)
  267. ctx.fillStyle = "#fff"
  268. ctx.setFontSize(24)
  269. ctx.textAlign = 'center'
  270. // ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  271. ctx.fillText(text, text_x, text_y)
  272. ctx.draw(false, this.getTempFilePath)
  273. },
  274. togglePopup() {
  275. this.setData({
  276. showPopup: !this.data.showPopup
  277. });
  278. },
  279. getTempFilePath: function () {
  280. wx.canvasToTempFilePath({
  281. canvasId: 'shareCanvas',
  282. success: (res) => {
  283. this.setData({
  284. shareTempFilePath: res.tempFilePath
  285. })
  286. }
  287. })
  288. },
  289. handleShare() {
  290. // wx.canvasToTempFilePath({
  291. // canvasId: 'shareCanvas',
  292. // success: (res) => {
  293. // wx.saveImageToPhotosAlbum({
  294. // filePath: res.tempFilePath,
  295. // success: () => {
  296. // wx.showToast({
  297. // title: '已保存到相册'
  298. // })
  299. // }
  300. // })
  301. // }
  302. // }, this)
  303. if (!this.data.shareTempFilePath) {
  304. wx.showModal({
  305. title: '提示',
  306. content: '图片绘制中,请稍后重试',
  307. showCancel: false
  308. })
  309. }
  310. wx.saveImageToPhotosAlbum({
  311. filePath: this.data.shareTempFilePath,
  312. success: (res) => {
  313. // this.setData({
  314. // showPopup: false
  315. // })
  316. },
  317. complete: res => {
  318. this.setData({
  319. showPopup: false,
  320. clock: '00:00:00'
  321. })
  322. }
  323. })
  324. },
  325. onShareAppMessage: function (t) {
  326. var a = this;
  327. return {
  328. path: "/pages/index/index",
  329. success: function (t) {
  330. },
  331. title: "钢琴时间打卡"
  332. };
  333. },
  334. })