index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view class="auth">
  3. <image src="/static/logo.png" mode="aspectFill"></image>
  4. <text class="margin-top-3 auth-title">欢迎使用空i企小程序</text>
  5. <text class="margin-top-3 auth-content">此页面是微信授权页面,授权之后你可以获取更优质的服务,您的隐私将会受到保护</text>
  6. <view class="margin-top-3">
  7. <button type='default' class="refuse" @click="refuseBtn">暂不授权</button>
  8. <button type='primary' class="allow" @click="getUserInfo()" v-if="!isAuth">登录授权</button>
  9. <button type="primary" class="allow" open-type="getPhoneNumber" style="font-size: 12px;"
  10. @getphonenumber="getPhoneNumber" v-if="isAuth && !isNeedPhone">手机号码授权</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import md5 from '@/common/md5.js';
  16. export default {
  17. data(){
  18. return {
  19. iv:'',
  20. encryptedData:'',
  21. isNeedPhone:getApp().globalData.user_phone,
  22. isAuth:getApp().globalData.isAuth
  23. }
  24. },
  25. onLoad() {
  26. this.loginLoad()
  27. },
  28. methods:{
  29. loginLoad(){
  30. let that = this;
  31. uni.login({
  32. success(res) {
  33. that.loginRequest(res.code)
  34. }
  35. })
  36. },
  37. loginRequest(codeRes){
  38. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  39. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=login&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  40. uni.request({
  41. url:url,
  42. method: 'POST',
  43. header: {
  44. 'content-type': 'application/x-www-form-urlencoded'
  45. },
  46. data: {
  47. code:codeRes
  48. },
  49. success: (res) => {
  50. console.log(res)
  51. if(res.data.code === 200){
  52. }
  53. },
  54. fail: () => {
  55. console.log("连接失败");
  56. }
  57. });
  58. },
  59. getUserInfo() {
  60. uni.getUserProfile({
  61. desc:'登录',
  62. success:(res)=> {
  63. getApp().globalData.user_headUrl = res.userInfo.avatarUrl;
  64. getApp().globalData.user_name = res.userInfo.nickName;
  65. this.iv = res.iv;
  66. this.encryptedData = res.encryptedData;
  67. this.isAuth = true;
  68. getApp().globalData.isAuth = true;
  69. //this.loginUserInfo()
  70. },
  71. fail:(err)=> {
  72. getApp().globalData.isAuth = false;
  73. }
  74. })
  75. // if (e.detail.errMsg == "getUserInfo:ok") {
  76. // getApp().globalData.user_headUrl = e.detail.userInfo.avatarUrl;
  77. // getApp().globalData.user_name = e.detail.userInfo.nickName;
  78. // this.iv = e.detail.iv;
  79. // this.encryptedData = e.detail.encryptedData;
  80. // console.log(e.detail)
  81. // //this.loginUserInfo()
  82. // } else {
  83. // console.log("用户信息授权失败");
  84. // getApp().globalData.isAuth = false;
  85. // }
  86. },
  87. getPhoneNumber(e){
  88. let that = this;
  89. if (e.detail.errMsg == 'getPhoneNumber:ok') { //允许授权执行跳转
  90. that.phoneRequest(e.detail.iv, e.detail.encryptedData, getApp().globalData.session_key)
  91. } else { //
  92. that.isNeedPhone = false;
  93. }
  94. },
  95. phoneRequest(myIv,myEncryptedData,sKey){
  96. let that = this;
  97. let md5Sign = md5("method="+'user'+"&timestamp="+getApp().globalData.globalTimestamp+"&secret="+getApp().globalData.secret)
  98. let url = getApp().globalData.shareUrl+'api/api.php'+'?method=user&source=user&action=phone&timestamp='+getApp().globalData.globalTimestamp +'&sign='+md5Sign
  99. uni.request({
  100. url:url,
  101. method: 'POST',
  102. header: {
  103. 'content-type': 'application/x-www-form-urlencoded'
  104. },
  105. data: {
  106. iv:myIv,
  107. sessionKey:sKey ||'MlkY744GF9fmIqXzS4YEkw==',
  108. encryptedData:myEncryptedData,
  109. },
  110. success: (res) => {
  111. console.log(res)
  112. if(res.data.code === 200){
  113. getApp().globalData.user_phone = res.data.phoneNumber;
  114. that.refuseBtn();
  115. }
  116. },
  117. fail: () => {
  118. console.log("连接失败");
  119. }
  120. });
  121. },
  122. refuseBtn(){
  123. uni.navigateBack({
  124. });
  125. },
  126. loginUserInfo(){
  127. let that = this;
  128. uni.request({
  129. url:getApp().globalData.shareUrl, //需要设置为全局
  130. method: 'POST',
  131. header: {
  132. 'content-type': 'application/x-www-form-urlencoded'
  133. },
  134. data: {
  135. method: 'auth',
  136. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  137. id:getApp().globalData.user_id,
  138. sign: md5('auth' + getApp().globalData.globalTimestamp),
  139. nickname:getApp().globalData.user_name,
  140. headimg:getApp().globalData.user_headUrl,
  141. auth_status:1,
  142. },
  143. success: res => {
  144. if(res.data.code === 200){
  145. getApp().globalData.user_id = res.data.msg.id;
  146. getApp().globalData.open_id = res.data.msg.openid;
  147. getApp().globalData.isAuth = true;
  148. getApp().globalData.user_headUrl = res.data.msg.headimg;
  149. getApp().globalData.user_name = res.data.msg.name;
  150. getApp().globalData.user_status = res.data.msg.status;
  151. getApp().globalData.user_phone = res.data.msg.phone;
  152. that.isNeedPhone = res.data.msg.phone;
  153. that.isAuth = getApp().globalData.isAuth;
  154. }
  155. else {
  156. uni.showToast({
  157. title: res.data.msg,
  158. icon: 'none',
  159. duration:2000
  160. });
  161. }
  162. // uni.navigateBack({
  163. // });
  164. }
  165. });
  166. },
  167. }
  168. }
  169. </script>
  170. <style>
  171. .auth{
  172. margin-top: 0;
  173. text-align: center;
  174. display: flex;
  175. flex-direction: column;
  176. justify-content: center;
  177. align-items: center;
  178. padding: 100upx;
  179. vertical-align: middle;
  180. }
  181. .auth image{
  182. width: 200upx;
  183. height: 200upx;
  184. }
  185. .auth view {
  186. display: flex;
  187. width: 490upx;
  188. justify-content: space-between;
  189. }
  190. .auth-title {
  191. font-size: 32upx;
  192. }
  193. .auth-content {
  194. font-size: 26upx;
  195. color: #a7aaa9;
  196. }
  197. .allow {
  198. background-color: #27BCEF;
  199. margin: 20rpx 0 200rpx;
  200. text-align: center;
  201. vertical-align: middle;
  202. touch-action: manipulation;
  203. cursor: pointer;
  204. background-image: none;
  205. white-space: nowrap;
  206. user-select: none;
  207. font-size: 14px;
  208. border: 0 !important;
  209. position: relative;
  210. text-decoration: none;
  211. height: 44px;
  212. width: 250rpx;
  213. line-height: 44px;
  214. box-shadow: inset 0 0 0 1px #27BCEF;
  215. background: #fff !important;
  216. color: #27BCEF !important;
  217. display: inline-block;
  218. border-radius: 10rpx;
  219. }
  220. .refuse {
  221. background-color: #19be6b;
  222. margin: 20rpx 20rpx 200rpx 20rpx;
  223. text-align: center;
  224. vertical-align: middle;
  225. touch-action: manipulation;
  226. cursor: pointer;
  227. background-image: none;
  228. white-space: nowrap;
  229. user-select: none;
  230. font-size: 14px;
  231. border: 0 !important;
  232. position: relative;
  233. text-decoration: none;
  234. height: 44px;
  235. width: 250rpx;
  236. line-height: 44px;
  237. box-shadow: inset 0 0 0 1px #8a8a8a;
  238. background: #fff !important;
  239. color: #8a8a8a !important;
  240. display: inline-block;
  241. border-radius: 10rpx;
  242. }
  243. .margin-top-3{
  244. margin-top: 10%;
  245. }
  246. </style>