index copy.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. isMaster: res.extra.auth.isMaster,
  84. registerFlag: res.extra.auth.registerFlag,
  85. auditState: res.extra.auth.auditState,
  86. }
  87. })
  88. if (res.extra.auth.registerFlag == 1 && res.extra.auth.auditState == 2) {
  89. uni.switchTab({
  90. url: '/pages/index/home'
  91. })
  92. } else if (res.extra.auth.registerFlag == 1 && res.extra.auth.auditState != 2) {
  93. uni.navigateTo({
  94. url: '/pages/index/registerState'
  95. })
  96. } else {
  97. uni.navigateTo({
  98. url: '/pages/index/authorize'
  99. })
  100. }
  101. }).catch(error => {
  102. console.log('error登录异常', error)
  103. this.$refs.uTips.show({
  104. title: '微信登录授权失败',
  105. type: 'warning',
  106. })
  107. })
  108. },
  109. // 获取登录权限
  110. appLoginWx() {
  111. uni.getProvider({
  112. service: 'oauth',
  113. success: (res) => {
  114. if (~res.provider.indexOf('weixin')) {
  115. uni.login({
  116. provider: 'weixin',
  117. success: (res2) => {
  118. uni.getUserInfo({
  119. provider: 'weixin',
  120. success: (info) => {
  121. uni.setStorage({
  122. key: 'wxLoginData',
  123. data: {
  124. code: res2.code,
  125. encryptedData: info.encryptedData,
  126. iv: info.iv,
  127. rawData: info.rawData,
  128. signature: info.signature,
  129. }
  130. })
  131. this.wxLoginData = uni.getStorageSync("wxLoginData")
  132. this.userNmae = JSON.parse(this.wxLoginData.rawData).nickName
  133. this.$refs.uTips.show({
  134. title: '获取角色信息成功',
  135. type: 'success',
  136. })
  137. },
  138. fail: (error) => {
  139. this.$refs.uTips.show({
  140. title: '微信登录授权失败',
  141. type: 'warning',
  142. })
  143. }
  144. })
  145. },
  146. fail: () => {
  147. this.$refs.uTips.show({
  148. title: '微信登录授权失败',
  149. type: 'warning',
  150. })
  151. }
  152. })
  153. } else {
  154. this.$refs.uTips.show({
  155. title: '请先安装微信或升级版本',
  156. type: 'warning',
  157. })
  158. }
  159. }
  160. });
  161. },
  162. },
  163. }
  164. </script>
  165. <style lang="less" scoped>
  166. page {
  167. width: 100%;
  168. height: 100%;
  169. }
  170. .container {
  171. width: 100%;
  172. height: 100%;
  173. float: left;
  174. position: absolute;
  175. .authorize-bg {
  176. width: 100%;
  177. height: 100%;
  178. position: absolute;
  179. left: 0;
  180. top: 0;
  181. }
  182. .logo {
  183. width: 90px;
  184. height: 90px;
  185. float: left;
  186. position: absolute;
  187. left: 50%;
  188. top: 40px;
  189. transform: translateX(-50%);
  190. }
  191. .authorize-box {
  192. width: 230px;
  193. float: left;
  194. position: absolute;
  195. left: 50%;
  196. top: 170px;
  197. transform: translateX(-50%);
  198. .authorize-button {
  199. width: 230px;
  200. height: 30px;
  201. float: left;
  202. padding: 0px;
  203. background-color: transparent;
  204. border: none;
  205. box-shadow: none;
  206. .button-text {
  207. height: 30px;
  208. float: left;
  209. line-height: 30px;
  210. font-size: 15px;
  211. font-family: PingFang SC;
  212. color: #FEFEFE;
  213. margin-right: 4px;
  214. }
  215. .iconfont {
  216. float: left;
  217. color: #FEFEFE;
  218. font-size: 28px;
  219. line-height: 30px;
  220. }
  221. }
  222. .authorize-button:after {
  223. border: none;
  224. }
  225. .authorize-info {
  226. width: 230px;
  227. height: 36px;
  228. float: left;
  229. margin-bottom: 30px;
  230. border-bottom: 1px solid #FFFFFF;
  231. font-size: 12px;
  232. font-family: PingFang SC;
  233. color: #FEFEFE;
  234. line-height: 36px;
  235. }
  236. .authorize-login {
  237. width: 220px;
  238. height: 36px;
  239. float: left;
  240. margin: 10px 5px;
  241. background: #FFFFFF;
  242. border-radius: 20px;
  243. font-size: 15px;
  244. font-family: PingFang SC;
  245. color: #52A63A;
  246. line-height: 36px;
  247. text-align: center;
  248. }
  249. .authorize-tip {
  250. width: 220px;
  251. height: 20px;
  252. float: left;
  253. margin: 5px 5px;
  254. font-size: 12px;
  255. font-family: PingFang SC;
  256. color: #FFFFFF;
  257. line-height: 20px;
  258. text-align: center;
  259. }
  260. }
  261. }
  262. </style>