index.vue 8.0 KB

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