c-pop-phone.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="mask" catchtouchmove="true" :class="!phoneStatus ? 'none' : phoneStatus ? 'show' : ''" @click.stop.prevent="cancel">
  3. <view class="mask-content" @click.stop.prevent="stopPrevent">
  4. <view class="mask-content-inner">
  5. <view class="head">
  6. <view class="icon" hover-class="hover" hover-stay-time="100" @click="cancel">
  7. <u-icon name="arrow-left" color="#000" size="28"></u-icon>
  8. </view>
  9. <view class="head__text">授权申请</view>
  10. </view>
  11. <view class="body">
  12. <view class="autho-info">获取您的微信手机号</view>
  13. <view class="explain-info">
  14. <text class="text">我已阅读并同意</text><text class="link" @click="getContract(2)">《个人信息保护政策》</text>
  15. </view>
  16. </view>
  17. <view class="action-wrap">
  18. <u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" type="primary" shape="round" size="large" text="允许"></u-button>
  19. </view>
  20. </view>
  21. </view>
  22. <u-toast ref="uToast"></u-toast>
  23. </view>
  24. </template>
  25. <script>
  26. import store from '@/store'
  27. import { setStorage, getStorage, removeStorage } from '@/util/index.js'
  28. import { mapActions, mapState, mapGetters } from 'vuex';
  29. const WXBizDataCrypt = require('@/common/WXBizDataCrypt.js')
  30. import mpWx from '@/common/mpWX.js'
  31. export default {
  32. name:"c-pop-phone",
  33. data() {
  34. return {
  35. maskState: 0,
  36. };
  37. },
  38. props: {
  39. onComplate: {
  40. type: Function,
  41. default: () => { }
  42. },
  43. },
  44. computed: {
  45. ...mapGetters(['phoneStatus']),
  46. },
  47. mounted() {
  48. },
  49. destroyed() {
  50. },
  51. watch:{
  52. phoneStatus(v){
  53. if(v){
  54. this.appInstance = getApp()
  55. console.log(this.appInstance)
  56. }
  57. }
  58. },
  59. methods: {
  60. ...mapActions({
  61. 'disPhone': 'disPhone',
  62. 'closePhone': 'closePhone',
  63. 'login': 'login',
  64. }),
  65. stopPrevent(e){
  66. // console.log(e)
  67. },
  68. toggleMask(type){
  69. let timer = type === 'show' ? 10 : 0;
  70. let state = type === 'show' ? 1 : 0;
  71. this.maskState = 2;
  72. setTimeout(()=>{
  73. this.maskState = state;
  74. }, timer)
  75. },
  76. /**
  77. * @description 确认
  78. * @param {Object} title: String desc: String
  79. */
  80. $confirm(options = {}){
  81. // 继承原来的
  82. // this.defautlOptions = Object.assign(this.defautlOptions, options);
  83. return new Promise((resolve,reject)=> {
  84. this.toggleMask('show')
  85. this._confirm = () => {
  86. resolve(1)
  87. this._confirm = null;
  88. }
  89. this._cancel = () => {
  90. resolve(0)
  91. this._cancel = null;
  92. }
  93. })
  94. },
  95. confirm() {
  96. // wx.showTabBar()
  97. // if(this._confirm){
  98. // this._confirm()
  99. // this.toggleMask();
  100. // }
  101. },
  102. cancel() {
  103. console.log('-----用户关闭获取手机号------')
  104. wx.showTabBar()
  105. this.closePhone()
  106. this.appInstance._cancel()
  107. },
  108. async getPhoneNumber(e){
  109. const appId = 'wxf939a0846ecf66a5'
  110. const { code, encryptedData, errMsg, iv } = e.detail
  111. // 可以获取手机号
  112. if( errMsg == 'getPhoneNumber:ok'){
  113. try{
  114. console.log('-----用户取手机号------')
  115. const fcode = await mpWx.login()
  116. const obj = await this.$api("app.getOpenId", {code: fcode})
  117. const { openid, sessionKey } = obj.data;
  118. const pc = new WXBizDataCrypt(appId, sessionKey)
  119. console.log(encryptedData)
  120. console.log(iv)
  121. const data = pc.decryptData(encryptedData , iv)
  122. console.log(data)
  123. const { phoneNumber, purePhoneNumber } = data;
  124. this.login({openid: openid, phone: phoneNumber}).then(res => {
  125. this.closePhone()
  126. this.appInstance._confirm()
  127. wx.showTabBar()
  128. })
  129. }catch(e){
  130. //TODO handle the exception
  131. console.log(e)
  132. }
  133. }else{
  134. console.log('-----用户拒绝获取手机号------')
  135. wx.showTabBar()
  136. this.closePhone()
  137. this.appInstance._cancel()
  138. }
  139. },
  140. loginAction() {
  141. this.$refs.uToast.show({
  142. type: 'loading',
  143. message: '正在登录,请稍后',
  144. })
  145. this.login({mobile: this.form.phone, code: this.form.code}).then(res => {
  146. this.$refs.uToast.hide()
  147. if(res.code == 1){
  148. // 这里此提示会被this.start()方法中的提示覆盖
  149. uni.$u.toast('登录成功')
  150. // 通知验证码组件内部开始倒计时
  151. this.$refs.uCode.start()
  152. }
  153. this.onComplate()
  154. })
  155. },
  156. getContract(type){
  157. this.$u.route({
  158. url: '/pages/signs/contract/contract',
  159. type: "navigateTo",
  160. params: {
  161. type: type
  162. }
  163. })
  164. },
  165. },
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. .mask{
  170. display: flex;
  171. flex-direction: column;
  172. align-items: flex-end;
  173. justify-content: flex-end;
  174. position: fixed;
  175. left: 0;
  176. top: var(--window-top);
  177. bottom: 0;
  178. width: 100vw;
  179. height: 100vh;
  180. background: rgba(0,0,0,0);
  181. z-index: 10001;
  182. transition: .3s;
  183. .mask-content{
  184. background-color: #fff;
  185. width: 100vw;
  186. align-self: stretch;
  187. min-height: 360rpx;
  188. padding-bottom: 100rpx;
  189. overflow: hidden;
  190. }
  191. .mask-content-inner{
  192. position: relative;
  193. display: flex;
  194. flex-direction: column;
  195. .head{
  196. padding: 0 15rpx;
  197. display: flex;
  198. align-items: center;
  199. height: 94rpx;
  200. border-bottom: 1px solid #efefef;
  201. .icon{
  202. display: flex;
  203. justify-content: center;
  204. align-content: center;
  205. width: 60rpx;
  206. height: 60rpx;
  207. margin-right: 30rpx;
  208. &.hover{
  209. background-color: #efefef;
  210. }
  211. }
  212. .head__text{
  213. font-size: 32rpx;
  214. color: #333;
  215. }
  216. }
  217. .body{
  218. padding-top: 40rpx;
  219. padding-left: 30rpx;
  220. padding-right: 30rpx;
  221. display: flex;
  222. flex-direction: column;
  223. .autho-info{
  224. font-size: 38rpx;
  225. color: #000;
  226. font-weight: 500;
  227. }
  228. .explain-info{
  229. padding-top: 15rpx;
  230. .text{
  231. color: #999;
  232. font-size: 24rpx;
  233. }
  234. .link{
  235. color: #FD8701;
  236. font-size: 24rpx;
  237. }
  238. }
  239. }
  240. .action-wrap{
  241. padding: 60rpx 30rpx 0;
  242. }
  243. }
  244. &.none{
  245. display: none;
  246. }
  247. &.show{
  248. background: rgba(0,0,0,.8);
  249. .mask-content{
  250. transform: translateY(0);
  251. }
  252. }
  253. }
  254. </style>