index.js 8.2 KB

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