login.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <!-- <view class="container">
  3. <view class="login-box">
  4. <text>您还没有登录</text>
  5. <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class="wx-login-btn">
  6. 抖音用户手机号授权登录</button>
  7. </view>
  8. </view> -->
  9. <view class="background">
  10. <view class="content-wrap">
  11. <view class="content">
  12. <image src="https://mall.zhaijieshi.cc/file/wx-dy/icon-login.png" mode="aspectFit"></image>
  13. <view class="no-login">您还未登录</view>
  14. <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="dy-login-btn">抖音用户手机号授权登录</button>
  15. </view>
  16. <image src="https://mall.zhaijieshi.cc/file/wx-dy/icon-close.png" mode="aspectFit" class="close-btn" @tap="closeLogin"></image>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. var api = require('../../../config/api.js');
  22. var util = require('../../../utils/util.js');
  23. var user = require('../../../utils/user.js');
  24. var app = getApp();
  25. export default {
  26. data() {
  27. return {
  28. username: '',
  29. mobile: '',
  30. code: '',
  31. inputTel: ''
  32. };
  33. },
  34. onLoad: function (options) {
  35. // 页面初始化 options为页面跳转所带来的参数
  36. // 页面渲染完成
  37. if (options.reg != null) {
  38. uni.showModal({
  39. title: '提示',
  40. content: '授权过期,请重新授权',
  41. showCancel: false
  42. });
  43. }
  44. // wx.login({
  45. // force: true,
  46. // success(res) {
  47. // console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
  48. // console.log(res);
  49. // },
  50. // fail(res) {
  51. // console.log(`login 调用失败`);
  52. // },
  53. // });
  54. },
  55. onReady: function () {},
  56. onShow: function () {
  57. // 页面显示
  58. let that = this;
  59. user.login().then((res) => {
  60. console.info(res);
  61. that.setData({
  62. code: res.code
  63. });
  64. });
  65. },
  66. onHide: function () {
  67. // 页面隐藏
  68. },
  69. onUnload: function () {
  70. // 页面关闭
  71. },
  72. methods: {
  73. getPhoneNumber: function (e) {
  74. console.log(e);
  75. var ivObj = e.detail.iv;
  76. var telObj = e.detail.encryptedData;
  77. if (ivObj == undefined || telObj == undefined) {
  78. uni.showToast({
  79. title: '获取授权失败',
  80. icon: 'error',
  81. duration: 2000
  82. });
  83. return;
  84. }
  85. var that = this;
  86. console.log('请求授权登录:code:' + that.code + 'encryptedData,' + telObj + 'iv,' + ivObj);
  87. //------执行Login---------
  88. util.request(api.AuthRegister, {
  89. code: that.code,
  90. encryptedData: telObj,
  91. iv: ivObj,
  92. }).then(function (res) {
  93. if (res.errno === 0) {
  94. //存储用户信息
  95. app.globalData.hasLogin = true;
  96. app.globalData.userInfo = res.data.userInfo;
  97. uni.setStorageSync('userInfo', res.data.userInfo);
  98. uni.setStorageSync('token', res.data.token);
  99. uni.setStorageSync('openId', res.data.openId);
  100. uni.showToast({
  101. title: '授权注册成功',
  102. icon: 'success',
  103. duration: 2000,
  104. complete: function () {
  105. setTimeout(function () {
  106. uni.navigateBack({
  107. delta: 1,
  108. fail(res) {
  109. console.info(res);
  110. uni.switchTab({
  111. url: '/pages/index/index'
  112. });
  113. }
  114. });
  115. }, 1000);
  116. }
  117. });
  118. } else {
  119. console.info(res);
  120. user.login().then((res) => {
  121. console.info(res);
  122. that.setData({
  123. code: res.code
  124. });
  125. });
  126. uni.showToast({
  127. title: '授权失败请重试',
  128. icon: 'error',
  129. duration: 3000
  130. });
  131. }
  132. });
  133. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  134. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
  135. //用户点击拒绝
  136. uni.showModal({
  137. title: '警告',
  138. content: '您点击了拒绝授权,部分功能无法使用!!!',
  139. showCancel: false,
  140. confirmText: '确定',
  141. success: function (res) {
  142. // 用户没有授权成功,不需要改变 isHide 的值
  143. if (res.confirm) {
  144. uni.switchTab({
  145. url: '/pages/index/index'
  146. });
  147. }
  148. }
  149. });
  150. }
  151. },
  152. closeLogin() {
  153. uni.navigateBack({
  154. delta: 1
  155. });
  156. }
  157. }
  158. };
  159. </script>
  160. <style>
  161. page {
  162. height: 100%;
  163. width: 100%;
  164. background-color: #f7f7f7;
  165. }
  166. .login-box {
  167. width: 100%;
  168. height: auto;
  169. overflow: hidden;
  170. padding: 0 40rpx;
  171. margin-top: 200rpx;
  172. align-items: center;
  173. text-align: center;
  174. }
  175. .login-box text {
  176. text-align: center;
  177. }
  178. .img {
  179. width: 50rpx;
  180. height: 50rpx;
  181. }
  182. .wx-login-btn {
  183. margin: 60rpx 0 40rpx 0;
  184. height: 96rpx;
  185. line-height: 96rpx;
  186. font-size: 30rpx;
  187. border-radius: 6rpx;
  188. width: 90%;
  189. color: #fff;
  190. right: 0;
  191. display: flex;
  192. justify-content: center;
  193. align-items: center;
  194. position: flex;
  195. bottom: 0;
  196. left: 0;
  197. padding: 0;
  198. margin-left: 5%;
  199. text-align: center;
  200. /* padding-left: -5rpx; */
  201. border-radius: 6rpx;
  202. letter-spacing: 3rpx;
  203. /*background-color: #242a48;*/
  204. background-color: #242a48;
  205. }
  206. .account-login-btn {
  207. width: 90%;
  208. margin: 0 auto;
  209. color: #fff;
  210. font-size: 30rpx;
  211. height: 96rpx;
  212. line-height: 96rpx;
  213. right: 0;
  214. display: flex;
  215. justify-content: center;
  216. align-items: center;
  217. position: flex;
  218. bottom: 0;
  219. left: 0;
  220. border-radius: 0;
  221. padding: 0;
  222. margin-left: 5%;
  223. text-align: center;
  224. /* padding-left: -5rpx; */
  225. border-top-left-radius: 50rpx;
  226. border-bottom-left-radius: 50rpx;
  227. border-top-right-radius: 50rpx;
  228. border-bottom-right-radius: 50rpx;
  229. letter-spacing: 3rpx;
  230. background-image: linear-gradient(to right, #9a9ba1 0%, #9a9ba1 100%);
  231. }
  232. .wechat {
  233. width: 80rpx;
  234. height: 80rpx;
  235. margin-right: 15rpx;
  236. }
  237. .form-text {
  238. margin-top: 100rpx;
  239. text-align: center;
  240. }
  241. .form-item {
  242. position: relative;
  243. height: 96rpx;
  244. border-bottom: 1px solid #d9d9d9;
  245. }
  246. .form-item .username,
  247. .form-item .password,
  248. .form-item .mobile,
  249. .form-item .code {
  250. position: absolute;
  251. top: 26rpx;
  252. left: 0;
  253. display: block;
  254. width: 100%;
  255. height: 44rpx;
  256. color: #333;
  257. font-size: 30rpx;
  258. }
  259. .form-item-code {
  260. margin-top: 32rpx;
  261. height: auto;
  262. overflow: hidden;
  263. width: 100%;
  264. float: left;
  265. width: 350rpx;
  266. }
  267. .form-item-code .form-item {
  268. float: left;
  269. width: 350rpx;
  270. }
  271. .form-item-getPhone {
  272. margin-top: 18rpx;
  273. height: auto;
  274. float: right;
  275. /*border: 1px solid #242a48;*/
  276. border-radius: 6rpx;
  277. color: #242a48;
  278. /*background: green;*/
  279. font-size: 30rpx;
  280. }
  281. .background {
  282. width: 750rpx;
  283. height: 100vh;
  284. background: rgba(0, 0, 0, 0.5);
  285. display: flex;
  286. align-items: center;
  287. justify-content: center;
  288. }
  289. .content {
  290. width: 576rpx;
  291. height: 400rpx;
  292. background: #ffffff;
  293. border-radius: 20rpx;
  294. position: relative;
  295. padding-top: 170rpx;
  296. }
  297. .content image {
  298. width: 282rpx;
  299. height: 282rpx;
  300. position: absolute;
  301. top: -141rpx;
  302. left: 146rpx;
  303. }
  304. .no-login {
  305. height: 40rpx;
  306. line-height: 40rpx;
  307. font-size: 38rpx;
  308. font-weight: bold;
  309. color: #333333;
  310. text-align: center;
  311. margin-bottom: 50rpx;
  312. }
  313. .close-btn {
  314. width: 70rpx;
  315. height: 70rpx;
  316. margin: 30rpx 254rpx 0;
  317. }
  318. button.dy-login-btn {
  319. width: 492rpx;
  320. height: 90rpx;
  321. line-height: 90rpx;
  322. background: #00b9ff;
  323. border-radius: 90rpx;
  324. color: #ffffff;
  325. font-size: 30rpx;
  326. border: none;
  327. outline: none;
  328. position: static;
  329. margin: 0 auto;
  330. }
  331. .dy-login-btn::after {
  332. border: none;
  333. }
  334. </style>