wxLogin.vue 8.0 KB

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