index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. this.canUseGetUserProfile = true
  80. }
  81. getLogo().then(res => {
  82. this.logoUrl = res.data.logo_url;
  83. });
  84. let that = this;
  85. // #ifdef MP
  86. Routine.getCode()
  87. .then(code => {
  88. this.code = code
  89. })
  90. // #endif
  91. },
  92. methods: {
  93. close(){
  94. this.$emit('authColse', false);
  95. },
  96. privacy(type) {
  97. uni.navigateTo({
  98. url: "/pages/users/privacy/index?type=" + type
  99. })
  100. },
  101. // 小程序 22.11.8日删除getUserProfile 接口获取用户昵称头像
  102. userLogin() {
  103. Routine.getCode()
  104. .then(code => {
  105. uni.showLoading({
  106. title: '正在登录中'
  107. });
  108. authLogin({
  109. code,
  110. spread_spid: app.globalData.spid,
  111. spread_code: app.globalData.code
  112. }).then(res => {
  113. if (res.data.key !== undefined && res.data.key) {
  114. uni.hideLoading();
  115. this.authKey = res.data.key;
  116. this.isPhoneBox = true;
  117. } else {
  118. uni.hideLoading();
  119. let time = res.data.expires_time - this.$Cache.time();
  120. this.$store.commit('LOGIN', {
  121. token: res.data.token,
  122. time: time
  123. });
  124. this.getUserInfo()
  125. }
  126. })
  127. })
  128. .catch(err => {
  129. console.log(err)
  130. });
  131. },
  132. // 弹窗关闭
  133. maskClose() {
  134. this.isUp = false;
  135. this.$emit('onLoadFun');
  136. },
  137. bindPhoneClose(data) {
  138. if (data.isStatus) {
  139. this.isPhoneBox = false;
  140. this.$emit('onLoadFun');
  141. // this.$util.Tips({
  142. // title: '登录成功',
  143. // icon: 'success'
  144. // }, {
  145. // tab: 3
  146. // });
  147. } else {
  148. this.isPhoneBox = false;
  149. }
  150. },
  151. // #ifdef MP
  152. /**
  153. * 获取个人用户信息
  154. */
  155. getUserInfo: function() {
  156. let that = this;
  157. getUserInfo().then(res => {
  158. uni.hideLoading();
  159. that.userInfo = res.data;
  160. that.$store.commit('SETUID', res.data.uid);
  161. that.$store.commit('UPDATE_USERINFO', res.data);
  162. that.$emit('onLoadFun');
  163. that.$util.Tips({
  164. title: '登录成功',
  165. icon: 'success'
  166. });
  167. });
  168. },
  169. setUserInfo(e) {
  170. uni.showLoading({
  171. title: '正在登录中'
  172. });
  173. Routine.getCode()
  174. .then(code => {
  175. this.getWxUser(code);
  176. })
  177. .catch(res => {
  178. uni.hideLoading();
  179. });
  180. },
  181. //小程序授权api替换 getUserInfo
  182. getUserProfile() {
  183. uni.showLoading({
  184. title: '正在登录中'
  185. });
  186. let self = this;
  187. Routine.getUserProfile()
  188. .then(res => {
  189. let userInfo = res.userInfo;
  190. userInfo.code = this.code;
  191. userInfo.spread_spid = app.globalData.spid || this.$Cache.get('spread'); //获取推广人ID
  192. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  193. Routine.authUserInfo(userInfo)
  194. .then(res => {
  195. if (res.data.key !== undefined && res.data.key) {
  196. uni.hideLoading();
  197. self.authKey = res.data.key;
  198. self.isPhoneBox = true;
  199. } else {
  200. uni.hideLoading();
  201. let time = res.data.expires_time - self.$Cache.time();
  202. self.$store.commit('LOGIN', {
  203. token: res.data.token,
  204. time: time
  205. });
  206. this.getUserInfo()
  207. }
  208. })
  209. .catch(res => {
  210. uni.hideLoading();
  211. uni.showToast({
  212. title: res.msg,
  213. icon: 'none',
  214. duration: 2000
  215. });
  216. });
  217. })
  218. .catch(res => {
  219. uni.hideLoading();
  220. });
  221. },
  222. getWxUser(code) {
  223. let self = this;
  224. Routine.getUserInfo()
  225. .then(res => {
  226. let userInfo = res.userInfo;
  227. userInfo.code = code;
  228. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  229. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  230. Routine.authUserInfo(userInfo)
  231. .then(res => {
  232. if (res.data.key !== undefined && res.data.key) {
  233. uni.hideLoading();
  234. self.authKey = res.data.key;
  235. self.isPhoneBox = true;
  236. } else {
  237. uni.hideLoading();
  238. let time = res.data.expires_time - self.$Cache.time();
  239. self.$store.commit('LOGIN', {
  240. token: res.data.token,
  241. time: time
  242. });
  243. self.$emit('onLoadFun');
  244. self.$util.Tips({
  245. title: res.msg,
  246. icon: 'success'
  247. });
  248. }
  249. })
  250. .catch(res => {
  251. uni.hideLoading();
  252. uni.showToast({
  253. title: res.msg,
  254. icon: 'none',
  255. duration: 2000
  256. });
  257. });
  258. })
  259. .catch(res => {
  260. uni.hideLoading();
  261. });
  262. },
  263. // #endif
  264. }
  265. };
  266. </script>
  267. <style lang="scss">
  268. .mask{
  269. z-index: 99;
  270. }
  271. .authorize{
  272. width: 100%;
  273. height: 680rpx;
  274. background-color: #fff;
  275. border-radius: 48rpx 48rpx 0 0;
  276. position: fixed;
  277. left: 0;
  278. bottom: 0;
  279. z-index: 667;
  280. padding-top: 50rpx;
  281. text-align: center;
  282. .pictrue{
  283. width: 152rpx;
  284. height: 152rpx;
  285. border-radius: 50%;
  286. margin: 0 auto;
  287. position: relative;
  288. image{
  289. width: 100%;
  290. height: 100%;
  291. border-radius: 50%;
  292. border:1px solid #eee;
  293. }
  294. .iconfont{
  295. position: absolute;
  296. width: 52rpx;
  297. height: 52rpx;
  298. background: #EEE;
  299. border-radius: 50%;
  300. color: #888;
  301. font-size: 30rpx;
  302. text-align: center;
  303. line-height: 52rpx;
  304. right: -267rpx;
  305. top: -20rpx;
  306. }
  307. }
  308. .title{
  309. margin-top: 28rpx;
  310. font-size: 36rpx;
  311. color: #333333;
  312. }
  313. .info{
  314. color: #9E9E9E;
  315. font-size: 28rpx;
  316. margin-top: 14rpx;
  317. .agree{
  318. color: #333;
  319. }
  320. }
  321. .btn1{
  322. width: 536rpx;
  323. height: 86rpx;
  324. border-radius: 43rpx;
  325. color: #fff;
  326. text-align: center;
  327. line-height: 86rpx;
  328. margin: 50rpx auto 0 auto;
  329. background-color: #2BA245;
  330. font-size: 30rpx;
  331. }
  332. .btn2{
  333. width: 536rpx;
  334. height: 86rpx;
  335. border-radius: 43rpx;
  336. border: 2rpx solid #2BA245;
  337. color: #2BA245;
  338. font-size: 30rpx;
  339. margin: 40rpx auto 0 auto;
  340. }
  341. }
  342. </style>