金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
8.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <template>
  2. <view>
  3. <lf-nav title="登录/注册" :showIcon="true" bgColor="#fff"></lf-nav>
  4. <view style="display: flex;justify-content: center;align-items: center;margin-top: 300rpx;">
  5. <image class="img" src="/static/images/login.png" mode="widthFix"></image>
  6. </view>
  7. <view class="fixed-btn">
  8. <button class="btn1" hover-class="lf-opacity" @click="toPhoneLogin">手机号登录</button>
  9. <button class="btn2" hover-class="lf-opacity" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信授权登录</button>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import { config, wechat, GetUrl } from '@/common/js/utils.js';
  15. export default {
  16. data(){
  17. return {
  18. code: '',
  19. url: '',
  20. logo: '',
  21. author: config.PACKAGES.author,
  22. config: '',
  23. showAgreement: false,
  24. agreement: '',
  25. open_id: '',
  26. h5_url: ''
  27. }
  28. },
  29. onShow(){
  30. var token = this.$cookieStorage.get('user_token');
  31. // this.setData({
  32. // token: token
  33. // });
  34. this.token = token;
  35. var initInfo = this.$cookieStorage.get('init');
  36. if (initInfo && initInfo.shop_show_logo) {
  37. this.setData({
  38. logo: initInfo.shop_show_logo
  39. });
  40. }
  41. if (token) {
  42. // uni.switchTab({
  43. // url: '/pages/user/personal/personal'
  44. // });
  45. this.$url('/pages/user/my/center', {type: 'launch'})
  46. } else {
  47. // #ifdef MP-WEIXIN
  48. this.wxLogin();
  49. // #endif
  50. }
  51. },
  52. onLoad(e){
  53. // 第三方平台配置颜色
  54. var bgConfig = this.$cookieStorage.get('globalConfig') || '';
  55. this.setData({
  56. config: bgConfig
  57. });
  58. if (e.url) {
  59. this.url = decodeURIComponent(e.url);
  60. console.log('this.url',this.url)
  61. }
  62. if (e.shop_id) {
  63. this.$cookieStorage.set('shop_id', e.shop_id);
  64. }
  65. this.getGreementInfo();
  66. },
  67. methods: {
  68. // 获取用户协议
  69. getGreementInfo() {
  70. console.log("获取协议")
  71. // this.$http.get({
  72. // api: 'api/user/agreement'
  73. // }).then(res => {
  74. // if (res.statusCode == 200) {
  75. // res = res.data;
  76. // if (res.status) {
  77. // this.setData({
  78. // agreement: res.data
  79. // });
  80. // }
  81. // }
  82. // });
  83. },
  84. // 小程序静默登录
  85. wxLogin(){
  86. uni.showLoading({
  87. title: '正在自动登录',
  88. mask: true
  89. });
  90. uni.login({
  91. success: res => {
  92. console.log(res);
  93. if (res.code) {
  94. this.autoLogin(res.code);
  95. } else {
  96. this.$msg('获取code失败');
  97. }
  98. },
  99. });
  100. },
  101. // 通过code自动登录
  102. autoLogin(code) {
  103. this.$http.post({
  104. api: 'api/oauth/miniprogram/login',
  105. data: {
  106. code: code,
  107. open_type: 'miniprogram',
  108. shop_id: this.$cookieStorage.get('shop_id') || '',
  109. agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '',
  110. clerk_id: this.$cookieStorage.get('clerk_id') || '',
  111. agent_code_time: this.$cookieStorage.get('agent_code_time') || '',
  112. shop_id_time: this.$cookieStorage.get('shop_id_time') || ''
  113. }
  114. }).then(res => {
  115. res = res.data;
  116. if (res.data && res.data.open_id) {
  117. this.setData({
  118. open_id: res.data.open_id
  119. });
  120. } // 如果接口返回token就直接登录,如果没有则弹出授权
  121. if (res.data.access_token) {
  122. console.log('已经返回给我了token');
  123. uni.hideLoading();
  124. var access_token = res.data.token_type + ' ' + res.data.access_token;
  125. var expires_in = res.data.expires_in || 315360000;
  126. this.$cookieStorage.set("user_token", access_token, expires_in);
  127. this.getUserInfo(access_token);
  128. } else {
  129. uni.hideLoading();
  130. }
  131. }).catch(rej => {
  132. uni.hideLoading();
  133. uni.showModal({
  134. content: '请求失败,请重试',
  135. showCancel: false,
  136. success: res => {
  137. if (res.confirm || !res.cancel && !res.confirm) {
  138. this.wxLogin();
  139. }
  140. }
  141. });
  142. });
  143. },
  144. // 获取用户手机号
  145. getPhoneNumber(e) {
  146. if (e.detail.encryptedData) {
  147. uni.login({
  148. success: res => {
  149. if (res.code) {
  150. this.setData({
  151. code: res.code
  152. });
  153. this.phone(e);
  154. } else {
  155. uni.showModal({
  156. content: " 获取code失败",
  157. showCancel: false
  158. });
  159. }
  160. }
  161. });
  162. return;
  163. } else {
  164. this.toPhoneLogin();
  165. }
  166. },
  167. // 通过手机号登录注册
  168. phone(e) {
  169. uni.showLoading({
  170. title: '正在登录',
  171. mask: true
  172. });
  173. this.$http.post({
  174. api: 'api/oauth/miniprogram/mobile',
  175. data: {
  176. open_type: 'miniprogram',
  177. code: this.code,
  178. encryptedData: e.detail.encryptedData,
  179. iv: e.detail.iv,
  180. open_id: this.open_id,
  181. shop_id: this.$cookieStorage.get('shop_id') || '',
  182. agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '',
  183. clerk_id: this.$cookieStorage.get('clerk_id') || '',
  184. agent_code_time: this.$cookieStorage.get('agent_code_time') || '',
  185. shop_id_time: this.$cookieStorage.get('shop_id_time') || ''
  186. }
  187. }).then(res => {
  188. if (res.statusCode == 200) {
  189. res = res.data;
  190. if (res.data.access_token) {
  191. var access_token = res.data.token_type + ' ' + res.data.access_token;
  192. var expires_in = res.data.expires_in || 315360000; // debugger;
  193. this.getUserInfo(access_token);
  194. this.$cookieStorage.set("user_token", access_token, expires_in); // this.$cookieStorage.set("user_token",access_token,expires_in);
  195. // uni.setStorageSync("user_token",access_token);
  196. } else {
  197. uni.showModal({
  198. content: res.message || '请求失败,请重试',
  199. showCancel: false
  200. });
  201. }
  202. } else {
  203. uni.showModal({
  204. content: '请求失败,请重试',
  205. showCancel: false
  206. });
  207. }
  208. uni.hideLoading();
  209. }).catch(rej => {
  210. uni.hideLoading();
  211. uni.showModal({
  212. content: '请求失败,请重试',
  213. showCancel: false
  214. });
  215. });
  216. },
  217. // 接口获取用户信息
  218. getUserInfo(token){
  219. this.$http.get({
  220. api: 'api/user',
  221. header: {
  222. Authorization: token
  223. }
  224. }).then(res => {
  225. // console.log("1111", res)
  226. // this.userInfo = res.data;
  227. this.$cookieStorage.set('user_info', res.data);
  228. if (this.url) {
  229. this.$url("/" + this.url, {type: 'redirect'});
  230. } else {
  231. this.$url('/pages/user/my/center', {type: 'launch'})
  232. }
  233. })
  234. },
  235. // 跳转到手机号验证码登录
  236. toPhoneLogin(){
  237. if (this.url) {
  238. // uni.navigateTo({
  239. // url: '/pages/user/loginType/loginType?url=' + encodeURIComponent(this.url) + '&open_id=' + this.open_id
  240. // });
  241. this.$url('/pages/user/loginType/loginType?url=' + encodeURIComponent(this.url) + '&open_id=' + this.open_id);
  242. } else {
  243. // wx.navigateTo({
  244. // url: '/pages/user/loginType/loginType?open_id=' + this.open_id
  245. // });
  246. this.$url('/pages/user/loginType/loginType?open_id=' + this.open_id);
  247. }
  248. },
  249. setData: function(obj) {
  250. let that = this;
  251. let keys = [];
  252. let val, data;
  253. Object.keys(obj).forEach(function(key) {
  254. keys = key.split('.');
  255. val = obj[key];
  256. data = that.$data;
  257. keys.forEach(function(key2, index) {
  258. if (index + 1 == keys.length) {
  259. that.$set(data, key2, val);
  260. } else {
  261. if (!data[key2]) {
  262. that.$set(data, key2, {});
  263. }
  264. }
  265. data = data[key2];
  266. });
  267. });
  268. }
  269. }
  270. }
  271. </script>
  272. <style lang="scss" scoped>
  273. .illustration{
  274. width: 750rpx;
  275. height: 750rpx;
  276. position: fixed;
  277. top: 44%;
  278. left: 0;
  279. margin-top: -375rpx;
  280. .img{
  281. width: 100%;
  282. height: 100%;
  283. }
  284. }
  285. .fixed-btn{
  286. width: 630rpx;
  287. height: max-content;
  288. position: fixed;
  289. left: calc(50% - 315rpx);
  290. top: calc(40% + 375rpx);
  291. display: flex;
  292. justify-content: space-between;
  293. .btn1, .btn2{
  294. width: 300rpx;
  295. height: 100rpx;
  296. border-radius: 60rpx;
  297. border: 1rpx solid #15716E;
  298. box-sizing: border-box;
  299. line-height: 100rpx;
  300. font-size: 32rpx;
  301. }
  302. .btn1{
  303. background-color: #F3F8F8;
  304. color: #15716E;
  305. }
  306. .btn2{
  307. background-color: #15716E;
  308. color: #FFFFFF;
  309. }
  310. }
  311. </style>