wxLogin.vue 7.3 KB

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