componentLogin.vue 6.6 KB

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