index.js 9.4 KB

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