getPhone.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <div class="phone-wrap" v-if="!isAuthorize">
  3. <div class="authorization-container">
  4. <uni-card>
  5. <template v-if="showByPlatform">
  6. <!-- #ifdef MP-WEIXIN -->
  7. <!-- <open-data type="chooseAvatar" class="userAvatar"></open-data> -->
  8. <image class="userAvatar" src="/static/components/avatar.png"></image>
  9. <view class="title">使用该功能,需要申请获取以下权限</view>
  10. <view class="tip">申请获取你微信绑定的手机号</view>
  11. <view class="card-actions">
  12. <button @click="cancel" size="mini" class="bt">拒绝</button>
  13. <button class="confirm bt" size="mini" open-type="getPhoneNumber" @getphonenumber="getphonenumber">允许</button>
  14. </view>
  15. <!-- #endif -->
  16. <!-- #ifndef MP-WEIXIN -->
  17. <uni-easyinput focus v-model="phoneNumber" type="number" placeholder="请输入手机号码" class="phone-inp"></uni-easyinput>
  18. <view class="title">使用该功能,需要获取你的手机号码</view>
  19. <view class="tip">请填写你的手机号码,并点击提交</view>
  20. <view class="card-actions">
  21. <button @click="cancel" size="mini" class="bt">拒绝</button>
  22. <button class="confirm bt" size="mini" @click="confirm">提交</button>
  23. </view>
  24. <!-- #endif -->
  25. </template>
  26. <template v-else>
  27. <uni-easyinput focus v-model="phoneNumber" type="number" placeholder="请输入手机号码" class="phone-inp"></uni-easyinput>
  28. <view class="title">使用该功能,需要获取你的手机号码</view>
  29. <view class="tip">请填写你的手机号码,并点击提交</view>
  30. <view class="card-actions">
  31. <button @click="cancel" size="mini" class="bt">拒绝</button>
  32. <button class="confirm bt" size="mini" @click="confirm">提交</button>
  33. </view>
  34. </template>
  35. </uni-card>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. import {setToken} from '@/libs/auth.js'
  41. import {login,getPhoneNumberApi} from '@/api/user.js'
  42. export default{
  43. props:{
  44. showByPlatform:{
  45. type:[String,Boolean],
  46. default:false
  47. }
  48. },
  49. data(){
  50. return{
  51. isAuthorize:false,
  52. phoneNumber:""
  53. }
  54. },
  55. methods:{
  56. cancel(){
  57. this.$emit('cancel')
  58. uni.showToast({
  59. icon:'none',
  60. title:"拒绝将无法使用"
  61. })
  62. },
  63. confirm(){
  64. var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
  65. if(!reg_tel.test(this.phoneNumber)){
  66. uni.showToast({
  67. icon:'none',
  68. title:"手机号码有误"
  69. })
  70. return
  71. }
  72. login({
  73. accountName:this.phoneNumber,
  74. password:"888888",
  75. accountTel:this.phoneNumber
  76. }).then((res)=>{
  77. let token=res.data.token;
  78. setToken(token)
  79. uni.setStorageSync('phone',this.phoneNumber)
  80. this.isAuthorize=true
  81. this.$emit('confirm',this.phoneNumber)
  82. }).catch((res)=>{})
  83. },
  84. getphonenumber(e){
  85. const {code,encryptedData,iv}=e.detail
  86. if(!code){
  87. this.$emit('cancel')
  88. uni.showToast({
  89. icon:'none',
  90. title:"拒绝将无法使用",
  91. duration:3000
  92. })
  93. }else{
  94. getPhoneNumberApi({code}).then((res)=>{
  95. const accountName=res.data.phone_info.purePhoneNumber
  96. const accountTel=res.data.phone_info.phoneNumber
  97. login({
  98. accountName,
  99. password:"888888",
  100. accountTel
  101. }).then((res)=>{
  102. let token=res.data.token;
  103. setToken(token)
  104. uni.setStorageSync('phone',accountName)
  105. this.isAuthorize=true
  106. this.$emit('confirm',accountName)
  107. }).catch((res)=>{})
  108. }).catch(()=>{
  109. uni.showToast({
  110. icon:'none',
  111. title:"授权失败,请稍后重试",
  112. duration:3000
  113. })
  114. })
  115. }
  116. },
  117. wxLogin(code,encryptedData,iv){
  118. const self=this;
  119. //this.isAuthorize=true
  120. uni.login({
  121. success(res) {
  122. const loginCode=res.code;
  123. console.log({
  124. loginCode,
  125. code,
  126. encryptedData,
  127. iv
  128. })
  129. },
  130. error(res){
  131. uni.showToast({
  132. icon:'none',
  133. title:"登录异常,请稍后重试"
  134. })
  135. }
  136. })
  137. }
  138. }
  139. }
  140. </script>
  141. <style lang="scss" scoped>
  142. .phone-wrap{
  143. position: fixed;
  144. left: 0;
  145. right: 0;
  146. top: 0;
  147. bottom: 0;
  148. z-index: 9999;
  149. background-color: rgba(0,0,0,0.3);
  150. display: flex;
  151. justify-content: center;
  152. align-items: center;
  153. .authorization-container{
  154. width: 600rpx;
  155. margin-top: -100rpx;
  156. .phone-inp{
  157. margin-bottom: 20rpx;
  158. }
  159. .userAvatar{
  160. width: 100rpx;
  161. height: 100rpx;
  162. display: block;
  163. border-radius: 50%;
  164. overflow: hidden;
  165. margin: 20rpx auto;
  166. }
  167. .title{
  168. font-size: 32rpx;
  169. padding-top: 16rpx;
  170. }
  171. .tip{
  172. font-size: 28rpx;
  173. color: #999;
  174. position: relative;
  175. padding-left: 20rpx;
  176. margin-top: 30rpx;
  177. &::after{
  178. display: block;
  179. content: "";
  180. width: 10rpx;
  181. height: 10rpx;
  182. border-radius: 50%;
  183. background-color: #999;
  184. position: absolute;
  185. left: 0;
  186. top: 15rpx;
  187. }
  188. }
  189. .card-actions{
  190. display: flex;
  191. justify-content: space-between;
  192. align-items: center;
  193. padding-top: 50rpx;
  194. .bt{
  195. margin: 0;
  196. }
  197. .confirm{
  198. color: #fff;
  199. background-color: #409eff;
  200. border-color: #409eff;
  201. margin-left: 10rpx;
  202. }
  203. }
  204. }
  205. }
  206. </style>