index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <view class="page">
  3. <view class="system-height" :style="{height:statusBarHeight}"></view>
  4. <!-- #ifdef MP -->
  5. <view class="title-bar" style="height: 43px;">
  6. <view class="icon" @click="back" v-if="!isHome">
  7. <image src="../static/left.png"></image>
  8. </view>
  9. <view class="icon" @click="home" v-else>
  10. <image src="../static/home.png"></image>
  11. </view>
  12. 账户登录
  13. </view>
  14. <!-- #endif -->
  15. <view class="wechat_login">
  16. <view class="img">
  17. <image src="../static/wechat_login.png" mode="widthFix"></image>
  18. </view>
  19. <view class="btn-wrapper">
  20. <!-- #ifdef H5 -->
  21. <button hover-class="none" @click="wechatLogin" class="bg-green btn1">微信登录</button>
  22. <!-- #endif -->
  23. <!-- #ifdef MP -->
  24. <button hover-class="none" @tap="getUserProfile" class="bg-green btn1">微信登录</button>
  25. <!-- #endif -->
  26. <!-- #ifdef MP-WEIXIN -->
  27. <button open-type="getPhoneNumber" type="primary" @getphonenumber="getPhoneNumber">一键登录</button>
  28. <!-- #endif -->
  29. <!-- <button hover-class="none" @click="isUp = true" class="btn2">手机号登录</button> -->
  30. </view>
  31. </view>
  32. <block v-if="isUp">
  33. <mobileLogin :isUp="isUp" @close="maskClose" @wechatPhone="wechatPhone" :social-code="socialCode"
  34. :social-state="socialState" />
  35. </block>
  36. <block v-if="isPhoneBox">
  37. <routinePhone :logoUrl="logoUrl" :isPhoneBox="isPhoneBox" @close="bindPhoneClose" :authKey="authKey">
  38. </routinePhone>
  39. </block>
  40. </view>
  41. </template>
  42. <script>
  43. const app = getApp();
  44. let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  45. import * as UserApi from "@/api/member/user";
  46. import * as AuthApi from "@/api/member/auth";
  47. import mobileLogin from '@/components/login_mobile/index.vue'
  48. import routinePhone from '@/components/login_mobile/routine_phone.vue'
  49. import * as BrokerageAPI from '@/api/trade/brokerage.js'
  50. import {
  51. getUserPhone
  52. } from '@/api/public';
  53. import Routine from '@/libs/routine';
  54. import wechat from "@/libs/wechat";
  55. export default {
  56. data() {
  57. return {
  58. isUp: false,
  59. phone: '',
  60. statusBarHeight: statusBarHeight,
  61. isHome: false,
  62. isPhoneBox: false,
  63. // logoUrl: '',
  64. code: '',
  65. authKey: '',
  66. options: '',
  67. userInfo: {},
  68. codeNum: 0,
  69. socialCode: '',
  70. socialState: '',
  71. }
  72. },
  73. components: {
  74. mobileLogin,
  75. routinePhone
  76. },
  77. onLoad(options) {
  78. // getLogo().then(res => {
  79. // this.logoUrl = res.data.logoUrl
  80. // })
  81. // #ifdef H5
  82. document.body.addEventListener("focusout", () => {
  83. setTimeout(() => {
  84. const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop ||
  85. 0;
  86. window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  87. }, 100);
  88. });
  89. const {
  90. code,
  91. state
  92. } = options;
  93. this.options = options
  94. // 获取确认授权code
  95. this.code = code || ''
  96. if (code && this.options.scope !== 'snsapi_base') {
  97. let spread = app.globalData.spid ? app.globalData.spid : 0;
  98. // 公众号授权登录回调
  99. wechat.auth(code, state, spread).then(res => {
  100. this.getUserInfo();
  101. this.wechatPhone();
  102. }).catch(error => {
  103. // 异常的情况,说明可能是账号没登录,所以发起手机绑定
  104. this.socialCode = code;
  105. this.socialState = state;
  106. this.isUp = true
  107. });
  108. }
  109. // #endif
  110. },
  111. methods: {
  112. back() {
  113. uni.navigateBack();
  114. },
  115. home() {
  116. uni.switchTab({
  117. url: '/pages/index/index'
  118. })
  119. },
  120. // 弹窗关闭
  121. maskClose() {
  122. this.isUp = false
  123. },
  124. bindPhoneClose(data) {
  125. if (data.isStatus) {
  126. this.isPhoneBox = false
  127. this.$util.Tips({
  128. title: '登录成功',
  129. icon: 'success'
  130. }, {
  131. tab: 3
  132. })
  133. } else {
  134. this.isPhoneBox = false
  135. }
  136. },
  137. getUserInfo() {
  138. UserApi.getUserInfo().then(res => {
  139. uni.hideLoading();
  140. this.userInfo = res.data
  141. this.$store.commit("UPDATE_USERINFO", res.data);
  142. this.$util.Tips({
  143. title: '登录成功',
  144. icon: 'success'
  145. }, {
  146. tab: 3
  147. })
  148. });
  149. },
  150. bindBrokerUser() {
  151. const spread = this.$Cache.get("spread");
  152. if (spread > 0) {
  153. BrokerageAPI.bindBrokerageUser(spread)
  154. }
  155. },
  156. // #ifdef MP
  157. getUserProfile() {
  158. let self = this;
  159. uni.showLoading({
  160. title: '正在登录中'
  161. });
  162. Routine.getUserProfile()
  163. .then(res => {
  164. Routine.getCode()
  165. .then(code => {
  166. self.getWxUser(code, res);
  167. })
  168. .catch(res => {
  169. uni.hideLoading();
  170. });
  171. })
  172. .catch(res => {
  173. uni.hideLoading();
  174. });
  175. },
  176. /**
  177. * 微信一键登录
  178. */
  179. async getPhoneNumber(e) {
  180. // 情况一:拒绝授权手机号码
  181. const phoneCode = e.detail.code
  182. if (!e.detail.code) {
  183. uni.showModal({
  184. title: '授权失败',
  185. content: '您已拒绝获取绑定手机号登录授权,可以使用其他手机号验证登录',
  186. confirmText: '知道了',
  187. confirmColor: '#3C9CFFFF'
  188. })
  189. return;
  190. }
  191. // 情况二:允许授权手机号码
  192. const loginCode = await Routine.getCode()
  193. AuthApi.weixinMiniAppLogin(phoneCode, loginCode, 'default').then(res => {
  194. const data = res.data;
  195. // TODO 芋艿:refreshToken 机制
  196. this.$store.commit("LOGIN", {
  197. 'token': data.accessToken
  198. });
  199. // 保存opendId用于支付
  200. this.$store.commit("OPENID", data.openid);
  201. // 设置当前userId
  202. this.$store.commit("SETUID", data.userId);
  203. this.getUserInfo();
  204. this.bindBrokerUser();
  205. }).catch(e => {
  206. this.$util.Tips({
  207. title: e
  208. });
  209. });
  210. },
  211. // #endif
  212. // #ifdef H5
  213. // 公众号登录
  214. wechatLogin() {
  215. debugger
  216. if (!this.code && this.options.scope !== 'snsapi_base') {
  217. this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
  218. } else {
  219. this.isUp = true;
  220. }
  221. },
  222. // 输入手机号后的回调
  223. wechatPhone() {
  224. debugger
  225. this.$Cache.clear('snsapiKey');
  226. if (this.options.back_url) {
  227. let url = uni.getStorageSync('snRouter');
  228. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  229. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  230. url = '/';
  231. }
  232. if (!url) {
  233. url = '/pages/index/index';
  234. }
  235. this.isUp = false
  236. uni.showToast({
  237. title: '登录成功',
  238. icon: 'none'
  239. })
  240. setTimeout(res => {
  241. location.href = url
  242. }, 800)
  243. } else {
  244. uni.navigateBack()
  245. }
  246. }
  247. // #endif
  248. }
  249. }
  250. </script>
  251. <style lang="scss">
  252. page {
  253. background: #fff;
  254. height: 100%;
  255. }
  256. .page {
  257. background: #fff;
  258. height: 100%;
  259. }
  260. .wechat_login {
  261. padding: 72rpx 34rpx;
  262. .img image {
  263. width: 100%;
  264. }
  265. .btn-wrapper {
  266. margin-top: 86rpx;
  267. padding: 0 66rpx;
  268. button {
  269. width: 100%;
  270. height: 86rpx;
  271. line-height: 86rpx;
  272. margin-bottom: 40rpx;
  273. border-radius: 120rpx;
  274. font-size: 30rpx;
  275. &.btn1 {
  276. color: #fff;
  277. }
  278. &.btn2 {
  279. color: #666666;
  280. border: 1px solid #666666;
  281. }
  282. }
  283. }
  284. }
  285. .title-bar {
  286. position: relative;
  287. display: flex;
  288. align-items: center;
  289. justify-content: center;
  290. font-size: 36rpx;
  291. }
  292. .icon {
  293. position: absolute;
  294. left: 30rpx;
  295. top: 0;
  296. display: flex;
  297. align-items: center;
  298. justify-content: center;
  299. width: 86rpx;
  300. height: 86rpx;
  301. image {
  302. width: 50rpx;
  303. height: 50rpx;
  304. }
  305. }
  306. </style>