componentLogin.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view>
  3. <u-popup v-model="show" mode="center" border-radius="24" :closeable="false" :mask-close-able="true" @close="close">
  4. <view class="u-mode-center-main">
  5. <view class="title">
  6. <view class="logo"><img class="img" src="@/assets/img/button/lock.svg" alt=""></view>
  7. <view class="span">智泊e家</view>
  8. </view>
  9. <view>
  10. <!-- <button class="login-btn" @click="decryptPhoneNumberTest"
  11. >
  12. <u-icon name="phone-fill" size="46"></u-icon> <text>手机号码一键登录test</text>
  13. </button> -->
  14. <button class="login-btn" @click="decryptPhoneNumberH5" open-type="getPhoneNumber"
  15. @getphonenumber="decryptPhoneNumber"
  16. >
  17. <u-icon name="phone-fill" size="46"></u-icon> <text>手机号码一键登录</text>
  18. </button>
  19. </view>
  20. </view>
  21. </u-popup>
  22. </view>
  23. </template>
  24. <script>
  25. import * as API_weixin from '@/apis/weixin.js'
  26. export default {
  27. name:"componentLogin",
  28. props:{
  29. },
  30. data() {
  31. return {
  32. show:false,
  33. userInfo:{},
  34. };
  35. },
  36. methods: {
  37. ashow(){
  38. this.show = true;
  39. },
  40. findByOpenId() {
  41. if(!this.jphelp.getOpenId()){
  42. setTimeout(()=>{
  43. this.findByOpenId()
  44. },500)
  45. return
  46. }
  47. API_weixin.findByOpenId({
  48. openId: this.jphelp.getOpenId(),
  49. noerror: true
  50. }).then((res) => {
  51. this.userInfo=res.data.regUser;
  52. this.jphelp.setPersonInfo(res.data.regUser);
  53. this.jphelp.setToken(res.data.token);
  54. this.jphelp.setPersonInfoPlus(res.data);
  55. this.$emit("findByOpenId",res)
  56. }).catch(error => {
  57. this.jphelp.logoff();
  58. this.userInfo = {};
  59. })
  60. },
  61. close() {
  62. // #ifdef MP-WEIXIN
  63. // #endif
  64. // if (this.userInfo.id) {
  65. // } else {
  66. // uni.switchTab({
  67. // url: "/pages/index/index"
  68. // })
  69. // }
  70. },
  71. createUser(phone) {
  72. this.show=false
  73. if (this.userInfo.id) {
  74. //console.log("changePhoneUser",phone)
  75. //this.changePhoneUser(phone)
  76. } else{
  77. console.log("createUser",phone)
  78. API_weixin.createUser({
  79. phone: phone,
  80. openId: this.jphelp.getOpenId()
  81. }).then((res2) => {
  82. this.findByOpenId()
  83. }).catch(error2 => {
  84. uni.showToast({
  85. icon: 'none',
  86. title: error2
  87. })
  88. })
  89. }
  90. },
  91. decryptPhoneNumberTest() {
  92. this.show = false
  93. var _this = this
  94. uni.showModal({
  95. title: '输入手机号测试',
  96. editable: true,
  97. //content: '这是一个模态弹窗',
  98. success: function(res) {
  99. if (res.confirm) {
  100. //_this.createUser(res.content)
  101. if (_this.userInfo.id) {
  102. _this.changePhoneUser(res.content)
  103. } else{
  104. _this.createUser(res.content)
  105. }
  106. } else if (res.cancel) {
  107. //.log('用户点击取消');
  108. }
  109. }
  110. });
  111. },
  112. decryptPhoneNumberH5() {
  113. // #ifdef H5
  114. this.show = false
  115. var _this = this
  116. uni.showModal({
  117. title: '输入手机号测试',
  118. editable: true,
  119. //content: '这是一个模态弹窗',
  120. success: function(res) {
  121. if (res.confirm) {
  122. //_this.createUser(res.content)
  123. if (_this.userInfo.id) {
  124. _this.changePhoneUser(res.content)
  125. } else{
  126. _this.createUser(res.content)
  127. }
  128. } else if (res.cancel) {
  129. //.log('用户点击取消');
  130. }
  131. }
  132. });
  133. // #endif
  134. },
  135. decryptPhoneNumber(e) {
  136. console.log(e, this.jphelp.getOpenId())
  137. var _this=this;
  138. if (!e.detail.code&&e.detail.errMsg == 'getPhoneNumber:ok') {
  139. uni.login({
  140. provider: 'weixin', //使用微信登录
  141. success: function(loginRes) {
  142. API_weixin.decryptData({
  143. code: loginRes.code,
  144. encryptedData: e.detail.encryptedData,
  145. iv: e.detail.iv
  146. }).then((res) => {
  147. console.log(res)
  148. if (res.data) {
  149. console.log(_this.userInfo.id)
  150. _this.createUser(res.data)
  151. } else {
  152. uni.showToast({
  153. icon: 'none',
  154. title: "获取手机号失败,请检查"
  155. })
  156. }
  157. }).catch(error => {
  158. uni.showToast({
  159. icon: 'none',
  160. title: error
  161. })
  162. //this.getPhone()
  163. })
  164. }
  165. });
  166. } else {
  167. if(e.detail.code){
  168. uni.showLoading({
  169. title: "加载中",
  170. mask: true,
  171. })
  172. API_weixin.findUserPhoneNumber(e.detail.code).then((res) => {
  173. if (res.data) {
  174. this.createUser(res.data)
  175. } else {
  176. uni.showToast({
  177. icon: 'none',
  178. title: "获取手机号失败,请检查"
  179. })
  180. }
  181. }).catch(error => {
  182. uni.showToast({
  183. icon: 'none',
  184. title: error
  185. })
  186. //this.getPhone()
  187. })
  188. }else{
  189. uni.showToast({
  190. icon: 'none',
  191. title: "获取手机号失败"
  192. })
  193. }
  194. }
  195. },
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. // 弹窗
  201. .u-mode-center-main {
  202. width: 560rpx !important;
  203. border-radius: 12px;
  204. padding: 80rpx 48rpx;
  205. .title {
  206. color: rgba(16, 16, 16, 1);
  207. font-size: 32rpx;
  208. display: flex;
  209. .logo{
  210. width: 50rpx;
  211. height: 50rpx;
  212. border-radius: 100rpx;
  213. background-color: rgba(22,119,255,1);
  214. display: flex;
  215. align-items: center;
  216. justify-content: center;
  217. .img{
  218. width: 35rpx;
  219. height: 30rpx;
  220. }
  221. }
  222. }
  223. .login-btn {
  224. background-color: rgba(0, 188, 99, 1);
  225. color: #fff;
  226. padding: 8rpx 70rpx;
  227. margin-top: 56rpx;
  228. margin-bottom: 16rpx;
  229. border-radius: 8px;
  230. display: flex;
  231. font-size: 32rpx;
  232. text {
  233. margin-left: 8rpx;
  234. }
  235. }
  236. }
  237. </style>