mp-weixin.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <view class="container">
  3. <view v-if="isPersonal === true" class="personal">
  4. <!-- 页面头部 -->
  5. <view class="header">
  6. <view class="title">
  7. <text>获取您的昵称、头像</text>
  8. </view>
  9. <view class="sub-title">
  10. <text>填写您的微信头像和昵称,以便获得更好的体验</text>
  11. </view>
  12. </view>
  13. <!-- 表单组件 -->
  14. <view class="login-form">
  15. <view class="form-item">
  16. <view class="form-item--label">头像</view>
  17. <button class="btn-normal" open-type="chooseAvatar" @click="onClickAvatar()" @chooseavatar="onChooseAvatar">
  18. <avatar-image :url="avatarUrl" :width="100" />
  19. </button>
  20. </view>
  21. <view class="form-item">
  22. <view class="form-item--label">昵称</view>
  23. <input class="form-item--input" type="nickname" v-model="form.nickName" maxlength="30" placeholder="请输入微信昵称" @input="onInputNickName" @blur="onInputNickName" />
  24. </view>
  25. </view>
  26. <!-- 操作按钮 -->
  27. <view class="login-btn">
  28. <view class="button" :class="{disabled}" @click="handleSubmit()">确认</view>
  29. </view>
  30. <view class="no-login-btn">
  31. <view class="button" @click="handleCancel()">暂不登录</view>
  32. </view>
  33. </view>
  34. <view v-if="isPersonal === false" class="authorize">
  35. <view class="store-info">
  36. <view class="header">
  37. <image class="image" :src="storeInfo && storeInfo.image_url ? storeInfo.image_url : '/static/default-logo.png'"></image>
  38. </view>
  39. </view>
  40. <view class="auth-title">申请获取以下权限</view>
  41. <view class="auth-subtitle">获得你的公开信息(昵称、头像等)</view>
  42. <view class="login-btn">
  43. <view class="button" :class="{disabled}" @click="handleLogin()">一键登录</view>
  44. </view>
  45. <view class="no-login-btn">
  46. <view class="button" @click="handleCancel()">暂不登录</view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import store from '@/store'
  53. import * as LoginApi from '@/api/login'
  54. import * as UploadApi from '@/api/upload'
  55. import AvatarImage from '@/components/avatar-image'
  56. import {isEmpty} from '@/utils/util'
  57. import * as Verify from '@/utils/verify'
  58. export default {
  59. components: {
  60. AvatarImage
  61. },
  62. data() {
  63. return {
  64. // 商城基本信息
  65. storeInfo: undefined,
  66. // 是否需要填写用户昵称和头像
  67. isPersonal: undefined,
  68. // 微信小程序登录凭证 (code)
  69. // 提交到后端,用于换取openid
  70. code: '',
  71. // 按钮禁用
  72. disabled: false,
  73. // 头像路径 (用于显示)
  74. avatarUrl: '',
  75. // 临时图片 (用于上传)
  76. tempFile: undefined,
  77. // 表单数据
  78. form: {
  79. avatarId: null,
  80. nickName: ''
  81. }
  82. }
  83. },
  84. created() {
  85. // 请求后端是否需要填写昵称头像 (微信小程序端)
  86. this.getIsPersonal()
  87. },
  88. methods: {
  89. /**
  90. * 请求后端是否需要填写昵称头像 (微信小程序端)
  91. * - 条件1: 后台开启了填写微信头像和昵称
  92. * - 条件2: 用户首次注册或者已注册但未填写过信息
  93. */
  94. async getIsPersonal() {
  95. const app = this
  96. LoginApi.isPersonalMpweixin({code: await app.getCode()}).then((result) => (app.isPersonal = result.data.isPersonalMpweixin))
  97. },
  98. // 获取code
  99. // https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
  100. getCode() {
  101. return new Promise((resolve, reject) => {
  102. uni.login({
  103. provider: 'weixin',
  104. success({code}) {
  105. console.log('code', code)
  106. resolve(code)
  107. },
  108. fail: reject
  109. })
  110. })
  111. },
  112. // 绑定昵称输入框 (用于微信小程序端快速填写昵称能力)
  113. onInputNickName({detail}) {
  114. console.log(detail)
  115. if (detail.value) {
  116. this.form.nickName = detail.value
  117. }
  118. },
  119. // 点击头像按钮事件
  120. onClickAvatar() {
  121. // #ifdef MP-WEIXIN
  122. return
  123. // #endif
  124. this.chooseImage()
  125. },
  126. // 选择头像事件 - 仅限微信小程序
  127. onChooseAvatar({detail}) {
  128. // #ifdef MP-WEIXIN
  129. const app = this
  130. app.avatarUrl = detail.avatarUrl
  131. app.tempFile = {path: app.avatarUrl}
  132. // #endif
  133. },
  134. // 选择图片
  135. chooseImage() {
  136. const app = this
  137. // 选择图片
  138. uni.chooseImage({
  139. count: 1,
  140. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  141. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  142. success({tempFiles}) {
  143. app.tempFile = tempFiles[0]
  144. app.avatarUrl = app.tempFile.path
  145. }
  146. })
  147. },
  148. // 上传图片
  149. uploadFile() {
  150. const app = this
  151. return UploadApi.image([app.tempFile], false)
  152. .then((fileIds) => {
  153. app.form.avatarId = fileIds[0]
  154. app.tempFile = null
  155. return true
  156. })
  157. .catch(() => {
  158. return false
  159. })
  160. },
  161. // 确认提交头像昵称
  162. async handleSubmit() {
  163. const app = this
  164. console.log('handleSubmit', app.form, app.tempFile)
  165. if (app.disabled === true) {
  166. return
  167. }
  168. if (app.tempFile === undefined || Verify.isEmpty(app.form.nickName)) {
  169. app.$toast('请填写头像和昵称~')
  170. return
  171. }
  172. // 按钮禁用
  173. app.disabled = true
  174. // 先上传头像图片
  175. if (!(await app.uploadFile())) {
  176. app.$toast('很抱歉,头像上传失败')
  177. app.disabled = false
  178. return
  179. }
  180. // 提交到后端
  181. app.onAuthSuccess({nickName: app.form.nickName, avatarId: app.form.avatarId})
  182. },
  183. // 一键登录按钮点击事件
  184. handleLogin() {
  185. // this.onAuthSuccess({ nickName: '微信用户', avatarUrl: '' })
  186. this.onAuthSuccess({})
  187. },
  188. // 授权成功事件
  189. // 这里分为两个逻辑:
  190. // 1.将code和userInfo提交到后端,如果存在该用户 则实现自动登录,无需再填写手机号
  191. // 2.如果不存在该用户, 则显示注册页面, 需填写手机号
  192. // 3.如果后端报错了, 则显示错误信息
  193. async onAuthSuccess(userInfo) {
  194. const app = this
  195. // 提交到后端
  196. store
  197. .dispatch('LoginMpWx', {
  198. partyData: {
  199. code: await app.getCode(),
  200. oauth: 'MP-WEIXIN',
  201. userInfo
  202. }
  203. })
  204. .then((result) => {
  205. // 一键登录成功
  206. app.$toast(result.message)
  207. // 相应全局事件订阅: 刷新上级页面数据
  208. uni.$emit('syncRefresh', true)
  209. // 跳转回原页面
  210. setTimeout(() => app.onNavigateBack(), 2000)
  211. })
  212. .catch((err) => {
  213. const resultData = err.result.data
  214. // 显示错误信息
  215. if (isEmpty(resultData)) {
  216. app.$toast(err.result.message)
  217. }
  218. // 跳转回原页面
  219. if (resultData.isBack) {
  220. setTimeout(() => app.onNavigateBack(1), 2000)
  221. }
  222. // 判断还需绑定手机号
  223. if (resultData.isBindMobile) {
  224. app.onEmitSuccess(userInfo)
  225. }
  226. })
  227. },
  228. // 将oauth提交给父级
  229. // 这里要重新获取code, 因为上一次获取的code不能复用(会报错)
  230. async onEmitSuccess(userInfo) {
  231. const app = this
  232. app.$emit('success', {
  233. oauth: 'MP-WEIXIN', // 第三方登录类型: MP-WEIXIN
  234. code: await app.getCode(), // 微信登录的code, 用于换取openid
  235. userInfo // 微信用户信息
  236. })
  237. },
  238. /**
  239. * 暂不登录
  240. */
  241. handleCancel() {
  242. // 跳转回原页面
  243. this.onNavigateBack()
  244. },
  245. /**
  246. * 登录成功-跳转回原页面
  247. */
  248. onNavigateBack(delta = 1) {
  249. const pages = getCurrentPages()
  250. if (pages.length > 1) {
  251. uni.navigateBack({
  252. delta: Number(delta || 1)
  253. })
  254. } else {
  255. this.$navTo('pages/index/index')
  256. }
  257. }
  258. }
  259. }
  260. </script>
  261. <style lang="scss" scoped>
  262. .container {
  263. padding: 0 60rpx;
  264. font-size: 32rpx;
  265. background: #fff;
  266. min-height: 100vh;
  267. }
  268. .personal {
  269. // 页面头部
  270. .header {
  271. padding-top: 120rpx;
  272. margin-bottom: 60rpx;
  273. .title {
  274. margin-bottom: 20rpx;
  275. color: #191919;
  276. font-size: 44rpx;
  277. }
  278. .sub-title {
  279. margin-bottom: 70rpx;
  280. color: #b3b3b3;
  281. font-size: 28rpx;
  282. }
  283. }
  284. .login-form {
  285. margin-bottom: 90rpx;
  286. }
  287. // 输入框元素
  288. .form-item {
  289. display: flex;
  290. align-items: center;
  291. padding: 18rpx;
  292. border-bottom: 1rpx solid #f3f1f2;
  293. margin-bottom: 30rpx;
  294. min-height: 96rpx;
  295. &--label {
  296. min-width: 150rpx;
  297. font-size: 28rpx;
  298. line-height: 50rpx;
  299. }
  300. &--input {
  301. font-size: 28rpx;
  302. letter-spacing: 1rpx;
  303. flex: 1;
  304. height: 100%;
  305. }
  306. &--parts {
  307. min-width: 100rpx;
  308. }
  309. }
  310. }
  311. .authorize {
  312. .store-info {
  313. padding: 80rpx 0 48rpx;
  314. border-bottom: 1rpx solid #e3e3e3;
  315. margin-bottom: 72rpx;
  316. text-align: center;
  317. .header {
  318. width: 190rpx;
  319. height: 190rpx;
  320. border: 4rpx solid #fff;
  321. margin: 0 auto 0;
  322. border-radius: 50%;
  323. overflow: hidden;
  324. box-shadow: 2rpx 0 10rpx rgba(50, 50, 50, 0.3);
  325. .image {
  326. display: block;
  327. width: 100%;
  328. height: 100%;
  329. }
  330. }
  331. }
  332. .auth-title {
  333. color: #585858;
  334. font-size: 34rpx;
  335. margin-bottom: 40rpx;
  336. }
  337. .auth-subtitle {
  338. color: #888;
  339. margin-bottom: 88rpx;
  340. font-size: 28rpx;
  341. }
  342. }
  343. .login-btn {
  344. margin-bottom: 20rpx;
  345. .button {
  346. width: 100%;
  347. height: 86rpx;
  348. background: linear-gradient(to right, $main-bg, $main-bg2);
  349. color: $main-text;
  350. font-size: 30rpx;
  351. border-radius: 80rpx;
  352. letter-spacing: 5rpx;
  353. display: flex;
  354. justify-content: center;
  355. align-items: center;
  356. // 禁用按钮
  357. &.disabled {
  358. opacity: 0.6;
  359. }
  360. }
  361. }
  362. .no-login-btn {
  363. .button {
  364. height: 86rpx;
  365. background: #dfdfdf;
  366. color: #fff;
  367. font-size: 30rpx;
  368. border-radius: 80rpx;
  369. display: flex;
  370. justify-content: center;
  371. align-items: center;
  372. }
  373. }
  374. </style>