wxLogin.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <view class="content">
  3. <!-- #ifdef MP -->
  4. <view class="flex-center">
  5. <view class="logo-img-box">
  6. <image class="logo-img" src="../../static/image/logo.png" mode="scaleToFill"></image>
  7. <!-- -->
  8. <button class="userInfo flex-center" type="warn" @tap="loding?'':userInfoData()">
  9. <image class="icon margin-r-10" src="../../static/icon/weichat.png" mode="widthFix"></image>
  10. <text>微信授权登录</text>
  11. </button>
  12. <agreement @checkedChange='changeChecked'></agreement>
  13. </view>
  14. </view>
  15. <view class="Mask" v-show="MaskShow">
  16. <view class="Mask-box">
  17. <view class="title">申请获取您的手机号</view>
  18. <view class="text">为了方便您的取货和我们的送货,并获取更多优惠活动,需要您的手机授权</view>
  19. <view class="btn_box">
  20. <button class="weixin" @click="outUser">取消</button>
  21. <button class="weixin" open-type="getPhoneNumber" @getphonenumber="PhoneNumber">授权</button>
  22. </view>
  23. </view>
  24. </view>
  25. <!-- #endif -->
  26. </view>
  27. </template>
  28. <script>
  29. // #ifdef H5
  30. import {
  31. loginWinxin
  32. } from '@/utils/wxAuthorized';
  33. // #endif
  34. // #ifdef MP-WEIXIN
  35. import {
  36. wechatMpAuth
  37. } from '@/api/wx';
  38. import {
  39. loginWinxinMp
  40. } from '@/utils/wxMinProgram';
  41. // #endif
  42. import {
  43. mapMutations
  44. } from 'vuex';
  45. import {
  46. getUserInfo,
  47. } from '@/api/login.js';
  48. import {
  49. binding
  50. } from '@/api/set.js';
  51. import agreement from "./agreement.vue"
  52. export default {
  53. components: {
  54. agreement
  55. },
  56. data() {
  57. return {
  58. checked: false,
  59. userInfo: {}, //授权用户信息
  60. code: '', //授权code
  61. loding: false, //判断是否在点击中
  62. MaskShow: false, // 手机号授权弹窗
  63. cache_key: '',
  64. };
  65. },
  66. onLoad(option) {
  67. console.log(option, '船只');
  68. this.loadData();
  69. },
  70. methods: {
  71. ...mapMutations('user', ['login', 'setUserInfo', 'logout']),
  72. getUserInfos(e) {
  73. console.log(e,'到这里');
  74. if (e && e.detail) {
  75. console.log('获取用户信息成功');
  76. console.log(e.detail.userInfo);
  77. this.userInfo = e.detail.userInfo;
  78. }
  79. },
  80. changeChecked(check) {
  81. this.checked = check;
  82. },
  83. loadData() {
  84. let obj = this;
  85. // #ifdef H5
  86. loginWinxin();
  87. // #endif
  88. // #ifdef MP-WEIXIN
  89. loginWinxinMp().then((e) => {
  90. console.log(e, 'loginWinxinMp');
  91. obj.code = e.code;
  92. });
  93. // #endif
  94. },
  95. // #ifndef H5
  96. // 用户确认授权
  97. userInfoData(e) {
  98. const that = this;
  99. if (!that.checked) {
  100. uni.showModal({
  101. title: '提示',
  102. content: '请先阅读同意《母婴界严选服务》《隐私条例》',
  103. showCancel: false,
  104. });
  105. return;
  106. }
  107. // #ifdef MP-WEIXIN
  108. if (!this.loding) {
  109. wx.getUserProfile({
  110. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  111. success: res => {
  112. console.log(res, 'res');
  113. that.userInfo = res;
  114. that.loadMp();
  115. },
  116. fail: err => {
  117. console.log(err, 'err');
  118. that.loding = false;
  119. that.getLoginCode();
  120. uni.showToast({
  121. title: '您拒绝了请求,不能正常使用小程序',
  122. icon: 'error',
  123. duration: 2000
  124. });
  125. }
  126. });
  127. }
  128. // #endif
  129. },
  130. // #endif
  131. // #ifdef MP-WEIXIN
  132. // 退出用户登录
  133. outUser() {
  134. this.logout();
  135. uni.switchTab({
  136. url: '/pages/index/index'
  137. })
  138. },
  139. getLoginCode() {
  140. const that = this;
  141. wx.login({
  142. success(e) {
  143. console.log(e, 'loginWinxinMp');
  144. that.code = e.code;
  145. },
  146. fill: function(e) {
  147. console.log(e)
  148. }
  149. })
  150. },
  151. // 小程序授权登录
  152. loadMp(option) {
  153. let obj = this;
  154. obj.loding = true;
  155. // 获取登录授权页数据
  156. let user = obj.userInfo;
  157. // 获取推广人id
  158. // #ifndef MP
  159. let spread_spid = uni.getStorageSync('spread') || '';
  160. // #endif
  161. // #ifdef MP
  162. let spread_code = uni.getStorageSync('spread_code') || '';
  163. // #endif
  164. uni.showLoading({
  165. title: '授权中',
  166. mask: true
  167. });
  168. wechatMpAuth({
  169. code: obj.code,
  170. avatar: user.userInfo.avatarUrl,
  171. nickname: user.userInfo.nickName,
  172. gender: user.userInfo.gender,
  173. // #ifndef MP
  174. parent_uid: spread_spid,
  175. // #endif
  176. // #ifdef MP
  177. parent_uid: spread_code
  178. // #endif
  179. })
  180. .then(({
  181. data
  182. }) => {
  183. obj.loding = false;
  184. obj.wchatAuth(data);
  185. console.log(data, '授权数据');
  186. })
  187. .catch(e => {
  188. obj.loding = false;
  189. uni.hideLoading();
  190. });
  191. },
  192. wchatAuth(data) {
  193. let obj = this;
  194. obj.cache_key = data.cache_key;
  195. // 保存token
  196. uni.setStorageSync('token', data.token);
  197. obj.GetUser().then((data) => {
  198. console.log(data, '返回用户的数据');
  199. try {
  200. uni.hideLoading();
  201. obj.login();
  202. // 保存返回用户数据
  203. obj.setUserInfo(data);
  204. uni.setStorageSync(
  205. 'jynickname', 1
  206. )
  207. if (data.mobile == null || !data.mobile) {
  208. obj.MaskShow = true;
  209. } else {
  210. obj.ToIndex();
  211. }
  212. } catch (e) {
  213. console.log(e, '登录');
  214. }
  215. }).catch((e) => {
  216. uni.hideLoading();
  217. })
  218. },
  219. // #endif
  220. ToIndex() {
  221. let ur = uni.getStorageSync('present') || '/pages/index/index';
  222. // 用于处理缓存bug
  223. if (ur == 'pages/product/product') {
  224. ur = '/pages/index/index';
  225. }
  226. uni.switchTab({
  227. url: ur,
  228. fail(e) {
  229. uni.navigateTo({
  230. url: ur,
  231. fail(e) {
  232. uni.navigateTo({
  233. url: '/pages/index/index'
  234. });
  235. }
  236. });
  237. }
  238. });
  239. // uni.switchTab({
  240. // url: '/pages/index/index'
  241. // });
  242. },
  243. // 获取用户数据
  244. GetUser() {
  245. // 获取用户基础信息
  246. return new Promise((ok, error) => {
  247. getUserInfo({})
  248. .then(({
  249. data
  250. }) => {
  251. ok(data)
  252. })
  253. .catch(e => {
  254. error(e)
  255. });
  256. })
  257. },
  258. // #ifdef MP
  259. // 绑定手机号
  260. PhoneNumber(e) {
  261. let obj = this;
  262. obj.MaskShow = false;
  263. let requestData = {
  264. code: e.detail.code,
  265. }
  266. obj.bindPhone(requestData).then((ar) => {
  267. obj.GetUser().then((data) => {
  268. obj.setUserInfo(data);
  269. uni.showToast({
  270. title: '绑定成功',
  271. mask: true,
  272. });
  273. setTimeout(
  274. function() {
  275. obj.ToIndex();
  276. }, 500
  277. )
  278. });
  279. })
  280. },
  281. // 绑定手机号
  282. bindPhone(data) {
  283. // 获取用户基础信息
  284. return new Promise((ok, error) => {
  285. binding(data)
  286. .then(function(e) {
  287. ok(e)
  288. })
  289. .catch(e => {
  290. error(e)
  291. });
  292. })
  293. }
  294. // #endif
  295. }
  296. };
  297. </script>
  298. <style lang="scss">
  299. page,
  300. .content {
  301. height: 100%;
  302. /* #ifdef MP-WEIXIN */
  303. .flex-center {
  304. height: 100%;
  305. text-align: center;
  306. line-height: 1;
  307. }
  308. /* #endif */
  309. }
  310. /* #ifdef MP-WEIXIN */
  311. .Mask {
  312. position: fixed;
  313. top: 0;
  314. width: 100%;
  315. height: 100%;
  316. background-color: rgba(51, 51, 51, 0.7);
  317. .Mask-box {
  318. margin: auto;
  319. margin-top: 320rpx;
  320. padding: 50rpx 30rpx;
  321. width: 90%;
  322. height: 450rpx;
  323. top: 500rpx;
  324. left: 10%;
  325. background-color: #ffffff;
  326. border-radius: 15rpx;
  327. .title {
  328. text-align: center;
  329. font-size: 35rpx;
  330. font-weight: 700;
  331. }
  332. .text {
  333. font-size: 30rpx;
  334. color: #848484;
  335. padding-top: 50rpx;
  336. }
  337. .btn_box {
  338. margin: 70rpx 0 0 0;
  339. display: flex;
  340. align-items: center;
  341. justify-content: space-between;
  342. }
  343. }
  344. }
  345. .weixin {
  346. background: $base-color;
  347. width: 40%;
  348. color: #ffffff;
  349. border-radius: 50rpx;
  350. border: none;
  351. margin: 0 20rpx;
  352. }
  353. .logo-img {
  354. width: 385rpx;
  355. height: 385rpx;
  356. margin: 0 auto;
  357. border-radius: 50rpx;
  358. }
  359. .userInfo {
  360. margin: 0 100rpx;
  361. margin-top: 50rpx;
  362. padding: 30rpx 50rpx;
  363. color: #ffffff;
  364. border-radius: 99rpx;
  365. background-color: $base-color !important;
  366. .icon {
  367. width: 55rpx;
  368. }
  369. }
  370. /* #endif */
  371. </style>