getPhone.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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="service_agreement">
  12. <checkbox-group @change="changeStatus">
  13. <label>
  14. <checkbox :checked="service_agreement_status" style="transform:scale(0.7)" />
  15. <text>我同意<text class="title">GOOMove</text>使用我授权的电话号码作为我的登录账号,查看</text>
  16. </label>
  17. </checkbox-group>
  18. <text class="link" @click="linkTo">《用户服务协议》</text>
  19. <text>及</text>
  20. <text class="link" @click="linkTo">《个人信息保护政策》</text>
  21. </view>
  22. <view class="card-actions">
  23. <button @click="cancel" size="mini" class="bt">拒绝</button>
  24. <button class="confirm bt" size="mini" @click="confirm" v-if="!service_agreement_status">允许</button>
  25. <button class="confirm bt" size="mini" open-type="getPhoneNumber" @getphonenumber="getphonenumber" v-else >允许</button>
  26. </view>
  27. <!-- #endif -->
  28. <!-- #ifndef MP-WEIXIN -->
  29. <uni-easyinput focus v-model="phoneNumber" type="number" placeholder="请输入手机号码" class="phone-inp"></uni-easyinput>
  30. <view class="title">使用该功能,需要获取你的手机号码</view>
  31. <view class="tip">请填写你的手机号码,并点击提交</view>
  32. <view class="card-actions">
  33. <button @click="cancel" size="mini" class="bt">拒绝</button>
  34. <button class="confirm bt" size="mini" @click="confirm">提交</button>
  35. </view>
  36. <!-- #endif -->
  37. </template>
  38. <template v-else>
  39. <uni-easyinput focus v-model="phoneNumber" type="number" placeholder="请输入手机号码" class="phone-inp"></uni-easyinput>
  40. <view class="title">使用该功能,需要获取你的手机号码</view>
  41. <view class="tip">请填写你的手机号码,并点击提交</view>
  42. <view class="card-actions">
  43. <button @click="cancel" size="mini" class="bt">拒绝</button>
  44. <button class="confirm bt" size="mini" @click="confirm">提交</button>
  45. </view>
  46. </template>
  47. </uni-card>
  48. </div>
  49. </div>
  50. </template>
  51. <script>
  52. import {setToken} from '@/libs/auth.js'
  53. import {login,getPhoneNumberApi} from '@/api/user.js'
  54. export default{
  55. props:{
  56. showByPlatform:{
  57. type:[String,Boolean],
  58. default:false
  59. }
  60. },
  61. data(){
  62. return{
  63. isAuthorize:false,
  64. phoneNumber:"",
  65. service_agreement_status:false
  66. }
  67. },
  68. methods:{
  69. cancel(){
  70. this.$emit('cancel')
  71. this.isAuthorize=true
  72. },
  73. confirm(){
  74. if(!this.service_agreement_status){
  75. uni.showToast({
  76. icon:'none',
  77. title:"请先阅读服务协议"
  78. })
  79. return
  80. }
  81. 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位手机号码正则
  82. if(!reg_tel.test(this.phoneNumber)){
  83. uni.showToast({
  84. icon:'none',
  85. title:"手机号码有误"
  86. })
  87. return
  88. }
  89. login({
  90. accountName:this.phoneNumber,
  91. password:"888888",
  92. accountTel:this.phoneNumber
  93. }).then((res)=>{
  94. let token=res.data.token;
  95. setToken(token)
  96. uni.setStorageSync('phone',this.phoneNumber)
  97. this.isAuthorize=true
  98. this.$emit('confirm',this.phoneNumber)
  99. }).catch((res)=>{})
  100. },
  101. getphonenumber(e){
  102. if(!this.service_agreement_status){
  103. uni.showToast({
  104. icon:'none',
  105. title:"请先阅读服务协议"
  106. })
  107. return
  108. }
  109. const {code,encryptedData,iv}=e.detail
  110. if(!code){
  111. this.$emit('cancel')
  112. }else{
  113. getPhoneNumberApi({code}).then((res)=>{
  114. const accountName=res.data.phone_info.purePhoneNumber
  115. const accountTel=res.data.phone_info.phoneNumber
  116. login({
  117. accountName,
  118. password:"888888",
  119. accountTel
  120. }).then((res)=>{
  121. let token=res.data.token;
  122. setToken(token)
  123. uni.setStorageSync('phone',accountName)
  124. this.isAuthorize=true
  125. this.$emit('confirm',accountName)
  126. }).catch((res)=>{})
  127. }).catch(()=>{
  128. uni.showToast({
  129. icon:'none',
  130. title:"授权失败,请稍后重试",
  131. duration:3000
  132. })
  133. })
  134. }
  135. },
  136. wxLogin(code,encryptedData,iv){
  137. const self=this;
  138. //this.isAuthorize=true
  139. uni.login({
  140. success(res) {
  141. const loginCode=res.code;
  142. console.log({
  143. loginCode,
  144. code,
  145. encryptedData,
  146. iv
  147. })
  148. },
  149. error(res){
  150. uni.showToast({
  151. icon:'none',
  152. title:"登录异常,请稍后重试"
  153. })
  154. }
  155. })
  156. },
  157. linkTo(){
  158. uni.navigateTo({
  159. url:'/pages/service_agreement/service_agreement'
  160. })
  161. },
  162. changeStatus(){
  163. this.service_agreement_status=!this.service_agreement_status
  164. console.log(this.service_agreement_status)
  165. }
  166. }
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. .phone-wrap{
  171. position: fixed;
  172. left: 0;
  173. right: 0;
  174. top: 0;
  175. bottom: 0;
  176. z-index: 9999;
  177. background-color: rgba(0,0,0,0.3);
  178. display: flex;
  179. justify-content: center;
  180. align-items: center;
  181. .authorization-container{
  182. width: 600rpx;
  183. margin-top: -100rpx;
  184. .phone-inp{
  185. margin-bottom: 20rpx;
  186. }
  187. .userAvatar{
  188. width: 100rpx;
  189. height: 100rpx;
  190. display: block;
  191. border-radius: 50%;
  192. overflow: hidden;
  193. margin: 20rpx auto;
  194. }
  195. .title{
  196. font-size: 32rpx;
  197. padding-top: 16rpx;
  198. }
  199. .tip{
  200. font-size: 28rpx;
  201. color: #999;
  202. position: relative;
  203. padding-left: 20rpx;
  204. margin-top: 30rpx;
  205. &::after{
  206. display: block;
  207. content: "";
  208. width: 10rpx;
  209. height: 10rpx;
  210. border-radius: 50%;
  211. background-color: #999;
  212. position: absolute;
  213. left: 0;
  214. top: 15rpx;
  215. }
  216. }
  217. .card-actions{
  218. display: flex;
  219. justify-content: space-between;
  220. align-items: center;
  221. padding-top: 50rpx;
  222. .bt{
  223. margin: 0;
  224. }
  225. .confirm{
  226. color: #fff;
  227. background-color: #409eff;
  228. border-color: #409eff;
  229. margin-left: 10rpx;
  230. }
  231. }
  232. }
  233. .service_agreement{
  234. checkbox-group{
  235. display: inline-block;
  236. }
  237. .title{
  238. font-weight: 600;
  239. padding: 0 2rpx;
  240. }
  241. .link{
  242. color: #409eff;
  243. }
  244. }
  245. }
  246. </style>