agent_sign_in.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <template>
  2. <view class="voucher">
  3. <image src="../../static/imgs/sign_in_before.jpg" class="bgImg"></image>
  4. <view class="voucher_con">
  5. <view class="line"></view>
  6. <view class="title">第{{ signInfo.season ? signInfo.season : '' }}届大卫博士创业密训{{ signInfo.type == 1 ? '密训' : '实训' }}营</view>
  7. <view class="voucher_icon">
  8. <image src="../../static/written_off.png"></image>
  9. <view>有效凭证</view>
  10. </view>
  11. <view class="info">
  12. <view class="flexS">
  13. <text>报名人:</text>
  14. <view>{{ signInfo.nickname }}</view>
  15. </view>
  16. <view class="flexS">
  17. <text>手机号:</text>
  18. <view>{{ signInfo.phone }}</view>
  19. </view>
  20. <view class="flexS">
  21. <text>报名时间:</text>
  22. <view>{{ signInfo.sign_time }}</view>
  23. </view>
  24. </view>
  25. <view class="btn flexC" @click="signIn">确认签到</view>
  26. </view>
  27. <view class="auth_login flexC" v-if="isAuthLogin"><button open-type="getUserInfo" @getuserinfo="userLogin" class="auth_btn">点击登录</button></view>
  28. <view class="phone_login" v-if="isPhoneLogin">
  29. <view class="phone_container">
  30. <view class="close" @click="isPhoneLogin = false"></view>
  31. <view class="top">
  32. <text>手机号验证</text>
  33. <div class="userImg"><open-data type="userAvatarUrl" class="getImgBtn"></open-data></div>
  34. </view>
  35. <view class="bottom">
  36. <input v-model="phone" type="text" placeholder="请输入手机号" class="input" maxlength="11" />
  37. <view class="bottom_eqcode">
  38. <input v-model="code" type="text" placeholder="输入验证码" class="input eqcode_input" />
  39. <view class="eqcode" @click="getCode">{{ countDown ? `重新获取(${countDown}s)` : '获取验证码' }}</view>
  40. </view>
  41. </view>
  42. <view class="submit_btn" @click="phoneLogin">提交</view>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import { wecatLogin, getUserInfo, phoneLogin, sendCode } from '../../api/index.js';
  49. import { getSign, agentSign, getActivityPlaceIp } from '../../api/sign_in.js';
  50. function Rad(d){
  51. return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
  52. }
  53. function GetDistance(lat1, lng1, lat2, lng2){
  54. var radLat1 = Rad(lat1);
  55. var radLat2 = Rad(lat2);
  56. var a = radLat1 - radLat2;
  57. var b = Rad(lng1) - Rad(lng2);
  58. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
  59. Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
  60. s = s * 6378.137;
  61. s = Math.round(s * 10000) / 10000;
  62. return s;
  63. }
  64. export default {
  65. data() {
  66. return {
  67. userInfo: {}, //用户信息
  68. id: '', //扫码获取的id
  69. signInfo: {}, //报名人信息
  70. isAuthLogin: false, //是否显示登陆授权按钮
  71. isPhoneLogin: false, //是否显示手机登陆弹窗
  72. wechatInfo: {
  73. latitude: '',
  74. longitude: '',
  75. avatarUrl: '',
  76. nickName: ''
  77. }, //微信信息
  78. phone: '', //手机号
  79. code: '', //验证码
  80. countDown: 0, //手机验证码倒计时
  81. loginStatus: true //登陆状态
  82. };
  83. },
  84. onLoad(options) {
  85. if (options.scene) {
  86. this.id = options.scene;
  87. uni.setStorageSync('agentId', options.scene);
  88. }
  89. },
  90. onShow() {
  91. // if (!uni.getStorageSync('token')) {
  92. // uni.showModal({
  93. // content: '您还未登录小程序,请先登录',
  94. // showCancel: false,
  95. // success: res => {
  96. // if (res.confirm) {
  97. // uni.switchTab({
  98. // url: '../index/index'
  99. // });
  100. // }
  101. // }
  102. // });
  103. // } else {
  104. // this.getSign();
  105. // }
  106. let that = this;
  107. that.getSetting();
  108. if (!uni.getStorageSync('token')) {
  109. that.isAuthLogin = true;
  110. } else {
  111. that.getUserInfo();
  112. that.getSign();
  113. }
  114. },
  115. watch: {
  116. isAuthLogin(a) {
  117. if (!a) {
  118. let that = this;
  119. wx.login({
  120. success({ code }) {
  121. that.wechatInfo.code = code;
  122. }
  123. });
  124. }
  125. },
  126. isPhoneLogin(a) {
  127. if (!a) {
  128. this.phone = '';
  129. this.code = '';
  130. }
  131. }
  132. },
  133. methods: {
  134. //获取代理报名信息
  135. getSign() {
  136. getSign({ id: this.id || uni.getStorageSync('agentId') }).then(res => {
  137. if (res.code == 200) {
  138. this.signInfo = res.data;
  139. } else {
  140. uni.showModal({
  141. content: res.message || '请求失败',
  142. showCancel: false,
  143. success: function(res) {
  144. if (res.confirm) {
  145. uni.switchTab({
  146. url: '../index/index'
  147. });
  148. }
  149. }
  150. });
  151. }
  152. });
  153. },
  154. //代理确认签到
  155. async signIn() {
  156. const _ip = await getActivityPlaceIp()
  157. if(!_ip || _ip.code !== 200) {
  158. uni.showModal({
  159. title: '签到失败',
  160. content: '获取活动举办地地址ip失败',
  161. showCancel: false
  162. })
  163. return false
  164. }
  165. let { data: { lng, lat, location_status, distance: maxDis } } = _ip
  166. let { longitude: lng1, latitude: lat1 } = this.wechatInfo
  167. const _dis = GetDistance(lat1, lng1, lat, lng)
  168. if(location_status && _dis > maxDis) {
  169. uni.showModal({
  170. title: '签到失败',
  171. content: '您的签到地址超出签到范围',
  172. showCancel: false
  173. })
  174. return false
  175. }
  176. agentSign({ id: this.signInfo.id }).then(res => {
  177. if (res.code == 200) {
  178. let { nickname, sign_time, phone, season, type } = this.signInfo;
  179. uni.showModal({
  180. title: '提示',
  181. content: '签到成功!',
  182. showCancel: false,
  183. success: res => {
  184. if (res.confirm) {
  185. uni.reLaunch({
  186. url:
  187. '../written_off/written_off?nickname=' +
  188. nickname +
  189. '&created_at=' +
  190. sign_time +
  191. '&phone=' +
  192. phone +
  193. '&path=' +
  194. 0 +
  195. '&season=' +
  196. season +
  197. '&type=' +
  198. type
  199. });
  200. }
  201. }
  202. });
  203. } else {
  204. uni.showModal({
  205. content: res.message || '请求失败',
  206. showCancel: false,
  207. success: res => {
  208. if (res.confirm) {
  209. uni.switchTab({
  210. url: '../index/index'
  211. });
  212. }
  213. }
  214. });
  215. }
  216. });
  217. },
  218. //判断用户是否打开定位权限
  219. getSetting() {
  220. let that = this;
  221. uni.getSetting({
  222. success: res => {
  223. // 从未触发过授权的情况
  224. if (typeof res.authSetting['scope.userLocation'] == 'undefined') {
  225. this.getLocation();
  226. return false;
  227. }
  228. //位置授权关闭 引导开启
  229. if (!res.authSetting['scope.userLocation']) {
  230. uni.showModal({
  231. title: '是否授权当前位置',
  232. content: '如需正常使用本程序,请在授权管理中选中“地理位置”,然后点按返回即可正常使用',
  233. showCancel: false,
  234. success: res => {
  235. if (res.confirm) {
  236. that.openSetting();
  237. }
  238. }
  239. });
  240. return false;
  241. }
  242. //位置授权开启,获取当前位置
  243. if (res.authSetting['scope.userLocation']) {
  244. this.getLocation();
  245. }
  246. },
  247. fail: err => {
  248. console.log(err);
  249. }
  250. });
  251. },
  252. openSetting() {
  253. wx.openSetting({
  254. success: res => {
  255. let that = this;
  256. if (res.authSetting['scope.userLocation']) {
  257. this.getLocation();
  258. }
  259. },
  260. fail: err => {
  261. console.log(err);
  262. uni.showModal({
  263. content: '开启位置权限失败,请手动开启',
  264. showCancel: false
  265. });
  266. }
  267. });
  268. },
  269. getLocation() {
  270. let that = this;
  271. uni.getLocation({
  272. // 用户同意授权地理位置
  273. success: ({ latitude, longitude }) => {
  274. that.wechatInfo.latitude = latitude;
  275. that.wechatInfo.longitude = longitude;
  276. },
  277. fail: function(err) {
  278. // 授权地理位置调用失败
  279. // if (err.errMsg == 'getLocation:fail system permission denied') {
  280. // uni.showModal({
  281. // content: '获取位置失败,请检查手机GPS或者微信使用手机定位权限是否开启',
  282. // showCancel: false
  283. // });
  284. // return false;
  285. // }
  286. if (err.errMsg == 'getLocation:fail auth deny') {
  287. uni.showModal({
  288. title: '是否授权当前位置',
  289. content: '取消位置授权,将影响您使用小程序,请重新开启授权',
  290. showCancel: false,
  291. success: res => {
  292. if (res.confirm) {
  293. that.openSetting();
  294. }
  295. }
  296. });
  297. }
  298. }
  299. });
  300. },
  301. //获取验证码
  302. getCode() {
  303. let that = this;
  304. if (that.countDown) {
  305. uni.showModal({
  306. content: '验证码已发送,请稍后重试',
  307. showCancel: false
  308. });
  309. return false;
  310. }
  311. if (!that.phone) {
  312. uni.showModal({
  313. content: '请输入手机号',
  314. showCancel: false
  315. });
  316. return false;
  317. }
  318. sendCode({
  319. phone: that.phone
  320. }).then(res => {
  321. if (res.code != 200) {
  322. uni.showModal({
  323. content: res.message || '获取验证码失败',
  324. showCancel: false
  325. });
  326. return false;
  327. }
  328. if (res.code == 200) {
  329. uni.showModal({
  330. content: '验证码发送成功',
  331. showCancel: false
  332. });
  333. this.countDown = 60;
  334. this.timer = setInterval(() => {
  335. this.countDown--;
  336. if (!this.countDown) {
  337. this.countDown = 0;
  338. clearInterval(this.timer);
  339. }
  340. }, 1000);
  341. }
  342. });
  343. },
  344. /*微信登录获取 token
  345. * @params openid用户openid 传空
  346. * @params avatar 用户头像
  347. * @params nickname 用户昵称
  348. * @params code 微信登录code
  349. * @params longitude 用户位置经度
  350. * @params latitude 用户位置维度
  351. */
  352. userLogin({ detail }) {
  353. let that = this;
  354. if (detail.errMsg === 'getUserInfo:ok') {
  355. that.wechatInfo.avatarUrl = detail.userInfo.avatarUrl;
  356. that.wechatInfo.nickName = detail.userInfo.nickName;
  357. uni.login({
  358. success: ({ code }) => {
  359. that.wechatInfo.code = code;
  360. that.getLocation();
  361. // if (!that.wechatInfo.longitude && !that.wechatInfo.latitude) {
  362. // uni.showModal({
  363. // content: '获取位置失败,请检查手机GPS或者微信使用手机定位权限是否开启',
  364. // showCancel: false
  365. // });
  366. // return false;
  367. // }
  368. let { avatarUrl, nickName, latitude, longitude } = that.wechatInfo;
  369. wecatLogin({
  370. openid: '',
  371. avatar: avatarUrl,
  372. nickname: nickName,
  373. code: code,
  374. longitude: longitude,
  375. latitude: latitude
  376. })
  377. .then(res => {
  378. if (res.code === 300) {
  379. that.isAuthLogin = false;
  380. that.isPhoneLogin = true;
  381. } else if (res.code === 200) {
  382. uni.setStorageSync('token', res.data.token);
  383. that.getSign();
  384. that.getUserInfo();
  385. } else {
  386. uni.showModal({
  387. content: res.message,
  388. showCancel: false
  389. });
  390. }
  391. })
  392. .catch(e => {})
  393. .finally(() => {
  394. that.isAuthLogin = false;
  395. });
  396. }
  397. });
  398. } else {
  399. uni.showModal({
  400. content: '不授权将无法获取到您的信息,请手动开启授权',
  401. showCancel: false,
  402. success: res => {
  403. if (res.confirm) {
  404. wx.openSetting({
  405. success: res => {
  406. let that = this;
  407. if (res.authSetting['scope.userInfo']) {
  408. uni.showModal({
  409. content: '用户权限已开启',
  410. showCancel: false
  411. });
  412. } else {
  413. uni.showModal({
  414. content: '您拒绝了用户信息权限',
  415. showCancel: false
  416. });
  417. }
  418. },
  419. fail: err => {
  420. uni.showModal({
  421. content: '开启用户信息权限失败',
  422. showCancel: false
  423. });
  424. }
  425. });
  426. }
  427. }
  428. });
  429. }
  430. },
  431. // 手机验证码登陆
  432. phoneLogin() {
  433. let that = this;
  434. if (!that.phone) {
  435. uni.showModal({
  436. content: '手机号不存在',
  437. showCancel: false
  438. });
  439. return false;
  440. }
  441. if (!that.code) {
  442. uni.showModal({
  443. content: '验证码不存在',
  444. showCancel: false
  445. });
  446. return false;
  447. }
  448. if (!that.loginStatus) return false;
  449. that.loginStatus = false;
  450. uni.login({
  451. success: ({ code }) => {
  452. phoneLogin({
  453. phone: that.phone,
  454. phone_code: that.code,
  455. avatar: that.wechatInfo.avatarUrl,
  456. nickname: that.wechatInfo.nickName,
  457. longitude: that.wechatInfo.longitude,
  458. latitude: that.wechatInfo.latitude,
  459. code
  460. })
  461. .then(res => {
  462. if (res.code === 200) {
  463. this.isPhoneLogin = false;
  464. uni.setStorageSync('token', res.data.token);
  465. that.getSign();
  466. that.getUserInfo();
  467. } else {
  468. uni.showModal({
  469. content: res.message || '请求失败',
  470. showCancel: false
  471. });
  472. }
  473. })
  474. .catch(e => {})
  475. .finally(() => {
  476. that.loginStatus = true;
  477. });
  478. }
  479. });
  480. },
  481. // 获取用户信息
  482. getUserInfo() {
  483. var that = this;
  484. that.getSetting();
  485. let { latitude, longitude } = that.wechatInfo;
  486. getUserInfo({ latitude, longitude })
  487. .then(res => {
  488. if (res.code === 200) {
  489. this.userInfo = res.data;
  490. uni.setStorageSync('userInfo', res.data);
  491. } else {
  492. uni.showModal({
  493. content: res.message || '请求失败',
  494. showCancel: false
  495. });
  496. }
  497. })
  498. .catch(e => {});
  499. }
  500. }
  501. };
  502. </script>
  503. <style lang="scss" scoped>
  504. .phone_login {
  505. position: fixed;
  506. top: 0;
  507. bottom: 0;
  508. left: 0;
  509. right: 0;
  510. z-index: 999;
  511. display: flex;
  512. align-items: center;
  513. justify-content: center;
  514. background-color: rgba(0, 0, 0, 0.3);
  515. .phone_container {
  516. width: 570rpx;
  517. background-color: #ffffff;
  518. border-radius: 10rpx;
  519. overflow: hidden;
  520. position: relative;
  521. .close {
  522. &::before {
  523. position: absolute;
  524. top: 26rpx;
  525. right: 26rpx;
  526. content: '\2716';
  527. display: block;
  528. width: 30rpx;
  529. height: 30rpx;
  530. color: #ffffff;
  531. z-index: 9999;
  532. }
  533. }
  534. .top {
  535. width: 100%;
  536. height: 220rpx;
  537. background-color: #fa6342;
  538. color: #ffffff;
  539. font-size: 40rpx;
  540. padding-top: 50rpx;
  541. font-weight: blod;
  542. box-sizing: border-box;
  543. display: flex;
  544. justify-content: flex-start;
  545. flex-direction: column;
  546. align-items: center;
  547. position: relative;
  548. .userImg {
  549. position: absolute;
  550. width: 138rpx;
  551. height: 138rpx;
  552. border-radius: 50%;
  553. bottom: -69rpx;
  554. overflow: hidden;
  555. .getImgBtn {
  556. width: 138rpx;
  557. height: 138rpx;
  558. }
  559. }
  560. }
  561. .bottom {
  562. padding: 100rpx 60rpx 30rpx;
  563. background-color: #ffffff;
  564. .input {
  565. width: 100%;
  566. height: 70rpx;
  567. padding: 0 17rpx;
  568. box-sizing: border-box;
  569. color: #000000;
  570. font-size: 30rpx;
  571. background-color: #eeeeee;
  572. border-radius: 2rpx;
  573. }
  574. .bottom_eqcode {
  575. display: flex;
  576. justify-content: space-between;
  577. align-items: center;
  578. margin-top: 60rpx;
  579. .eqcode_input {
  580. width: 240rpx;
  581. }
  582. .eqcode {
  583. width: calc(100% - 240rpx);
  584. color: #fa6342;
  585. font-size: 32rpx;
  586. text-align: right;
  587. }
  588. }
  589. }
  590. .submit_btn {
  591. width: 100%;
  592. height: 86rpx;
  593. line-height: 86rpx;
  594. text-align: center;
  595. background-color: rgba(250, 99, 66, 0.2);
  596. color: #fa6342;
  597. font-size: 32rpx;
  598. }
  599. }
  600. }
  601. .auth_login {
  602. position: fixed;
  603. top: 0;
  604. bottom: 0;
  605. left: 0;
  606. right: 0;
  607. z-index: 999;
  608. background-color: rgba(0, 0, 0, 0.8);
  609. .auth_btn {
  610. width: 80%;
  611. height: 88rpx;
  612. background: #ea4a41;
  613. color: #fff;
  614. }
  615. }
  616. .voucher {
  617. hieght: 100vh;
  618. min-height: 100vh;
  619. width: 100vw;
  620. position: relative;
  621. .bgImg {
  622. height: 100vh;
  623. width: 100vw;
  624. position: fixed;
  625. top: 0;
  626. left: 0;
  627. }
  628. .voucher_con {
  629. position: fixed;
  630. height: 80%;
  631. width: 100%;
  632. bottom: 0;
  633. left: 0;
  634. background: #fff;
  635. border-top-left-radius: 60rpx;
  636. border-top-right-radius: 60rpx;
  637. .line {
  638. width: 128rpx;
  639. height: 8rpx;
  640. background: #f1f3f5;
  641. margin: 36rpx auto 0;
  642. }
  643. .title {
  644. color: #333;
  645. font-size: 42rpx;
  646. margin: 48rpx auto 101rpx;
  647. text-align: center;
  648. font-weight: bold;
  649. }
  650. .voucher_icon {
  651. text-align: center;
  652. image {
  653. height: 192rpx;
  654. width: 192rpx;
  655. margin-bottom: 51rpx;
  656. }
  657. view {
  658. font-size: 48rpx;
  659. color: #07b159;
  660. }
  661. }
  662. .info {
  663. margin: 30rpx 0 40rpx 100rpx;
  664. > view {
  665. margin-top: 34rpx;
  666. font-size: 28rpx;
  667. text {
  668. color: #656565;
  669. }
  670. view {
  671. color: #333333;
  672. }
  673. }
  674. }
  675. .btn {
  676. width: 640rpx;
  677. margin: 0 auto;
  678. height: 88rpx;
  679. color: #fff;
  680. background-color: #07b159;
  681. border-radius: 44rpx;
  682. letter-spacing: 2rpx;
  683. }
  684. }
  685. }
  686. </style>