c-pop-adulthood.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <view class="mask" catchtouchmove="true" :class="!adulthoodStatus ? 'none' : adulthoodStatus ? 'show' : ''">
  3. <view class="mask-content" @click.stop.prevent="stopPrevent">
  4. <view class="mask-content-inner">
  5. <view class="logo-wrap">
  6. <image style="width: 100%;height: 100%;" src="/static/adulthood.png" mode="aspectFill"></image>
  7. </view>
  8. <view class="action-msg">
  9. <text>请在使用前请确认您已满18周岁</text>
  10. </view>
  11. <view class="action-wrap">
  12. <u-button type="warning" shape="circle" size="large" text="未满18岁" @click="exitApp(0)"></u-button>
  13. <view style="width: 100rpx;"></view>
  14. <u-button type="warning" shape="circle" size="large" text="已满18岁" @click="setAdult(1)"></u-button>
  15. </view>
  16. </view>
  17. </view>
  18. <u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
  19. <u-toast ref="uToast"></u-toast>
  20. </view>
  21. </template>
  22. <script>
  23. import store from '@/store'
  24. import { setStorage, getStorage, removeStorage } from '@/util/index.js'
  25. import { mapActions, mapState, mapGetters } from 'vuex';
  26. export default {
  27. name:"c-pop-adulthood",
  28. data() {
  29. return {
  30. maskState: 0,
  31. isAuth: false,
  32. tips: '获取验证码',
  33. seconds: 60,
  34. send: false,
  35. form: {
  36. phone: '',
  37. code: '',
  38. }
  39. };
  40. },
  41. props: {
  42. onComplate: {
  43. type: Function,
  44. default: () => { }
  45. },
  46. },
  47. computed: {
  48. ...mapGetters(['adulthoodStatus']),
  49. },
  50. mounted() {
  51. // 微信隐藏tabBar
  52. // #<!-- #ifdef MP-WEIXIN -->
  53. // if(this.adulthoodStatus){
  54. // wx.hideTabBar()
  55. // }
  56. // <!-- #endif -->
  57. },
  58. destroyed() {
  59. // 微信显示tabBar
  60. // #<!-- #ifdef MP-WEIXIN -->
  61. // if(!this.adulthoodStatus){
  62. // wx.showTabBar()
  63. // }
  64. // <!-- #endif -->
  65. },
  66. methods: {
  67. ...mapActions({
  68. 'login': 'login',
  69. 'loginRefresh': 'loginRefresh',
  70. 'adulthood': 'adulthood',
  71. }),
  72. stopPrevent(e){
  73. console.log(e)
  74. },
  75. toggleMask(type){
  76. let timer = type === 'show' ? 10 : 0;
  77. let state = type === 'show' ? 1 : 0;
  78. this.maskState = 2;
  79. setTimeout(()=>{
  80. this.maskState = state;
  81. }, timer)
  82. },
  83. /**
  84. * @description 确认
  85. * @param {Object} title: String desc: String
  86. */
  87. $confirm(options = {}){
  88. // 继承原来的
  89. // this.defautlOptions = Object.assign(this.defautlOptions, options);
  90. return new Promise((resolve,reject)=> {
  91. this.toggleMask('show')
  92. this._confirm = () => {
  93. resolve(1)
  94. this._confirm = null;
  95. }
  96. this._cancel = () => {
  97. resolve(0)
  98. this._cancel = null;
  99. }
  100. })
  101. },
  102. confirm() {
  103. wx.showTabBar()
  104. if(this._confirm){
  105. this._confirm()
  106. this.toggleMask();
  107. }
  108. },
  109. cancel() {
  110. wx.showTabBar()
  111. if(this._cancel){
  112. this._cancel()
  113. this.toggleMask();
  114. }
  115. },
  116. getPosition(){
  117. const that = this;
  118. return new Promise((resolve,reject) => {
  119. uni.getSetting({
  120. success(res) {
  121. console.log(res)
  122. if(!res.authSetting['scope.userLocation']){
  123. console.log("没有权限")
  124. uni.authorize({
  125. scope: 'scope.userLocation',
  126. success () {
  127. console.log("用户已同意")
  128. uni.getLocation({
  129. type: 'wgs84',
  130. success: function (res) {
  131. that.getAddress(res.latitude,res.longitude).then((posObj) => {
  132. resolve(posObj)
  133. }).catch(() => {
  134. reject({})
  135. })
  136. }
  137. });
  138. },
  139. fail() {
  140. console.log("用户拒绝")
  141. reject()
  142. }
  143. })
  144. }else{
  145. uni.getLocation({
  146. type: 'wgs84',
  147. success: function (res) {
  148. that.getAddress(res.latitude,res.longitude).then((posObj) => {
  149. resolve(posObj)
  150. }).catch(() => {
  151. reject({})
  152. })
  153. },
  154. fail() {
  155. reject()
  156. }
  157. });
  158. }
  159. }
  160. })
  161. })
  162. },
  163. getAddress(latitude,longitude){
  164. return new Promise((resolve,reject) => {
  165. const keys = 'PLZBZ-HSD66-JRXSW-MHAU5-4BTOQ-FUBLQ';
  166. wx.request({
  167. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${keys}`,
  168. header: {
  169. 'Content-Type': 'application/json'
  170. },
  171. success: function(res) {
  172. //console.log(res.data.result.address_component.district, res.data.result)
  173. // 国家
  174. let country = res.data.result.address_component.nation
  175. // 省
  176. let province = res.data.result.address_component.province;
  177. // 市
  178. let city = res.data.result.address_component.city;
  179. // 区
  180. let district = res.data.result.address_component.district;
  181. console.log(city)
  182. resolve({country: country,province: province, city: city, district: district})
  183. // apiUpdateWxData({sex:0, country: country,province: province, city: city, district: district})
  184. },
  185. fail:function(err){
  186. reject(err)
  187. }
  188. })
  189. })
  190. },
  191. async setAdult(type) {
  192. this.$refs.uToast.show({
  193. type: 'loading',
  194. message: '',
  195. })
  196. // uni.showModal({
  197. // title: '提示',
  198. // content: "点击了,方法"
  199. // });
  200. try{
  201. const objres = await this.getPosition();
  202. // 已成年
  203. if(type == 1){
  204. // uni.showModal({
  205. // title: '提示',
  206. // content: "获取位置成功"
  207. // });
  208. console.log("success: 获取位置成功")
  209. store.dispatch('disAdulthood', {detail: true, pos: objres}).then(res => {
  210. console.log("设置成年")
  211. // uni.showModal({
  212. // title: '提示',
  213. // content: "设置成年成功"
  214. // });
  215. this.$refs.uToast.hide()
  216. })
  217. return
  218. }
  219. }catch(e){
  220. //TODO handle the exception
  221. if(type == 1){
  222. // uni.showModal({
  223. // title: '提示',
  224. // content: "获取位置失败"
  225. // });
  226. console.log("error: 获取位置失败")
  227. store.dispatch('disAdulthood', {detail: true, pos: {country: "未知地区",province: "未知地区", city: "未知地区", district: "未知地区"}}).then(res => {
  228. console.log("设置成年")
  229. this.$refs.uToast.hide()
  230. })
  231. return
  232. }
  233. }
  234. },
  235. exitApp(){
  236. store.dispatch('disAdulthood', {detail: false, pos: {}}).then(res => {
  237. this.$refs.uToast.hide()
  238. })
  239. uni.showModal({
  240. title: '提示',
  241. content: "未满18周岁不可饮酒",
  242. confirmText: "退出",
  243. showCancel: false,
  244. success() {
  245. wx.exitMiniProgram({
  246. success(e) {
  247. console.log("退出")
  248. },
  249. fail(e) {
  250. console.log("退出失败")
  251. },
  252. })
  253. }
  254. });
  255. },
  256. codeChange(text) {
  257. this.tips = text;
  258. },
  259. getCode() {
  260. if(this.$refs.uCode.canGetCode) {
  261. // 模拟向后端请求验证码
  262. this.$refs.uToast.show({
  263. type: 'loading',
  264. message: '正在获取验证码',
  265. })
  266. // TODO 发起请求:
  267. this.$api('h5.getCode', {mobile: this.form.phone}).then(res => {
  268. console.log(res)
  269. this.$refs.uToast.hide()
  270. if(res.code == 1){
  271. // 这里此提示会被this.start()方法中的提示覆盖
  272. uni.$u.toast('验证码已发送')
  273. // 通知验证码组件内部开始倒计时
  274. this.$refs.uCode.start()
  275. }
  276. })
  277. } else {
  278. uni.$u.toast('倒计时结束后再发送');
  279. }
  280. },
  281. end() {
  282. this.send = false;
  283. },
  284. start() {
  285. this.send = true;
  286. },
  287. loginAction() {
  288. this.$refs.uToast.show({
  289. type: 'loading',
  290. message: '正在登录,请稍后',
  291. })
  292. this.login({mobile: this.form.phone, code: this.form.code}).then(res => {
  293. this.$refs.uToast.hide()
  294. if(res.code == 1){
  295. // 这里此提示会被this.start()方法中的提示覆盖
  296. uni.$u.toast('登录成功')
  297. // 通知验证码组件内部开始倒计时
  298. this.$refs.uCode.start()
  299. }
  300. this.onComplate()
  301. })
  302. }
  303. },
  304. }
  305. </script>
  306. <style lang="scss" scoped>
  307. .mask{
  308. display: flex;
  309. flex-direction: column;
  310. align-items: flex-end;
  311. justify-content: flex-end;
  312. position: fixed;
  313. left: 0;
  314. top: var(--window-top);
  315. bottom: 0;
  316. width: 100vw;
  317. height: 100vh;
  318. background: rgba(0,0,0,0);
  319. z-index: 10001;
  320. transition: .3s;
  321. .mask-content{
  322. background-color: #fff;
  323. width: 100vw;
  324. align-self: stretch;
  325. min-height: 597rpx;
  326. border-top-left-radius: 28rpx;
  327. border-top-right-radius: 28rpx;
  328. padding-bottom: 100rpx;
  329. overflow: hidden;
  330. // padding-top: 60rpx;
  331. // display: flex;
  332. // flex-direction: column;
  333. // width: 590rpx;
  334. // max-height: 45vh;
  335. // min-height: 361rpx;
  336. // background: #fff;
  337. // border-radius: 15px;
  338. }
  339. .mask-content-inner{
  340. position: relative;
  341. display: flex;
  342. flex-direction: column;
  343. align-items: center;
  344. .auth-bg{
  345. position: absolute;
  346. top: 0;
  347. left: 0;
  348. right: 0;
  349. width: 100vw;
  350. height: 393rpx;
  351. }
  352. .logo-wrap{
  353. width: 101vw;
  354. height: 345rpx;
  355. }
  356. .action-wrap{
  357. margin-top: 48rpx;
  358. padding: 0 30rpx;
  359. align-self: stretch;
  360. display: flex;
  361. }
  362. .action-msg{
  363. display: flex;
  364. justify-content: center;
  365. margin-top: 48rpx;
  366. font-size: 28rpx;
  367. font-family: PingFangSC-Semibold, PingFang SC;
  368. font-weight: 600;
  369. color: #000000;
  370. }
  371. }
  372. &.none{
  373. display: none;
  374. }
  375. &.show{
  376. background: rgba(0,0,0,.8);
  377. .mask-content{
  378. transform: translateY(0);
  379. }
  380. }
  381. }
  382. </style>