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