index.js 7.1 KB

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