index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="app-container">
  3. <view class="main-container">
  4. <view class="header">
  5. <image :src="configInfo.minLogo1"></image>
  6. <view>{{ configInfo.minAppName }}</view>
  7. </view>
  8. <view class="bottom">
  9. <view class="content">
  10. <view>申请获取以下权限</view>
  11. <text>获取你的公开信息(昵称,头像、地区等)</text>
  12. </view>
  13. <button class="button" type="primary" @tap="wxLogin">授权登录</button>
  14. <!-- <view class="tips">登录即代表同意<text @tap="toAgreement(1)">《用户协议》</text>和<text @tap="toAgreement(2)">《隐私声明》</text></view> -->
  15. </view>
  16. </view>
  17. <u-popup :round="10" :show="isShowPhoneDialog">
  18. <view class="phone-dialog">
  19. <view class="content">
  20. <view>申请获取以下权限</view>
  21. <text>获得你的公开信息(手机号码)</text>
  22. </view>
  23. <view class="btn">
  24. <button @tap="isShowPhoneDialog = false">取消</button>
  25. <button type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">确定授权</button>
  26. </view>
  27. </view>
  28. </u-popup>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. configInfo: {},
  36. userId: null,
  37. isNotOpenid: false, // 是否因为 支付前没有openid 进入的登录页
  38. isShowPhoneDialog: false // 是否显示获取手机号授权弹窗
  39. }
  40. },
  41. async onLoad({ isNotOpenid }) {
  42. this.isNotOpenid = isNotOpenid ? true : false
  43. this.configInfo = await this.$getConfigInfo()
  44. if (this.$getStorage('is_qywx') !== true && this.$getStorage('is_qywx') !== false) {
  45. console.log('重新获取系统信息')
  46. uni.getSystemInfo({
  47. success: function (e) {
  48. console.log(e)
  49. if (e.environment == 'wxwork') {
  50. this.$setStorage('is_qywx', true)
  51. } else {
  52. this.$setStorage('is_qywx', false)
  53. }
  54. }
  55. })
  56. }
  57. },
  58. methods: {
  59. wxLogin() {
  60. console.log('是否从企业微信端进入:' + this.$getStorage('is_qywx'))
  61. // 没有openid
  62. if (this.isNotOpenid == true) {
  63. console.log('没有openid')
  64. wx.login({
  65. provider: 'weixin',
  66. success: loginRes => {
  67. console.log(loginRes)
  68. this.$axios({
  69. url: '/user/auth',
  70. params: {
  71. code: loginRes.code,
  72. work: this.$getStorage('is_qywx'),
  73. serviceId: this.$getStorage('is_qywx') ? this.$store.state.userId : ''
  74. }
  75. }).then(res => {
  76. this.userId = res.data.userId
  77. this.mobile = res.data.mobile
  78. this.$setStorage('token', res.data.token)
  79. if (!this.mobile) {
  80. return (this.isShowPhoneDialog = true)
  81. }
  82. this.loginSuccess()
  83. })
  84. }
  85. })
  86. }
  87. // 企业微信端
  88. else if (this.$getStorage('is_qywx') == true) {
  89. console.log('从企业微信端进入')
  90. wx.qy.login({
  91. success: loginRes => {
  92. console.log(loginRes)
  93. if (loginRes.code) {
  94. this.$axios({
  95. url: '/user/auth',
  96. params: {
  97. code: loginRes.code,
  98. work: true
  99. }
  100. }).then(res => {
  101. this.userId = res.data.userId
  102. this.mobile = res.data.mobile
  103. this.$setStorage('token', res.data.token)
  104. if (!this.mobile || this.$getStorage('isActiveLogout')) {
  105. return (this.isShowPhoneDialog = true)
  106. }
  107. this.loginSuccess()
  108. })
  109. } else {
  110. console.log('登录失败!' + loginRes.errMsg)
  111. }
  112. }
  113. })
  114. }
  115. // 微信端
  116. else if (this.$getStorage('is_qywx') == false) {
  117. console.log('从微信端进入')
  118. wx.login({
  119. provider: 'weixin',
  120. success: loginRes => {
  121. console.log(loginRes)
  122. this.$axios({
  123. url: '/user/auth',
  124. params: {
  125. code: loginRes.code,
  126. work: false
  127. }
  128. }).then(res => {
  129. this.userId = res.data.userId
  130. this.mobile = res.data.mobile
  131. this.$setStorage('token', res.data.token)
  132. if (!this.mobile || this.$getStorage('isActiveLogout')) {
  133. return (this.isShowPhoneDialog = true)
  134. }
  135. this.loginSuccess()
  136. })
  137. }
  138. })
  139. }
  140. return false
  141. },
  142. // 获取配置信息
  143. async getConfigInfo() {
  144. return new Promise((resolve, reject) => {
  145. this.$axios({
  146. url: '/common/config/get',
  147. method: 'get'
  148. }).then(res => {
  149. resolve(res.data)
  150. })
  151. })
  152. },
  153. // 保存用户信息
  154. async saveUserInfo(userInfo) {
  155. const configInfo = await this.getConfigInfo()
  156. return new Promise((resolve, reject) => {
  157. this.$axios({
  158. url: '/user/userinfo/save',
  159. params: {
  160. userId: this.userId,
  161. avatarUrl: configInfo.minLogo3,
  162. nickName: `微信用户_${userInfo.mobile.slice(7, 11)}`
  163. }
  164. }).then(res => {
  165. resolve(res.data)
  166. })
  167. })
  168. },
  169. // 获取手机号
  170. getPhoneNumber(e) {
  171. this.$axios({
  172. url: '/user/mobile/grant',
  173. params: {
  174. userId: this.userId,
  175. iv: e.detail.iv,
  176. encryptedData: e.detail.encryptedData
  177. }
  178. }).then(res => {
  179. this.$setStorage('token', res.data.token)
  180. this.$setStorage('isActiveLogout', false)
  181. this.isShowPhoneDialog = false
  182. this.loginSuccess()
  183. })
  184. },
  185. // 登录成功
  186. async loginSuccess() {
  187. const userInfo = await this.$getUserInfo(this.userId)
  188. if (!userInfo.avatar || !userInfo.nickName) {
  189. await this.saveUserInfo(userInfo)
  190. }
  191. this.$store.commit('changeIsLogin', true)
  192. this.$setStorage('isLogin', true)
  193. this.$successToast('登录成功')
  194. this.$isResolve()
  195. this.$backPage(1, 1000)
  196. },
  197. toAgreement(type) {
  198. this.$navToPage({
  199. url: '/pages/login/agreement?type=' + type
  200. })
  201. }
  202. }
  203. }
  204. </script>
  205. <style lang="scss">
  206. .app-container {
  207. background: #ffffff;
  208. box-sizing: border-box;
  209. }
  210. .main-container {
  211. .header {
  212. border-bottom: 1px solid #ccc;
  213. text-align: center;
  214. height: 400rpx;
  215. margin-bottom: 50rpx;
  216. display: flex;
  217. flex-direction: column;
  218. align-items: center;
  219. justify-content: center;
  220. view {
  221. font-size: 38rpx;
  222. margin-top: 30rpx;
  223. }
  224. image {
  225. width: 200rpx;
  226. height: 200rpx;
  227. }
  228. }
  229. .bottom {
  230. padding: 0 50rpx;
  231. .content {
  232. font-size: 28rpx;
  233. margin-bottom: 90rpx;
  234. text {
  235. display: block;
  236. color: #9d9d9d;
  237. margin-top: 30rpx;
  238. }
  239. }
  240. .button {
  241. border-radius: 80rpx;
  242. margin: 70rpx 0 30rpx;
  243. font-size: 35rpx;
  244. }
  245. .tips {
  246. font-size: 26rpx;
  247. color: #666666;
  248. text {
  249. color: #ff3f42;
  250. }
  251. }
  252. }
  253. }
  254. .phone-dialog {
  255. padding: 40rpx;
  256. box-sizing: border-box;
  257. .content {
  258. margin-top: 30rpx;
  259. margin-bottom: 30rpx;
  260. line-height: 50rpx;
  261. text {
  262. color: #9d9d9d;
  263. }
  264. }
  265. .btn {
  266. display: flex;
  267. justify-content: center;
  268. margin-top: 60rpx;
  269. button::after {
  270. border: none;
  271. }
  272. button {
  273. width: 180rpx;
  274. height: 70rpx;
  275. line-height: 70rpx;
  276. margin: 0;
  277. font-size: 28rpx;
  278. margin: 0 30rpx;
  279. &:first-child {
  280. background: #f5f5f5;
  281. color: #00ba5c;
  282. }
  283. }
  284. }
  285. }
  286. </style>