index copy.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <view class="container">
  3. <image class="authorize-bg" src="../../static/images/loginBg.png"></image>
  4. <image class="logo" src="../../static/images/loginLogo.png"></image>
  5. <view class="authorize-box">
  6. <button class="authorize-button" open-type="getUserInfo" @click="appLoginWx()">
  7. <view class="button-text">获取角色信息</view>
  8. <view class="iconfont iconshangchuan1"></view>
  9. </button>
  10. <view class="authorize-info">{{userNmae}}</view>
  11. <!-- <button class="authorize-button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  12. <view class="button-text">获取手机号</view>
  13. <view class="iconfont iconshangchuan1"></view>
  14. </button>
  15. <view class="authorize-info">{{wxPhoneData? '****************' : ''}}</view> -->
  16. <view class="authorize-login" @click="getUserInfo">登录</view>
  17. <view class="authorize-tip">点击登录默认同意<text style="text-decoration: underline;">商家入驻协议</text></view>
  18. </view>
  19. <u-top-tips ref="uTips"></u-top-tips>
  20. </view>
  21. </template>
  22. <script>
  23. const NET = require('@/utils/request')
  24. const API = require('@/config/api')
  25. export default {
  26. data() {
  27. return {
  28. wxLoginData: null,
  29. wxPhoneData: null,
  30. userNmae: '',
  31. timer: false,
  32. }
  33. },
  34. onReady() {
  35. this.appLoginWx()
  36. setTimeout(() => {
  37. this.timer = true
  38. }, 10)
  39. },
  40. methods: {
  41. getPhoneNumber(e) {
  42. if (this.wxLoginData && this.timer) {
  43. let _that = this
  44. wx.checkSession({
  45. success(res) {
  46. _that.tipxx = '未过期'
  47. },
  48. fail(err) {
  49. _that.tipxx = '已过期'
  50. }
  51. })
  52. uni.setStorage({
  53. key: 'wxPhoneData',
  54. data: {
  55. encryptedData2: e.detail.encryptedData,
  56. iv2: e.detail.iv,
  57. }
  58. })
  59. this.wxPhoneData = uni.getStorageSync("wxPhoneData")
  60. } else {
  61. this.$refs.uTips.show({
  62. title: '请先等待角色信息获取完成',
  63. type: 'warning',
  64. })
  65. }
  66. },
  67. // 获取个人数据
  68. getUserInfo() {
  69. NET.request(API.WxLogin, {
  70. ...this.wxLoginData,
  71. ...this.wxPhoneData,
  72. }, 'POST').then(res => {
  73. uni.setStorage({
  74. key: 'token',
  75. data: res.data.token
  76. })
  77. uni.setStorage({
  78. key: 'userData',
  79. data: {
  80. headImage: res.extra.auth2.merchantImg,
  81. userName: res.data.name,
  82. userId: res.data.userId,
  83. tenantName: res.data.tenantName,
  84. isMaster: res.extra.auth.isMaster,
  85. registerFlag: res.extra.auth.registerFlag,
  86. auditState: res.extra.auth.auditState,
  87. }
  88. })
  89. if (res.extra.auth.registerFlag == 1 && res.extra.auth.auditState == 2) {
  90. uni.switchTab({
  91. url: '/pages/index/home'
  92. })
  93. } else if (res.extra.auth.registerFlag == 1 && res.extra.auth.auditState != 2) {
  94. uni.navigateTo({
  95. url: '/pages/index/registerState'
  96. })
  97. } else {
  98. uni.navigateTo({
  99. url: '/pages/index/authorize'
  100. })
  101. }
  102. }).catch(error => {
  103. console.log('error登录异常', error)
  104. this.$refs.uTips.show({
  105. title: '微信登录授权失败',
  106. type: 'warning',
  107. })
  108. })
  109. },
  110. // 获取登录权限
  111. appLoginWx() {
  112. uni.getProvider({
  113. service: 'oauth',
  114. success: (res) => {
  115. if (~res.provider.indexOf('weixin')) {
  116. uni.login({
  117. provider: 'weixin',
  118. success: (res2) => {
  119. uni.getUserInfo({
  120. provider: 'weixin',
  121. success: (info) => {
  122. uni.setStorage({
  123. key: 'wxLoginData',
  124. data: {
  125. code: res2.code,
  126. encryptedData: info.encryptedData,
  127. iv: info.iv,
  128. rawData: info.rawData,
  129. signature: info.signature,
  130. }
  131. })
  132. this.wxLoginData = uni.getStorageSync("wxLoginData")
  133. this.userNmae = JSON.parse(this.wxLoginData.rawData).nickName
  134. this.$refs.uTips.show({
  135. title: '获取角色信息成功',
  136. type: 'success',
  137. })
  138. },
  139. fail: (error) => {
  140. this.$refs.uTips.show({
  141. title: '微信登录授权失败',
  142. type: 'warning',
  143. })
  144. }
  145. })
  146. },
  147. fail: () => {
  148. this.$refs.uTips.show({
  149. title: '微信登录授权失败',
  150. type: 'warning',
  151. })
  152. }
  153. })
  154. } else {
  155. this.$refs.uTips.show({
  156. title: '请先安装微信或升级版本',
  157. type: 'warning',
  158. })
  159. }
  160. }
  161. });
  162. },
  163. },
  164. }
  165. </script>
  166. <style lang="less" scoped>
  167. page {
  168. width: 100%;
  169. height: 100%;
  170. }
  171. .container {
  172. width: 100%;
  173. height: 100%;
  174. float: left;
  175. position: absolute;
  176. .authorize-bg {
  177. width: 100%;
  178. height: 100%;
  179. position: absolute;
  180. left: 0;
  181. top: 0;
  182. }
  183. .logo {
  184. width: 90px;
  185. height: 90px;
  186. float: left;
  187. position: absolute;
  188. left: 50%;
  189. top: 40px;
  190. transform: translateX(-50%);
  191. }
  192. .authorize-box {
  193. width: 230px;
  194. float: left;
  195. position: absolute;
  196. left: 50%;
  197. top: 170px;
  198. transform: translateX(-50%);
  199. .authorize-button {
  200. width: 230px;
  201. height: 30px;
  202. float: left;
  203. padding: 0px;
  204. background-color: transparent;
  205. border: none;
  206. box-shadow: none;
  207. .button-text {
  208. height: 30px;
  209. float: left;
  210. line-height: 30px;
  211. font-size: 15px;
  212. font-family: PingFang SC;
  213. color: #FEFEFE;
  214. margin-right: 4px;
  215. }
  216. .iconfont {
  217. float: left;
  218. color: #FEFEFE;
  219. font-size: 28px;
  220. line-height: 30px;
  221. }
  222. }
  223. .authorize-button:after {
  224. border: none;
  225. }
  226. .authorize-info {
  227. width: 230px;
  228. height: 36px;
  229. float: left;
  230. margin-bottom: 30px;
  231. border-bottom: 1px solid #FFFFFF;
  232. font-size: 12px;
  233. font-family: PingFang SC;
  234. color: #FEFEFE;
  235. line-height: 36px;
  236. }
  237. .authorize-login {
  238. width: 220px;
  239. height: 36px;
  240. float: left;
  241. margin: 10px 5px;
  242. background: #FFFFFF;
  243. border-radius: 20px;
  244. font-size: 15px;
  245. font-family: PingFang SC;
  246. color: #52A63A;
  247. line-height: 36px;
  248. text-align: center;
  249. }
  250. .authorize-tip {
  251. width: 220px;
  252. height: 20px;
  253. float: left;
  254. margin: 5px 5px;
  255. font-size: 12px;
  256. font-family: PingFang SC;
  257. color: #FFFFFF;
  258. line-height: 20px;
  259. text-align: center;
  260. }
  261. }
  262. }
  263. </style>