index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view :style="colorStyle">
  3. <view class="authorize">
  4. <view class="pictrue">
  5. <image :src="logoUrl"></image>
  6. <view class="iconfont icon-guanbi4" @click='close'></view>
  7. </view>
  8. <view class="title">账号登录</view>
  9. <view class="info">登录注册即同意商城<text class="agree" @click="privacy('user')">《用户协议》</text>与<text class="agree" @click="privacy('privacy')">《隐私协议》</text></view>
  10. <button hover-class="none" v-if="mp_is_new" @tap="userLogin"
  11. class="btn1">微信授权登录</button>
  12. <button v-else-if="canUseGetUserProfile && code" hover-class="none" @tap="getUserProfile"
  13. class="btn1">微信授权登录</button>
  14. <button v-else hover-class="none" open-type="getUserInfo" @getuserinfo="setUserInfo"
  15. class="btn1">微信授权登录</button>
  16. <button hover-class="none" @click="isUp = true" class="btn2 acea-row row-center-wrapper">手机一键登录</button>
  17. </view>
  18. <block v-if="isUp">
  19. <mobileLogin :isUp="isUp" @close="maskClose" :authKey="authKey" @wechatPhone="wechatPhone"></mobileLogin>
  20. </block>
  21. <block v-if="isPhoneBox">
  22. <routinePhone :logoUrl="logoUrl" :isPhoneBox="isPhoneBox" @close="bindPhoneClose" :authKey="authKey">
  23. </routinePhone>
  24. </block>
  25. <view class="mask" @click='close'></view>
  26. </view>
  27. </template>
  28. <script>
  29. const app = getApp();
  30. import mobileLogin from '../loginMobile/index.vue';
  31. import routinePhone from '../loginMobile/routine_phone.vue';
  32. import {
  33. getLogo,
  34. silenceAuth,
  35. wechatAuthV2,
  36. authLogin
  37. } from '@/api/public';
  38. import {
  39. LOGO_URL,
  40. EXPIRES_TIME,
  41. USER_INFO,
  42. STATE_R_KEY
  43. } from '@/config/cache';
  44. import {
  45. getUserInfo
  46. } from '@/api/user.js';
  47. import Routine from '@/libs/routine';
  48. import wechat from '@/libs/wechat';
  49. import colors from '@/mixins/color.js';
  50. export default {
  51. mixins:[colors],
  52. props: {
  53. isShowAuth: {
  54. type: Boolean,
  55. default: false
  56. }
  57. },
  58. data() {
  59. return {
  60. isUp: false,
  61. phone: '',
  62. isPhoneBox: false,
  63. logoUrl: '',
  64. code: '',
  65. authKey: '',
  66. options: '',
  67. userInfo: {},
  68. codeNum: 0,
  69. canUseGetUserProfile: false,
  70. mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false
  71. };
  72. },
  73. components: {
  74. mobileLogin,
  75. routinePhone
  76. },
  77. mounted(options) {
  78. if (uni.getUserProfile) {
  79. console.log('getUserProfile')
  80. this.canUseGetUserProfile = true
  81. }
  82. getLogo().then(res => {
  83. this.logoUrl = res.data.logo_url;
  84. });
  85. let that = this;
  86. // #ifdef MP
  87. Routine.getCode()
  88. .then(code => {
  89. this.code = code
  90. })
  91. // #endif
  92. },
  93. methods: {
  94. close(){
  95. this.$emit('authColse', false);
  96. },
  97. privacy(type) {
  98. uni.navigateTo({
  99. url: "/pages/users/privacy/index?type=" + type
  100. })
  101. },
  102. // 小程序 22.11.8日删除getUserProfile 接口获取用户昵称头像
  103. userLogin() {
  104. Routine.getCode()
  105. .then(code => {
  106. uni.showLoading({
  107. title: '正在登录中'
  108. });
  109. authLogin({
  110. code,
  111. spread_spid: app.globalData.spid,
  112. spread_code: app.globalData.code
  113. }).then(res => {
  114. if (res.data.key !== undefined && res.data.key) {
  115. uni.hideLoading();
  116. this.authKey = res.data.key;
  117. this.isPhoneBox = true;
  118. } else {
  119. uni.hideLoading();
  120. let time = res.data.expires_time - this.$Cache.time();
  121. this.$store.commit('LOGIN', {
  122. token: res.data.token,
  123. time: time
  124. });
  125. this.getUserInfo()
  126. }
  127. })
  128. })
  129. .catch(err => {
  130. console.log(err)
  131. });
  132. },
  133. // 弹窗关闭
  134. maskClose() {
  135. this.isUp = false;
  136. this.$emit('onLoadFun');
  137. },
  138. bindPhoneClose(data) {
  139. if (data.isStatus) {
  140. this.isPhoneBox = false;
  141. this.$emit('onLoadFun');
  142. // this.$util.Tips({
  143. // title: '登录成功',
  144. // icon: 'success'
  145. // }, {
  146. // tab: 3
  147. // });
  148. } else {
  149. this.isPhoneBox = false;
  150. }
  151. },
  152. // #ifdef MP
  153. /**
  154. * 获取个人用户信息
  155. */
  156. getUserInfo: function() {
  157. let that = this;
  158. getUserInfo().then(res => {
  159. uni.hideLoading();
  160. that.userInfo = res.data;
  161. that.$store.commit('SETUID', res.data.uid);
  162. that.$store.commit('UPDATE_USERINFO', res.data);
  163. that.$emit('onLoadFun');
  164. that.$util.Tips({
  165. title: '登录成功',
  166. icon: 'success'
  167. });
  168. });
  169. },
  170. setUserInfo(e) {
  171. uni.showLoading({
  172. title: '正在登录中'
  173. });
  174. Routine.getCode()
  175. .then(code => {
  176. this.getWxUser(code);
  177. })
  178. .catch(res => {
  179. uni.hideLoading();
  180. });
  181. },
  182. //小程序授权api替换 getUserInfo
  183. getUserProfile() {
  184. uni.showLoading({
  185. title: '正在登录中'
  186. });
  187. let self = this;
  188. Routine.getUserProfile()
  189. .then(res => {
  190. let userInfo = res.userInfo;
  191. userInfo.code = this.code;
  192. userInfo.spread_spid = app.globalData.spid || this.$Cache.get('spread'); //获取推广人ID
  193. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  194. Routine.authUserInfo(userInfo)
  195. .then(res => {
  196. if (res.data.key !== undefined && res.data.key) {
  197. uni.hideLoading();
  198. self.authKey = res.data.key;
  199. self.isPhoneBox = true;
  200. } else {
  201. uni.hideLoading();
  202. let time = res.data.expires_time - self.$Cache.time();
  203. self.$store.commit('LOGIN', {
  204. token: res.data.token,
  205. time: time
  206. });
  207. this.getUserInfo()
  208. }
  209. })
  210. .catch(res => {
  211. uni.hideLoading();
  212. uni.showToast({
  213. title: res.msg,
  214. icon: 'none',
  215. duration: 2000
  216. });
  217. });
  218. })
  219. .catch(res => {
  220. uni.hideLoading();
  221. });
  222. },
  223. getWxUser(code) {
  224. let self = this;
  225. Routine.getUserInfo()
  226. .then(res => {
  227. let userInfo = res.userInfo;
  228. userInfo.code = code;
  229. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  230. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  231. Routine.authUserInfo(userInfo)
  232. .then(res => {
  233. if (res.data.key !== undefined && res.data.key) {
  234. uni.hideLoading();
  235. self.authKey = res.data.key;
  236. self.isPhoneBox = true;
  237. } else {
  238. uni.hideLoading();
  239. let time = res.data.expires_time - self.$Cache.time();
  240. self.$store.commit('LOGIN', {
  241. token: res.data.token,
  242. time: time
  243. });
  244. self.$emit('onLoadFun');
  245. self.$util.Tips({
  246. title: res.msg,
  247. icon: 'success'
  248. });
  249. }
  250. })
  251. .catch(res => {
  252. uni.hideLoading();
  253. uni.showToast({
  254. title: res.msg,
  255. icon: 'none',
  256. duration: 2000
  257. });
  258. });
  259. })
  260. .catch(res => {
  261. uni.hideLoading();
  262. });
  263. },
  264. // #endif
  265. }
  266. };
  267. </script>
  268. <style lang="scss">
  269. .mask{
  270. z-index: 99;
  271. }
  272. .authorize{
  273. width: 100%;
  274. height: 680rpx;
  275. background-color: #fff;
  276. border-radius: 48rpx 48rpx 0 0;
  277. position: fixed;
  278. left: 0;
  279. bottom: 0;
  280. z-index: 667;
  281. padding-top: 50rpx;
  282. text-align: center;
  283. .pictrue{
  284. width: 152rpx;
  285. height: 152rpx;
  286. border-radius: 50%;
  287. margin: 0 auto;
  288. position: relative;
  289. image{
  290. width: 100%;
  291. height: 100%;
  292. border-radius: 50%;
  293. border:1px solid #eee;
  294. }
  295. .iconfont{
  296. position: absolute;
  297. width: 52rpx;
  298. height: 52rpx;
  299. background: #EEE;
  300. border-radius: 50%;
  301. color: #888;
  302. font-size: 30rpx;
  303. text-align: center;
  304. line-height: 52rpx;
  305. right: -267rpx;
  306. top: -20rpx;
  307. }
  308. }
  309. .title{
  310. margin-top: 28rpx;
  311. font-size: 36rpx;
  312. color: #333333;
  313. }
  314. .info{
  315. color: #9E9E9E;
  316. font-size: 28rpx;
  317. margin-top: 14rpx;
  318. .agree{
  319. color: #333;
  320. }
  321. }
  322. .btn1{
  323. width: 536rpx;
  324. height: 86rpx;
  325. border-radius: 43rpx;
  326. color: #fff;
  327. text-align: center;
  328. line-height: 86rpx;
  329. margin: 50rpx auto 0 auto;
  330. background-color: #2BA245;
  331. font-size: 30rpx;
  332. }
  333. .btn2{
  334. width: 536rpx;
  335. height: 86rpx;
  336. border-radius: 43rpx;
  337. border: 2rpx solid #2BA245;
  338. color: #2BA245;
  339. font-size: 30rpx;
  340. margin: 40rpx auto 0 auto;
  341. }
  342. }
  343. </style>