index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. wx.getLocation({
  122. success: (res) => {
  123. // let validLocation = this.validLocation(res.latitude, res.longitude)
  124. wx.request({
  125. url: api.checkPositionUrl,
  126. method: 'GET',
  127. data: {
  128. latitude: res.latitude,
  129. longitude: res.longitude
  130. },
  131. success: res => {
  132. let validLocation = false;
  133. if (res.data.status == 'success' && res.data.result == 'ok') {
  134. validLocation = api.isTest ? true : false;
  135. }
  136. validLocation = api.isTest ? true : false;
  137. this.setData({
  138. is_btn_disabled: !validLocation
  139. })
  140. this.checkCard()
  141. },
  142. });
  143. }
  144. })
  145. },
  146. checkCard: function() {
  147. let isLearning = this.data.isLearning
  148. let is_btn_disabled = this.data.is_btn_disabled
  149. let that = this;
  150. if (is_btn_disabled) {
  151. wx.showToast({
  152. title: '请到学校之后再打卡',
  153. icon: 'none',
  154. duration: 800
  155. })
  156. return;
  157. }
  158. if (isLearning) {
  159. let check_card_id = wx.getStorageSync('check_card_id');
  160. wx.request({
  161. url: api.endCheckCardUrl,
  162. method: 'GET',
  163. data: {
  164. 'check_card_id': check_card_id
  165. },
  166. success: res => {
  167. if (res.data.status == 'success') {
  168. that.setData({
  169. isLearning: !isLearning
  170. })
  171. that.endClock()
  172. } else {
  173. wx.showToast({
  174. title: res.data.info,
  175. icon: 'none',
  176. duration: 800
  177. })
  178. }
  179. }
  180. })
  181. } else {
  182. wx.request({
  183. url: api.startCheckCardUrl,
  184. method: 'GET',
  185. data: {
  186. 'student_id': app.globalData.ptStudent.id
  187. },
  188. success: res => {
  189. if (res.data.status == 'success') {
  190. wx.setStorageSync('check_card_id', res.data.check_card_id)
  191. that.setData({
  192. isLearning: !isLearning,
  193. startTime: new Date().getTime()
  194. })
  195. that.startClock()
  196. } else {
  197. wx.showToast({
  198. title: res.data.info,
  199. icon: 'none',
  200. duration: 800
  201. })
  202. }
  203. }
  204. })
  205. }
  206. },
  207. startClock() {
  208. let interval = setInterval(() => {
  209. let now = new Date().getTime()
  210. let startTime = this.data.startTime
  211. let diff = now - startTime
  212. let hours = Math.floor(diff / HOUR)
  213. hours = (hours < 10 ? '0' + hours : hours)
  214. diff = diff % HOUR
  215. let mins = Math.floor(diff / MIN)
  216. mins = (mins < 10 ? '0' + mins : mins)
  217. diff = diff % MIN
  218. let sec = Math.ceil(diff / SEC)
  219. sec = (sec < 10 ? '0' + sec : sec)
  220. this.setData({
  221. clock: "" + hours + ":" + mins + ":" + sec
  222. })
  223. }, SEC)
  224. this.interval = interval
  225. },
  226. endClock() {
  227. clearInterval(this.interval)
  228. this.setData({
  229. isLearning: false,
  230. showPopup: true
  231. })
  232. const ctx = wx.createCanvasContext('shareCanvas')
  233. let width = this.data.shareCanvasWidth
  234. let height = this.data.shareCanvasHeight
  235. let image = this.data.shareImage
  236. let text = this.data.shareText
  237. let text_x = this.data.shareTextPosX
  238. let text_y = this.data.shareTextPosY
  239. // ctx.fillStyle = "#fff"
  240. // ctx.fillRect(0, 0, width, height)
  241. ctx.drawImage(image)
  242. ctx.fillStyle = "#fff"
  243. ctx.setFontSize(24)
  244. ctx.textAlign = 'center'
  245. // ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  246. ctx.fillText(text, text_x, text_y)
  247. ctx.draw(false, this.getTempFilePath)
  248. },
  249. togglePopup() {
  250. this.setData({
  251. showPopup: !this.data.showPopup
  252. });
  253. },
  254. getTempFilePath: function () {
  255. wx.canvasToTempFilePath({
  256. canvasId: 'shareCanvas',
  257. success: (res) => {
  258. this.setData({
  259. shareTempFilePath: res.tempFilePath
  260. })
  261. }
  262. })
  263. },
  264. handleShare() {
  265. // wx.canvasToTempFilePath({
  266. // canvasId: 'shareCanvas',
  267. // success: (res) => {
  268. // wx.saveImageToPhotosAlbum({
  269. // filePath: res.tempFilePath,
  270. // success: () => {
  271. // wx.showToast({
  272. // title: '已保存到相册'
  273. // })
  274. // }
  275. // })
  276. // }
  277. // }, this)
  278. if (!this.data.shareTempFilePath) {
  279. wx.showModal({
  280. title: '提示',
  281. content: '图片绘制中,请稍后重试',
  282. showCancel: false
  283. })
  284. }
  285. wx.saveImageToPhotosAlbum({
  286. filePath: this.data.shareTempFilePath,
  287. success: (res) => {
  288. },
  289. complete: res => {
  290. this.setData({
  291. showPopup: false
  292. })
  293. }
  294. })
  295. }
  296. })