wxLogin.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <view class="content">
  3. <view class="back-img"><image src="https://sos.liuniu946.com/static/img/img002.png" model="scoll-img"></image></view>
  4. <!-- <view class="name">美天选菜</view>
  5. <view class="tip">台州买菜用美天</view> -->
  6. <button class="weixin" open-type="getUserInfo" @getuserinfo="userInfoData">微信用户一键登录</button>
  7. <!-- <view class="login" @click="Tologin">手机号码登录</view> -->
  8. <view class="info" @click="checkboxChange">
  9. <radio style="transform:scale(0.7)" color="#ff5252" :checked="istype" />
  10. 我已阅读并同意
  11. <view class="text">
  12. <!-- <view class="text" @click="agreement">用户协议,</view> -->
  13. <view class="text" @click.stop="privacy">隐私协议</view>
  14. </view>
  15. </view>
  16. <view class="Mask" v-show="MaskShow">
  17. <view class="Mask-box">
  18. <view class="title">申请获取您的手机号</view>
  19. <view class="text">为了方便更好的服务,需要您的手机授权</view>
  20. <button class="weixin" open-type="getPhoneNumber" @getphonenumber="PhoneNumber">获取手机号授权</button>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import { mapMutations, mapState } from 'vuex';
  27. // #ifdef MP-WEIXIN
  28. import { loginWinxinMp } from '@/utils/wxMinProgram';
  29. import { wechatMpAuth } from '@/api/wx';
  30. // #endif
  31. // #ifdef H5
  32. import { loginWinxin } from '@/utils/wxAuthorized';
  33. // #endif
  34. import { getUserInfo, bangding } from '@/api/login.js';
  35. export default {
  36. data() {
  37. return {
  38. userInfo: {}, //授权用户信息
  39. code: '', //授权code
  40. selected: false,
  41. disabled: false,
  42. MaskShow: false,
  43. cache_key: '',
  44. iv: '',
  45. encryptedData: '',
  46. istype:false
  47. };
  48. },
  49. onShow(option) {
  50. this.loadData();
  51. },
  52. methods: {
  53. ...mapMutations(['login', 'setUserInfo']),
  54. checkboxChange() {
  55. this.istype = !this.istype;
  56. console.log(this.istype);
  57. },
  58. // 获取微信登录信息
  59. loadData() {
  60. console.log(1)
  61. // #ifdef H5
  62. console.log(2)
  63. loginWinxin();
  64. // #endif
  65. // #ifdef MP-WEIXIN
  66. console.log(3)
  67. let obj = this;
  68. wx.login({
  69. success(e) {
  70. obj.code = e.code;
  71. },
  72. fill: function(e) {
  73. console.log(e);
  74. }
  75. });
  76. // #endif
  77. },
  78. // 用户确认授权
  79. userInfoData(e) {
  80. if (!this.istype) {
  81. this.$api.msg('请查看并同意隐私政策');
  82. return;
  83. }
  84. let user = e.detail;
  85. user.code = this.code;
  86. this.loadMp(user);
  87. this.disabled = true;
  88. },
  89. // #ifdef MP-WEIXIN
  90. // 登录
  91. loadMp(option) {
  92. let obj = this;
  93. // 获取登录授权页数据
  94. wechatMpAuth({
  95. code: option.code,
  96. iv: option.iv,
  97. encryptedData: option.encryptedData
  98. }).then(({ data }) => {
  99. obj.wchatAuth(data);
  100. });
  101. },
  102. // #endif
  103. wchatAuth(data) {
  104. let obj = this;
  105. // 保存token
  106. uni.setStorageSync('token', data.userinfo.token);
  107. obj.cache_key = data.cache_key;
  108. obj.login();
  109. // 获取用户基础信息
  110. obj.setUserInfo(data.userinfo);
  111. if (data.userinfo.mobile == null || data.userinfo.mobile == '') {
  112. obj.MaskShow = true;
  113. } else {
  114. obj.ToIndex();
  115. }
  116. },
  117. PhoneNumber(e) {
  118. let obj = this;
  119. obj.MaskShow = false;
  120. (obj.iv = e.detail.iv), (obj.encryptedData = e.detail.encryptedData);
  121. bangding({
  122. flag: 1,
  123. cache_key: obj.cache_key,
  124. code: obj.code,
  125. iv: obj.iv,
  126. encryptedData: obj.encryptedData
  127. })
  128. .then(function(e) {
  129. obj.$api.msg(e.msg);
  130. // 获取用户基础信息
  131. obj.GetUser();
  132. obj.$api.msg(e.msg);
  133. obj.$nextTick(function() {
  134. obj.ToIndex();
  135. });
  136. })
  137. .catch(e => {
  138. console.log(e.message);
  139. });
  140. },
  141. GetUser() {
  142. // 获取用户基础信息
  143. getUserInfo({})
  144. .then(({ data }) => {
  145. this.setUserInfo(data);
  146. console.log(uni.getStorageSync('userInfo'), 'userInfo');
  147. })
  148. .catch(e => {
  149. console.log(e);
  150. });
  151. },
  152. ToIndex() {
  153. uni.switchTab({
  154. url: '/pages/index/index'
  155. })
  156. let obj = this;
  157. let ur = uni.getStorageSync('present') || '/pages/index/index';
  158. // 用于处理缓存bug
  159. if (ur == 'pages/index/index') {
  160. ur = '/pages/index/index';
  161. }
  162. uni.switchTab({
  163. url: ur,
  164. fail(e) {
  165. uni.navigateTo({
  166. url: ur,
  167. fail(e) {
  168. uni.navigateTo({
  169. url: '/pages/index/index'
  170. });
  171. }
  172. });
  173. }
  174. });
  175. },
  176. //手机号登录
  177. Tologin() {
  178. uni.navigateTo({
  179. url: '/pages/public/login'
  180. });
  181. },
  182. // 菜佬用户协议
  183. agreement() {
  184. uni.navigateTo({
  185. url: '/pages/public/agreement'
  186. });
  187. },
  188. //隐私协议
  189. privacy() {
  190. uni.navigateTo({
  191. url: '/pages/public/privacy'
  192. });
  193. }
  194. }
  195. };
  196. </script>
  197. <style lang="scss">
  198. page {
  199. background: #ffffff;
  200. height: 100%;
  201. .content {
  202. height: 100%;
  203. text-align: center;
  204. .Mask {
  205. position: fixed;
  206. top: 0;
  207. width: 100%;
  208. height: 100%;
  209. background-color: rgba(51, 51, 51, 0.7);
  210. .Mask-box {
  211. margin: auto;
  212. margin-top: 320rpx;
  213. padding: 50rpx 50rpx;
  214. width: 80%;
  215. height: 450rpx;
  216. top: 500rpx;
  217. left: 10%;
  218. background-color: #ffffff;
  219. border-radius: 15rpx;
  220. .title {
  221. font-size: 35rpx;
  222. font-weight: 700;
  223. }
  224. .text {
  225. font-size: 30rpx;
  226. color: #848484;
  227. padding-top: 50rpx;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. .back-img {
  234. width: 100%;
  235. padding-top: 150rpx;
  236. image {
  237. width: 521rpx;
  238. height: 537rpx;
  239. }
  240. }
  241. .back-name {
  242. margin: 0rpx auto;
  243. margin-top: 50rpx;
  244. width: 239rpx;
  245. height: 98rpx;
  246. image {
  247. width: 100%;
  248. height: 100%;
  249. }
  250. }
  251. .name {
  252. color: #48ac34;
  253. font-size: 55rpx;
  254. font-weight: bold;
  255. letter-spacing: 15rpx;
  256. margin-top: 35rpx;
  257. }
  258. .tip {
  259. color: #48ac34;
  260. font-weight: bold;
  261. font-size: 28rpx;
  262. letter-spacing: 30rpx;
  263. }
  264. .weixin {
  265. background: linear-gradient(90deg, rgba(255, 102, 102, 1), rgba(255, 79, 79, 1));
  266. width: 65%;
  267. margin: 100rpx auto;
  268. color: #ffffff;
  269. border-radius: 50rpx;
  270. border: none;
  271. }
  272. .login {
  273. font-size: 26rpx;
  274. font-weight: 500;
  275. text-decoration: underline;
  276. }
  277. .info {
  278. // position: fixed;
  279. // bottom: 60rpx;
  280. margin: 0rpx auto;
  281. width: 100%;
  282. font-size: 26rpx;
  283. .text {
  284. color: #ff6666;
  285. display: inline-block;
  286. }
  287. }
  288. </style>