index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="page" :data-theme="theme" :style="{'background-image': `url(${backBg})`}">
  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/images/left.png"></image>
  8. </view>
  9. <view class="icon" @click="home" v-else>
  10. <image src="../static/images/home.png"></image>
  11. </view>
  12. 账户登录
  13. </view>
  14. <!-- #endif -->
  15. <view class="wechat_login">
  16. <view class="img acea-row row-center">
  17. <image :src="mobileLoginLogo" mode="widthFix" class="image"></image>
  18. </view>
  19. <view class="company">{{companyName}}</view>
  20. <view class="btn-wrapper">
  21. <!-- #ifdef H5 -->
  22. <button hover-class="none" @click="wechatLogin" class="bg-green btn1"><text
  23. class='iconfont icon-weixin2'></text>立即登录</button>
  24. <!-- #endif -->
  25. <!-- #ifdef MP -->
  26. <button v-if="wxLogin" hover-class="none" @click="getUserProfile" class="btn1 bg-color">立即登录</button>
  27. <view v-else>
  28. <button v-if="routinePhoneVerification == 1 || routinePhoneVerification.length===3"
  29. hover-class="none" @click="onUserPhone" class="btn1 bg-color"><text
  30. class='iconfont'></text>手机号一键登录</button>
  31. <button v-if="routinePhoneVerification == 2 || routinePhoneVerification.length===3"
  32. hover-class="none" @click="onUserPhone('isPhone')" class="btn2">手动绑定手机号</button>
  33. </view>
  34. <!-- #endif -->
  35. </view>
  36. </view>
  37. <block v-if="isUp">
  38. <mobileLogin :theme="theme" :isUp="isUp" @close="maskClose" :authKey="authKey" @wechatPhone="wechatPhone">
  39. </mobileLogin>
  40. </block>
  41. <atModel v-if="isPhoneBox" :userPhoneType="true" :isPhoneBox="isPhoneBox" :authKey="authKey"
  42. :content="getPhoneContent" @closeModel="bindPhoneClose" @confirmModel="confirmModel"></atModel>
  43. </view>
  44. </template>
  45. <script>
  46. import {
  47. silenceBindingSpread
  48. } from "../../../utils";
  49. import {
  50. mapGetters
  51. } from "vuex";
  52. const app = getApp();
  53. let statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  54. import mobileLogin from '../components/login_mobile/index.vue'
  55. import atModel from '@/components/accredit/index.vue'
  56. import {
  57. loginConfigApi
  58. } from '@/api/public';
  59. import Routine from '@/libs/routine';
  60. import wechat from "@/libs/wechat";
  61. export default {
  62. data() {
  63. return {
  64. isUp: false,
  65. phone: '',
  66. statusBarHeight: statusBarHeight,
  67. isHome: false,
  68. isPhoneBox: false,
  69. logoUrl: '',
  70. code: '',
  71. authKey: '',
  72. options: '',
  73. userInfo: {},
  74. codeNum: 0,
  75. urlDomain: this.$Cache.get("imgHost"),
  76. backBg: '', //背景图片
  77. theme: app.globalData.theme,
  78. getPhoneContent: '申请获取您的手机号用于注册,完成后可使用商城更多功能',
  79. wxCode: '', //小程序code值
  80. companyName: '', //公司名称
  81. routinePhoneVerification: '', //小程序手机号校验类型(多选)1微信小程序验证 2短信验证
  82. loginConfig: '', //小程序绑定手机号,isPhone其他手机号绑定
  83. wxLogin: true, //登录显示
  84. mobileLoginLogo: '' // 登录页logo
  85. }
  86. },
  87. computed: mapGetters(['isLogin', 'globalData']),
  88. components: {
  89. mobileLogin,
  90. atModel
  91. },
  92. onLoad(options) {
  93. this.routinePhoneVerification = this.globalData.routinePhoneVerification
  94. this.mobileLoginLogo = this.globalData.mobileLoginLogo
  95. this.companyName = this.globalData.companyName
  96. //背景图片
  97. switch (this.globalData.theme) {
  98. case 'theme1':
  99. this.backBg = `${this.urlDomain}crmebimage/presets/wxbj1.png`;
  100. break;
  101. case 'theme2':
  102. this.backBg = `${this.urlDomain}crmebimage/presets/wxbj2.png`;
  103. break;
  104. case 'theme3':
  105. this.backBg = `${this.urlDomain}crmebimage/presets/wxbj3.png`;
  106. break;
  107. case 'theme4':
  108. this.backBg = `${this.urlDomain}crmebimage/presets/wxbj4.png`;
  109. break;
  110. case 'theme5':
  111. this.backBg = `${this.urlDomain}crmebimage/presets/wxbj5.png`;
  112. break;
  113. }
  114. //logo图片地址
  115. loginConfigApi().then(res => {
  116. this.logoUrl = res.data.logoUrl
  117. })
  118. let that = this
  119. // #ifdef H5
  120. document.body.addEventListener("focusout", () => {
  121. setTimeout(() => {
  122. const scrollHeight = document.documentElement.scrollTop || document.body.scrollTop ||
  123. 0;
  124. window.scrollTo(0, Math.max(scrollHeight - 1, 0));
  125. }, 100);
  126. });
  127. const {
  128. code,
  129. state,
  130. scope
  131. } = options;
  132. this.options = options
  133. // 获取确认授权code
  134. this.code = code || ''
  135. if (code && this.options.scope !== 'snsapi_base') {
  136. let spread = this.globalData.spread ? this.globalData.spread : 0;
  137. //公众号授权登录回调 wechatAuth(code, Cache.get("spread"), loginType)
  138. wechat.auth(code, spread).then(res => {
  139. if (res.type === 'register') {
  140. this.authKey = res.key;
  141. this.isUp = true
  142. }
  143. if (res.type === 'login') {
  144. this.$store.commit('LOGIN', {
  145. token: res.token
  146. });
  147. this.$store.commit("SETUID", res.id);
  148. this.getUserInfo(res);
  149. }
  150. if (data.isNew && data.newPeopleCouponList && data.newPeopleCouponList.length !== 0) {
  151. this.$Cache.set('newGift', data.newPeopleCouponList);
  152. } else {
  153. this.$Cache.clear('newGift');
  154. }
  155. }).catch(error => {});
  156. }
  157. // #endif
  158. let pages = getCurrentPages();
  159. },
  160. methods: {
  161. //绑定手机号弹窗回调
  162. confirmModel() {
  163. this.isPhoneBox = false;
  164. this.isUp = true
  165. },
  166. back() {
  167. uni.navigateBack();
  168. },
  169. home() {
  170. uni.switchTab({
  171. url: '/pages/index/index'
  172. })
  173. },
  174. modelCancel() {
  175. this.isPhoneBox = false;
  176. },
  177. // 弹窗关闭
  178. maskClose() {
  179. // this.isUp = false //点击模态框会关闭登录弹框,防止用户误触而关闭
  180. },
  181. bindPhoneClose(data) {
  182. if (data.isStatus) {
  183. this.isPhoneBox = false
  184. this.$util.Tips({
  185. title: '登录成功',
  186. icon: 'success'
  187. }, {
  188. tab: 3
  189. })
  190. } else {
  191. this.isPhoneBox = false
  192. }
  193. },
  194. // #ifdef MP
  195. /**
  196. * 登录成功之后回调
  197. */
  198. getUserInfo(data) {
  199. this.$store.dispatch("GetTokenIsExist");
  200. //分销绑定
  201. silenceBindingSpread(true, this.globalData.spread);
  202. this.$store.dispatch('GetGlobalConfig');
  203. this.$store.commit('UPDATE_USERINFO', {
  204. avatar: data.avatar,
  205. nickname: data.nickname,
  206. phone: data.phone
  207. });
  208. // #ifdef MP
  209. if(!this.$Cache.get('wechatQRcode')) Routine.getQrcode()
  210. // #endif
  211. this.$util.Tips({
  212. title: '登录成功',
  213. icon: 'success'
  214. }, {
  215. tab: 3
  216. });
  217. },
  218. //绑定手机号
  219. onUserPhone(type) {
  220. this.loginConfig = type;
  221. //如果是其他手机号绑定,调整页面。否则授权本机手机号登录
  222. if (this.loginConfig === 'isPhone') {
  223. uni.navigateTo({
  224. url: `/pages/users/app_login/index?code=${this.wxCode}&authKey=${this.authKey}`
  225. })
  226. } else {
  227. this.isPhoneBox = true
  228. }
  229. },
  230. //wx小程序立即登录
  231. getUserProfile() {
  232. let self = this;
  233. uni.showLoading({
  234. title: '正在登录中'
  235. });
  236. console.log('11111111')
  237. const hostSDKVersion = uni.getSystemInfoSync().hostSDKVersion; //小程序基础库版本号
  238. if (Routine.compareVersion(hostSDKVersion, '2.21.2') >= 0) {
  239. Routine.getCode()
  240. .then(code => {
  241. let userInfo = {
  242. code: code,
  243. spreadPid: this.globalData.spread, //获取推广人ID
  244. type: 'routine'
  245. };
  246. self.getWxUser(userInfo);
  247. })
  248. .catch(res => {
  249. uni.hideLoading();
  250. });
  251. } else {
  252. Routine.getUserProfile().then(res => {
  253. Routine.getCode()
  254. .then(code => {
  255. let userInfo = {
  256. code: code,
  257. spreadPid: this.globalData.spread, //获取推广人ID
  258. type: 'routine'
  259. };
  260. self.getWxUser(userInfo);
  261. })
  262. .catch(res => {
  263. uni.hideLoading();
  264. });
  265. })
  266. .catch(res => {
  267. uni.hideLoading();
  268. });
  269. }
  270. },
  271. // 登录调用方法
  272. getWxUser(userInfo) {
  273. let self = this;
  274. Routine.authUserInfo(userInfo)
  275. .then(res => {
  276. self.authKey = res.data.key;
  277. if (res.data.type === 'register') {
  278. uni.hideLoading();
  279. self.wxLogin = false;
  280. }
  281. if (res.data.type === 'login') {
  282. uni.hideLoading();
  283. self.getUserInfo(res.data);
  284. }
  285. })
  286. .catch(res => {
  287. uni.hideLoading();
  288. uni.showToast({
  289. title: res,
  290. icon: 'none',
  291. duration: 2000
  292. });
  293. });
  294. },
  295. // #endif
  296. // #ifdef H5
  297. // 获取url后面的参数
  298. getQueryString(name) {
  299. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  300. var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
  301. var r = window.location.search.substr(1).match(reg);
  302. var q = window.location.pathname.substr(1).match(reg_rewrite);
  303. if (r != null) {
  304. return unescape(r[2]);
  305. } else if (q != null) {
  306. return unescape(q[2]);
  307. } else {
  308. return null;
  309. }
  310. },
  311. // 公众号登录
  312. wechatLogin() {
  313. if (!this.code && this.options.scope !== 'snsapi_base') {
  314. this.$wechat.oAuth('snsapi_userinfo', '/pages/users/wechat_login/index');
  315. } else {
  316. this.isUp = true;
  317. }
  318. },
  319. // 输入手机号后的回调
  320. wechatPhone() {
  321. this.$Cache.clear('snsapiKey');
  322. if (this.options.back_url) {
  323. let url = uni.getStorageSync('snRouter');
  324. url = url.indexOf('/pages/index/index') != -1 ? '/' : url;
  325. if (url.indexOf('/pages/users/wechat_login/index') !== -1) {
  326. url = '/';
  327. }
  328. if (!url) {
  329. url = '/pages/index/index';
  330. }
  331. this.isUp = false
  332. uni.showToast({
  333. title: '登录成功',
  334. icon: 'none'
  335. })
  336. setTimeout(res => {
  337. location.href = url
  338. }, 800)
  339. } else {
  340. uni.navigateBack()
  341. }
  342. }
  343. // #endif
  344. }
  345. }
  346. </script>
  347. <style lang="scss">
  348. page {
  349. background: #fff;
  350. height: 100%;
  351. }
  352. </style>
  353. <style lang="scss" scoped>
  354. .icon-weixin2 {
  355. margin-right: 10rpx;
  356. }
  357. .company {
  358. font-size: 40rpx;
  359. color: #333;
  360. text-align: center;
  361. font-weight: 500;
  362. margin: 32rpx 0 96rpx 0;
  363. }
  364. .page {
  365. background: #fff;
  366. width: 100%;
  367. height: 100%;
  368. background-repeat: no-repeat;
  369. background-size: 100% 100%;
  370. }
  371. .wechat_login {
  372. padding-top: 238rpx;
  373. .img .image {
  374. width: 152rpx;
  375. height: 152rpx;
  376. }
  377. .btn-wrapper {
  378. margin-top: 86rpx;
  379. padding: 0 66rpx;
  380. button {
  381. width: 100%;
  382. height: 86rpx;
  383. line-height: 84rpx;
  384. margin-bottom: 32rpx;
  385. border-radius: 120rpx;
  386. font-size: 32rpx;
  387. &.btn1 {
  388. color: #fff;
  389. }
  390. &.btn2 {
  391. color: #666666;
  392. border: 2rpx solid #666666;
  393. }
  394. }
  395. }
  396. }
  397. .title-bar {
  398. position: relative;
  399. display: flex;
  400. align-items: center;
  401. justify-content: center;
  402. font-size: 36rpx;
  403. }
  404. .icon {
  405. position: absolute;
  406. left: 30rpx;
  407. top: 0;
  408. display: flex;
  409. align-items: center;
  410. justify-content: center;
  411. width: 86rpx;
  412. height: 86rpx;
  413. image {
  414. width: 50rpx;
  415. height: 50rpx;
  416. }
  417. }
  418. </style>