index.js 9.3 KB

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