login.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <view class="login">
  3. <view class="img">
  4. <image src="https://t9.9026.com/imgs/bg_1.jpg" mode="scaleToFill"></image>
  5. </view>
  6. <view class="title">
  7. <view class="line"></view>
  8. <view class="logintype"><text>登录方式</text></view>
  9. <view class="line"></view>
  10. </view>
  11. <view class="typebtn">
  12. <view class="item" @click="goYouyue">
  13. <image src="/static/icon/youyue.png" mode="scaleToFill"></image>
  14. <text>加入优悦会</text>
  15. </view>
  16. <view class="item" @click="getmsg">
  17. <image src="/static/icon/wechart.png" mode="scaleToFill"></image>
  18. <text v-if="!isLoginOff">微信登录</text>
  19. <text v-if="isLoginOff">授权登录</text>
  20. </view>
  21. </view>
  22. <u-modal @close="closeMask" closeOnClickOverlay="true" :show="modal.show" :title="modal.title"
  23. :show-confirm-button="false">
  24. <view>
  25. <button class="avatar" open-type="chooseAvatar" @chooseavatar="handleChooseAvatar">
  26. <image class="user-avatar" style="height: 176rpx;width: 176rpx;border-radius: 50%;"
  27. :src="modal.avatar?modal.avatar:'/static/icon/avatar.png'" />
  28. </button>
  29. <input class="avatar" type="nickname" :value="modal.nickname" placeholder="填写昵称"
  30. @change="handleChangeNickname">
  31. <button class="confirm" @click="handleConfirmWechatUserInfo">提交</button>
  32. </view>
  33. </u-modal>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. imgBase64:'',
  41. isLoginOff: false,
  42. modal: {
  43. show: false,
  44. title: '用户信息获取',
  45. nickname: '',
  46. avatar: ''
  47. },
  48. code: '',
  49. encryptedData: '',
  50. iv: '',
  51. data: {}
  52. }
  53. },
  54. onLoad() {
  55. this.isLoginOff = this.$store.getters.userInfo
  56. },
  57. methods: {
  58. //关闭遮罩层
  59. closeMask() {
  60. this.modal.show = false
  61. },
  62. //提交
  63. handleConfirmWechatUserInfo() {
  64. if (!this.modal.avatar) {
  65. this.$u.toast('请上传头像')
  66. return
  67. }
  68. if (!this.modal.nickname) {
  69. this.$u.toast('请填写昵称')
  70. return
  71. }
  72. this.$loading('数据提交中...')
  73. this.$api.my.update({
  74. avatar: this.modal.avatar,
  75. nickname: this.modal.nickname
  76. }).then(res => {
  77. this.$hideLoading()
  78. this.$store.dispatch('user/info', res.data)
  79. this.modal.show = false
  80. uni.switchTab({
  81. url: '/pages/my/my'
  82. })
  83. })
  84. },
  85. handleGetWechatUserInfo() {
  86. this.modal.show = true
  87. },
  88. // 获取头像
  89. handleChooseAvatar(e) {
  90. console.log(e.detail, '------>e.detail');
  91. this.modal.avatar = e.detail.avatarUrl
  92. uni.getFileSystemManager().readFile({
  93. filePath: e.detail.avatarUrl, //选择图片返回的相对路径
  94. encoding: 'base64', //编码格式
  95. success: res => { //成功的回调
  96. console.log(res);
  97. this.imgBase64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符,在页面无法显示的哦
  98. this.$api.my.UserUploadFile({
  99. file:this.imgBase64
  100. }).then(res=>{
  101. console.log(res.data,'------->res.data');
  102. })
  103. },
  104. fail: (e) => {
  105. console.log("图片转换失败");
  106. }
  107. })
  108. },
  109. //获取昵称
  110. handleChangeNickname(res) {
  111. this.modal.nickname = res.detail.value
  112. },
  113. // 加入优悦会
  114. goYouyue() {
  115. uni.navigateTo({
  116. url: '/pages/login/youyue'
  117. })
  118. },
  119. getCode() {
  120. return new Promise((resolve, reject) => {
  121. uni.getUserInfo({
  122. success: loginRes => {
  123. this.data.encryptData = loginRes.encryptedData,
  124. this.data.iv = loginRes.iv
  125. resolve(this.data)
  126. }
  127. })
  128. })
  129. },
  130. //获取微信登录的code码
  131. getmsg() {
  132. if (this.isLoginOff) {
  133. uni.switchTab({
  134. url: '/pages/my/my'
  135. })
  136. } else {
  137. uni.login({
  138. provider: uni.$u.platform,
  139. success: res => {
  140. this.getCode().then((data) => {
  141. const params = {
  142. code: res.code,
  143. iv: data.iv,
  144. encryptData: data.encryptData
  145. }
  146. console.log(params);
  147. this.$api.my.myLogin(params).then(res => {
  148. let {
  149. token
  150. } = res.data
  151. this.$store.dispatch('user/token', token)
  152. this.handleGetWechatUserInfo()
  153. this.handleConfirmWechatUserInfo()
  154. })
  155. })
  156. }
  157. })
  158. }
  159. },
  160. }
  161. }
  162. </script>
  163. <style lang="scss" scoped>
  164. $pageColor:#F9F9F9;
  165. $bgColor:#FFFFFF;
  166. .login {
  167. height: 100%;
  168. background: $bgColor;
  169. box-sizing: border-box;
  170. }
  171. .avatar {
  172. width: 176rpx;
  173. height: 176rpx;
  174. border-radius: 50%;
  175. }
  176. .user-avatar {
  177. object-fit: cover;
  178. object-position: center;
  179. }
  180. .userinfo {
  181. position: absolute;
  182. top: 38rpx;
  183. left: 200rpx;
  184. .unlogin {
  185. width: 220rpx;
  186. font-size: 44rpx;
  187. font-family: PingFang-SC-Heavy, PingFang-SC;
  188. font-weight: 500;
  189. color: #FFFFFF;
  190. display: block;
  191. margin-top: 20rpx;
  192. }
  193. .username {
  194. width: 184rpx;
  195. height: 44rpx;
  196. font-size: 44rpx;
  197. font-family: PingFang-SC-Heavy, PingFang-SC;
  198. font-weight: 500;
  199. color: #FFFFFF;
  200. line-height: 44rpx;
  201. display: block;
  202. margin-bottom: 24rpx;
  203. }
  204. .userId {
  205. font-size: 32rpx;
  206. font-family: PingFang-SC-Medium, PingFang-SC;
  207. font-weight: 400;
  208. color: #FFFFFF;
  209. line-height: 32rpx;
  210. }
  211. }
  212. .img {
  213. image {
  214. width: 100vw;
  215. height: 910rpx;
  216. }
  217. }
  218. .title {
  219. height: 40rpx;
  220. width: 690rpx;
  221. // margin-top: 168rpx;
  222. margin-left: 30rpx;
  223. margin-bottom: 72rpx;
  224. display: flex;
  225. align-items: center;
  226. justify-content: space-between;
  227. .line {
  228. width: 240rpx;
  229. height: 2rpx;
  230. background: #EDEDED;
  231. border-radius: 1rpx;
  232. }
  233. .logintype {
  234. text {
  235. font-size: 28rpx;
  236. font-family: PingFang-SC-Medium, PingFang-SC;
  237. font-weight: 500;
  238. color: #333333;
  239. }
  240. }
  241. }
  242. .typebtn {
  243. display: flex;
  244. align-items: center;
  245. justify-content: space-around;
  246. .item {
  247. width: 140rpx;
  248. height: 156rpx;
  249. display: flex;
  250. flex-direction: column;
  251. align-items: center;
  252. justify-content: center;
  253. image {
  254. width: 104rpx;
  255. height: 104rpx;
  256. margin-bottom: 12rpx;
  257. }
  258. text {
  259. display: block;
  260. font-size: 28rpx;
  261. font-family: PingFang-SC-Medium, PingFang-SC;
  262. font-weight: 500;
  263. color: #666666;
  264. }
  265. }
  266. }
  267. </style>