ns-login.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <view>
  3. <view @touchmove.prevent.stop>
  4. <uni-popup ref="auth" :custom="true" :mask-click="false">
  5. <view class="uni-tip">
  6. <view class="uni-tip-title">您还未登录</view>
  7. <view class="uni-tip-content">请先登录之后再进行操作</view>
  8. <view class="uni-tip-icon"><image :src="$util.img('/upload/uniapp/member/login.png')" mode="widthFix"></image></view>
  9. <view class="uni-tip-group-button">
  10. <button type="default" class="uni-tip-button color-title close" @click="close">暂不登录</button>
  11. <!-- #ifdef MP-WEIXIN || MP-QQ || MP-BAIDU -->
  12. <button type="primary" open-type="getUserInfo" @getuserinfo="login" class="uni-tip-button">立即登录</button>
  13. <!-- #endif -->
  14. <!-- #ifdef MP-ALIPAY -->
  15. <button type="primary" open-type="getAuthorize" scope="userInfo" @getAuthorize="login" class="uni-tip-button color-base-bg">立即登录</button>
  16. <!-- #endif -->
  17. <!-- #ifdef H5 -->
  18. <button type="primary" class="uni-tip-button color-base-bg" @click="login">立即登录</button>
  19. <!-- #endif -->
  20. </view>
  21. </view>
  22. </uni-popup>
  23. </view>
  24. <bind-mobile ref="bindMobile"></bind-mobile>
  25. </view>
  26. </template>
  27. <script>
  28. import uniPopup from '../uni-popup/uni-popup.vue';
  29. import Config from 'common/js/config.js';
  30. import bindMobile from '../bind-mobile/bind-mobile.vue';
  31. // import auth from 'common/js/auth.js';
  32. export default {
  33. // mixins: [auth],
  34. name: 'ns-login',
  35. components: {
  36. uniPopup,
  37. bindMobile
  38. },
  39. data() {
  40. return {
  41. url: '',
  42. registerConfig: {}
  43. };
  44. },
  45. created() {
  46. this.getRegisterConfig();
  47. },
  48. mounted() {
  49. // #ifdef H5
  50. if (this.$util.isWeiXin()) {
  51. const getUrlCode = function() {
  52. var url = location.search;
  53. var theRequest = new Object();
  54. if (url.indexOf('?') != -1) {
  55. var str = url.substr(1);
  56. var strs = str.split('&');
  57. for (var i = 0; i < strs.length; i++) {
  58. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
  59. }
  60. }
  61. return theRequest;
  62. };
  63. var urlParams = getUrlCode();
  64. if (urlParams.source_member) uni.setStorageSync('source_member', urlParams.source_member);
  65. if (urlParams.code != undefined) {
  66. this.$api.sendRequest({
  67. url: '/wechat/api/wechat/authcodetoopenid',
  68. data: {
  69. code: urlParams.code
  70. },
  71. success: res => {
  72. if (res.code >= 0) {
  73. let data = {};
  74. if (res.data.openid) data.wx_openid = res.data.openid;
  75. if (res.data.unionid) data.wx_unionid = res.data.unionid;
  76. if (res.data.userinfo) Object.assign(data, res.data.userinfo);
  77. this.authLogin(data);
  78. }
  79. }
  80. });
  81. }
  82. }
  83. // #endif
  84. },
  85. methods: {
  86. /**
  87. * 获取注册配置
  88. */
  89. getRegisterConfig() {
  90. this.$api.sendRequest({
  91. url: '/api/register/config',
  92. success: res => {
  93. if (res.code >= 0) {
  94. this.registerConfig = res.data.value;
  95. }
  96. }
  97. });
  98. },
  99. open(url) {
  100. if (url) this.url = url;
  101. // #ifndef H5
  102. this.$refs.auth.open();
  103. // #endif
  104. // #ifdef H5
  105. if (this.$util.isWeiXin()) {
  106. let authData = uni.getStorageSync('authInfo');
  107. if (authData && authData.wx_openid && !uni.getStorageSync('loginLock')) {
  108. this.authLogin(authData);
  109. } else {
  110. this.$api.sendRequest({
  111. url: '/wechat/api/wechat/authcode',
  112. data: {
  113. redirect_url: location.href
  114. },
  115. success: res => {
  116. if (res.code >= 0) {
  117. location.href = res.data;
  118. } else {
  119. this.$util.showToast({ title: '公众号配置错误' });
  120. }
  121. }
  122. });
  123. }
  124. } else {
  125. this.$refs.auth.open();
  126. }
  127. // #endif
  128. },
  129. close() {
  130. this.$refs.auth.close();
  131. },
  132. login(e) {
  133. if (!uni.getStorageSync('loginLock')) {
  134. // #ifdef MP
  135. if (e.detail.errMsg == 'getUserInfo:ok') {
  136. this.getCode(data => {
  137. if (data) {
  138. this.authLogin(data);
  139. } else {
  140. this.$refs.auth.close();
  141. this.toLogin();
  142. }
  143. });
  144. }
  145. // #endif
  146. // #ifndef MP
  147. this.$refs.auth.close();
  148. this.toLogin();
  149. // #endif
  150. } else {
  151. this.$refs.auth.close();
  152. this.toLogin();
  153. }
  154. },
  155. /**
  156. * 跳转去登录页
  157. */
  158. toLogin() {
  159. if (this.url) this.$util.redirectTo('/pages/login/login/login', { back: encodeURIComponent(this.url) });
  160. else this.$util.redirectTo('/pages/login/login/login');
  161. },
  162. /**
  163. * 授权登录
  164. */
  165. authLogin(data) {
  166. uni.showLoading({ title: '登录中' });
  167. uni.setStorage({
  168. key: 'authInfo',
  169. data: data
  170. });
  171. if (uni.getStorageSync('source_member')) data.source_member = uni.getStorageSync('source_member');
  172. this.$api.sendRequest({
  173. url: '/api/login/auth',
  174. data,
  175. success: res => {
  176. this.$refs.auth.close();
  177. if (res.code >= 0) {
  178. uni.setStorage({
  179. key: 'token',
  180. data: res.data.token,
  181. success: () => {
  182. uni.removeStorageSync('loginLock');
  183. uni.removeStorageSync('unbound');
  184. uni.removeStorageSync('authInfo');
  185. this.$store.dispatch('getCartNumber');
  186. this.$store.commit('setToken', res.data.token);
  187. if (res.data.is_register && this.$refs.registerReward.getReward()) {
  188. this.$refs.registerReward.open();
  189. }
  190. }
  191. });
  192. setTimeout(() => {
  193. uni.hideLoading();
  194. }, 1000);
  195. } else if (this.registerConfig.third_party == 1 && this.registerConfig.bind_mobile == 1) {
  196. uni.hideLoading();
  197. this.$refs.bindMobile.open();
  198. } else if (this.registerConfig.third_party == 0) {
  199. uni.hideLoading();
  200. this.toLogin();
  201. } else {
  202. uni.hideLoading();
  203. this.$util.showToast({ title: res.message });
  204. }
  205. },
  206. fail: () => {
  207. uni.hideLoading();
  208. this.$refs.auth.close();
  209. this.$util.showToast({ title: '登录失败' });
  210. }
  211. });
  212. }
  213. }
  214. };
  215. </script>
  216. <style lang="scss">
  217. .uni-tip {
  218. width: 580rpx;
  219. background: #fff;
  220. box-sizing: border-box;
  221. border-radius: 10rpx;
  222. overflow: hidden;
  223. height: initial;
  224. }
  225. .uni-tip-title {
  226. text-align: center;
  227. font-weight: bold;
  228. font-size: $font-size-toolbar;
  229. color: $color-title;
  230. padding-top: 50rpx;
  231. }
  232. .uni-tip-content {
  233. padding: 0 30rpx;
  234. color: $color-sub;
  235. font-size: $font-size-base;
  236. text-align: center;
  237. }
  238. .uni-tip-icon {
  239. width: 100%;
  240. text-align: center;
  241. margin-top: 50rpx;
  242. }
  243. .uni-tip-icon image {
  244. width: 300rpx;
  245. }
  246. .uni-tip-group-button {
  247. margin-top: 30rpx;
  248. line-height: 120rpx;
  249. display: flex;
  250. padding: 0 50rpx 50rpx 50rpx;
  251. justify-content: space-between;
  252. }
  253. .uni-tip-button {
  254. width: 200rpx;
  255. height: 80rpx;
  256. line-height: 80rpx;
  257. text-align: center;
  258. border: none;
  259. border-radius: 80rpx;
  260. padding: 0 !important;
  261. margin: 0 !important;
  262. background: #fff;
  263. font-size: $font-size-base;
  264. }
  265. .uni-tip-group-button .close {
  266. border: 1px solid #eee;
  267. }
  268. .uni-tip-button:after {
  269. border: none;
  270. }
  271. </style>