wxLogin.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="content">
  3. <!-- #ifndef H5 -->
  4. <!-- <image class="bg-img" :src="baseURL+urlFile+'/img/img09.png'" mode=" scaleToFill"></image> -->
  5. <view class="logo-img-box">
  6. <image class="logo-img" src="../../static/img/logo.png" mode=" aspectFit"></image>
  7. <!-- 获取头像登录 -->
  8. <!-- <button class="userInfo" type="warn" @click="isclick?'':userInfoData()" :class="{'nocaction': isclick}">
  9. <text class="iconfont iconweixin"></text>
  10. <text>
  11. 微信授权登录
  12. </text>
  13. </button> -->
  14. <!-- 获取手机号登录 -->
  15. <template v-if="!hasphone">
  16. <button class="userInfo" type="warn" :class="{ nocaction: isclick }" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  17. <text class="iconfont iconweixin"></text>
  18. <text>微信授权登录</text>
  19. </button>
  20. </template>
  21. <template v-if="hasphone">
  22. <button class="userInfo" type="warn" :class="{ nocaction: isclick }" @click="isclick ? '' : userInfoData()">
  23. <text class="iconfont iconweixin"></text>
  24. <text>微信授权登录</text>
  25. </button>
  26. </template>
  27. <!-- 550*450 -->
  28. <uni-popup ref="popupsq" type="center">
  29. <view class="popupsq-wrap">
  30. <view class="" style="text-align: center;font-size: 32rpx;font-weight: bold;padding: 40rpx 0;color: #000;">陪诊</view>
  31. <view class="" style="font-size: 26rpx;padding-bottom: 35rpx;color: #000;">该程序获取以下授权:</view>
  32. <view class="" style="font-size: 26rpx;color:#b7b7b7;">获取您的公开信息(昵称,头像),以便于个性话头像和昵称</view>
  33. <view class="flex" style="padding-top: 40rpx;">
  34. <view class="btn qx" style="" @click="close()">取消</view>
  35. <view class="btn qr" style="" @click="isclick ? '' : userInfoData()">确认授权</view>
  36. </view>
  37. </view>
  38. </uni-popup>
  39. </view>
  40. <!-- #endif -->
  41. </view>
  42. </template>
  43. <script>
  44. import { getUserInfo } from '@/api/login.js';
  45. // #ifdef H5
  46. import { loginWinxin } from '@/utils/wxAuthorized';
  47. // #endif
  48. // #ifdef MP-WEIXIN
  49. import { loginWinxinMp } from '@/utils/wxMinProgram';
  50. import { wechatMpAuth } from '@/api/wx';
  51. // #endif
  52. import { mapMutations, mapState } from 'vuex';
  53. export default {
  54. data() {
  55. return {
  56. userInfo: {}, //授权用户信息
  57. code: '', //授权code
  58. isclick: false, //是否点击了
  59. iv1: '',
  60. encryptedData1: '',
  61. hasphone: false
  62. };
  63. },
  64. onLoad() {
  65. this.loadData();
  66. },
  67. computed: {
  68. // ...mapState(['baseURL','urlFile'])
  69. },
  70. methods: {
  71. // ...mapMutations(['login', 'setUserInfo']),
  72. ...mapMutations('user', ['login', 'setUserInfo']),
  73. close() {
  74. this.$refs.popupsq.close();
  75. },
  76. getPhoneNumber(e) {
  77. let obj = this;
  78. console.log(e, 'e+++++++++++++++');
  79. // 用户同意授权
  80. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  81. console.log('chenggong++++++++++++++');
  82. obj.hasphone = true;
  83. obj.iv1 = e.detail.iv;
  84. obj.encryptedData1 = e.detail.encryptedData;
  85. obj.$refs.popupsq.open();
  86. } else {
  87. obj.isclick = false;
  88. uni.showToast({
  89. title: '您拒绝了请求,不能正常使用小程序',
  90. icon: 'error',
  91. duration: 2000
  92. });
  93. return;
  94. }
  95. },
  96. loadData() {
  97. let obj = this;
  98. // #ifdef H5
  99. loginWinxin();
  100. // #endif
  101. // #ifdef MP-WEIXIN
  102. loginWinxinMp().then(e => {
  103. console.log(e, 'ddddddddddddddddddddddd');
  104. // obj.code = e.code
  105. wx.login({
  106. success(e) {
  107. console.log(e, 'loginWinxinMp');
  108. obj.code = e.code;
  109. console.log(obj.code, 'wxlogin.code++++++');
  110. },
  111. fill: function(e) {
  112. console.log(e);
  113. }
  114. });
  115. });
  116. // #endif
  117. },
  118. // 用户确认授权
  119. userInfoData() {
  120. let that = this;
  121. if (that.isclick) {
  122. return;
  123. }
  124. that.isclick = true;
  125. wx.getUserProfile({
  126. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  127. success: res => {
  128. console.log(res, 'that.userInfo+++++++++++++++++++');
  129. that.userInfo = res;
  130. uni.showLoading({
  131. title: '授权中',
  132. mask: true
  133. });
  134. that.loadMp();
  135. },
  136. fail: err => {
  137. that.isclick = false;
  138. uni.showToast({
  139. title: '您拒绝了请求,不能正常使用小程序',
  140. icon: 'error',
  141. duration: 2000
  142. });
  143. return;
  144. }
  145. });
  146. // this.userInfo = e;
  147. // console.log(e,'用户确认授权')
  148. // this.loadMp()
  149. },
  150. // #ifdef MP-WEIXIN
  151. loadMp() {
  152. let obj = this;
  153. // 获取登录授权页数据
  154. let user = obj.userInfo;
  155. console.log(user);
  156. // 获取推广人id
  157. let spread_spid = uni.getStorageSync('spread') || '';
  158. // #ifdef MP
  159. let spread_code = uni.getStorageSync('spread_code') || '';
  160. // #endif
  161. wechatMpAuth({
  162. code: obj.code,
  163. iv: user.iv,
  164. iv1: obj.iv1,
  165. encryptedData1: obj.encryptedData1,
  166. encryptedData: user.encryptedData,
  167. spread_spid: spread_spid,
  168. // #ifdef MP
  169. spread_code: spread_code
  170. // #endif
  171. })
  172. .then(({ data }) => {
  173. obj.wchatAuth(data);
  174. console.log(data, 'wechatMpAuth++++++++++++++++++++++++++');
  175. })
  176. .catch(err => {
  177. // obj.loding = false;
  178. // uni.hideLoading();
  179. });
  180. },
  181. // #endif
  182. wchatAuth(data) {
  183. let obj = this;
  184. // 保存token
  185. uni.setStorageSync('token', data.token);
  186. console.log(data.token, 'token++++++++++++++');
  187. // 获取用户基础信息
  188. getUserInfo({}).then(e => {
  189. console.log('userInfo+++++++++++', e);
  190. obj.login();
  191. uni.hideLoading();
  192. // 保存返回用户数据
  193. obj.setUserInfo(e.data);
  194. let ur = uni.getStorageSync('present') || '/pages/index/index';
  195. // 用于处理缓存bug
  196. if (ur == 'pages/shop/product') {
  197. ur = '/pages/index/index';
  198. }
  199. uni.switchTab({
  200. url: ur,
  201. fail(e) {
  202. uni.navigateTo({
  203. url: ur,
  204. fail(e) {
  205. uni.navigateTo({
  206. url: '/pages/index/index'
  207. });
  208. }
  209. });
  210. }
  211. });
  212. });
  213. }
  214. }
  215. };
  216. </script>
  217. <style lang="scss">
  218. page,
  219. .content {
  220. height: 100%;
  221. background-color: #fff;
  222. }
  223. .bg-img,
  224. .logo-img-box {
  225. position: absolute;
  226. top: 0;
  227. left: 0;
  228. width: 100%;
  229. height: 100%;
  230. }
  231. .logo-img {
  232. margin-top: 20vh;
  233. margin-left: 176rpx;
  234. width: 385rpx;
  235. height: 394rpx;
  236. }
  237. .userInfo {
  238. margin: 0 100rpx;
  239. margin-top: 50rpx;
  240. color: #ffffff;
  241. border-radius: 99rpx;
  242. background-color: #24a17d !important;
  243. }
  244. .nocaction {
  245. background-color: #999;
  246. }
  247. .popupsq-wrap {
  248. width: 550rpx;
  249. height: 450rpx;
  250. background-color: #fff;
  251. border-radius: 20rpx;
  252. padding: 0 30rpx;
  253. .btn {
  254. width: 240rpx;
  255. height: 90rpx;
  256. text-align: center;
  257. line-height: 90rpx;
  258. font-size: 30rpx;
  259. border-radius: 45rpx;
  260. font-weight: bold;
  261. }
  262. .qx {
  263. color: #000;
  264. border: #f0f0f0 1px solid;
  265. }
  266. .qr {
  267. color: #fff;
  268. background-color: #24a17d;
  269. }
  270. }
  271. </style>