c-pop-phone.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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, openid: null})
  117. const { openid, sessionKey } = obj.data;
  118. const pc = new WXBizDataCrypt(appId, sessionKey)
  119. const data = pc.decryptData(encryptedData , iv)
  120. console.log('获取手机号', data)
  121. const { phoneNumber, purePhoneNumber } = data;
  122. this.login({openid: openid, phone: phoneNumber}).then(res => {
  123. this.closePhone()
  124. this.appInstance._confirm()
  125. wx.showTabBar()
  126. })
  127. }catch(e){
  128. const fcode = await mpWx.login()
  129. const obj = await this.$api("app.getOpenId", {code: fcode})
  130. const { openid, sessionKey } = obj.data;
  131. this.login({openid: openid, phone: ''}).then(res => {
  132. this.closePhone()
  133. this.appInstance._confirm()
  134. wx.showTabBar()
  135. })
  136. //TODO handle the exception
  137. console.log(e)
  138. }
  139. }else{
  140. console.log('-----用户拒绝获取手机号------')
  141. }
  142. },
  143. loginAction() {
  144. this.$refs.uToast.show({
  145. type: 'loading',
  146. message: '正在登录,请稍后',
  147. })
  148. this.login({mobile: this.form.phone, code: this.form.code}).then(res => {
  149. this.$refs.uToast.hide()
  150. if(res.code == 1){
  151. // 这里此提示会被this.start()方法中的提示覆盖
  152. uni.$u.toast('登录成功')
  153. // 通知验证码组件内部开始倒计时
  154. this.$refs.uCode.start()
  155. }
  156. this.onComplate()
  157. })
  158. },
  159. getContract(type){
  160. this.$u.route({
  161. url: '/pages/signs/contract/contract',
  162. type: "navigateTo",
  163. params: {
  164. type: type
  165. }
  166. })
  167. },
  168. },
  169. }
  170. </script>
  171. <style lang="scss" scoped>
  172. .mask{
  173. display: flex;
  174. flex-direction: column;
  175. align-items: flex-end;
  176. justify-content: flex-end;
  177. position: fixed;
  178. left: 0;
  179. top: var(--window-top);
  180. bottom: 0;
  181. width: 100vw;
  182. height: 100vh;
  183. background: rgba(0,0,0,0);
  184. z-index: 10001;
  185. transition: .3s;
  186. .mask-content{
  187. background-color: #fff;
  188. width: 100vw;
  189. align-self: stretch;
  190. min-height: 360rpx;
  191. padding-bottom: 100rpx;
  192. overflow: hidden;
  193. }
  194. .mask-content-inner{
  195. position: relative;
  196. display: flex;
  197. flex-direction: column;
  198. .head{
  199. padding: 0 15rpx;
  200. display: flex;
  201. align-items: center;
  202. height: 94rpx;
  203. border-bottom: 1px solid #efefef;
  204. .icon{
  205. display: flex;
  206. justify-content: center;
  207. align-content: center;
  208. width: 60rpx;
  209. height: 60rpx;
  210. margin-right: 30rpx;
  211. &.hover{
  212. background-color: #efefef;
  213. }
  214. }
  215. .head__text{
  216. font-size: 32rpx;
  217. color: #333;
  218. }
  219. }
  220. .body{
  221. padding-top: 40rpx;
  222. padding-left: 30rpx;
  223. padding-right: 30rpx;
  224. display: flex;
  225. flex-direction: column;
  226. .autho-info{
  227. font-size: 38rpx;
  228. color: #000;
  229. font-weight: 500;
  230. }
  231. .explain-info{
  232. padding-top: 15rpx;
  233. .text{
  234. color: #999;
  235. font-size: 24rpx;
  236. }
  237. .link{
  238. color: #FD8701;
  239. font-size: 24rpx;
  240. }
  241. }
  242. }
  243. .action-wrap{
  244. padding: 60rpx 30rpx 0;
  245. }
  246. }
  247. &.none{
  248. display: none;
  249. }
  250. &.show{
  251. background: rgba(0,0,0,.8);
  252. .mask-content{
  253. transform: translateY(0);
  254. }
  255. }
  256. }
  257. </style>